Jelajahi Sumber

如果没有在sys_device表中,接收到的事件不存入库,同时不推websocket,同时在推送的websocket消息体中增加事件等级Id

james 6 hari lalu
induk
melakukan
569ad51b3e

+ 162 - 17
service-sas/service-sas-biz/src/main/java/com/usky/sas/mqtt/MqttService.java

@@ -71,6 +71,9 @@ public class MqttService {
     private SasConfigService sasConfigService;
     @Autowired
     private SasDeviceService deviceService;
+
+    @Autowired
+    private SasEventTypeGroupService eventTypeGroupService;
     @Autowired
     private SasGisMapper gisMapper;
     
@@ -259,6 +262,26 @@ public class MqttService {
         }
     }
 
+    /**
+     * 查询设备信息,如果设备不存在返回 null
+     */
+    private SasDevice findDevice(String deviceId, Integer channel, Integer deviceTypeCode) {
+        return deviceService.getOne(new LambdaQueryWrapper<SasDevice>()
+                .eq(SasDevice::getDeviceId, deviceId)
+                .eq(SasDevice::getChannel, channel)
+                .eq(SasDevice::getDeviceType, deviceTypeCode)
+                .last("limit 1"));
+    }
+
+    /**
+     * 查询事件类型分组信息
+     */
+    private SasEventTypeGroup findEventTypeGroup(Integer deviceTypeCode) {
+        return eventTypeGroupService.getOne(new LambdaQueryWrapper<SasEventTypeGroup>()
+                .eq(SasEventTypeGroup::getDeviceType, deviceTypeCode)
+                .last("limit 1"));
+    }
+
     @Transactional(rollbackFor = Exception.class)
     public void handleUsbEvent(String payload, String topic) {
         try {
@@ -267,6 +290,14 @@ public class MqttService {
 
             if ("event".equals(method)) {
                 UsbEventMessage message = mapper.readValue(payload, UsbEventMessage.class);
+                
+                // 1. 检查设备是否存在
+                SasDevice device = findDevice(message.getDeviceId(), message.getChannel(), SystemTypeCodeEnum.usb.getCode());
+                if (device == null) {
+                    log.warn("设备不存在,丢弃USB事件: deviceId={}, channel={}", message.getDeviceId(), message.getChannel());
+                    return;
+                }
+
                 SasUsbEvent event = new SasUsbEvent();
                 String eventId = message.getId() != null && !message.getId().isEmpty()
                         ? message.getId()
@@ -320,7 +351,11 @@ public class MqttService {
                     info.setEventTypeName(code.getName());
                 }
 
-                enrichDeviceInfo(info, event.getDeviceId(), event.getChannel(), SystemTypeCodeEnum.usb.getCode());
+                enrichDeviceInfo(info, device);
+                SasEventTypeGroup eventTypeGroup = findEventTypeGroup(SystemTypeCodeEnum.usb.getCode());
+                if (eventTypeGroup != null) {
+                    info.setEventLevelId(eventTypeGroup.getEventLevel());
+                }
                 info.setDeviceType(SystemTypeCodeEnum.usb.getCode());
                 SasWebSocket.sendAll(info);
 
@@ -341,6 +376,14 @@ public class MqttService {
 
             if ("event".equals(method)) {
                 SnapEventMessage message = mapper.readValue(payload, SnapEventMessage.class);
+
+                // 1. 检查设备是否存在
+                SasDevice device = findDevice(message.getDeviceId(), message.getChannel(), SystemTypeCodeEnum.snap.getCode());
+                if (device == null) {
+                    log.warn("设备不存在,丢弃抓拍事件: deviceId={}, channel={}", message.getDeviceId(), message.getChannel());
+                    return;
+                }
+
                 SasSnapEvent event = new SasSnapEvent();
 
                 String eventId = message.getId() != null && !message.getId().isEmpty()
@@ -393,7 +436,11 @@ public class MqttService {
                     info.setEventTypeName(code.getName());
                 }
 
-                enrichDeviceInfo(info, event.getDeviceId(), event.getChannel(), SystemTypeCodeEnum.snap.getCode());
+                enrichDeviceInfo(info, device);
+                SasEventTypeGroup eventTypeGroup = findEventTypeGroup(SystemTypeCodeEnum.snap.getCode());
+                if (eventTypeGroup != null) {
+                    info.setEventLevelId(eventTypeGroup.getEventLevel());
+                }
                 info.setDeviceType(SystemTypeCodeEnum.snap.getCode());
                 SasWebSocket.sendAll(info);
 
@@ -414,6 +461,14 @@ public class MqttService {
 
             if ("event".equals(method)) {
                 EntranceEventMessage message = mapper.readValue(payload, EntranceEventMessage.class);
+
+                // 1. 检查设备是否存在
+                SasDevice device = findDevice(message.getDeviceId(), message.getChannel(), SystemTypeCodeEnum.entrance.getCode());
+                if (device == null) {
+                    log.warn("设备不存在,丢弃门禁事件: deviceId={}, channel={}", message.getDeviceId(), message.getChannel());
+                    return;
+                }
+
                 SasEntranceEvent event = new SasEntranceEvent();
                 String eventId = message.getId() != null && !message.getId().isEmpty()
                         ? message.getId()
@@ -466,7 +521,11 @@ public class MqttService {
                     info.setEventTypeName(code.getName());
                 }
 
-                enrichDeviceInfo(info, event.getDeviceId(), event.getChannel(), SystemTypeCodeEnum.entrance.getCode());
+                enrichDeviceInfo(info, device);
+                SasEventTypeGroup eventTypeGroup = findEventTypeGroup(SystemTypeCodeEnum.entrance.getCode());
+                if (eventTypeGroup != null) {
+                    info.setEventLevelId(eventTypeGroup.getEventLevel());
+                }
                 info.setDeviceType(SystemTypeCodeEnum.entrance.getCode());
                 SasWebSocket.sendAll(info);
 
@@ -487,6 +546,14 @@ public class MqttService {
 
             if ("event".equals(method)) {
                 ParkingEventMessage message = mapper.readValue(payload, ParkingEventMessage.class);
+
+                // 1. 检查设备是否存在
+                SasDevice device = findDevice(message.getDeviceId(), message.getChannel(), SystemTypeCodeEnum.parking.getCode());
+                if (device == null) {
+                    log.warn("设备不存在,丢弃停车场事件: deviceId={}, channel={}", message.getDeviceId(), message.getChannel());
+                    return;
+                }
+
                 SasParkingEvent event = new SasParkingEvent();
                 String eventId = message.getId() != null && !message.getId().isEmpty()
                         ? message.getId()
@@ -538,7 +605,11 @@ public class MqttService {
                     info.setEventTypeName(code.getName());
                 }
 
-                enrichDeviceInfo(info, event.getDeviceId(), event.getChannel(), SystemTypeCodeEnum.parking.getCode());
+                enrichDeviceInfo(info, device);
+                SasEventTypeGroup eventTypeGroup = findEventTypeGroup(SystemTypeCodeEnum.parking.getCode());
+                if (eventTypeGroup != null) {
+                    info.setEventLevelId(eventTypeGroup.getEventLevel());
+                }
                 info.setDeviceType(SystemTypeCodeEnum.parking.getCode());
                 SasWebSocket.sendAll(info);
 
@@ -559,6 +630,14 @@ public class MqttService {
 
             if ("event".equals(method)) {
                 RoadblockEventMessage message = mapper.readValue(payload, RoadblockEventMessage.class);
+
+                // 1. 检查设备是否存在
+                SasDevice device = findDevice(message.getDeviceId(), message.getChannel(), SystemTypeCodeEnum.roadblock.getCode());
+                if (device == null) {
+                    log.warn("设备不存在,丢弃阻车路障事件: deviceId={}, channel={}", message.getDeviceId(), message.getChannel());
+                    return;
+                }
+
                 SasRoadblockEvent event = new SasRoadblockEvent();
                 String eventId = message.getId() != null && !message.getId().isEmpty()
                         ? message.getId()
@@ -610,7 +689,11 @@ public class MqttService {
                     info.setEventTypeName(code.getName());
                 }
 
-                enrichDeviceInfo(info, event.getDeviceId(), event.getChannel(), SystemTypeCodeEnum.roadblock.getCode());
+                enrichDeviceInfo(info, device);
+                SasEventTypeGroup eventTypeGroup = findEventTypeGroup(SystemTypeCodeEnum.roadblock.getCode());
+                if (eventTypeGroup != null) {
+                    info.setEventLevelId(eventTypeGroup.getEventLevel());
+                }
                 info.setDeviceType(SystemTypeCodeEnum.roadblock.getCode());
                 SasWebSocket.sendAll(info);
 
@@ -631,6 +714,14 @@ public class MqttService {
 
             if ("event".equals(method)) {
                 AlarsasEventMessage message = mapper.readValue(payload, AlarsasEventMessage.class);
+
+                // 1. 检查设备是否存在
+                SasDevice device = findDevice(message.getDeviceId(), message.getChannel(), SystemTypeCodeEnum.alarm.getCode());
+                if (device == null) {
+                    log.warn("设备不存在,丢弃入侵报警事件: deviceId={}, channel={}", message.getDeviceId(), message.getChannel());
+                    return;
+                }
+
                 SasAlarsasEvent event = new SasAlarsasEvent();
                 String eventId = message.getId() != null && !message.getId().isEmpty()
                         ? message.getId()
@@ -682,7 +773,11 @@ public class MqttService {
                     info.setEventTypeName(code.getName());
                 }
 
-                enrichDeviceInfo(info, event.getDeviceId(), event.getChannel(), SystemTypeCodeEnum.alarm.getCode());
+                enrichDeviceInfo(info, device);
+                SasEventTypeGroup eventTypeGroup = findEventTypeGroup(SystemTypeCodeEnum.alarm.getCode());
+                if (eventTypeGroup != null) {
+                    info.setEventLevelId(eventTypeGroup.getEventLevel());
+                }
                 info.setDeviceType(SystemTypeCodeEnum.alarm.getCode());
                 SasWebSocket.sendAll(info);
 
@@ -703,6 +798,14 @@ public class MqttService {
 
             if ("event".equals(method)) {
                 PatrolEventMessage message = mapper.readValue(payload, PatrolEventMessage.class);
+
+                // 1. 检查设备是否存在
+                SasDevice device = findDevice(message.getDeviceId(), message.getChannel(), SystemTypeCodeEnum.patrol.getCode());
+                if (device == null) {
+                    log.warn("设备不存在,丢弃巡检事件: deviceId={}, channel={}", message.getDeviceId(), message.getChannel());
+                    return;
+                }
+
                 SasPatrolEvent event = new SasPatrolEvent();
                 String eventId = message.getId() != null && !message.getId().isEmpty()
                         ? message.getId()
@@ -754,7 +857,11 @@ public class MqttService {
                     info.setEventTypeName(code.getName());
                 }
 
-                enrichDeviceInfo(info, event.getDeviceId(), event.getChannel(), SystemTypeCodeEnum.patrol.getCode());
+                enrichDeviceInfo(info, device);
+                SasEventTypeGroup eventTypeGroup = findEventTypeGroup(SystemTypeCodeEnum.patrol.getCode());
+                if (eventTypeGroup != null) {
+                    info.setEventLevelId(eventTypeGroup.getEventLevel());
+                }
                 info.setDeviceType(SystemTypeCodeEnum.patrol.getCode());
                 SasWebSocket.sendAll(info);
 
@@ -775,6 +882,14 @@ public class MqttService {
 
             if ("event".equals(method)) {
                 AcquisitionEventMessage message = mapper.readValue(payload, AcquisitionEventMessage.class);
+
+                // 1. 检查设备是否存在
+                SasDevice device = findDevice(message.getDeviceId(), message.getChannel(), SystemTypeCodeEnum.acquisition.getCode());
+                if (device == null) {
+                    log.warn("设备不存在,丢弃数据采集事件: deviceId={}, channel={}", message.getDeviceId(), message.getChannel());
+                    return;
+                }
+
                 SasAcquisitionEvent event = new SasAcquisitionEvent();
                 String eventId = message.getId() != null && !message.getId().isEmpty()
                         ? message.getId()
@@ -826,7 +941,11 @@ public class MqttService {
                     info.setEventTypeName(code.getName());
                 }
 
-                enrichDeviceInfo(info, event.getDeviceId(), event.getChannel(), SystemTypeCodeEnum.acquisition.getCode());
+                enrichDeviceInfo(info, device);
+                SasEventTypeGroup eventTypeGroup = findEventTypeGroup(SystemTypeCodeEnum.acquisition.getCode());
+                if (eventTypeGroup != null) {
+                    info.setEventLevelId(eventTypeGroup.getEventLevel());
+                }
                 info.setDeviceType(SystemTypeCodeEnum.acquisition.getCode());
                 SasWebSocket.sendAll(info);
 
@@ -847,6 +966,14 @@ public class MqttService {
 
             if ("event".equals(method)) {
                 PerceptionEventMessage message = mapper.readValue(payload, PerceptionEventMessage.class);
+
+                // 1. 检查设备是否存在
+                SasDevice device = findDevice(message.getDeviceId(), message.getChannel(), SystemTypeCodeEnum.perception.getCode());
+                if (device == null) {
+                    log.warn("设备不存在,丢弃状态感知事件: deviceId={}, channel={}", message.getDeviceId(), message.getChannel());
+                    return;
+                }
+
                 SasPerceptionEvent event = new SasPerceptionEvent();
                 String eventId = message.getId() != null && !message.getId().isEmpty()
                         ? message.getId()
@@ -898,7 +1025,11 @@ public class MqttService {
                     info.setEventTypeName(code.getName());
                 }
 
-                enrichDeviceInfo(info, event.getDeviceId(), event.getChannel(), SystemTypeCodeEnum.perception.getCode());
+                enrichDeviceInfo(info, device);
+                SasEventTypeGroup eventTypeGroup = findEventTypeGroup(SystemTypeCodeEnum.perception.getCode());
+                if (eventTypeGroup != null) {
+                    info.setEventLevelId(eventTypeGroup.getEventLevel());
+                }
                 info.setDeviceType(SystemTypeCodeEnum.perception.getCode());
                 SasWebSocket.sendAll(info);
 
@@ -919,6 +1050,14 @@ public class MqttService {
 
             if ("event".equals(method)) {
                 CollectionEventMessage message = mapper.readValue(payload, CollectionEventMessage.class);
+
+                // 1. 检查设备是否存在
+                SasDevice device = findDevice(message.getDeviceId(), message.getChannel(), SystemTypeCodeEnum.collection.getCode());
+                if (device == null) {
+                    log.warn("设备不存在,丢弃状态采集事件: deviceId={}, channel={}", message.getDeviceId(), message.getChannel());
+                    return;
+                }
+
                 SasCollectionEvent event = new SasCollectionEvent();
                 String eventId = message.getId() != null && !message.getId().isEmpty()
                         ? message.getId()
@@ -970,7 +1109,11 @@ public class MqttService {
                     info.setEventTypeName(code.getName());
                 }
 
-                enrichDeviceInfo(info, event.getDeviceId(), event.getChannel(), SystemTypeCodeEnum.collection.getCode());
+                enrichDeviceInfo(info, device);
+                SasEventTypeGroup eventTypeGroup = findEventTypeGroup(SystemTypeCodeEnum.collection.getCode());
+                if (eventTypeGroup != null) {
+                    info.setEventLevelId(eventTypeGroup.getEventLevel());
+                }
                 info.setDeviceType(SystemTypeCodeEnum.collection.getCode());
                 SasWebSocket.sendAll(info);
 
@@ -1026,13 +1169,7 @@ public class MqttService {
     /**
      * 补充设备信息到 BrieflyEventInfo
      */
-    private void enrichDeviceInfo(BrieflyEventInfo info, String deviceId, Integer channel, Integer deviceTypeCode) {
-        SasDevice device = deviceService.getOne(new LambdaQueryWrapper<SasDevice>()
-                .eq(SasDevice::getDeviceId, deviceId)
-                .eq(SasDevice::getChannel, channel)
-                .eq(SasDevice::getDeviceType, deviceTypeCode)
-                .last("limit 1")); // 确保只查一条
-        
+    private void enrichDeviceInfo(BrieflyEventInfo info, SasDevice device) {
         if (device != null) {
             info.setAddress(device.getAddress());
             if (StrUtil.isBlank(info.getNote())) {
@@ -1040,6 +1177,14 @@ public class MqttService {
             }
         }
     }
+    
+    /**
+     * 补充设备信息到 BrieflyEventInfo (重载,用于兼容)
+     */
+    private void enrichDeviceInfo(BrieflyEventInfo info, String deviceId, Integer channel, Integer deviceTypeCode) {
+        SasDevice device = findDevice(deviceId, channel, deviceTypeCode);
+        enrichDeviceInfo(info, device);
+    }
 
     private String extractDeviceId(String topic) {
         String[] parts = topic.split("/");

+ 5 - 1
service-sas/service-sas-biz/src/main/java/com/usky/sas/mqtt/dto/BrieflyEventInfo.java

@@ -3,7 +3,6 @@ package com.usky.sas.mqtt.dto;
 import lombok.Data;
 
 import java.io.Serializable;
-import java.time.LocalDateTime;
 
 /**
  * 简要事件信息 (WebSocket 推送)
@@ -26,6 +25,11 @@ public class BrieflyEventInfo implements Serializable {
      * 事件类型名称
      */
     private String eventTypeName;
+    
+    /**
+     * 事件等级ID
+     */
+    private Integer eventLevelId;
 
     /**
      * 触发时间