|
|
@ -20,6 +20,8 @@ import lombok.RequiredArgsConstructor; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.AbstractMap; |
|
|
|
|
|
import java.util.ArrayList; |
|
|
import java.util.HashMap; |
|
|
import java.util.HashMap; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.Map; |
|
|
import java.util.Map; |
|
|
@ -97,15 +99,19 @@ public class CsdjService { |
|
|
|
|
|
|
|
|
// 4. 处理デジタル入力端子(告警/恢复)
|
|
|
// 4. 处理デジタル入力端子(告警/恢复)
|
|
|
List<CsdjDigitalInputTerminal> digitalTerminals = notificationData.getDigitalInputTerminals(); |
|
|
List<CsdjDigitalInputTerminal> digitalTerminals = notificationData.getDigitalInputTerminals(); |
|
|
|
|
|
Map<String, Long> deviceAlertHistoryMap = new HashMap<>(); |
|
|
if (digitalTerminals != null && !digitalTerminals.isEmpty()) { |
|
|
if (digitalTerminals != null && !digitalTerminals.isEmpty()) { |
|
|
for (CsdjDigitalInputTerminal terminal : digitalTerminals) { |
|
|
for (CsdjDigitalInputTerminal terminal : digitalTerminals) { |
|
|
processDigitalTerminal(csdjEntity, notificationData, terminal, deviceEntity); |
|
|
Map.Entry<String, Long> entry = processDigitalTerminal(csdjEntity, notificationData, terminal, deviceEntity); |
|
|
|
|
|
if (entry.getValue() != null) { |
|
|
|
|
|
deviceAlertHistoryMap.put(entry.getKey(), entry.getValue()); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 5. 更新 csdj_device_realtime 表
|
|
|
// 5. 更新 csdj_device_realtime 表
|
|
|
Long companyId = deviceEntity.getCompanyId(); |
|
|
Long companyId = deviceEntity.getCompanyId(); |
|
|
saveDeviceRealtime(terminalId, companyId, csdjEntity.getData(), notificationData); |
|
|
saveDeviceRealtime(terminalId, companyId, csdjEntity.getData(), notificationData, deviceAlertHistoryMap); |
|
|
|
|
|
|
|
|
log.info("[CSDJ] 处理 CSDJ 数据完成,terminalId:{}", csdjEntity.getTerminalId()); |
|
|
log.info("[CSDJ] 处理 CSDJ 数据完成,terminalId:{}", csdjEntity.getTerminalId()); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
@ -113,7 +119,7 @@ public class CsdjService { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void processDigitalTerminal(CsdjEntity csdjEntity, CsdjNotificationData notificationData, |
|
|
private Map.Entry<String, Long> processDigitalTerminal(CsdjEntity csdjEntity, CsdjNotificationData notificationData, |
|
|
CsdjDigitalInputTerminal terminal, DeviceEntity deviceEntity) { |
|
|
CsdjDigitalInputTerminal terminal, DeviceEntity deviceEntity) { |
|
|
String terminalId = csdjEntity.getTerminalId(); |
|
|
String terminalId = csdjEntity.getTerminalId(); |
|
|
String deviceId = terminal.getDeviceId(); |
|
|
String deviceId = terminal.getDeviceId(); |
|
|
@ -147,12 +153,14 @@ public class CsdjService { |
|
|
entity.setDeviceId(terminal.getDeviceId()); |
|
|
entity.setDeviceId(terminal.getDeviceId()); |
|
|
entity.setRetainAlert(deviceEntity.getRetainAlert()); |
|
|
entity.setRetainAlert(deviceEntity.getRetainAlert()); |
|
|
|
|
|
|
|
|
|
|
|
Long dbId = null; |
|
|
|
|
|
|
|
|
if (isAlarm) { |
|
|
if (isAlarm) { |
|
|
dataProcessService.setAlertBlock(deviceInfoVO); |
|
|
dataProcessService.setAlertBlock(deviceInfoVO); |
|
|
entity.setAlertBlock(deviceInfoVO.getAlertBlock()); |
|
|
entity.setAlertBlock(deviceInfoVO.getAlertBlock()); |
|
|
|
|
|
|
|
|
entity.setReceive_ts(csdjEntity.getTs()); |
|
|
entity.setReceive_ts(csdjEntity.getTs()); |
|
|
Long dbId = dashboardAlertDao.insertAlertHistory(entity, 3); |
|
|
dbId = dashboardAlertDao.insertAlertHistory(entity, 3); |
|
|
csdjAlarmHistory.setAlertHistoryId(dbId); |
|
|
csdjAlarmHistory.setAlertHistoryId(dbId); |
|
|
|
|
|
|
|
|
csdjAlarmHistory.setAlarmTime(notificationData.getNotificationTime()); |
|
|
csdjAlarmHistory.setAlarmTime(notificationData.getNotificationTime()); |
|
|
@ -175,6 +183,7 @@ public class CsdjService { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
csdjDao.insertAlarm(csdjAlarmHistory); |
|
|
csdjDao.insertAlarm(csdjAlarmHistory); |
|
|
|
|
|
return new AbstractMap.SimpleEntry<>(deviceId, dbId); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -239,113 +248,155 @@ public class CsdjService { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void saveDeviceRealtime(String terminalId, Long companyId, String rawHex, |
|
|
private void saveDeviceRealtime(String terminalId, Long companyId, String rawHex, |
|
|
CsdjNotificationData data) { |
|
|
CsdjNotificationData data, Map<String, Long> deviceAlertHistoryMap) { |
|
|
try { |
|
|
try { |
|
|
// 1. 先从数据库读取旧数据
|
|
|
// 1. 收集所有 deviceId
|
|
|
CsdjDeviceRealtime deviceRealtime = csdjDao.getDeviceRealtime(terminalId); |
|
|
List<String> deviceIds = new ArrayList<>(); |
|
|
if (deviceRealtime == null) { |
|
|
if (data.getDigitalInputTerminals() != null) { |
|
|
// 如果是新设备,初始化
|
|
|
for (CsdjDigitalInputTerminal t : data.getDigitalInputTerminals()) { |
|
|
deviceRealtime = new CsdjDeviceRealtime(); |
|
|
if (t.getDeviceId() != null) deviceIds.add(t.getDeviceId()); |
|
|
deviceRealtime.setTerminalId(terminalId); |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (data.getAnalogInputTerminals() != null) { |
|
|
|
|
|
for (CsdjAnalogInputTerminal t : data.getAnalogInputTerminals()) { |
|
|
|
|
|
if (t.getDeviceId() != null) deviceIds.add(t.getDeviceId()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (data.getDigitalOutputTerminals() != null) { |
|
|
|
|
|
for (CsdjDigitalOutputTerminal t : data.getDigitalOutputTerminals()) { |
|
|
|
|
|
if (t.getDeviceId() != null) deviceIds.add(t.getDeviceId()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
CsdjSystemState systemState = data.getSystemState(); |
|
|
|
|
|
if (systemState != null && systemState.getDeviceId() != null) { |
|
|
|
|
|
deviceIds.add(systemState.getDeviceId()); |
|
|
} |
|
|
} |
|
|
deviceRealtime.setDeviceId(terminalId); |
|
|
|
|
|
deviceRealtime.setCompanyId(companyId); |
|
|
|
|
|
deviceRealtime.setLastNotificationTime(data.getNotificationTime()); |
|
|
|
|
|
deviceRealtime.setLastNotificationTimestamp(data.getJapanTimestamp()); |
|
|
|
|
|
deviceRealtime.setLastRawHex(rawHex); |
|
|
|
|
|
|
|
|
|
|
|
// 2. 只更新此次上报的数据
|
|
|
// 2. 为每个 deviceId 创建/更新一行
|
|
|
|
|
|
String digitalInputJson = data.getDigitalInputTerminals() != null && !data.getDigitalInputTerminals().isEmpty() |
|
|
|
|
|
? objectMapper.writeValueAsString(data.getDigitalInputTerminals()) : null; |
|
|
|
|
|
String analogInputJson = data.getAnalogInputTerminals() != null && !data.getAnalogInputTerminals().isEmpty() |
|
|
|
|
|
? objectMapper.writeValueAsString(data.getAnalogInputTerminals()) : null; |
|
|
|
|
|
String digitalOutputJson = data.getDigitalOutputTerminals() != null && !data.getDigitalOutputTerminals().isEmpty() |
|
|
|
|
|
? objectMapper.writeValueAsString(data.getDigitalOutputTerminals()) : null; |
|
|
|
|
|
|
|
|
|
|
|
for (String deviceId : deviceIds) { |
|
|
|
|
|
// 先读取旧数据,没有则新建
|
|
|
|
|
|
CsdjDeviceRealtime row = csdjDao.getDeviceRealtime(terminalId, deviceId); |
|
|
|
|
|
if (row == null) { |
|
|
|
|
|
row = new CsdjDeviceRealtime(); |
|
|
|
|
|
} |
|
|
|
|
|
row.setTerminalId(terminalId); |
|
|
|
|
|
row.setDeviceId(deviceId); |
|
|
|
|
|
row.setCompanyId(companyId); |
|
|
|
|
|
row.setLastNotificationTime(data.getNotificationTime()); |
|
|
|
|
|
row.setLastNotificationTimestamp(data.getJapanTimestamp()); |
|
|
|
|
|
row.setLastRawHex(rawHex); |
|
|
|
|
|
|
|
|
|
|
|
// JSON字段所有行都设置
|
|
|
|
|
|
row.setDigitalInputJson(digitalInputJson); |
|
|
|
|
|
row.setAnalogInputJson(analogInputJson); |
|
|
|
|
|
row.setDigitalOutputJson(digitalOutputJson); |
|
|
|
|
|
|
|
|
|
|
|
// 根据 deviceId 填充匹配的端子数据
|
|
|
|
|
|
fillDigitalInput(row, deviceId, data.getDigitalInputTerminals()); |
|
|
|
|
|
fillAnalogInput(row, deviceId, data.getAnalogInputTerminals()); |
|
|
|
|
|
fillDigitalOutput(row, deviceId, data.getDigitalOutputTerminals()); |
|
|
|
|
|
|
|
|
|
|
|
// 系统状态所有行都设置
|
|
|
|
|
|
if (systemState != null) { |
|
|
|
|
|
row.setSystemStateNo(systemState.getStateNo()); |
|
|
|
|
|
row.setSystemScheduledTimeNo(systemState.getScheduledTimeNo()); |
|
|
|
|
|
row.setSystemModeState(systemState.getModeState()); |
|
|
|
|
|
row.setSystemIsPowerOff(systemState.getIsPowerOff() ? 1 : 0); |
|
|
|
|
|
row.setSystemIsBatteryLow(systemState.getIsBatteryLow() ? 1 : 0); |
|
|
|
|
|
row.setSystemIsLineDisconnected(systemState.getIsLineDisconnected() ? 1 : 0); |
|
|
|
|
|
row.setSystemIsSlaveCommunicationError(systemState.getIsSlaveCommunicationError() ? 1 : 0); |
|
|
|
|
|
row.setSystemStateRaw(systemState.getStateRaw()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 处理デジタル入力端子
|
|
|
// 设置告警历史ID
|
|
|
if (data.getDigitalInputTerminals() != null && !data.getDigitalInputTerminals().isEmpty()) { |
|
|
if (deviceAlertHistoryMap != null) { |
|
|
List<CsdjDigitalInputTerminal> list = data.getDigitalInputTerminals(); |
|
|
Long alertHistoryId = deviceAlertHistoryMap.get(deviceId); |
|
|
deviceRealtime.setDigitalInputJson(objectMapper.writeValueAsString(list)); |
|
|
if (alertHistoryId != null) { |
|
|
|
|
|
row.setAlertHistoryId(alertHistoryId); |
|
|
for (int i = 0; i < list.size(); i++) { |
|
|
|
|
|
CsdjDigitalInputTerminal t = list.get(i); |
|
|
|
|
|
if (i == 0) { |
|
|
|
|
|
deviceRealtime.setDi1No(t.getTerminalNo()); |
|
|
|
|
|
deviceRealtime.setDi1IsAlarm(t.getIsAlarm() ? 1 : 0); |
|
|
|
|
|
deviceRealtime.setDi1StateRaw(t.getStateRaw()); |
|
|
|
|
|
deviceRealtime.setDi1StateText(t.getStateText()); |
|
|
|
|
|
deviceRealtime.setDi1AbnormalMode(t.getAbnormalMode()); |
|
|
|
|
|
} else if (i == 1) { |
|
|
|
|
|
deviceRealtime.setDi2No(t.getTerminalNo()); |
|
|
|
|
|
deviceRealtime.setDi2IsAlarm(t.getIsAlarm() ? 1 : 0); |
|
|
|
|
|
deviceRealtime.setDi2StateRaw(t.getStateRaw()); |
|
|
|
|
|
deviceRealtime.setDi2StateText(t.getStateText()); |
|
|
|
|
|
deviceRealtime.setDi2AbnormalMode(t.getAbnormalMode()); |
|
|
|
|
|
} else if (i == 2) { |
|
|
|
|
|
deviceRealtime.setDi3No(t.getTerminalNo()); |
|
|
|
|
|
deviceRealtime.setDi3IsAlarm(t.getIsAlarm() ? 1 : 0); |
|
|
|
|
|
deviceRealtime.setDi3StateRaw(t.getStateRaw()); |
|
|
|
|
|
deviceRealtime.setDi3StateText(t.getStateText()); |
|
|
|
|
|
deviceRealtime.setDi3AbnormalMode(t.getAbnormalMode()); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
csdjDao.saveOrUpdateDeviceRealtime(row); |
|
|
} |
|
|
} |
|
|
|
|
|
} catch (JsonProcessingException e) { |
|
|
|
|
|
log.error("[CSDJ] 序列化 JSON 失败", e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 处理アナログ入力端子
|
|
|
private void fillDigitalInput(CsdjDeviceRealtime row, String deviceId, List<CsdjDigitalInputTerminal> terminals) { |
|
|
if (data.getAnalogInputTerminals() != null && !data.getAnalogInputTerminals().isEmpty()) { |
|
|
if (terminals == null) return; |
|
|
List<CsdjAnalogInputTerminal> list = data.getAnalogInputTerminals(); |
|
|
for (int i = 0; i < terminals.size() && i < 3; i++) { |
|
|
deviceRealtime.setAnalogInputJson(objectMapper.writeValueAsString(list)); |
|
|
CsdjDigitalInputTerminal t = terminals.get(i); |
|
|
|
|
|
if (deviceId.equals(t.getDeviceId())) { |
|
|
for (int i = 0; i < list.size(); i++) { |
|
|
if (i == 0) { |
|
|
CsdjAnalogInputTerminal t = list.get(i); |
|
|
row.setDi1No(t.getTerminalNo()); |
|
|
if (i == 0) { |
|
|
row.setDi1IsAlarm(t.getIsAlarm() ? 1 : 0); |
|
|
deviceRealtime.setAi1No(t.getTerminalNo()); |
|
|
row.setDi1StateRaw(t.getStateRaw()); |
|
|
deviceRealtime.setAi1IsThresholdAlarm(t.getIsThresholdAlarm() ? 1 : 0); |
|
|
row.setDi1StateText(t.getStateText()); |
|
|
deviceRealtime.setAi1ThresholdNo(t.getThresholdNo()); |
|
|
row.setDi1AbnormalMode(t.getAbnormalMode()); |
|
|
deviceRealtime.setAi1AdditionalData(t.getAdditionalData()); |
|
|
} else if (i == 1) { |
|
|
deviceRealtime.setAi1StateRaw(t.getStateRaw()); |
|
|
row.setDi2No(t.getTerminalNo()); |
|
|
} else if (i == 1) { |
|
|
row.setDi2IsAlarm(t.getIsAlarm() ? 1 : 0); |
|
|
deviceRealtime.setAi2No(t.getTerminalNo()); |
|
|
row.setDi2StateRaw(t.getStateRaw()); |
|
|
deviceRealtime.setAi2IsThresholdAlarm(t.getIsThresholdAlarm() ? 1 : 0); |
|
|
row.setDi2StateText(t.getStateText()); |
|
|
deviceRealtime.setAi2ThresholdNo(t.getThresholdNo()); |
|
|
row.setDi2AbnormalMode(t.getAbnormalMode()); |
|
|
deviceRealtime.setAi2AdditionalData(t.getAdditionalData()); |
|
|
} else { |
|
|
deviceRealtime.setAi2StateRaw(t.getStateRaw()); |
|
|
row.setDi3No(t.getTerminalNo()); |
|
|
} |
|
|
row.setDi3IsAlarm(t.getIsAlarm() ? 1 : 0); |
|
|
|
|
|
row.setDi3StateRaw(t.getStateRaw()); |
|
|
|
|
|
row.setDi3StateText(t.getStateText()); |
|
|
|
|
|
row.setDi3AbnormalMode(t.getAbnormalMode()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 处理デジタル出力端子
|
|
|
private void fillAnalogInput(CsdjDeviceRealtime row, String deviceId, List<CsdjAnalogInputTerminal> terminals) { |
|
|
if (data.getDigitalOutputTerminals() != null && !data.getDigitalOutputTerminals().isEmpty()) { |
|
|
if (terminals == null) return; |
|
|
List<CsdjDigitalOutputTerminal> list = data.getDigitalOutputTerminals(); |
|
|
for (int i = 0; i < terminals.size() && i < 2; i++) { |
|
|
deviceRealtime.setDigitalOutputJson(objectMapper.writeValueAsString(list)); |
|
|
CsdjAnalogInputTerminal t = terminals.get(i); |
|
|
|
|
|
if (deviceId.equals(t.getDeviceId())) { |
|
|
for (int i = 0; i < list.size(); i++) { |
|
|
if (i == 0) { |
|
|
CsdjDigitalOutputTerminal t = list.get(i); |
|
|
row.setAi1No(t.getTerminalNo()); |
|
|
if (i == 0) { |
|
|
row.setAi1IsThresholdAlarm(t.getIsThresholdAlarm() ? 1 : 0); |
|
|
deviceRealtime.setDo1No(t.getTerminalNo()); |
|
|
row.setAi1ThresholdNo(t.getThresholdNo()); |
|
|
deviceRealtime.setDo1OutputState(t.getOutputState()); |
|
|
row.setAi1AdditionalData(t.getAdditionalData()); |
|
|
deviceRealtime.setDo1StateRaw(t.getStateRaw()); |
|
|
row.setAi1StateRaw(t.getStateRaw()); |
|
|
deviceRealtime.setDo1AdditionalData(t.getAdditionalData()); |
|
|
} else { |
|
|
} else if (i == 1) { |
|
|
row.setAi2No(t.getTerminalNo()); |
|
|
deviceRealtime.setDo2No(t.getTerminalNo()); |
|
|
row.setAi2IsThresholdAlarm(t.getIsThresholdAlarm() ? 1 : 0); |
|
|
deviceRealtime.setDo2OutputState(t.getOutputState()); |
|
|
row.setAi2ThresholdNo(t.getThresholdNo()); |
|
|
deviceRealtime.setDo2StateRaw(t.getStateRaw()); |
|
|
row.setAi2AdditionalData(t.getAdditionalData()); |
|
|
deviceRealtime.setDo2AdditionalData(t.getAdditionalData()); |
|
|
row.setAi2StateRaw(t.getStateRaw()); |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 处理システム状態
|
|
|
private void fillDigitalOutput(CsdjDeviceRealtime row, String deviceId, List<CsdjDigitalOutputTerminal> terminals) { |
|
|
if (data.getSystemState() != null) { |
|
|
if (terminals == null) return; |
|
|
CsdjSystemState s = data.getSystemState(); |
|
|
for (int i = 0; i < terminals.size() && i < 2; i++) { |
|
|
deviceRealtime.setSystemStateNo(s.getStateNo()); |
|
|
CsdjDigitalOutputTerminal t = terminals.get(i); |
|
|
deviceRealtime.setSystemScheduledTimeNo(s.getScheduledTimeNo()); |
|
|
if (deviceId.equals(t.getDeviceId())) { |
|
|
deviceRealtime.setSystemModeState(s.getModeState()); |
|
|
if (i == 0) { |
|
|
deviceRealtime.setSystemIsPowerOff(s.getIsPowerOff() ? 1 : 0); |
|
|
row.setDo1No(t.getTerminalNo()); |
|
|
deviceRealtime.setSystemIsBatteryLow(s.getIsBatteryLow() ? 1 : 0); |
|
|
row.setDo1OutputState(t.getOutputState()); |
|
|
deviceRealtime.setSystemIsLineDisconnected(s.getIsLineDisconnected() ? 1 : 0); |
|
|
row.setDo1StateRaw(t.getStateRaw()); |
|
|
deviceRealtime.setSystemIsSlaveCommunicationError(s.getIsSlaveCommunicationError() ? 1 : 0); |
|
|
row.setDo1AdditionalData(t.getAdditionalData()); |
|
|
deviceRealtime.setSystemStateRaw(s.getStateRaw()); |
|
|
} else { |
|
|
|
|
|
row.setDo2No(t.getTerminalNo()); |
|
|
|
|
|
row.setDo2OutputState(t.getOutputState()); |
|
|
|
|
|
row.setDo2StateRaw(t.getStateRaw()); |
|
|
|
|
|
row.setDo2AdditionalData(t.getAdditionalData()); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 3. 保存
|
|
|
|
|
|
csdjDao.saveOrUpdateDeviceRealtime(deviceRealtime); |
|
|
|
|
|
} catch (JsonProcessingException e) { |
|
|
|
|
|
log.error("[CSDJ] 序列化 JSON 失败", e); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|