diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/AlarmService.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/AlarmService.java index 0898de5..d9c7f56 100644 --- a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/AlarmService.java +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/AlarmService.java @@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.hnac.hzims.alarm.entity.AlarmEntity; import com.hnac.hzims.alarm.vo.AlarmCountVo; import org.springblade.core.mp.base.BaseService; +import org.springframework.web.socket.TextMessage; import java.util.List; @@ -17,4 +18,6 @@ public interface AlarmService extends BaseService { List counts(AlarmEntity alarm); List broadcast(String startTime, String endTime); + + TextMessage majorAlarm(List depts); } \ No newline at end of file diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/AlarmServiceImpl.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/AlarmServiceImpl.java index e9dd76f..18c43d1 100644 --- a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/AlarmServiceImpl.java +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/AlarmServiceImpl.java @@ -1,7 +1,9 @@ package com.hnac.hzims.alarm.show.service.impl; +import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.hnac.hzims.alarm.config.service.AlarmConfigService; import com.hnac.hzims.alarm.constants.AlarmConstants; import com.hnac.hzims.alarm.entity.AlarmEntity; @@ -23,10 +25,9 @@ import org.springblade.core.tool.utils.StringUtil; import org.springblade.system.user.entity.User; import org.springblade.system.user.feign.IUserClient; import org.springframework.stereotype.Service; +import org.springframework.web.socket.TextMessage; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; /** @@ -165,7 +166,33 @@ public class AlarmServiceImpl extends BaseServiceImpl return this.list(wrapper); } - + /** + * 弹框告警 + * @param depts + * @return + */ + @Override + public TextMessage majorAlarm(List depts) { + // 站点查询 + List stations = this.stations(); + if(CollectionUtil.isEmpty(stations)){ + return null; + } + List effectives = stations.stream().filter(station->depts.contains(station.getRefDept())).map(StationEntity::getCode).collect(Collectors.toList()); + if(CollectionUtil.isEmpty(effectives)){ + return null; + } + List alarms = this.list(Wrappers.lambdaQuery() + .in(AlarmEntity::getStationId,effectives) + .in(AlarmEntity::getAlarmType, Arrays.asList(AlarmConstants.FAULT,AlarmConstants.EARLY)) + .eq(AlarmEntity::getIsShowAlert,0) + //.ge(AlarmEntity::getAlarmType, DateUtil.format(new Date(),DateUtil.PATTERN_DATE) + " 00:00:00") + ); + if(CollectionUtil.isEmpty(alarms)){ + return null; + } + return new TextMessage(JSONObject.toJSONString(alarms.stream().sorted(Comparator.comparing(AlarmEntity::getAlarmTime).reversed()).collect(Collectors.toList()))); + } /** diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/alart/AlarmHandler.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/alart/AlarmHandler.java new file mode 100644 index 0000000..a44e902 --- /dev/null +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/alart/AlarmHandler.java @@ -0,0 +1,70 @@ +package com.hnac.hzims.alarm.ws.alart; + +import com.alibaba.fastjson.JSONObject; +import com.hnac.hzims.alarm.show.service.AlarmService; +import lombok.extern.slf4j.Slf4j; +import org.springblade.core.tool.utils.CollectionUtil; +import org.springblade.core.tool.utils.ObjectUtil; +import org.springblade.core.tool.utils.StringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.socket.CloseStatus; +import org.springframework.web.socket.TextMessage; +import org.springframework.web.socket.WebSocketSession; +import org.springframework.web.socket.handler.TextWebSocketHandler; + +import java.io.IOException; +import java.util.List; + +/** + * @author ysj + */ +@Slf4j +public class AlarmHandler extends TextWebSocketHandler { + + @Autowired + private AlarmService alarmService; + + @Override + public void afterConnectionEstablished(WebSocketSession session) { + String[] split = session.getUri().toString().split("/"); + String uid = split[split.length - 1]; + AlarmSessionManager.add(uid, session); + session.getAttributes().put("userId", uid); + log.info("sessionId: " + session.getId()); + log.info("session connection successful!"); + AlarmSocketPool.pool.put(session.getId(), this); + } + + @Override + public void afterConnectionClosed(WebSocketSession session, CloseStatus status) { + AlarmSessionManager.removeAndClose(session.getId()); + AlarmSocketPool.pool.remove(session.getId()); + log.info("sessionId: " + session.getId()); + log.info("uri: " + session.getUri()); + log.info("session closed successful!"); + } + + @Override + public void handleTransportError(WebSocketSession session, Throwable exception) { + AlarmSessionManager.removeAndClose(session.getId()); + AlarmSocketPool.pool.remove(session.getId()); + } + + @Override + protected void handleTextMessage(WebSocketSession session, TextMessage textMessage) throws IOException { + String userId = (String) session.getAttributes().get("userId"); + String message = textMessage.getPayload(); + if(StringUtil.isBlank(message)){ + return; + } + List depts = JSONObject.parseArray(message,Long.class); + if(CollectionUtil.isEmpty(depts)){ + return; + } + TextMessage sendMessage = alarmService.majorAlarm(depts); + if(ObjectUtil.isEmpty(sendMessage)){ + return; + } + session.sendMessage(sendMessage); + } +} diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/alart/AlarmSessionManager.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/alart/AlarmSessionManager.java new file mode 100644 index 0000000..b54e6b2 --- /dev/null +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/alart/AlarmSessionManager.java @@ -0,0 +1,63 @@ +package com.hnac.hzims.alarm.ws.alart; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.socket.WebSocketSession; + +import java.io.IOException; +import java.util.concurrent.ConcurrentHashMap; + +/** + * @author ysj + */ +@Slf4j +public class AlarmSessionManager { + /** + * ws会话池 + */ + public static ConcurrentHashMap SESSION_POOL = new ConcurrentHashMap<>(); + + /** + * 添加会话 + * + * @param uid 标记 + * @param session 会话对象 + */ + public static void add(String uid, WebSocketSession session) { + if (SESSION_POOL.containsKey(uid)) { + AlarmSessionManager.removeAndClose(uid); + } + SESSION_POOL.put(uid, session); + log.info("添加 WebSocketSession 会话成功,uid=" + uid); + } + + + /** + * 获取 ws 会话 + * + * @param uid + */ + public static WebSocketSession get(String uid) { + return SESSION_POOL.get(uid); + } + + /** + * 移除 ws 会话并关闭会话 + * + * @param uid + */ + public static void removeAndClose(String uid) { + WebSocketSession session = SESSION_POOL.get(uid); + if (session != null) { + try { + //关闭连接 + session.close(); + } catch (IOException ex) { + throw new RuntimeException("关闭ws会话失败!", ex); + } + } + + SESSION_POOL.remove(uid); + } + + +} diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/alart/AlarmSocketConfig.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/alart/AlarmSocketConfig.java new file mode 100644 index 0000000..bbceab3 --- /dev/null +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/alart/AlarmSocketConfig.java @@ -0,0 +1,33 @@ +package com.hnac.hzims.alarm.ws.alart; + + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.socket.WebSocketHandler; +import org.springframework.web.socket.config.annotation.EnableWebSocket; +import org.springframework.web.socket.config.annotation.WebSocketConfigurer; +import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; +import org.springframework.web.socket.server.standard.ServerEndpointExporter; + +/** + * @author ysj + */ +@Configuration +@EnableWebSocket +public class AlarmSocketConfig implements WebSocketConfigurer{ + @Override + public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { + // 集中监控弹框处理器 + registry.addHandler(monitorHandler(), "/alarm/alart/{uid}").setAllowedOrigins("*"); + } + + @Bean + public WebSocketHandler monitorHandler() { + return new AlarmHandler(); + } + + @Bean + public ServerEndpointExporter serverEndpointExporter() { + return new ServerEndpointExporter(); + } +} diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/alart/AlarmSocketPool.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/alart/AlarmSocketPool.java new file mode 100644 index 0000000..c7bc6cc --- /dev/null +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/alart/AlarmSocketPool.java @@ -0,0 +1,13 @@ +package com.hnac.hzims.alarm.ws.alart; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * @author ysj + */ +public class AlarmSocketPool { + + public static Map pool = new ConcurrentHashMap<>(); + +}