From 49448956220f2d647d867ed21e38f5cfd0ecc794 Mon Sep 17 00:00:00 2001 From: tianyongbao Date: Mon, 23 Mar 2026 23:49:57 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=8E=B7=E5=8F=96=E5=BC=80=E5=85=B3?= =?UTF-8?q?=E7=94=B5=E5=8E=8B=E5=92=8C=E7=94=B5=E6=B5=81=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../intc/iot/handler/DeviceDataHandler.java | 54 +++++++------------ 1 file changed, 20 insertions(+), 34 deletions(-) diff --git a/intc-modules/intc-iot/src/main/java/com/intc/iot/handler/DeviceDataHandler.java b/intc-modules/intc-iot/src/main/java/com/intc/iot/handler/DeviceDataHandler.java index 3934493..de0467b 100644 --- a/intc-modules/intc-iot/src/main/java/com/intc/iot/handler/DeviceDataHandler.java +++ b/intc-modules/intc-iot/src/main/java/com/intc/iot/handler/DeviceDataHandler.java @@ -1973,56 +1973,42 @@ public class DeviceDataHandler { } // 处理电压电流数据 + // 上报结构: switch{n}_VoltCur -> {"A":"{\"volt\":224,\"cur\":0.00}","B":"...","C":"..."} String voltCurKey = "switch" + switchIndex + "_VoltCur"; if (params.containsKey(voltCurKey)) { try { JSONObject voltCurData = params.getJSONObject(voltCurKey); - // 解析A、B、C三相数据,找到非0的值 Double voltage = null; Double current = null; + // 解析A、B、C三相数据,每个相的值是JSON字符串,需要二次解析 for (String phase : new String[]{"A", "B", "C"}) { - if (voltCurData.containsKey(phase)) { - String phaseDataStr = voltCurData.getStr(phase); - if (phaseDataStr != null && !phaseDataStr.isEmpty()) { - try { - // 解析JSON字符串,例如: {"volt":227,"cur":0.00} - JSONObject phaseData = JSONUtil.parseObj(phaseDataStr); - Double phaseVolt = phaseData.getDouble("volt"); - Double phaseCur = phaseData.getDouble("cur"); + if (!voltCurData.containsKey(phase)) continue; + String phaseDataStr = voltCurData.getStr(phase); + if (phaseDataStr == null || phaseDataStr.isEmpty()) continue; + try { + JSONObject phaseData = JSONUtil.parseObj(phaseDataStr); + Double phaseVolt = phaseData.getDouble("volt"); + Double phaseCur = phaseData.getDouble("cur"); - // 只取非0的值 - if (phaseVolt != null && phaseVolt > 0) { - voltage = phaseVolt; - } - if (phaseCur != null && phaseCur > 0) { - current = phaseCur; - } - - // 如果已经找到非0的值,可以提前退出 - if (voltage != null && voltage > 0 && current != null && current > 0) { - break; - } - } catch (Exception e) { - log.warn("解析相{}的电压电流数据失败: {}", phase, e.getMessage()); - } + if (phaseVolt != null && phaseVolt > 0 && voltage == null) { + voltage = phaseVolt; } + if (phaseCur != null && phaseCur > 0 && current == null) { + current = phaseCur; + } + if (voltage != null && current != null) break; + } catch (Exception e) { + log.warn("解析相{}的电压电流数据失败: {}", phase, e.getMessage()); } } - // 如果找到了非0的电压或电流,更新到数据库 - if ((voltage != null && voltage > 0) || (current != null && current > 0)) { + if (voltage != null || current != null) { com.intc.fishery.domain.DeviceSwitch updateVoltCur = new com.intc.fishery.domain.DeviceSwitch(); updateVoltCur.setId(sw.getId()); - - if (voltage != null && voltage > 0) { - updateVoltCur.setDetectVoltageValue(voltage); - } - if (current != null && current > 0) { - updateVoltCur.setDetectElectricValue(current); - } - + if (voltage != null) updateVoltCur.setDetectVoltageValue(voltage); + if (current != null) updateVoltCur.setDetectElectricValue(current); deviceSwitchMapper.updateById(updateVoltCur); log.debug("更新开关电压电流: 设备={}, 开关索引={}, 电压={}V, 电流={}A", deviceName, switchIndex, voltage, current);