From cca368206b6257262f98a3b7399ef8ad8be02196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=85=B4=E7=90=B3?= <1911390090@qq.com> Date: Fri, 20 Dec 2024 10:43:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=AA=E9=98=B3=E8=83=BD=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/DcDeviceController.java | 62 ++++++++++++++----- 1 file changed, 48 insertions(+), 14 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 4acb2422..0dbf335d 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 @@ -529,27 +529,61 @@ private VideoController videoController; decimalFormat.setRoundingMode(RoundingMode.HALF_UP); for (DcDevice dcDevice : dcDevices) { String deviceId = dcDevice.getIotDeviceId(); - for (String propertyId : propertyIds) { +// for (String propertyId : propertyIds) { try (Response response = new OkHttpClient().newCall(new Request.Builder() - .url(iotAddress + UniversalEnum.GETS_THE_LATEST_DATA_ABOUT_THE_SPECIFIED_ATTRIBUTES_OF_A_DEVICE.getValue() + deviceId + UniversalEnum.SLASH.getValue() + propertyId) + .url(iotAddress + UniversalEnum.GETS_THE_LATEST_DATA_ABOUT_THE_SPECIFIED_ATTRIBUTES_OF_A_DEVICE.getValue() + deviceId + UniversalEnum.SLASH.getValue()) .build()).execute()) { String responseBody = response.body().string(); - JsonObject jsonObject = JsonParser.parseString(responseBody).getAsJsonObject(); - if (jsonObject.has("data"))// 确认 "data" 字段是否存在 +// JsonObject jsonObject = JsonParser.parseString(responseBody).getAsJsonObject(); + JSONObject jsonObject = JSONObject.parseObject(responseBody); + JSONArray jsonArray = jsonObject.getJSONArray("data"); + if (jsonArray!=null)// 确认 "data" 字段是否存在 { - JsonObject data = jsonObject.getAsJsonObject("data"); - String property = data.get("property").getAsString(); - String formatValueStr = data.get("formatValue").getAsString(); - String valueWithoutUnit = formatValueStr.replaceAll("[^\\d.]", ""); - - double value2 = Double.parseDouble(valueWithoutUnit); - double value = Double.parseDouble(decimalFormat.format(value2)); - // 在合并时重新应用格式化 - attributeMap.merge(property, value, (oldVal, newVal) -> Double.parseDouble(decimalFormat.format(oldVal + newVal))); +// JsonArray data = jsonObject.getAsJsonArray("data"); + List<Object> cumulativeElectricityConsumptionInTheYearList = jsonArray.stream().filter(item -> ((JSONObject) JSON.toJSON(item)).getString("property").equals("cumulativeElectricityConsumptionInTheYear")).collect(Collectors.toList()); + String cumulativeElectricityConsumptionInTheYear = ""; + if (cumulativeElectricityConsumptionInTheYearList.size()>0) { + cumulativeElectricityConsumptionInTheYear = ((JSONObject) JSON.toJSON(cumulativeElectricityConsumptionInTheYearList.get(0))).getString("formatValue").replaceAll("[^\\d.]", ""); + }else { + cumulativeElectricityConsumptionInTheYear = "0"; + } + List<Object> cumulativeElectricityConsumptionOnTheDayList = jsonArray.stream().filter(item -> ((JSONObject) JSON.toJSON(item)).getString("property").equals("cumulativeElectricityConsumptionOnTheDay")).collect(Collectors.toList()); + String cumulativeElectricityConsumptionOnTheDay = ""; + if (cumulativeElectricityConsumptionOnTheDayList.size()>0) { + cumulativeElectricityConsumptionOnTheDay = ((JSONObject) JSON.toJSON(cumulativeElectricityConsumptionOnTheDayList.get(0))).getString("formatValue").replaceAll("[^\\d.]", ""); + }else { + cumulativeElectricityConsumptionOnTheDay = "0"; + } + List<Object> theAccumulatedChargeOfTheYearList = jsonArray.stream().filter(item -> ((JSONObject) JSON.toJSON(item)).getString("property").equals("theAccumulatedChargeOfTheYear")).collect(Collectors.toList()); + String theAccumulatedChargeOfTheYear = ""; + if (theAccumulatedChargeOfTheYearList.size()>0) { + theAccumulatedChargeOfTheYear = ((JSONObject) JSON.toJSON(theAccumulatedChargeOfTheYearList.get(0))).getString("formatValue").replaceAll("[^\\d.]", ""); + }else { + theAccumulatedChargeOfTheYear = "0"; + } + List<Object> dailyAccumulatedChargeList = jsonArray.stream().filter(item -> ((JSONObject) JSON.toJSON(item)).getString("property").equals("dailyAccumulatedCharge")).collect(Collectors.toList()); + String dailyAccumulatedCharge = ""; + if (theAccumulatedChargeOfTheYearList.size()>0) { + dailyAccumulatedCharge = ((JSONObject) JSON.toJSON(dailyAccumulatedChargeList.get(0))).getString("formatValue").replaceAll("[^\\d.]", ""); + }else { + dailyAccumulatedCharge = "0"; + } +// String property = data.get("property").getAsString(); +// String formatValueStr = data.get("formatValue").getAsString(); +// String valueWithoutUnit = formatValueStr.replaceAll("[^\\d.]", ""); +// +// double value2 = Double.parseDouble(valueWithoutUnit); +// double value = Double.parseDouble(decimalFormat.format(value2)); +// // 在合并时重新应用格式化 + attributeMap.merge("cumulativeElectricityConsumptionInTheYear", Double.parseDouble(decimalFormat.format(Double.parseDouble(cumulativeElectricityConsumptionInTheYear))), (oldVal, newVal) -> Double.parseDouble(decimalFormat.format(oldVal + newVal))); + attributeMap.merge("cumulativeElectricityConsumptionOnTheDay", Double.parseDouble(decimalFormat.format(Double.parseDouble(cumulativeElectricityConsumptionOnTheDay))), (oldVal, newVal) -> Double.parseDouble(decimalFormat.format(oldVal + newVal))); + attributeMap.merge("theAccumulatedChargeOfTheYear", Double.parseDouble(decimalFormat.format(Double.parseDouble(theAccumulatedChargeOfTheYear))), (oldVal, newVal) -> Double.parseDouble(decimalFormat.format(oldVal + newVal))); + attributeMap.merge("dailyAccumulatedCharge", Double.parseDouble(decimalFormat.format(Double.parseDouble(dailyAccumulatedCharge))), (oldVal, newVal) -> Double.parseDouble(decimalFormat.format(oldVal + newVal))); +// attributeMap.merge(property, value, (oldVal, newVal) -> Double.parseDouble(decimalFormat.format(oldVal + newVal))); } } - } +// } }