From 4f82abb999a9ef8c08c561a55fa42d338a6ca13f Mon Sep 17 00:00:00 2001 From: wangsixiang <2970484253@qq.com> Date: Mon, 1 Jul 2024 14:54:17 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/DcDeviceController.java | 100 ++++++++++++++++-- 1 file changed, 91 insertions(+), 9 deletions(-) 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 723f5748..e36a030f 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 @@ -719,15 +719,8 @@ public class DcDeviceController extends BaseController { return getDataTable(dcDeviceService.selectDeviceNameList(dcDevice)); } @GetMapping("/deviceParameter") - public TableDataInfo selectDeviceParameterProperties(DcDevice dcDevice) { - if (dcDevice==null||dcDevice.getDeviceType()==null){ - TableDataInfo tableDataInfo = new TableDataInfo(); - tableDataInfo.setCode(UniversalEnum.FIVE_HUNDRED.getNumber()); - tableDataInfo.setMsg(UniversalEnum.PARAMETER_ERROR.getValue()); - return tableDataInfo; - } - startPage(); - return getDataTable(dcDeviceService.selectDeviceParameterProperties(dcDevice)); + public AjaxResult selectDeviceParameterProperties(DcDevice dcDevice) { + return AjaxResult.success(dcDeviceService.selectDeviceParameterProperties(dcDevice)); } /** * 查询设备指定属性属性列表 @@ -828,5 +821,94 @@ public class DcDeviceController extends BaseController { String key = configService.selectConfigByKey(UniversalEnum.SBX_FUN_KL.getValue());//密钥 return AjaxResult.success(key.equals(secretKey)); } + //设备分析 + @PostMapping("/properties/deviceAnalysis") + public AjaxResult deviceAnalysis(@RequestBody HashMap map) throws IOException, HttpException { + if (map==null||!map.containsKey("deviceId")||!map.containsKey("deviceType") ||!map.containsKey("dateTime")){ + return AjaxResult.error(UniversalEnum.PARAMETER_ERROR.getValue()); + } + String type = map.get("deviceType").toString();//设备类型 + String deviceId = map.get("deviceId").toString();//设备id + DcDevice dcDevice = new DcDevice(); + dcDevice.setDeviceType(type); + HashMap props = new HashMap<>(); + // 设置查询条件的键为“timestamp$BTW”,表示时间戳在一定范围内 + props.put("terms[0].column", "timestamp$BTW"); + ArrayList dateList = new ArrayList<>(); + // 添加当前日期的开始和结束时间到列表,用于设定时间范围 + SimpleDateFormat dateFormat = new SimpleDateFormat(UniversalEnum.TIME_FORMAT_YEARS_MONTH_DAY.getValue()); + try { + Date date = dateFormat.parse((String) map.get("dateTime")); + dateList.add(DateUtil.beginOfDay(date).toString()); + dateList.add(DateUtil.endOfDay(date).toString()); + } catch (ParseException e) { + e.printStackTrace(); + } + // 将日期列表以逗号分隔并设置为查询条件的值 + props.put("terms[0].value", String.join(UniversalEnum.COMMA.getValue(), dateList)); + props.put("paging", false); + props.put("sorts[0].order", "asc"); + props.put("sorts[0].name", "timestamp"); + List> mapList = dcDeviceService.selectDeviceParameterProperties(dcDevice);//属性的参数信息 + List resultsMapValue=new ArrayList<>(); + for (HashMap hashMap : mapList) { + HashMap resultsMap = new HashMap<>(); + String name = hashMap.get("name").toString();//属性名称 + String propertyId = hashMap.get("propertyId").toString();//属性id + String propertyName = hashMap.get("propertyName").toString();//属性取值 + AjaxResult ajaxResult = queryDeviceProperties(deviceId, propertyId, props);//取出属性全部信息 + String code = ajaxResult.get("code").toString(); + if ("500".equals(code)) { + return ajaxResult; + } + if (UniversalEnum.THIRTEEN.getValue().equals(type) || UniversalEnum.SIXTEEN.getValue().equals(type)) { //判断是否为设备箱/远端机 + JSONObject data = (JSONObject) ajaxResult.get("data"); + JSONArray dataVale = (JSONArray) data.get("data"); + List jsonObjectList = dataVale.toJavaList(JSONObject.class); + // 定义一个时间格式转换的工具 + SimpleDateFormat sdf = new SimpleDateFormat(UniversalEnum.TIME_FORMAT_ALL.getValue(), Locale.getDefault()); + // 使用Stream API提取每个JSONObject中的timestamp和propertyName值 + List> results = jsonObjectList.stream() + .map(jsonObject -> { + long timestamp = jsonObject.getLongValue("timestamp"); // 假设timestamp是long类型 + Date date = new Date(timestamp); // 将时间戳转换为Date对象 + String formattedTime = sdf.format(date); // 将Date对象格式化为字符串 + JSONObject formatValue = jsonObject.getJSONObject("formatValue"); // 获取嵌套的JSONObject + String result = formatValue.getString(propertyName); // 从嵌套的JSONObject中获取电压值 + Map hashMaps = new HashMap<>(); + hashMaps.put("timestamp", formattedTime); // 使用"timestamp"作为键来存储格式化后的时间 + hashMaps.put("result", result); // 使用"voltage"作为键来存储电压值 + return hashMaps; + }).collect(Collectors.toList()); + results.sort(Comparator.comparing(hashMaps -> hashMaps.get("timestamp"))); + resultsMap.put("name", name); + resultsMap.put("results", results); + } + if (UniversalEnum.THREE.getValue().equals(type) || UniversalEnum.SEVENTEEN.getValue().equals(type) || + UniversalEnum.FIFTEEN.getValue().equals(type)) { //气象预警/ups设备/太阳能板 + JSONObject data = (JSONObject) ajaxResult.get("data"); + JSONArray dataValue = (JSONArray) data.get("data"); + List jsonObjectList = dataValue.toJavaList(JSONObject.class); + // 定义一个时间格式转换的工具 + SimpleDateFormat sdf = new SimpleDateFormat(UniversalEnum.TIME_FORMAT_ALL.getValue(), Locale.getDefault()); + List> results = jsonObjectList.stream() + .map(jsonObject -> { + long timestamp = jsonObject.getLongValue("timestamp"); // 假设timestamp是long类型 + Date date = new Date(timestamp); // 将时间戳转换为Date对象 + String formattedTime = sdf.format(date); // 将Date对象格式化为字符串 + String result = jsonObject.getString(propertyName); + HashMap hashMaps = new HashMap<>(); + hashMaps.put("timestamp", formattedTime); // 使用时间戳作为键可能不是最佳实践,但这里保持与原始逻辑一致 + hashMaps.put("result", result); + return hashMaps; + }).collect(Collectors.toList()); + results.sort(Comparator.comparing(hashMaps -> hashMaps.get("timestamp"))); + resultsMap.put("name", name); + resultsMap.put("results", results); + } + resultsMapValue.add(resultsMap); + } + return AjaxResult.success(resultsMapValue); + } }