|
@@ -54,38 +54,41 @@ public class ThreadTask implements Runnable{
|
|
|
try {
|
|
|
List<JSONObject> deviceRealtimeDataList = new ArrayList<>();
|
|
|
for (Map<String, Object> map : deviceList) {
|
|
|
- JSONObject devicerealtimeDataTotal = null;
|
|
|
- try {
|
|
|
-
|
|
|
- Map<String, String> paramRealtime = new HashMap<>();
|
|
|
- paramRealtime.put("deviceType", deviceType);
|
|
|
- paramRealtime.put("deviceCode", String.valueOf(map.get("deviceCode")));
|
|
|
- Map<String, String> headers = new HashMap<>();
|
|
|
- headers.put("Authorization", "Bearer " + KprDangyangWaterBizFun.dangyangToken);
|
|
|
- devicerealtimeDataTotal = JSONObject.parseObject(HttpUtil.sendGet(
|
|
|
- KprDangyangWaterBizFun.realtimeDataListUrl, paramRealtime, headers));
|
|
|
- } catch (Exception ex) {
|
|
|
- log.error("任务线程" + taskId + " 调用异常:" + ex.getLocalizedMessage());
|
|
|
- }
|
|
|
- try {
|
|
|
- if (devicerealtimeDataTotal != null && devicerealtimeDataTotal.getInteger("code") == 0 &&
|
|
|
- devicerealtimeDataTotal.getJSONObject("data") != null
|
|
|
- && devicerealtimeDataTotal.getJSONObject("data").getJSONArray("list") != null
|
|
|
- && !devicerealtimeDataTotal.getJSONObject("data").getJSONArray("list").isEmpty()) {
|
|
|
- //TODO 查询数据不为空,插入infulxdb
|
|
|
- JSONObject deviceRealtimeData = devicerealtimeDataTotal.getJSONObject("data")
|
|
|
- .getJSONArray("list").getJSONObject(0);
|
|
|
- adjustTime(deviceRealtimeData, collectionFrequency<60?60:collectionFrequency);//时间间隔round处理
|
|
|
- deviceRealtimeDataList.add(deviceRealtimeData);
|
|
|
- } else {
|
|
|
- log.error("任务线程" + taskId + " 执行设备"
|
|
|
- + String.valueOf(map.get("deviceCode")) +
|
|
|
- "失败:" + (devicerealtimeDataTotal == null ?
|
|
|
- null : devicerealtimeDataTotal.toJSONString()));
|
|
|
+// if(map.get("deviceCode").equals("108")) {//测试用
|
|
|
+ JSONObject devicerealtimeDataTotal = null;
|
|
|
+ try {
|
|
|
+
|
|
|
+ Map<String, String> paramRealtime = new HashMap<>();
|
|
|
+ paramRealtime.put("deviceType", deviceType);
|
|
|
+ paramRealtime.put("deviceCode", String.valueOf(map.get("deviceCode")));
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
+ headers.put("Authorization", "Bearer " + KprDangyangWaterBizFun.dangyangToken);
|
|
|
+ devicerealtimeDataTotal = JSONObject.parseObject(HttpUtil.sendGet(
|
|
|
+ KprDangyangWaterBizFun.realtimeDataListUrl, paramRealtime, headers));
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error("任务线程" + taskId + " 调用异常:" + ex.getLocalizedMessage());
|
|
|
}
|
|
|
- } catch (Exception ex) {
|
|
|
- log.error("任务线程" + taskId + " " + String.valueOf(map.get("deviceCode")) + "查询异常:" + ex.getLocalizedMessage() + ";resJson:" + devicerealtimeDataTotal.toJSONString());
|
|
|
- }
|
|
|
+ try {
|
|
|
+ if (devicerealtimeDataTotal != null && devicerealtimeDataTotal.getInteger("code") == 0 &&
|
|
|
+ devicerealtimeDataTotal.getJSONObject("data") != null
|
|
|
+ && devicerealtimeDataTotal.getJSONObject("data").getJSONArray("list") != null
|
|
|
+ && !devicerealtimeDataTotal.getJSONObject("data").getJSONArray("list").isEmpty()) {
|
|
|
+ //TODO 查询数据不为空,插入infulxdb
|
|
|
+ JSONObject deviceRealtimeData = devicerealtimeDataTotal.getJSONObject("data")
|
|
|
+ .getJSONArray("list").getJSONObject(0);
|
|
|
+ adjustTime(deviceRealtimeData, collectionFrequency < 60 ? 60 : collectionFrequency);//时间间隔round处理
|
|
|
+ deviceRealtimeDataList.add(deviceRealtimeData);
|
|
|
+ } else {
|
|
|
+ log.error("任务线程" + taskId + " 执行设备"
|
|
|
+ + String.valueOf(map.get("deviceCode")) +
|
|
|
+ "失败:" + (devicerealtimeDataTotal == null ?
|
|
|
+ null : devicerealtimeDataTotal.toJSONString()));
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error("任务线程" + taskId + " " + String.valueOf(map.get("deviceCode")) + "查询异常:" + ex.getLocalizedMessage() + ";resJson:" + devicerealtimeDataTotal.toJSONString());
|
|
|
+ }
|
|
|
+// break;
|
|
|
+// }
|
|
|
}
|
|
|
//TODO 开始插入数据库
|
|
|
//第三方对应的字段集
|
|
@@ -215,24 +218,54 @@ public class ThreadTask implements Runnable{
|
|
|
|
|
|
private static final SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
public static void adjustTime(JSONObject deviceRealtimeData, int collectionFrequency) {
|
|
|
- // 获取原始时间字符串
|
|
|
- String timeString = deviceRealtimeData.getString("time");
|
|
|
+ // 假设时间字段的名称为 "time"
|
|
|
+ String originalTimeStr = deviceRealtimeData.getString("time"); // 获取原始时间字符串
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
- // 定义日期时间格式
|
|
|
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
+ try {
|
|
|
+ // 将时间字符串解析为 Date 对象
|
|
|
+ Date originalDate = sdf.parse(originalTimeStr);
|
|
|
+ long originalTimeInSeconds = originalDate.getTime() / 1000; // 转换为秒
|
|
|
+
|
|
|
+ // 计算当前时间与时间间隔的余数
|
|
|
+ long remainder = originalTimeInSeconds % collectionFrequency;
|
|
|
+
|
|
|
+ // 找到最接近的时间
|
|
|
+ long adjustedTimeInSeconds;
|
|
|
+ if (remainder < collectionFrequency / 2) {
|
|
|
+ // 如果余数小于一半,向下取整
|
|
|
+ adjustedTimeInSeconds = originalTimeInSeconds - remainder;
|
|
|
+ } else {
|
|
|
+ // 否则向上取整
|
|
|
+ adjustedTimeInSeconds = originalTimeInSeconds + (collectionFrequency - remainder);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将调整后的时间转换回 Date 对象
|
|
|
+ Date adjustedDate = new Date(adjustedTimeInSeconds * 1000);
|
|
|
+
|
|
|
+ // 将调整后的时间格式化为字符串
|
|
|
+ String adjustedTimeStr = sdf.format(adjustedDate);
|
|
|
+
|
|
|
+ // 更新 JSONObject 中的时间
|
|
|
+ deviceRealtimeData.put("time", adjustedTimeStr);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace(); // 处理解析异常
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- // 解析为 LocalDateTime 对象
|
|
|
- LocalDateTime originalTime = LocalDateTime.parse(timeString, formatter);
|
|
|
+ public static void main(String[] args) {
|
|
|
+ // 示例 JSONObject
|
|
|
+ JSONObject deviceRealtimeData = new JSONObject();
|
|
|
+ deviceRealtimeData.put("time", "2024-09-19 16:24:59"); // 示例时间字符串
|
|
|
+ deviceRealtimeData.put("otherData", "exampleData");
|
|
|
|
|
|
- // 计算最接近的时间间隔
|
|
|
- long seconds = originalTime.getSecond();
|
|
|
- long adjustedSeconds = Math.round((double) seconds / collectionFrequency) * collectionFrequency;
|
|
|
+ int collectionFrequency = 600; // 时间间隔为600秒(10分钟)
|
|
|
|
|
|
- // 创建新的时间
|
|
|
- LocalDateTime adjustedTime = originalTime.truncatedTo(ChronoUnit.MINUTES).plusSeconds(adjustedSeconds);
|
|
|
+ // 调整时间
|
|
|
+ adjustTime(deviceRealtimeData, collectionFrequency);
|
|
|
|
|
|
- // 更新 JSON 对象中的时间
|
|
|
- deviceRealtimeData.put("time", adjustedTime.format(formatter));
|
|
|
+ // 输出调整后的结果
|
|
|
+ System.out.println(deviceRealtimeData.toString());
|
|
|
}
|
|
|
|
|
|
}
|