From c7c666b8e9f414f978c583e83118d3ad3d76db62 Mon Sep 17 00:00:00 2001 From: yang_shj <1069818635@QQ.com> Date: Thu, 14 Sep 2023 14:04:16 +0800 Subject: [PATCH] =?UTF-8?q?#=E5=91=8A=E8=AD=A6=E4=BB=A3=E7=A0=81=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alarm/show/service/ConditionAlarmService.java | 12 ++ .../service/impl/ConditionAlarmServiceImpl.java | 50 ++++++++ .../show/service/impl/LevelAlarmServiceImpl.java | 2 +- .../alarm/ws/condition/ConditionAlarmRegular.java | 61 ++++++++++ .../ws/condition/ConditionAlarmWebSocket.java | 126 +++++++++++++++++++++ .../hzims/alarm/ws/hz3000/SystemAlarmRegular.java | 6 +- 6 files changed, 253 insertions(+), 4 deletions(-) create mode 100644 hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/ConditionAlarmService.java create mode 100644 hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/ConditionAlarmServiceImpl.java create mode 100644 hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/condition/ConditionAlarmRegular.java create mode 100644 hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/condition/ConditionAlarmWebSocket.java diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/ConditionAlarmService.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/ConditionAlarmService.java new file mode 100644 index 0000000..d79f7f9 --- /dev/null +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/ConditionAlarmService.java @@ -0,0 +1,12 @@ +package com.hnac.hzims.alarm.show.service; + +/** + * 等级处理接口 + * @author ysj + */ +public interface ConditionAlarmService { + + String message(); + + void receive(String message); +} \ No newline at end of file diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/ConditionAlarmServiceImpl.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/ConditionAlarmServiceImpl.java new file mode 100644 index 0000000..6f763da --- /dev/null +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/ConditionAlarmServiceImpl.java @@ -0,0 +1,50 @@ +package com.hnac.hzims.alarm.show.service.impl; + +import com.hnac.hzims.alarm.show.service.AlarmService; +import com.hnac.hzims.alarm.show.service.ConditionAlarmService; +import com.hnac.hzims.alarm.show.service.MessageService; +import com.hnac.hzims.equipment.feign.IEmInfoClient; +import com.hnac.hzims.operational.station.feign.IStationClient; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; + +/** + * 等级告警实现类 + * @author ysj + */ +@Slf4j +@Service +@RequiredArgsConstructor +public class ConditionAlarmServiceImpl implements ConditionAlarmService { + + private final AlarmService alarmService; + + private final MessageService messageService; + + private final RedisTemplate redisTemplate; + + private final IEmInfoClient deviceClient; + + private final IStationClient stationClient; + + public final static String device_cache_cofig_final = "hzims:equipment:emInfo:deviceCode.emInfoList"; + + /** + * 发送websocket消息 + * @return + */ + @Override + public String message() { + return ""; + } + + /** + * 处理websocket消息 + * @param message + */ + @Override + public void receive(String message) { + } +} \ No newline at end of file diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/LevelAlarmServiceImpl.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/LevelAlarmServiceImpl.java index 663332c..e8f89e1 100644 --- a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/LevelAlarmServiceImpl.java +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/LevelAlarmServiceImpl.java @@ -49,7 +49,7 @@ public class LevelAlarmServiceImpl implements LevelAlarmService { public final static String device_cache_cofig_final = "hzims:equipment:emInfo:deviceCode.emInfoList"; /** - * 发生websocket消息 + * 发送websocket消息 * @return */ @Override diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/condition/ConditionAlarmRegular.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/condition/ConditionAlarmRegular.java new file mode 100644 index 0000000..5f38a60 --- /dev/null +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/condition/ConditionAlarmRegular.java @@ -0,0 +1,61 @@ +package com.hnac.hzims.alarm.ws.condition; + +import com.hnac.hzims.alarm.show.service.ConditionAlarmService; +import com.hnac.hzims.alarm.show.service.LevelAlarmService; +import com.hnac.hzims.alarm.ws.level.LevelAlarmWebSocket; +import lombok.extern.slf4j.Slf4j; +import org.springblade.core.tool.utils.ObjectUtil; +import org.springblade.core.tool.utils.StringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.net.URI; + +/** + * @author ysj + */ +@Slf4j +@Component +@EnableScheduling +public class ConditionAlarmRegular { + + @Autowired + private ConditionAlarmService conditionService; + + private LevelAlarmWebSocket socket; + + @Value("${hzims.condition.ws-url}") + private String level_wss_url; + + // 两分钟进行一次检查链接是否存活,不存活了重新建立链接 + @Scheduled(cron = "0 0/2 * * * ?") + private void regular(){ + // 检查链接存活状态 + if(ObjectUtil.isEmpty(socket) || !socket.isOpen()){ + log.error("level websocket survival check : {}","死亡"); + this.createSocket(); + if(ObjectUtil.isNotEmpty(socket) && socket.isOpen()){ + String message = conditionService.message(); + if(!StringUtil.isEmpty(message)){ + socket.send(message); + } + } + log.error("level websocket survival check : {}","重新建立链接"); + }else{ + log.error("level websocket survival check : {}","存活"); + } + } + + // 创建websocket链接 + private void createSocket() { + try{ + socket = new LevelAlarmWebSocket(new URI(level_wss_url)); + socket.connectBlocking(); + }catch (Exception e){ + log.error("level create error : {}",e.getMessage()); + } + } +} \ No newline at end of file diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/condition/ConditionAlarmWebSocket.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/condition/ConditionAlarmWebSocket.java new file mode 100644 index 0000000..29183f6 --- /dev/null +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/condition/ConditionAlarmWebSocket.java @@ -0,0 +1,126 @@ +package com.hnac.hzims.alarm.ws.condition; + +import com.hnac.hzims.alarm.show.service.ConditionAlarmService; +import com.hnac.hzims.alarm.show.service.LevelAlarmService; +import lombok.extern.slf4j.Slf4j; +import org.java_websocket.client.WebSocketClient; +import org.java_websocket.handshake.ServerHandshake; +import org.springblade.core.tool.utils.SpringUtil; +import org.springblade.core.tool.utils.StringUtil; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; +import java.net.Socket; +import java.net.URI; +import java.security.SecureRandom; +import java.security.cert.X509Certificate; +import java.util.Optional; + +/** + * @author ysj + */ +@Slf4j +public class ConditionAlarmWebSocket extends WebSocketClient { + + + private final ConditionAlarmService conditionService; + + /** + * 构造等级告警websocket + * @param uri + */ + public ConditionAlarmWebSocket(URI uri) { + super(uri); + conditionService = SpringUtil.getBean(ConditionAlarmService.class); + connection(this); + } + + // 链接到服务器回调接口 + @Override + public void onOpen(ServerHandshake handshakedata) { + log.error("systemAlarm websocket open"); + } + + // 接收到服务器消息回调接口 + @Override + public void onMessage(String message) { + if(StringUtil.isEmpty(message)){ + log.error("level alarm on message is null"); + }else{ + // 等级告警数据处理 + conditionService.receive(message); + } + } + + // 与服务器链接中断回调接口 + @Override + public void onClose(int code, String reason, boolean remote) { + log.error("level alarm websocket close"); + } + + // 与服务器通讯异常触发 + @Override + public void onError(Exception e) { + log.error("level alarm websocket error : {}",e.getMessage()); + } + + /** + * 建立链接 + * @param webSocket + */ + private void connection(ConditionAlarmWebSocket webSocket) { + SSLContext context = init(); + if(Optional.ofNullable(context).isPresent()){ + Socket socket = create(context); + if(Optional.ofNullable(socket).isPresent()){ + webSocket.setSocket(socket); + } + } + } + + /** + * 创建Socket + * @param context + * @return + */ + private Socket create(SSLContext context) { + Socket socket = null; + try{ + socket = context.getSocketFactory().createSocket(); + }catch (Exception e){ + log.error("level alarm socket create error : {}",e.getMessage()); + } + return socket; + } + + /** + * 协议初始化 + * @return SSLContext + */ + private SSLContext init() { + SSLContext SSL = null; + try{ + SSL = SSLContext.getInstance("TLS"); + SSL.init(null, new TrustManager[]{new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] chain, + String authType) { + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + }}, new SecureRandom()); + }catch (Exception e){ + log.error("level alarm SSL init error : {}",e.getMessage()); + } + return SSL; + } + +} \ No newline at end of file diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/hz3000/SystemAlarmRegular.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/hz3000/SystemAlarmRegular.java index 4534401..2c93157 100644 --- a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/hz3000/SystemAlarmRegular.java +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/hz3000/SystemAlarmRegular.java @@ -20,8 +20,8 @@ import java.net.URI; @EnableScheduling public class SystemAlarmRegular { - @Value("${hzims.system.alarm-url}") - private String system_alarm_url; + @Value("${hzims.system.wss-url}") + private String system_wss_url; private SystemAlarmWebSocket client; @@ -42,7 +42,7 @@ public class SystemAlarmRegular { // 创建websocket链接 private void createClient() { try{ - client = new SystemAlarmWebSocket(new URI(system_alarm_url)); + client = new SystemAlarmWebSocket(new URI(system_wss_url)); client.connectBlocking(); }catch (Exception e){ log.error("level create error : {}",e.getMessage());