|
|
@@ -271,16 +271,22 @@ public class MqttService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 查询设备信息,如果设备不存在返回 null
|
|
|
+ * 【已修改】只根据 deviceId + deviceTypeCode 查询,不判断 channel
|
|
|
*/
|
|
|
- private SasDevice findDevice(String deviceId, Integer channel, Integer deviceTypeCode) {
|
|
|
+ private SasDevice findDevice(String deviceId, Integer deviceTypeCode) {
|
|
|
return deviceService.getOne(new LambdaQueryWrapper<SasDevice>()
|
|
|
.eq(SasDevice::getDeviceId, deviceId)
|
|
|
- .eq(SasDevice::getChannel, channel)
|
|
|
.eq(SasDevice::getDeviceType, deviceTypeCode)
|
|
|
.last("limit 1"));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 兼容旧调用(自动忽略 channel)
|
|
|
+ */
|
|
|
+ private SasDevice findDevice(String deviceId, Integer channel, Integer deviceTypeCode) {
|
|
|
+ return findDevice(deviceId, deviceTypeCode);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 查询事件类型分组信息
|
|
|
*/
|
|
|
@@ -301,11 +307,10 @@ public class MqttService {
|
|
|
if(message.getEventCode() == null && root.get("code") != null) {
|
|
|
message.setEventCode(root.get("code").asInt());
|
|
|
}
|
|
|
-
|
|
|
- // 1. 检查设备是否存在
|
|
|
- SasDevice device = findDevice(message.getDeviceId(), message.getChannel(), SystemTypeCodeEnum.usb.getCode());
|
|
|
+
|
|
|
+ SasDevice device = findDevice(message.getDeviceId(), SystemTypeCodeEnum.usb.getCode());
|
|
|
if (device == null) {
|
|
|
- log.warn("设备不存在,丢弃USB事件: deviceId={}, channel={}", message.getDeviceId(), message.getChannel());
|
|
|
+ log.warn("设备不存在,丢弃USB事件: deviceId={}", message.getDeviceId());
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -313,7 +318,6 @@ public class MqttService {
|
|
|
String eventId = message.getId() != null && !message.getId().isEmpty()
|
|
|
? message.getId()
|
|
|
: IdUtil.randomUUID();
|
|
|
- // 如果该事件已存在(设备重发/重复消息),避免主键冲突,直接跳过
|
|
|
if (usbEventService.getById(eventId) != null) {
|
|
|
log.info("USB事件已存在, 跳过保存, eventId={}", eventId);
|
|
|
return;
|
|
|
@@ -325,7 +329,6 @@ public class MqttService {
|
|
|
event.setCreateTime(LocalDateTime.now());
|
|
|
event.setUpdateTime(LocalDateTime.now());
|
|
|
|
|
|
- // 处理图片信息
|
|
|
if (message.getEventPic() != null) {
|
|
|
UsbEventMessage.PicInfo picInfo = message.getEventPic();
|
|
|
if (picInfo != null && StrUtil.isNotBlank(picInfo.getUrl()) && StrUtil.isNotBlank(picInfo.getPath())) {
|
|
|
@@ -336,27 +339,23 @@ public class MqttService {
|
|
|
pic.setCreateTime(LocalDateTime.now());
|
|
|
pic.setUpdateTime(LocalDateTime.now());
|
|
|
sasPicMapper.insert(pic);
|
|
|
-
|
|
|
- // 设置事件图片ID
|
|
|
event.setPicId(pic.getId());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
usbEventService.save(event);
|
|
|
|
|
|
- // WebSocket 推送
|
|
|
BrieflyEventInfo info = BeanUtil.toBean(event, BrieflyEventInfo.class);
|
|
|
info.setTriggerTime(message.getTriggerTime());
|
|
|
info.setCreateTime(LocalDateTime.now().format(formatter));
|
|
|
-
|
|
|
- // 设置图片URL(如果有)
|
|
|
+
|
|
|
if (event.getPicId() != null) {
|
|
|
SasPic pic = sasPicMapper.selectById(event.getPicId());
|
|
|
if (pic != null && StrUtil.isNotBlank(pic.getUrl()) && StrUtil.isNotBlank(pic.getPath())) {
|
|
|
info.setEventUrl(pic.getUrl() + pic.getPath());
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
SasUsbEventCode code = usbEventCodeMapper.selectById(event.getEventCode());
|
|
|
if (code != null) {
|
|
|
info.setEventTypeName(code.getName());
|
|
|
@@ -401,6 +400,16 @@ public class MqttService {
|
|
|
SnapEventInfoResult result = eventInfoVo.getResult();
|
|
|
SnapEventInfo eventInfo = result.getEventInfo();
|
|
|
SasSnapEvent bean = (SasSnapEvent)BeanUtil.toBean(eventInfo, SasSnapEvent.class);
|
|
|
+
|
|
|
+ String eventId = eventResult.getId() != null && !eventResult.getId().isEmpty()
|
|
|
+ ? eventResult.getId()
|
|
|
+ : IdUtil.randomUUID();
|
|
|
+ if (snapEventService.getById(eventId) != null) {
|
|
|
+ log.info("抓拍事件已存在, 跳过保存, eventId={}", eventId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ bean.setEventId(eventId);
|
|
|
+
|
|
|
BrieflyEventInfo info = (BrieflyEventInfo)BeanUtil.toBeanIgnoreError(bean, BrieflyEventInfo.class);
|
|
|
if (eventInfo.getEventPic() != null) {
|
|
|
SasPic pic = (SasPic)BeanUtil.toBean(eventInfo.getEventPic(), SasPic.class);
|
|
|
@@ -421,22 +430,11 @@ public class MqttService {
|
|
|
eventInfo.setEventCode(root.get("code").asInt());
|
|
|
}
|
|
|
|
|
|
- // 1. 检查设备是否存在
|
|
|
- SasDevice device = findDevice(eventInfo.getDeviceId(), eventInfo.getChannel(), SystemTypeCodeEnum.snap.getCode());
|
|
|
+ SasDevice device = findDevice(eventInfo.getDeviceId(), SystemTypeCodeEnum.snap.getCode());
|
|
|
if (device == null) {
|
|
|
- log.warn("设备不存在,丢弃抓拍事件: deviceId={}, channel={}", eventInfo.getDeviceId(), eventInfo.getChannel());
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- String eventId = eventResult.getId() != null && !eventResult.getId().isEmpty()
|
|
|
- ? eventResult.getId()
|
|
|
- : IdUtil.randomUUID();
|
|
|
- // 如果该事件已存在(设备重发/重复消息),避免主键冲突,直接跳过
|
|
|
- if (snapEventService.getById(eventId) != null) {
|
|
|
- log.info("抓拍事件已存在, 跳过保存, eventId={}", eventId);
|
|
|
+ log.warn("设备不存在,丢弃抓拍事件: deviceId={}", eventInfo.getDeviceId());
|
|
|
return;
|
|
|
}
|
|
|
- bean.setEventId(eventId);
|
|
|
BeanUtil.copyProperties(eventInfo, bean, "eventId");
|
|
|
bean.setTriggerTime(parseTime(eventInfo.getTriggerTime()));
|
|
|
bean.setCreateTime(LocalDateTime.now());
|
|
|
@@ -446,7 +444,7 @@ public class MqttService {
|
|
|
|
|
|
info.setTriggerTime(eventInfo.getTriggerTime());
|
|
|
info.setCreateTime(LocalDateTime.now().format(formatter));
|
|
|
-
|
|
|
+
|
|
|
SasSnapTypeCode code = snapTypeCodeMapper.selectById(bean.getEventCode());
|
|
|
if (code != null) {
|
|
|
info.setEventTypeName(code.getName());
|
|
|
@@ -481,10 +479,9 @@ public class MqttService {
|
|
|
message.setEventCode(root.get("code").asInt());
|
|
|
}
|
|
|
|
|
|
- // 1. 检查设备是否存在
|
|
|
- SasDevice device = findDevice(message.getDeviceId(), message.getChannel(), SystemTypeCodeEnum.entrance.getCode());
|
|
|
+ SasDevice device = findDevice(message.getDeviceId(), SystemTypeCodeEnum.entrance.getCode());
|
|
|
if (device == null) {
|
|
|
- log.warn("设备不存在,丢弃门禁事件: deviceId={}, channel={}", message.getDeviceId(), message.getChannel());
|
|
|
+ log.warn("设备不存在,丢弃门禁事件: deviceId={}", message.getDeviceId());
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -564,55 +561,82 @@ public class MqttService {
|
|
|
String method = root.path("method").asText();
|
|
|
|
|
|
if ("event".equals(method)) {
|
|
|
- ParkingEventResult eventResult = (ParkingEventResult)this.mapper.readValue(payload, ParkingEventResult.class);
|
|
|
- Map<String, Object> map = new HashMap();
|
|
|
+ ParkingEventResult eventResult = this.mapper.readValue(payload, ParkingEventResult.class);
|
|
|
+
|
|
|
+ SasConfig config = sasConfigService.getConfig();
|
|
|
+ if (config == null || StrUtil.isBlank(config.getHost()) || StrUtil.isBlank(config.getKeyds())) {
|
|
|
+ log.error("停车场事件处理失败:系统配置未初始化或host/keyds为空");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
map.put("eventId", eventResult.getId());
|
|
|
JsonRpcRequest getParkingEventInfoJson = new JsonRpcRequest("getEvent", map, (Long)null);
|
|
|
- Map<String, Object> getParkingEventInfo = new HashMap();
|
|
|
- getParkingEventInfo.put("key", this.sasConfigService.getConfig().getKeyds());
|
|
|
+ Map<String, Object> getParkingEventInfo = new HashMap<>();
|
|
|
+ getParkingEventInfo.put("key", config.getKeyds());
|
|
|
getParkingEventInfo.put("json", getParkingEventInfoJson.toString());
|
|
|
log.info("请求AG报文:{}", JSONUtil.toJsonStr(getParkingEventInfo));
|
|
|
- String resultEvent = HttpUtil.post(this.sasConfigService.getConfig().getHost() + "/agbox/device/parking", getParkingEventInfo);
|
|
|
+
|
|
|
+ String resultEvent = HttpUtil.post(config.getHost() + "/agbox/device/parking", getParkingEventInfo);
|
|
|
log.info("请求AG响应:{}", resultEvent);
|
|
|
- ParkingEventInfoVo parkingEventInfoVo = (ParkingEventInfoVo)this.mapper.readValue(resultEvent, ParkingEventInfoVo.class);
|
|
|
+
|
|
|
+ if (StrUtil.isBlank(resultEvent)) {
|
|
|
+ log.error("停车场事件:AG接口返回空字符串,终止处理");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ ParkingEventInfoVo parkingEventInfoVo = this.mapper.readValue(resultEvent, ParkingEventInfoVo.class);
|
|
|
+ if (parkingEventInfoVo == null) {
|
|
|
+ log.error("停车场事件:解析返回结果为null,响应:{}", resultEvent);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
ParkingEventInfoResult result = parkingEventInfoVo.getResult();
|
|
|
+ if (result == null) {
|
|
|
+ log.error("停车场事件:返回结果result为null,响应:{}", resultEvent);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
ParkingEventInfo eventInfo = result.getEventInfo();
|
|
|
- SasParkingEvent bean = (SasParkingEvent)BeanUtil.toBean(eventInfo, SasParkingEvent.class);
|
|
|
+ if (eventInfo == null) {
|
|
|
+ log.error("停车场事件:eventInfo为null,响应:{}", resultEvent);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ SasParkingEvent bean = BeanUtil.toBean(eventInfo, SasParkingEvent.class);
|
|
|
bean.setCreateTime(LocalDateTime.parse(eventInfo.getReceivingTime(), this.formatter));
|
|
|
bean.setTriggerTime(LocalDateTime.parse(eventInfo.getTriggerTime(), this.formatter));
|
|
|
bean.setEventId(eventResult.getId());
|
|
|
bean.setAccessType(eventInfo.getIo());
|
|
|
bean.setCarType(eventInfo.getCarCode());
|
|
|
bean.setPlateType(eventInfo.getPlateCode());
|
|
|
+
|
|
|
if (eventInfo.getEventPic() != null) {
|
|
|
- SasPic pic = (SasPic)BeanUtil.toBean(eventInfo.getEventPic(), SasPic.class);
|
|
|
+ SasPic pic = BeanUtil.toBean(eventInfo.getEventPic(), SasPic.class);
|
|
|
pic.setId(IdUtil.randomUUID());
|
|
|
this.sasPicMapper.insert(pic);
|
|
|
bean.setEventPicId(pic.getId());
|
|
|
}
|
|
|
|
|
|
if (eventInfo.getPlacePic() != null) {
|
|
|
- SasPic pic = (SasPic)BeanUtil.toBean(eventInfo.getPlacePic(), SasPic.class);
|
|
|
+ SasPic pic = BeanUtil.toBean(eventInfo.getPlacePic(), SasPic.class);
|
|
|
pic.setId(IdUtil.randomUUID());
|
|
|
this.sasPicMapper.insert(pic);
|
|
|
bean.setPlatePicId(pic.getId());
|
|
|
}
|
|
|
- if(eventInfo.getEventCode() == null && root.get("code") != null) {
|
|
|
+
|
|
|
+ if (eventInfo.getEventCode() == null && root.get("code") != null) {
|
|
|
eventInfo.setEventCode(root.get("code").asInt());
|
|
|
}
|
|
|
|
|
|
- // 1. 检查设备是否存在
|
|
|
- SasDevice device = findDevice(eventInfo.getDeviceId(), eventInfo.getChannel(), SystemTypeCodeEnum.parking.getCode());
|
|
|
+ SasDevice device = findDevice(eventInfo.getDeviceId(), SystemTypeCodeEnum.parking.getCode());
|
|
|
if (device == null) {
|
|
|
- log.warn("设备不存在,丢弃停车场事件: deviceId={}, channel={}", eventInfo.getDeviceId(), eventInfo.getChannel());
|
|
|
+ log.warn("设备不存在,丢弃停车场事件: deviceId={}", eventInfo.getDeviceId());
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
SasParkingEvent event = new SasParkingEvent();
|
|
|
- String eventId = eventResult.getId() != null && !eventResult.getId().isEmpty()
|
|
|
- ? eventResult.getId()
|
|
|
- : IdUtil.randomUUID();
|
|
|
- // 如果该事件已存在(设备重发/重复消息),避免主键冲突,直接跳过
|
|
|
+ String eventId = StrUtil.isNotBlank(eventResult.getId()) ? eventResult.getId() : IdUtil.randomUUID();
|
|
|
if (parkingEventService.getById(eventId) != null) {
|
|
|
log.info("停车场事件已存在, 跳过保存, eventId={}", eventId);
|
|
|
return;
|
|
|
@@ -628,16 +652,14 @@ public class MqttService {
|
|
|
BrieflyEventInfo info = BeanUtil.toBean(event, BrieflyEventInfo.class);
|
|
|
info.setTriggerTime(eventInfo.getTriggerTime());
|
|
|
info.setCreateTime(LocalDateTime.now().format(formatter));
|
|
|
-
|
|
|
- // 设置图片URL(如果有)
|
|
|
+
|
|
|
if (eventInfo.getEventPic() != null) {
|
|
|
info.setEventUrl(eventInfo.getEventPic().getUrl() + eventInfo.getEventPic().getPath());
|
|
|
}
|
|
|
-
|
|
|
if (eventInfo.getPlacePic() != null) {
|
|
|
info.setEventUrl(eventInfo.getPlacePic().getUrl() + eventInfo.getPlacePic().getPath());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
SasParkingEventCode code = parkingEventCodeMapper.selectById(event.getEventCode());
|
|
|
if (code != null) {
|
|
|
info.setEventTypeName(code.getName());
|
|
|
@@ -672,10 +694,9 @@ public class MqttService {
|
|
|
message.setEventCode(root.get("code").asInt());
|
|
|
}
|
|
|
|
|
|
- // 1. 检查设备是否存在
|
|
|
- SasDevice device = findDevice(message.getDeviceId(), message.getChannel(), SystemTypeCodeEnum.roadblock.getCode());
|
|
|
+ SasDevice device = findDevice(message.getDeviceId(), SystemTypeCodeEnum.roadblock.getCode());
|
|
|
if (device == null) {
|
|
|
- log.warn("设备不存在,丢弃阻车路障事件: deviceId={}, channel={}", message.getDeviceId(), message.getChannel());
|
|
|
+ log.warn("设备不存在,丢弃阻车路障事件: deviceId={}", message.getDeviceId());
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -683,7 +704,6 @@ public class MqttService {
|
|
|
String eventId = message.getId() != null && !message.getId().isEmpty()
|
|
|
? message.getId()
|
|
|
: IdUtil.randomUUID();
|
|
|
- // 如果该事件已存在(设备重发/重复消息),避免主键冲突,直接跳过
|
|
|
if (roadblockEventService.getById(eventId) != null) {
|
|
|
log.info("阻车路障事件已存在, 跳过保存, eventId={}", eventId);
|
|
|
return;
|
|
|
@@ -694,7 +714,6 @@ public class MqttService {
|
|
|
event.setCreateTime(LocalDateTime.now());
|
|
|
event.setUpdateTime(LocalDateTime.now());
|
|
|
|
|
|
- // 处理图片信息
|
|
|
if (message.getRoadblockPic() != null) {
|
|
|
RoadblockEventMessage.PicInfo picInfo = message.getRoadblockPic();
|
|
|
if (picInfo != null && StrUtil.isNotBlank(picInfo.getUrl()) && StrUtil.isNotBlank(picInfo.getPath())) {
|
|
|
@@ -705,8 +724,6 @@ public class MqttService {
|
|
|
pic.setCreateTime(LocalDateTime.now());
|
|
|
pic.setUpdateTime(LocalDateTime.now());
|
|
|
sasPicMapper.insert(pic);
|
|
|
-
|
|
|
- // 设置事件图片ID
|
|
|
event.setPicId(pic.getId());
|
|
|
}
|
|
|
}
|
|
|
@@ -716,15 +733,14 @@ public class MqttService {
|
|
|
BrieflyEventInfo info = BeanUtil.toBean(event, BrieflyEventInfo.class);
|
|
|
info.setTriggerTime(message.getTriggerTime());
|
|
|
info.setCreateTime(LocalDateTime.now().format(formatter));
|
|
|
-
|
|
|
- // 设置图片URL(如果有)
|
|
|
+
|
|
|
if (event.getPicId() != null) {
|
|
|
SasPic pic = sasPicMapper.selectById(event.getPicId());
|
|
|
if (pic != null && StrUtil.isNotBlank(pic.getUrl()) && StrUtil.isNotBlank(pic.getPath())) {
|
|
|
info.setEventUrl(pic.getUrl() + pic.getPath());
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
SasRoadblockEventCode code = roadblockEventCodeMapper.selectById(event.getEventCode());
|
|
|
if (code != null) {
|
|
|
info.setEventTypeName(code.getName());
|
|
|
@@ -753,74 +769,60 @@ public class MqttService {
|
|
|
JsonNode root = mapper.readTree(payload);
|
|
|
String method = root.path("method").asText();
|
|
|
|
|
|
+ // 【核心修复】匹配真实设备的 method: "event"
|
|
|
if ("event".equals(method)) {
|
|
|
- AlarmEventResult eventResult = (AlarmEventResult)this.mapper.readValue(payload, AlarmEventResult.class);
|
|
|
- Map<String, Object> map = new HashMap();
|
|
|
- map.put("eventId", eventResult.getId());
|
|
|
- JsonRpcRequest getAlarmEventInfoJson = new JsonRpcRequest("getEvent", map, (Long)null);
|
|
|
- Map<String, Object> getAlarmEventInfo = new HashMap();
|
|
|
- getAlarmEventInfo.put("key", this.sasConfigService.getConfig().getKeyds());
|
|
|
- getAlarmEventInfo.put("json", getAlarmEventInfoJson.toString());
|
|
|
- log.info("请求AG报文:{}", JSONUtil.toJsonStr(getAlarmEventInfo));
|
|
|
- String resultEvent = HttpUtil.post(this.sasConfigService.getConfig().getHost() + "/agbox/device/alarm", getAlarmEventInfo);
|
|
|
- log.info("请求AG响应:{}", resultEvent);
|
|
|
- AlarmEventInfoVo alarmEventInfoVo = (AlarmEventInfoVo)this.mapper.readValue(resultEvent, AlarmEventInfoVo.class);
|
|
|
- AlarmEventInfoResult result = alarmEventInfoVo.getResult();
|
|
|
- AlarmEventInfo eventInfo = result.getEventInfo();
|
|
|
-
|
|
|
- SasAlarsasEvent event = new SasAlarsasEvent();
|
|
|
- if (eventInfo.getEventPic() != null) {
|
|
|
- SasPic pic = (SasPic)BeanUtil.toBean(eventInfo.getEventPic(), SasPic.class);
|
|
|
- pic.setId(IdUtil.randomUUID());
|
|
|
- this.sasPicMapper.insert(pic);
|
|
|
- event.setPicId(pic.getId());
|
|
|
+ // 直接从根节点读取所有字段,完全适配你真实的消息格式
|
|
|
+ String deviceId = root.path("deviceId").asText();
|
|
|
+ Integer channel = root.path("channel").asInt();
|
|
|
+ Integer eventCode = root.path("code").asInt(); // 真实消息里是 code 字段!
|
|
|
+ String id = root.path("id").asText();
|
|
|
+ String triggerTime = root.path("triggerTime").asText();
|
|
|
+ String note = root.path("note").asText();
|
|
|
+ Integer eventSystem = root.path("eventSystem").asInt();
|
|
|
+
|
|
|
+ // 打印日志,确认字段都读到了
|
|
|
+ log.info("【入侵告警】收到消息:deviceId={}, channel={}, eventCode={}, id={}",
|
|
|
+ deviceId, channel, eventCode, id);
|
|
|
+
|
|
|
+ // 设备校验
|
|
|
+ SasDevice device = findDevice(deviceId, SystemTypeCodeEnum.alarm.getCode());
|
|
|
+ if (device == null) {
|
|
|
+ log.warn("【入侵告警】设备不存在,丢弃消息:deviceId={}", deviceId);
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- if (eventInfo.getGis() != null) {
|
|
|
- SasGis gis = (SasGis)BeanUtil.toBean(eventInfo.getGis(), SasGis.class);
|
|
|
- gis.setId(IdUtil.randomUUID());
|
|
|
- this.gisMapper.insert(gis);
|
|
|
- event.setGisId(gis.getId());
|
|
|
- }
|
|
|
- if(eventInfo.getEventCode() == null && root.get("code") != null) {
|
|
|
- eventInfo.setEventCode(root.get("code").asInt());
|
|
|
- }
|
|
|
+ // 构建事件实体
|
|
|
+ SasAlarsasEvent event = new SasAlarsasEvent();
|
|
|
+ String eventId = StrUtil.isNotBlank(id) ? id : IdUtil.randomUUID();
|
|
|
|
|
|
- // 1. 检查设备是否存在
|
|
|
- SasDevice device = findDevice(eventInfo.getDeviceId(), eventInfo.getChannel(), SystemTypeCodeEnum.alarm.getCode());
|
|
|
- if (device == null) {
|
|
|
- log.warn("设备不存在,丢弃入侵报警事件: deviceId={}, channel={}", eventInfo.getDeviceId(), eventInfo.getChannel());
|
|
|
+ // 防重复
|
|
|
+ if (alarsasEventService.getById(eventId) != null) {
|
|
|
+ log.info("【入侵告警】事件已存在,跳过保存:eventId={}", eventId);
|
|
|
return;
|
|
|
}
|
|
|
- String eventId = eventResult.getId() != null && !eventResult.getId().isEmpty()
|
|
|
- ? eventResult.getId()
|
|
|
- : IdUtil.randomUUID();
|
|
|
- // 如果该事件已存在(设备重发/重复消息),避免主键冲突,直接跳过
|
|
|
-// if (alarsasEventService.getById(eventId) != null) {
|
|
|
-// log.info("入侵报警事件已存在, 跳过保存, eventId={}", eventId);
|
|
|
-// return;
|
|
|
-// }
|
|
|
+
|
|
|
+ // 手动赋值,100% 不会丢字段
|
|
|
event.setEventId(eventId);
|
|
|
- BeanUtil.copyProperties(eventResult, event, "eventId");
|
|
|
- event.setTriggerTime(parseTime(eventInfo.getTriggerTime()));
|
|
|
+ event.setDeviceId(deviceId);
|
|
|
+ event.setChannel(channel);
|
|
|
+ event.setEventCode(eventCode);
|
|
|
+ event.setNote(note);
|
|
|
+ event.setEventSystem(eventSystem);
|
|
|
+ event.setTriggerTime(parseTime(triggerTime));
|
|
|
event.setCreateTime(LocalDateTime.now());
|
|
|
event.setUpdateTime(LocalDateTime.now());
|
|
|
|
|
|
- alarsasEventService.saveOrUpdate(event);
|
|
|
+ // 保存到数据库
|
|
|
+ alarsasEventService.save(event);
|
|
|
+ log.info("【入侵告警】保存成功!eventId={}, deviceId={}, eventCode={}",
|
|
|
+ eventId, deviceId, eventCode);
|
|
|
|
|
|
+ // 后续的 WebSocket 推送逻辑(保持不变)
|
|
|
BrieflyEventInfo info = BeanUtil.toBean(event, BrieflyEventInfo.class);
|
|
|
- info.setTriggerTime(eventInfo.getTriggerTime());
|
|
|
+ info.setTriggerTime(triggerTime);
|
|
|
info.setCreateTime(LocalDateTime.now().format(formatter));
|
|
|
-
|
|
|
- // 设置图片URL(如果有)
|
|
|
- if (event.getPicId() != null) {
|
|
|
- SasPic pic = sasPicMapper.selectById(event.getPicId());
|
|
|
- if (pic != null && StrUtil.isNotBlank(pic.getUrl()) && StrUtil.isNotBlank(pic.getPath())) {
|
|
|
- info.setEventUrl(pic.getUrl() + pic.getPath());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- SasAlarsasEventCode code = alarsasEventCodeMapper.selectById(event.getEventCode());
|
|
|
+
|
|
|
+ SasAlarsasEventCode code = alarsasEventCodeMapper.selectById(eventCode);
|
|
|
if (code != null) {
|
|
|
info.setEventTypeName(code.getName());
|
|
|
}
|
|
|
@@ -833,12 +835,11 @@ public class MqttService {
|
|
|
info.setDeviceType(SystemTypeCodeEnum.alarm.getCode());
|
|
|
SasWebSocket.sendAll(info);
|
|
|
|
|
|
- log.info("入侵报警事件保存并推送成功, eventId={}", event.getEventId());
|
|
|
} else if ("heart".equals(method)) {
|
|
|
handleHeartbeat(payload, SystemTypeCodeEnum.alarm.getCode());
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- log.error("处理入侵报警事件失败", e);
|
|
|
+ log.error("【入侵告警】处理失败,payload={}", payload, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -863,10 +864,9 @@ public class MqttService {
|
|
|
PatrolEventInfoResult result = patrolEventInfoVo.getResult();
|
|
|
PatrolEventInfo eventInfo = result.getEventInfo();
|
|
|
|
|
|
- // 1. 检查设备是否存在
|
|
|
- SasDevice device = findDevice(eventInfo.getDeviceId(), 0, SystemTypeCodeEnum.patrol.getCode());
|
|
|
+ SasDevice device = findDevice(eventInfo.getDeviceId(), SystemTypeCodeEnum.patrol.getCode());
|
|
|
if (device == null) {
|
|
|
- log.warn("设备不存在,丢弃巡检事件: deviceId={}, channel={}", eventInfo.getDeviceId(), eventInfo.getChannel());
|
|
|
+ log.warn("设备不存在,丢弃巡检事件: deviceId={}", eventInfo.getDeviceId());
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -874,20 +874,17 @@ public class MqttService {
|
|
|
String eventId = patrolEventInfoVo.getId() != null && !patrolEventInfoVo.getId().isEmpty()
|
|
|
? patrolEventInfoVo.getId()
|
|
|
: IdUtil.randomUUID();
|
|
|
- // 如果该事件已存在(设备重发/重复消息),避免主键冲突,直接跳过
|
|
|
if (patrolEventService.getById(eventId) != null) {
|
|
|
log.info("巡检事件已存在, 跳过保存, eventId={}", eventId);
|
|
|
return;
|
|
|
}
|
|
|
event.setEventId(eventId);
|
|
|
BeanUtil.copyProperties(eventInfo, event, "eventId");
|
|
|
- // 关键:清洗空格 + 指定格式化器解析
|
|
|
String triggerTimeStr = eventInfo.getTriggerTime().replaceAll("\\s", " ");
|
|
|
event.setTriggerTime(LocalDateTime.parse(triggerTimeStr, formatter));
|
|
|
event.setCreateTime(LocalDateTime.now());
|
|
|
event.setUpdateTime(LocalDateTime.now());
|
|
|
|
|
|
- // 处理图片信息
|
|
|
if (eventInfo.getEventPic() != null) {
|
|
|
SasPic pic = (SasPic) BeanUtil.toBean(eventInfo.getEventPic(), SasPic.class);
|
|
|
pic.setId(IdUtil.randomUUID());
|
|
|
@@ -910,15 +907,14 @@ public class MqttService {
|
|
|
BrieflyEventInfo info = BeanUtil.toBean(event, BrieflyEventInfo.class);
|
|
|
info.setTriggerTime(eventInfo.getTriggerTime());
|
|
|
info.setCreateTime(LocalDateTime.now().format(formatter));
|
|
|
-
|
|
|
- // 设置图片URL(如果有)
|
|
|
+
|
|
|
if (event.getPicId() != null) {
|
|
|
SasPic pic = sasPicMapper.selectById(event.getPicId());
|
|
|
if (pic != null && StrUtil.isNotBlank(pic.getUrl()) && StrUtil.isNotBlank(pic.getPath())) {
|
|
|
info.setEventUrl(pic.getUrl() + pic.getPath());
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
SasPatrolEventCode code = patrolEventCodeMapper.selectById(event.getEventCode());
|
|
|
if (code != null) {
|
|
|
info.setEventTypeName(code.getName());
|
|
|
@@ -953,10 +949,9 @@ public class MqttService {
|
|
|
message.setEventCode(root.get("code").asInt());
|
|
|
}
|
|
|
|
|
|
- // 1. 检查设备是否存在
|
|
|
- SasDevice device = findDevice(message.getDeviceId(), message.getChannel(), SystemTypeCodeEnum.acquisition.getCode());
|
|
|
+ SasDevice device = findDevice(message.getDeviceId(), SystemTypeCodeEnum.acquisition.getCode());
|
|
|
if (device == null) {
|
|
|
- log.warn("设备不存在,丢弃数据采集事件: deviceId={}, channel={}", message.getDeviceId(), message.getChannel());
|
|
|
+ log.warn("设备不存在,丢弃数据采集事件: deviceId={}", message.getDeviceId());
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -964,7 +959,6 @@ public class MqttService {
|
|
|
String eventId = message.getId() != null && !message.getId().isEmpty()
|
|
|
? message.getId()
|
|
|
: IdUtil.randomUUID();
|
|
|
- // 如果该事件已存在(设备重发/重复消息),避免主键冲突,直接跳过
|
|
|
if (acquisitionEventService.getById(eventId) != null) {
|
|
|
log.info("数据采集事件已存在, 跳过保存, eventId={}", eventId);
|
|
|
return;
|
|
|
@@ -975,7 +969,6 @@ public class MqttService {
|
|
|
event.setCreateTime(LocalDateTime.now());
|
|
|
event.setUpdateTime(LocalDateTime.now());
|
|
|
|
|
|
- // 处理图片信息
|
|
|
if (message.getAcquisitionPic() != null) {
|
|
|
AcquisitionEventMessage.PicInfo picInfo = message.getAcquisitionPic();
|
|
|
if (picInfo != null && StrUtil.isNotBlank(picInfo.getUrl()) && StrUtil.isNotBlank(picInfo.getPath())) {
|
|
|
@@ -986,8 +979,6 @@ public class MqttService {
|
|
|
pic.setCreateTime(LocalDateTime.now());
|
|
|
pic.setUpdateTime(LocalDateTime.now());
|
|
|
sasPicMapper.insert(pic);
|
|
|
-
|
|
|
- // 设置事件图片ID
|
|
|
event.setPicId(pic.getId());
|
|
|
}
|
|
|
}
|
|
|
@@ -997,15 +988,14 @@ public class MqttService {
|
|
|
BrieflyEventInfo info = BeanUtil.toBean(event, BrieflyEventInfo.class);
|
|
|
info.setTriggerTime(message.getTriggerTime());
|
|
|
info.setCreateTime(LocalDateTime.now().format(formatter));
|
|
|
-
|
|
|
- // 设置图片URL(如果有)
|
|
|
+
|
|
|
if (event.getPicId() != null) {
|
|
|
SasPic pic = sasPicMapper.selectById(event.getPicId());
|
|
|
if (pic != null && StrUtil.isNotBlank(pic.getUrl()) && StrUtil.isNotBlank(pic.getPath())) {
|
|
|
info.setEventUrl(pic.getUrl() + pic.getPath());
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
SasAcquisitionEventCode code = acquisitionEventCodeMapper.selectById(event.getEventCode());
|
|
|
if (code != null) {
|
|
|
info.setEventTypeName(code.getName());
|
|
|
@@ -1040,10 +1030,9 @@ public class MqttService {
|
|
|
message.setEventCode(root.get("code").asInt());
|
|
|
}
|
|
|
|
|
|
- // 1. 检查设备是否存在
|
|
|
- SasDevice device = findDevice(message.getDeviceId(), message.getChannel(), SystemTypeCodeEnum.perception.getCode());
|
|
|
+ SasDevice device = findDevice(message.getDeviceId(), SystemTypeCodeEnum.perception.getCode());
|
|
|
if (device == null) {
|
|
|
- log.warn("设备不存在,丢弃状态感知事件: deviceId={}, channel={}", message.getDeviceId(), message.getChannel());
|
|
|
+ log.warn("设备不存在,丢弃状态感知事件: deviceId={}", message.getDeviceId());
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -1051,7 +1040,6 @@ public class MqttService {
|
|
|
String eventId = message.getId() != null && !message.getId().isEmpty()
|
|
|
? message.getId()
|
|
|
: IdUtil.randomUUID();
|
|
|
- // 如果该事件已存在(设备重发/重复消息),避免主键冲突,直接跳过
|
|
|
if (perceptionEventService.getById(eventId) != null) {
|
|
|
log.info("状态感知事件已存在, 跳过保存, eventId={}", eventId);
|
|
|
return;
|
|
|
@@ -1062,7 +1050,6 @@ public class MqttService {
|
|
|
event.setCreateTime(LocalDateTime.now());
|
|
|
event.setUpdateTime(LocalDateTime.now());
|
|
|
|
|
|
- // 处理图片信息
|
|
|
if (message.getPerceptionPic() != null) {
|
|
|
PerceptionEventMessage.PicInfo picInfo = message.getPerceptionPic();
|
|
|
if (picInfo != null && StrUtil.isNotBlank(picInfo.getUrl()) && StrUtil.isNotBlank(picInfo.getPath())) {
|
|
|
@@ -1073,8 +1060,6 @@ public class MqttService {
|
|
|
pic.setCreateTime(LocalDateTime.now());
|
|
|
pic.setUpdateTime(LocalDateTime.now());
|
|
|
sasPicMapper.insert(pic);
|
|
|
-
|
|
|
- // 设置事件图片ID
|
|
|
event.setPicId(pic.getId());
|
|
|
}
|
|
|
}
|
|
|
@@ -1084,15 +1069,14 @@ public class MqttService {
|
|
|
BrieflyEventInfo info = BeanUtil.toBean(event, BrieflyEventInfo.class);
|
|
|
info.setTriggerTime(message.getTriggerTime());
|
|
|
info.setCreateTime(LocalDateTime.now().format(formatter));
|
|
|
-
|
|
|
- // 设置图片URL(如果有)
|
|
|
+
|
|
|
if (event.getPicId() != null) {
|
|
|
SasPic pic = sasPicMapper.selectById(event.getPicId());
|
|
|
if (pic != null && StrUtil.isNotBlank(pic.getUrl()) && StrUtil.isNotBlank(pic.getPath())) {
|
|
|
info.setEventUrl(pic.getUrl() + pic.getPath());
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
SasPerceptionEventCode code = perceptionEventCodeMapper.selectById(event.getEventCode());
|
|
|
if (code != null) {
|
|
|
info.setEventTypeName(code.getName());
|
|
|
@@ -1127,10 +1111,9 @@ public class MqttService {
|
|
|
message.setEventCode(root.get("code").asInt());
|
|
|
}
|
|
|
|
|
|
- // 1. 检查设备是否存在
|
|
|
- SasDevice device = findDevice(message.getDeviceId(), message.getChannel(), SystemTypeCodeEnum.collection.getCode());
|
|
|
+ SasDevice device = findDevice(message.getDeviceId(), SystemTypeCodeEnum.collection.getCode());
|
|
|
if (device == null) {
|
|
|
- log.warn("设备不存在,丢弃状态采集事件: deviceId={}, channel={}", message.getDeviceId(), message.getChannel());
|
|
|
+ log.warn("设备不存在,丢弃状态采集事件: deviceId={}", message.getDeviceId());
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -1138,7 +1121,6 @@ public class MqttService {
|
|
|
String eventId = message.getId() != null && !message.getId().isEmpty()
|
|
|
? message.getId()
|
|
|
: IdUtil.randomUUID();
|
|
|
- // 如果该事件已存在(设备重发/重复消息),避免主键冲突,直接跳过
|
|
|
if (collectionEventService.getById(eventId) != null) {
|
|
|
log.info("状态采集事件已存在, 跳过保存, eventId={}", eventId);
|
|
|
return;
|
|
|
@@ -1149,7 +1131,6 @@ public class MqttService {
|
|
|
event.setCreateTime(LocalDateTime.now());
|
|
|
event.setUpdateTime(LocalDateTime.now());
|
|
|
|
|
|
- // 处理图片信息
|
|
|
if (message.getScenePicInfo() != null) {
|
|
|
SasPic bean = (SasPic)BeanUtil.toBean(message.getScenePicInfo(), SasPic.class);
|
|
|
bean.setId(IdUtil.randomUUID());
|
|
|
@@ -1169,8 +1150,7 @@ public class MqttService {
|
|
|
BrieflyEventInfo info = BeanUtil.toBean(event, BrieflyEventInfo.class);
|
|
|
info.setTriggerTime(message.getTriggerTime());
|
|
|
info.setCreateTime(LocalDateTime.now().format(formatter));
|
|
|
-
|
|
|
- // 设置图片URL(如果有)
|
|
|
+
|
|
|
if (message.getEventPicInfo() != null) {
|
|
|
info.setEventUrl(message.getEventPicInfo().getUrl() + message.getEventPicInfo().getPath());
|
|
|
}
|
|
|
@@ -1178,7 +1158,7 @@ public class MqttService {
|
|
|
if (message.getScenePicInfo() != null) {
|
|
|
info.setEventUrl(message.getScenePicInfo().getUrl() + message.getScenePicInfo().getPath());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
SasCollectionEventCode code = collectionEventCodeMapper.selectById(event.getEventCode());
|
|
|
if (code != null) {
|
|
|
info.setEventTypeName(code.getName());
|
|
|
@@ -1201,9 +1181,6 @@ public class MqttService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 统一处理心跳逻辑
|
|
|
- */
|
|
|
private void handleHeartbeat(String payload, int deviceTypeCode) {
|
|
|
try {
|
|
|
GlobalDeviceHeartInfo heartInfo = mapper.readValue(payload, GlobalDeviceHeartInfo.class);
|
|
|
@@ -1217,22 +1194,18 @@ public class MqttService {
|
|
|
device.setTriggerTime(LocalDateTime.now());
|
|
|
device.setUpdateTime(LocalDateTime.now());
|
|
|
|
|
|
- // 更新 GIS 信息
|
|
|
if (heartInfoGis != null && StrUtil.isNotBlank(device.getGisId())) {
|
|
|
SasGis gis = gisMapper.selectById(device.getGisId());
|
|
|
if (gis != null) {
|
|
|
- // GisHeartInfo: longitude, latitude, altitude
|
|
|
- // SasGis: lon, lat, alt
|
|
|
if (heartInfoGis.getLongitude() != null) gis.setLon(heartInfoGis.getLongitude());
|
|
|
if (heartInfoGis.getLatitude() != null) gis.setLat(heartInfoGis.getLatitude());
|
|
|
if (heartInfoGis.getAltitude() != null) gis.setAlt(heartInfoGis.getAltitude());
|
|
|
-
|
|
|
gisMapper.updateById(gis);
|
|
|
}
|
|
|
}
|
|
|
return device;
|
|
|
}).collect(Collectors.toList());
|
|
|
-
|
|
|
+
|
|
|
deviceService.updateBatchById(updateList);
|
|
|
log.debug("更新设备心跳成功, deviceId={}, count={}", heartInfo.getDeviceId(), updateList.size());
|
|
|
}
|
|
|
@@ -1241,9 +1214,6 @@ public class MqttService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 补充设备信息到 BrieflyEventInfo
|
|
|
- */
|
|
|
private void enrichDeviceInfo(BrieflyEventInfo info, SasDevice device) {
|
|
|
if (device != null) {
|
|
|
info.setAddress(device.getAddress());
|
|
|
@@ -1252,12 +1222,9 @@ public class MqttService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 补充设备信息到 BrieflyEventInfo (重载,用于兼容)
|
|
|
- */
|
|
|
+
|
|
|
private void enrichDeviceInfo(BrieflyEventInfo info, String deviceId, Integer channel, Integer deviceTypeCode) {
|
|
|
- SasDevice device = findDevice(deviceId, channel, deviceTypeCode);
|
|
|
+ SasDevice device = findDevice(deviceId, deviceTypeCode);
|
|
|
enrichDeviceInfo(info, device);
|
|
|
}
|
|
|
|
|
|
@@ -1340,5 +1307,4 @@ public class MqttService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-}
|
|
|
+}
|