From 7f8015910cbf9311e0c228498147597189a4835f Mon Sep 17 00:00:00 2001 From: lau572 <1010031226@qq.com> Date: Fri, 7 Jun 2024 11:31:18 +0800 Subject: [PATCH] =?UTF-8?q?websocket=E6=8E=A8=E9=80=81=E5=BE=85=E5=A4=84?= =?UTF-8?q?=E7=90=86=E6=95=B0=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../websocket/constant/WebSocketEvent.java | 14 +++++++ .../controller/DcEventController.java | 8 ++-- .../device/handler/DeviceMessageHandler.java | 18 +++++++-- .../zc/business/service/IDcEventService.java | 4 +- .../service/impl/DcEventServiceImpl.java | 38 +++++++++++-------- .../service/impl/DcWarningServiceImpl.java | 22 +++++++++-- 6 files changed, 75 insertions(+), 29 deletions(-) diff --git a/ruoyi-common/src/main/java/com/zc/common/core/websocket/constant/WebSocketEvent.java b/ruoyi-common/src/main/java/com/zc/common/core/websocket/constant/WebSocketEvent.java index caf3bd10..85a10ec6 100644 --- a/ruoyi-common/src/main/java/com/zc/common/core/websocket/constant/WebSocketEvent.java +++ b/ruoyi-common/src/main/java/com/zc/common/core/websocket/constant/WebSocketEvent.java @@ -3,4 +3,18 @@ package com.zc.common.core.websocket.constant; public class WebSocketEvent { public static final String REACTIVE = "reactive"; + + //感知事件 + public static final String WARNING = "1"; + + //交通事件 + public static final String EVENT = "0"; + + //感知事件+待确认数量统计 + public static final String EVENT_COUNT = "eventCount"; + + //设备状态 + public static final String DEVICE_STATE = "deviceState"; + + } diff --git a/zc-business/src/main/java/com/zc/business/controller/DcEventController.java b/zc-business/src/main/java/com/zc/business/controller/DcEventController.java index 14b32b58..f24c3fae 100644 --- a/zc-business/src/main/java/com/zc/business/controller/DcEventController.java +++ b/zc-business/src/main/java/com/zc/business/controller/DcEventController.java @@ -233,12 +233,12 @@ public class DcEventController extends BaseController * * @author liuwenge * @date 2024/6/6 18:45 - * @param type + * @param * @return com.ruoyi.common.core.domain.AjaxResult */ @ApiOperation("获取事件数量") - @GetMapping( "/getCountNum/{type}") - public AjaxResult getCountNum(@ApiParam(name = "type", value = "类型 1:告警 2:待确认 3:处置中", required = true) @PathVariable("type") String type){ - return dcEventService.getCountNum(type); + @GetMapping( "/getCountNum") + public Long getCountNum(){ + return dcEventService.getCountNum(); } } diff --git a/zc-business/src/main/java/com/zc/business/message/device/handler/DeviceMessageHandler.java b/zc-business/src/main/java/com/zc/business/message/device/handler/DeviceMessageHandler.java index 8d014ecf..24452d53 100644 --- a/zc-business/src/main/java/com/zc/business/message/device/handler/DeviceMessageHandler.java +++ b/zc-business/src/main/java/com/zc/business/message/device/handler/DeviceMessageHandler.java @@ -17,6 +17,7 @@ import com.zc.business.enums.WarningStateEnum; import com.zc.business.enums.WarningSubclassEnum; import com.zc.business.service.*; import com.zc.common.core.websocket.WebSocketService; +import com.zc.common.core.websocket.constant.WebSocketEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -50,6 +51,9 @@ public class DeviceMessageHandler { @Resource private IDcWarningService dcWarningService; + @Resource + private IDcEventService dcEventService; + @Resource private IMiddleDatabaseService middleDatabaseService; @@ -88,7 +92,7 @@ public class DeviceMessageHandler { } }); - WebSocketService.broadcast(DEVICE_STATE, dcDevices); //推送设备状态更新 + WebSocketService.broadcast(WebSocketEvent.DEVICE_STATE, dcDevices); //推送设备状态更新 // 批量更新中间库设备状态 middleDatabaseService.updateMiddleDatabaseDeviceByList(dcDevices); } @@ -593,7 +597,7 @@ public class DeviceMessageHandler { //todo 首页推送事件消息 3气象检测器 if (!meteorologicalDetectorData.getPrecipitationType().equals("0")) {//降水类型 0=无降;1=雨;2=雪;3=毛毛雨;4=雨夹雪; - WebSocketService.broadcast("3", meteorologicalDetectorData); + WebSocketService.broadcast(WebSocketEvent.WARNING, meteorologicalDetectorData); JSONObject object = new JSONObject(); object.put("meteorologicalDetectorData", meteorologicalDetectorData); String string = object.toString(); @@ -604,9 +608,13 @@ public class DeviceMessageHandler { dcWarning.setOtherConfig(string); dcWarning.setWarningTitle("气象预警"); dcWarningService.insertDcWarning(dcWarning); + + //待确认数量 + Long count = dcEventService.getCountNum(); + WebSocketService.broadcast(WebSocketEvent.EVENT_COUNT, count); } if (!meteorologicalDetectorData.getVisibilityType().equals("4")) {// 能见度类型,4 表示能见度良好3表示阴霾 2表示雾 1表示浓雾; - WebSocketService.broadcast("3", meteorologicalDetectorData); + WebSocketService.broadcast(WebSocketEvent.WARNING, meteorologicalDetectorData); JSONObject object = new JSONObject(); object.put("meteorologicalDetectorData", meteorologicalDetectorData); String string = object.toString(); @@ -617,6 +625,10 @@ public class DeviceMessageHandler { dcWarning.setWarningTitle("气象预警"); dcWarningService.insertDcWarning(dcWarning); + + //待确认数量 + Long count = dcEventService.getCountNum(); + WebSocketService.broadcast(WebSocketEvent.EVENT_COUNT, count); } diff --git a/zc-business/src/main/java/com/zc/business/service/IDcEventService.java b/zc-business/src/main/java/com/zc/business/service/IDcEventService.java index 6e2943eb..081762b4 100644 --- a/zc-business/src/main/java/com/zc/business/service/IDcEventService.java +++ b/zc-business/src/main/java/com/zc/business/service/IDcEventService.java @@ -145,8 +145,8 @@ public interface IDcEventService * * @author liuwenge * @date 2024/6/6 18:45 - * @param type 类型 1:告警 2:待确认 3:处置中 + * @param * @return com.ruoyi.common.core.domain.AjaxResult */ - AjaxResult getCountNum(String type); + Long getCountNum(); } diff --git a/zc-business/src/main/java/com/zc/business/service/impl/DcEventServiceImpl.java b/zc-business/src/main/java/com/zc/business/service/impl/DcEventServiceImpl.java index b50c21a4..46786c06 100644 --- a/zc-business/src/main/java/com/zc/business/service/impl/DcEventServiceImpl.java +++ b/zc-business/src/main/java/com/zc/business/service/impl/DcEventServiceImpl.java @@ -21,6 +21,7 @@ import com.zc.business.utils.StakeMarkUtils; import com.zc.business.utils.diff.Diff; import com.zc.business.utils.diff.model.Result; import com.zc.common.core.websocket.WebSocketService; +import com.zc.common.core.websocket.constant.WebSocketEvent; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -573,7 +574,12 @@ public class DcEventServiceImpl extends ServiceImpl impl Map contentMap = new HashMap<>(); contentMap.put("content", content); contentMap.put("event", dcEvent); - WebSocketService.broadcast(SUBEVENT, contentMap); //推送事件消息 0不是感知事件 + + WebSocketService.broadcast(WebSocketEvent.EVENT, contentMap); //推送事件消息 0不是感知事件 + + Long count = getCountNum(); + WebSocketService.broadcast(WebSocketEvent.EVENT_COUNT, count); + } //根据路网设施查询桩号 @@ -1051,6 +1057,9 @@ public class DcEventServiceImpl extends ServiceImpl impl break; } } + //推送待处理数量 + Long count = getCountNum(); + WebSocketService.broadcast(WebSocketEvent.EVENT_COUNT,count); return i7; } @@ -1223,6 +1232,11 @@ public class DcEventServiceImpl extends ServiceImpl impl int i = dcEventMapper.updateDcEventState(id, state); if (i > 0) { + + //推送待处理数量 + Long count = getCountNum(); + WebSocketService.broadcast(WebSocketEvent.EVENT_COUNT,count); + DcEvent dcEvent = dcEventMapper.selectDcEventMainById(id); //中间库 @@ -1929,26 +1943,18 @@ public class DcEventServiceImpl extends ServiceImpl impl * * @author liuwenge * @date 2024/6/6 18:45 - * @param type 类型 1:告警 2:待确认 3:处置中 + * @param * @return com.ruoyi.common.core.domain.AjaxResult */ @Override - public AjaxResult getCountNum(String type){ - if (StringUtils.isEmpty(type)){ - return AjaxResult.error("类型不能为空"); - } + public Long getCountNum(){ + + Long warningCount = dcWarningMapper.selectWarningNum(); + + Long eventCount = dcEventMapper.selectEventNumByStatus("0"); - Long num = 0L; - //告警 - if (type.equals("1")){ - num = dcWarningMapper.selectWarningNum(); - } else if (type.equals("2")){ - num = dcEventMapper.selectEventNumByStatus("0"); - } else if (type.equals("3")){ - num = dcEventMapper.selectEventNumByStatus("1"); - } - return AjaxResult.success(num); + return warningCount + eventCount; } diff --git a/zc-business/src/main/java/com/zc/business/service/impl/DcWarningServiceImpl.java b/zc-business/src/main/java/com/zc/business/service/impl/DcWarningServiceImpl.java index 696ff268..5f272186 100644 --- a/zc-business/src/main/java/com/zc/business/service/impl/DcWarningServiceImpl.java +++ b/zc-business/src/main/java/com/zc/business/service/impl/DcWarningServiceImpl.java @@ -18,6 +18,7 @@ import com.zc.business.service.IDcTrafficPoliceService; import com.zc.business.service.IDcWarningService; import com.zc.business.utils.StakeMarkUtils; import com.zc.common.core.websocket.WebSocketService; +import com.zc.common.core.websocket.constant.WebSocketEvent; import org.apache.commons.lang3.StringUtils; import org.json.JSONArray; import org.json.JSONObject; @@ -167,9 +168,12 @@ public class DcWarningServiceImpl implements IDcWarningService redisCache.deleteObject(WARNINGDATA+key); String id = dcWarning.getId(); redisCache.setCacheObject(WARNINGDATA+key,id,Integer.parseInt(strategyTime),TimeUnit.MINUTES); + int i = dcWarningMapper.insertDcWarning(dcWarning); + //事件推送至 Websocket extracted(dcWarning); - return dcWarningMapper.insertDcWarning(dcWarning); + + return i; } String otherConfig=""; if (map.get("otherConfig")!=null){ @@ -201,8 +205,9 @@ public class DcWarningServiceImpl implements IDcWarningService dcWarning.setEndTime(DateUtils.getObtainDateAfter(60));//注入过期时间(默认值为60分钟) } //事件推送至 Websocket + int i = dcWarningMapper.insertDcWarning(dcWarning);//如果没有配置策略直接加入数据库; extracted(dcWarning); - return dcWarningMapper.insertDcWarning(dcWarning);//如果没有配置策略直接加入数据库; + return i; } /** @@ -227,7 +232,9 @@ public class DcWarningServiceImpl implements IDcWarningService Map contentMap = new HashMap<>(); contentMap.put("content",content); contentMap.put("event",dcWarning); - WebSocketService.broadcast(SUBEVENT, contentMap); //推送事件消息 0不是感知事件 + WebSocketService.broadcast(WebSocketEvent.WARNING, contentMap); //推送事件消息 0不是感知事件 + Long count = dcEventService.getCountNum(); + WebSocketService.broadcast(WebSocketEvent.EVENT_COUNT, count); //推送待处理数量 } //优先级策略(还差优先级策略需要配置) @@ -516,6 +523,9 @@ public class DcWarningServiceImpl implements IDcWarningService if (insertDcEvent==0){ return AjaxResult.error("操作失败"); } + //推送待处理数量 + Long count = dcEventService.getCountNum(); + WebSocketService.broadcast(WebSocketEvent.EVENT_COUNT,count); return AjaxResult.success("操作成功"); } @@ -924,7 +934,11 @@ public class DcWarningServiceImpl implements IDcWarningService @Override public Integer falseAlarmResolution(DcWarning dcWarning) { dcWarning.setUpdateTime(DateUtils.getNowDate()); - return dcWarningMapper.falseAlarmResolution(dcWarning); + Integer i = dcWarningMapper.falseAlarmResolution(dcWarning); + //推送待处理数量 + Long count = dcEventService.getCountNum(); + WebSocketService.broadcast(WebSocketEvent.EVENT_COUNT,count); + return i; } }