fix: 获取开关电压和电流逻辑修改。

This commit is contained in:
tianyongbao
2026-03-23 23:49:57 +08:00
parent 0526aefb01
commit 4944895622

View File

@@ -1973,56 +1973,42 @@ public class DeviceDataHandler {
} }
// 处理电压电流数据 // 处理电压电流数据
// 上报结构: switch{n}_VoltCur -> {"A":"{\"volt\":224,\"cur\":0.00}","B":"...","C":"..."}
String voltCurKey = "switch" + switchIndex + "_VoltCur"; String voltCurKey = "switch" + switchIndex + "_VoltCur";
if (params.containsKey(voltCurKey)) { if (params.containsKey(voltCurKey)) {
try { try {
JSONObject voltCurData = params.getJSONObject(voltCurKey); JSONObject voltCurData = params.getJSONObject(voltCurKey);
// 解析A、B、C三相数据找到非0的值
Double voltage = null; Double voltage = null;
Double current = null; Double current = null;
// 解析A、B、C三相数据每个相的值是JSON字符串需要二次解析
for (String phase : new String[]{"A", "B", "C"}) { for (String phase : new String[]{"A", "B", "C"}) {
if (voltCurData.containsKey(phase)) { if (!voltCurData.containsKey(phase)) continue;
String phaseDataStr = voltCurData.getStr(phase); String phaseDataStr = voltCurData.getStr(phase);
if (phaseDataStr != null && !phaseDataStr.isEmpty()) { if (phaseDataStr == null || phaseDataStr.isEmpty()) continue;
try { try {
// 解析JSON字符串例如: {"volt":227,"cur":0.00}
JSONObject phaseData = JSONUtil.parseObj(phaseDataStr); JSONObject phaseData = JSONUtil.parseObj(phaseDataStr);
Double phaseVolt = phaseData.getDouble("volt"); Double phaseVolt = phaseData.getDouble("volt");
Double phaseCur = phaseData.getDouble("cur"); Double phaseCur = phaseData.getDouble("cur");
// 只取非0的值 if (phaseVolt != null && phaseVolt > 0 && voltage == null) {
if (phaseVolt != null && phaseVolt > 0) {
voltage = phaseVolt; voltage = phaseVolt;
} }
if (phaseCur != null && phaseCur > 0) { if (phaseCur != null && phaseCur > 0 && current == null) {
current = phaseCur; current = phaseCur;
} }
if (voltage != null && current != null) break;
// 如果已经找到非0的值可以提前退出
if (voltage != null && voltage > 0 && current != null && current > 0) {
break;
}
} catch (Exception e) { } catch (Exception e) {
log.warn("解析相{}的电压电流数据失败: {}", phase, e.getMessage()); log.warn("解析相{}的电压电流数据失败: {}", phase, e.getMessage());
} }
} }
}
}
// 如果找到了非0的电压或电流更新到数据库 if (voltage != null || current != null) {
if ((voltage != null && voltage > 0) || (current != null && current > 0)) {
com.intc.fishery.domain.DeviceSwitch updateVoltCur = new com.intc.fishery.domain.DeviceSwitch(); com.intc.fishery.domain.DeviceSwitch updateVoltCur = new com.intc.fishery.domain.DeviceSwitch();
updateVoltCur.setId(sw.getId()); updateVoltCur.setId(sw.getId());
if (voltage != null) updateVoltCur.setDetectVoltageValue(voltage);
if (voltage != null && voltage > 0) { if (current != null) updateVoltCur.setDetectElectricValue(current);
updateVoltCur.setDetectVoltageValue(voltage);
}
if (current != null && current > 0) {
updateVoltCur.setDetectElectricValue(current);
}
deviceSwitchMapper.updateById(updateVoltCur); deviceSwitchMapper.updateById(updateVoltCur);
log.debug("更新开关电压电流: 设备={}, 开关索引={}, 电压={}V, 电流={}A", log.debug("更新开关电压电流: 设备={}, 开关索引={}, 电压={}V, 电流={}A",
deviceName, switchIndex, voltage, current); deviceName, switchIndex, voltage, current);