fix: 获取开关电压和电流逻辑修改。
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user