diff --git a/zc-business/src/main/java/com/zc/business/controller/DcDeviceController.java b/zc-business/src/main/java/com/zc/business/controller/DcDeviceController.java index 0dbf335d..2da55385 100644 --- a/zc-business/src/main/java/com/zc/business/controller/DcDeviceController.java +++ b/zc-business/src/main/java/com/zc/business/controller/DcDeviceController.java @@ -11,6 +11,7 @@ import com.ruoyi.common.annotation.Log; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.page.TableDataInfo; +import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.utils.spring.SpringUtils; @@ -22,6 +23,7 @@ import com.zc.business.enums.UniversalEnum; import com.zc.business.interfaces.OperationLog; import com.zc.business.request.DeviceGetPropertiesOperateRequest; import com.zc.business.service.IDcDeviceService; +import com.zc.business.service.IDcWarningService; import com.zc.common.core.httpclient.OkHttp; import com.zc.common.core.httpclient.exception.HttpException; import com.zc.common.core.httpclient.request.RequestParams; @@ -63,7 +65,8 @@ import java.util.stream.Collectors; @RestController @RequestMapping("/business/device") public class DcDeviceController extends BaseController { - + @Resource + private RedisCache redisCache; @Resource private IDcDeviceService dcDeviceService; @Resource @@ -406,8 +409,130 @@ private VideoController videoController; } @ApiOperation("太阳能状况统计") @GetMapping("/properties/history/oneDay/{propertyId}") - public AjaxResult queryDevicePropertiesDay( - @PathVariable @Parameter(description = "属性ID") String propertyId) throws HttpException, IOException, ParseException { + public AjaxResult queryDevicePropertiesDay(@PathVariable @Parameter(description = "属性ID") String propertyId) throws HttpException, IOException, ParseException { +// HashMap props = new HashMap<>(); +// // 设置查询条件的键为“timestamp$BTW”,表示时间戳在一定范围内 +// props.put("terms[0].column", "timestamp$BTW"); +// ArrayList dateList = new ArrayList<>(); +// // 添加当前日期的开始和结束时间到列表,用于设定时间范围 +// dateList.add(DateUtil.beginOfDay(new Date()).toString()); +// dateList.add(DateUtil.endOfDay(new Date()).toString()); +// // 将日期列表以逗号分隔并设置为查询条件的值 +// props.put("terms[0].value", String.join(",", dateList)); +// props.put("paging", false); +// props.put("sorts[0].order", "asc"); +// props.put("sorts[0].name", "timestamp"); +// Map parameter = new HashMap<>(); +// String[] endStakeMark = {"208", "979"}; +// String[] startStakeMark = {"054", "378"}; +// parameter.put("deviceType", "15"); +// parameter.put("endStakeMark", endStakeMark); +// parameter.put("startStakeMark", startStakeMark); +// // parameter.put("deviceState","1"); +// // 创建一个映射来存储按小时汇总的数据 +// Map allDevicesHourlyAggregates = new TreeMap<>(); +// +// List dcDevices = dcDeviceService.devicePileNumberQueryDevice(parameter); +// for (DcDevice dcDevice : dcDevices) { +// AjaxResult ajaxResult = queryDeviceProperties(dcDevice.getIotDeviceId(), propertyId, props); +// if (!ajaxResult.get("code").equals(UniversalEnum.TWO_HUNDRED.getNumber())) { +// return ajaxResult; +// } +// // 定义时间格式 +// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); +// +// Object data = JSON.parseObject(ajaxResult.get("data").toString()).get("data"); +// JSONArray dataArray = JSON.parseArray(data.toString()); +// // 创建一个映射来存储单个设备按小时汇总的数据 +// Map> deviceHourlyLatestData = new TreeMap<>(); +// +// DecimalFormat decimalFormat = new DecimalFormat("#.##"); +// decimalFormat.setRoundingMode(RoundingMode.HALF_UP); +// +// // 处理每个数据点 +// for (Object o : dataArray) { +// JSONObject jsonObject = JSON.parseObject(o.toString()); +// String formatValueStr = jsonObject.getString("formatValue"); +// String timestampStr = jsonObject.getString("timestamp"); +// +// // 将 Unix 时间戳转换为 Date 对象 +// long timestamp = Long.parseLong(timestampStr); +// Date date = new Date(timestamp); +// +// // 将 Date 对象格式化为字符串 +// String formattedTimestamp = sdf.format(date); +// Calendar calendar = Calendar.getInstance(); +// calendar.setTime(date); +// +// // 提取小时部分 +// int hour = calendar.get(Calendar.HOUR_OF_DAY); +// String key = String.format("%d-%02d-%02d %02d:00:00", +// calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, +// calendar.get(Calendar.DAY_OF_MONTH), hour); +// +// // 转换为 double 类型,去除单位 +// String valueWithoutUnit = formatValueStr.replaceAll("[^\\d.]", ""); +// double parsedValue = Double.parseDouble(valueWithoutUnit); +// // 保留两位小数 +// double formatValue = Double.parseDouble(decimalFormat.format(parsedValue)); +// +// // 检查当前小时是否有数据,如果没有则直接添加 +// if (!deviceHourlyLatestData.containsKey(key)) { +// Map 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 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)); +// }); +// } +// } + Map allDevicesHourlyAggregates= redisCache.getCacheObject(propertyId); + return AjaxResult.success(allDevicesHourlyAggregates); + } + + public void solarEnergyConditionStatisticsOne() { + RedisCache redisCache = SpringUtils.getBean(RedisCache.class); + Map stringDoubleMap = solarEnergyConditionStatistics("dailyAccumulatedCharge"); + if (stringDoubleMap != null) { + redisCache.setCacheObject("dailyAccumulatedCharge", stringDoubleMap); + } + } + public void solarEnergyConditionStatisticsTwo(){ + RedisCache redisCache = SpringUtils.getBean(RedisCache.class); + Map stringDoubleMap = solarEnergyConditionStatistics("cumulativeElectricityConsumptionOnTheDay"); + if (stringDoubleMap!=null) { + redisCache.setCacheObject("cumulativeElectricityConsumptionOnTheDay", stringDoubleMap); + } + } + public void solarEnergyConditionStatisticsThree(){ + RedisCache redisCache = SpringUtils.getBean(RedisCache.class); + Map stringDoubleMap = solarEnergyConditionStatistics("generatingPower"); + if (stringDoubleMap!=null){ + redisCache.setCacheObject("generatingPower", stringDoubleMap); + } + } + + public Map solarEnergyConditionStatistics (String propertyId) { + + IDcDeviceService dcDeviceService = SpringUtils.getBean(IDcDeviceService.class); + HashMap props = new HashMap<>(); // 设置查询条件的键为“timestamp$BTW”,表示时间戳在一定范围内 props.put("terms[0].column", "timestamp$BTW"); @@ -432,9 +557,17 @@ private VideoController videoController; List dcDevices = dcDeviceService.devicePileNumberQueryDevice(parameter); for (DcDevice dcDevice : dcDevices) { - AjaxResult ajaxResult = queryDeviceProperties(dcDevice.getIotDeviceId(), propertyId, props); + + AjaxResult ajaxResult = null; + try { + ajaxResult = queryDeviceProperties(dcDevice.getIotDeviceId(), propertyId, props); + } catch (HttpException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } if (!ajaxResult.get("code").equals(UniversalEnum.TWO_HUNDRED.getNumber())) { - return ajaxResult; + return null; } // 定义时间格式 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @@ -501,10 +634,8 @@ private VideoController videoController; }); } } - return AjaxResult.success(allDevicesHourlyAggregates); + return allDevicesHourlyAggregates; } - - @ApiOperation("太阳能重点数据") @GetMapping("/properties/latestOne") public AjaxResult getDeviceLatest() throws HttpException, IOException {