From 3b9233341759039a23bf5ee3ce28bbeab53e1722 Mon Sep 17 00:00:00 2001 From: wangsixiang <2970484253@qq.com> Date: Tue, 30 Jul 2024 09:46:36 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E5=88=86=E6=9E=90=E5=BC=82?= =?UTF-8?q?=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/DcDeviceController.java | 114 +++++++++--------- 1 file changed, 59 insertions(+), 55 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 dec4d9d1..07d5677d 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 @@ -985,62 +985,66 @@ public class DcDeviceController extends BaseController { 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); + try { + 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); } - resultsMapValue.add(resultsMap); + }catch (Exception e){ + e.printStackTrace(); } return AjaxResult.success(resultsMapValue); }