Browse Source

太阳能板数据监测小时数据修改

develop
王兴琳 5 days ago
parent
commit
2e53762e34
  1. 40
      zc-business/src/main/java/com/zc/business/controller/DcDeviceController.java

40
zc-business/src/main/java/com/zc/business/controller/DcDeviceController.java

@ -416,7 +416,7 @@ private VideoController videoController;
dateList.add(DateUtil.beginOfDay(new Date()).toString());
dateList.add(DateUtil.endOfDay(new Date()).toString());
// 将日期列表以逗号分隔并设置为查询条件的值
props.put("terms[0].value", String.join(UniversalEnum.COMMA.getValue(), dateList));
props.put("terms[0].value", String.join(",", dateList));
props.put("paging", false);
props.put("sorts[0].order", "asc");
props.put("sorts[0].name", "timestamp");
@ -439,12 +439,11 @@ private VideoController videoController;
// 定义时间格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Object data = JSON.parseObject(queryDeviceProperties(dcDevice.getIotDeviceId(), propertyId, props).get("data").toString()).get("data");
Object data = JSON.parseObject(ajaxResult.get("data").toString()).get("data");
JSONArray dataArray = JSON.parseArray(data.toString());
// 创建一个映射来存储单个设备按小时汇总的数据
Map<String, Double> deviceHourlyAggregates = new TreeMap<>();
Map<String, Map<String, Object>> deviceHourlyLatestData = new TreeMap<>();
List<Map<String, Object>> list = new ArrayList<>();
DecimalFormat decimalFormat = new DecimalFormat("#.##");
decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
@ -471,22 +470,41 @@ private VideoController videoController;
// 转换为 double 类型,去除单位
String valueWithoutUnit = formatValueStr.replaceAll("[^\\d.]", "");
// double formatValue = Double.parseDouble(valueWithoutUnit);
double parsedValue = Double.parseDouble(valueWithoutUnit);
// 保留两位小数
double formatValue = Double.parseDouble(decimalFormat.format(parsedValue));
// 汇总每个小时的数据
deviceHourlyAggregates.merge(key, formatValue, (oldVal, newVal) -> Double.parseDouble(decimalFormat.format(oldVal + newVal))); }
// 将设备的数据合并到所有设备的汇总数据中
for (String key : deviceHourlyAggregates.keySet()) {
allDevicesHourlyAggregates.merge(key, deviceHourlyAggregates.get(key), (oldVal, newVal) -> Double.parseDouble(decimalFormat.format(oldVal + newVal)));
}
// 检查当前小时是否有数据,如果没有则直接添加
if (!deviceHourlyLatestData.containsKey(key)) {
Map<String, Object> dataPoint = new HashMap<>();
dataPoint.put("value", formatValue);
dataPoint.put("timestamp", timestamp);
deviceHourlyLatestData.put(key, dataPoint);
} else {
// 如果已经有数据,比较时间戳,保留最新的数据
long existingTimestamp = (long) deviceHourlyLatestData.get(key).get("timestamp");
if (timestamp > existingTimestamp) {
Map<String, Object> dataPoint = new HashMap<>();
dataPoint.put("value", formatValue);
dataPoint.put("timestamp", timestamp);
deviceHourlyLatestData.put(key, dataPoint);
}
}
}
// 将设备的数据合并到所有设备的汇总数据中
for (String key : deviceHourlyLatestData.keySet()) {
double latestValue = (double) deviceHourlyLatestData.get(key).get("value");
allDevicesHourlyAggregates.merge(key, latestValue, (oldVal, newVal) -> {
double sum = oldVal + newVal;
return Double.parseDouble(decimalFormat.format(sum));
});
}
}
return AjaxResult.success(allDevicesHourlyAggregates);
}
@ApiOperation("太阳能重点数据")
@GetMapping("/properties/latestOne")
public AjaxResult getDeviceLatest() throws HttpException, IOException {

Loading…
Cancel
Save