From a8863c45ac528b5c2c3a13114f85d819d99ed7a6 Mon Sep 17 00:00:00 2001 From: tyty Date: Thu, 14 Sep 2023 14:05:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=8E=E8=87=AA3000=E9=9B=86=E4=B8=AD?= =?UTF-8?q?=E7=9B=91=E6=8E=A7=E5=91=8A=E8=AD=A6=E4=BB=A3=E7=A0=81=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hzims-service/hzims-alarm/pom.xml | 4 + .../hzims/alarm/show/service/FdpAlarmService.java | 12 ++ .../show/service/impl/FdpAlarmServiceImpl.java | 126 +++++++++++++++ .../show/service/impl/MessageServiceImpl.java | 56 ++++++- .../show/service/impl/SystemAlarmServiceImpl.java | 169 +++++---------------- .../hnac/hzims/alarm/ws/fdp/FdpAlarmRegular.java | 51 +++++++ .../hnac/hzims/alarm/ws/fdp/FdpAlarmWebSocket.java | 125 +++++++++++++++ .../hzims/alarm/ws/hz3000/SystemAlarmRegular.java | 2 +- 8 files changed, 413 insertions(+), 132 deletions(-) create mode 100644 hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/FdpAlarmService.java create mode 100644 hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/FdpAlarmServiceImpl.java create mode 100644 hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/fdp/FdpAlarmRegular.java create mode 100644 hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/fdp/FdpAlarmWebSocket.java diff --git a/hzims-service/hzims-alarm/pom.xml b/hzims-service/hzims-alarm/pom.xml index 1143588..c5892ec 100644 --- a/hzims-service/hzims-alarm/pom.xml +++ b/hzims-service/hzims-alarm/pom.xml @@ -134,6 +134,10 @@ com.hnac.hzims message-api + + com.xuxueli + xxl-job-core + diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/FdpAlarmService.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/FdpAlarmService.java new file mode 100644 index 0000000..8015ac3 --- /dev/null +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/FdpAlarmService.java @@ -0,0 +1,12 @@ +package com.hnac.hzims.alarm.show.service; + +/** + * 告警处理接口 + * @author ysj + */ +public interface FdpAlarmService { + + String sendMessage(); + + void receiveMessage(String message); +} diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/FdpAlarmServiceImpl.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/FdpAlarmServiceImpl.java new file mode 100644 index 0000000..acba00a --- /dev/null +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/FdpAlarmServiceImpl.java @@ -0,0 +1,126 @@ +package com.hnac.hzims.alarm.show.service.impl; + +import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.hnac.hzims.alarm.entity.AlarmEntity; +import com.hnac.hzims.alarm.show.service.AlarmService; +import com.hnac.hzims.alarm.show.service.FdpAlarmService; +import com.hnac.hzims.alarm.show.service.MessageService; +import com.hnac.hzims.alarm.vo.FdpAlarmVo; +import com.hnac.hzims.message.fegin.IMessageClient; +import com.hnac.hzims.operational.station.entity.StationEntity; +import com.hnac.hzims.operational.station.feign.IStationClient; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springblade.core.log.exception.ServiceException; +import org.springblade.core.tool.api.R; +import org.springblade.core.tool.utils.CollectionUtil; +import org.springblade.core.tool.utils.DateUtil; +import org.springblade.system.feign.ISysClient; +import org.springblade.system.user.feign.IUserClient; +import org.springframework.stereotype.Service; + +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; + +import static com.hnac.hzims.alarm.constants.AlarmConstants.EARLY; +import static com.hnac.hzims.alarm.constants.AlarmConstants.EARLY_WARNING; + +/** + * 等级告警实现类 + * @author ysj + */ +@Slf4j +@Service +@RequiredArgsConstructor +public class FdpAlarmServiceImpl implements FdpAlarmService { + + + private final MessageService messageService; + private final IUserClient userClient; + private final ISysClient sysClient; + + private final IMessageClient messageClient; + private final AlarmService alarmService; + private final IStationClient stationClient; + /** + * 定时发送消息内容 + * @return String + */ + @Override + public String sendMessage() { + R> listAll = stationClient.getListAll(); + if (!listAll.isSuccess()||CollectionUtil.isEmpty(listAll.getData())){ + throw new ServiceException("FdpAlarm send message is null"); + } + List stations = listAll.getData(); + Map map = new ConcurrentHashMap<>(); + map.put("stations",stations.stream().map(StationEntity::getCode).collect(Collectors.joining(","))); + return JSONObject.toJSONString(map); + } + + + /** + * 接收服务推送消息 + * @param message + */ + @Override + public void receiveMessage(String message) { + // 对象转换 + List alarms = JSONObject.parseArray(message, FdpAlarmVo.class); + if(CollectionUtil.isEmpty(alarms)){ + return; + } + + R> listAll = stationClient.getListAll(); + if (!listAll.isSuccess()||CollectionUtil.isEmpty(listAll.getData())){ + throw new ServiceException("FdpAlarm send message is null"); + } + List stations = listAll.getData(); + // 查询当天已经记录的告警 + List historys = alarmService.list(Wrappers.lambdaQuery(). + ge(AlarmEntity::getCreateTime, DateUtil.format(new Date(), DateUtil.PATTERN_DATE) + " 00:00:00") + .eq(AlarmEntity::getAlarmSource, EARLY_WARNING)); + + // 数据过滤 + List entitys = alarms.stream() + .filter(alarm -> CollectionUtil.isNotEmpty(historys) || !historys.stream().map(AlarmEntity::getAlarmId).collect(Collectors.toList()).contains(alarm.getFaultId())) + .map(item->{ + //转换对象 + AlarmEntity entity = getAlarmEntity(item); + // 短信 + CompletableFuture.runAsync(() -> messageService.message(entity)); + // web/app消息推送 + CompletableFuture.runAsync(() -> messageService.webAppMessage(entity)); + // 微信公众号推送 + CompletableFuture.runAsync(() -> messageService.weChatMessage(entity)); + return entity; + }).collect(Collectors.toList()); + if(CollectionUtil.isEmpty(entitys)){ + return; + } + // 批量保存 + alarmService.saveBatch(entitys); + } + + private AlarmEntity getAlarmEntity(FdpAlarmVo item) { + AlarmEntity entity = new AlarmEntity(); + entity.setAlarmId(item.getFaultId()); + entity.setAlarmTime(item.getCreateTime()); + entity.setAlarmContext(item.getFinfo()); + entity.setAlarmType(EARLY); + entity.setAlarmStatus(Integer.valueOf(String.valueOf(item.getStatus()))); + entity.setStationId(item.getStation()); + entity.setAlarmStatus(EARLY_WARNING); + R stationByCode = stationClient.getStationByCode(item.getStation()); + if (stationByCode.isSuccess()&& ObjectUtils.isNotEmpty(stationByCode.getData())){ + entity.setCreateDept(stationByCode.getData().getCreateDept()); + } + return entity; + } +} diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/MessageServiceImpl.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/MessageServiceImpl.java index 02440d8..1ab0bae 100644 --- a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/MessageServiceImpl.java +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/MessageServiceImpl.java @@ -1,11 +1,25 @@ package com.hnac.hzims.alarm.show.service.impl; +import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.hnac.hzims.alarm.entity.AlarmEntity; import com.hnac.hzims.alarm.show.service.MessageService; +import com.hnac.hzims.message.MessageConstants; +import com.hnac.hzims.message.dto.MessagePushRecordDto; +import com.hnac.hzims.message.fegin.IMessageClient; +import com.hnac.hzims.operational.station.entity.StationEntity; +import com.hnac.hzims.operational.station.feign.IStationClient; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springblade.core.tool.api.R; +import org.springblade.core.tool.utils.CollectionUtil; +import org.springblade.system.feign.ISysClient; +import org.springblade.system.user.entity.User; +import org.springblade.system.user.feign.IUserClient; import org.springframework.stereotype.Service; +import java.util.Arrays; +import java.util.List; + /** * 消息推送实现类 * @author ysj @@ -14,7 +28,10 @@ import org.springframework.stereotype.Service; @Service @RequiredArgsConstructor public class MessageServiceImpl implements MessageService { - + private final ISysClient sysClient; + private final IStationClient stationClient; + private final IUserClient userClient; + private final IMessageClient messageClient; /** * 短信推送 * @param entity : 告警对象 @@ -30,6 +47,43 @@ public class MessageServiceImpl implements MessageService { */ @Override public void webAppMessage(AlarmEntity entity) { + R stationByCode = stationClient.getStationByCode(entity.getStationId()); + if (!stationByCode.isSuccess() && ObjectUtils.isEmpty(stationByCode.getData())) { + log.error("消息发送失败:站点code获取失败",entity); + return; + } + Long dept = stationByCode.getData().getCreateDept(); + // 获取站点用户 + R> result = userClient.userListByDeptId(dept); + if (!result.isSuccess() || CollectionUtil.isEmpty(result.getData())) { + log.error("消息发送失败:用户获取失败",entity); + return; + } + MessagePushRecordDto message = new MessagePushRecordDto(); + message.setBusinessClassify("warning"); + message.setBusinessKey(MessageConstants.BusinessClassifyEnum.WARNING.getKey()); + message.setSubject(MessageConstants.BusinessClassifyEnum.WARNING.getDescription()); + message.setTaskId(entity.getId()); + message.setTenantId("200000"); + message.setContent(entity.getAlarmContext()); + message.setTypes(Arrays.asList(MessageConstants.APP_PUSH, MessageConstants.WS_PUSH)); + message.setPushType(MessageConstants.IMMEDIATELY); + message.setDeptId(dept); + message.setCreateDept(dept); + R deptName = sysClient.getDeptName(dept); + if (deptName.isSuccess()) { + message.setDeptName(deptName.getData()); + } + message.setCreateDept(dept); + result.getData().forEach(user -> { + message.setPusher(String.valueOf(user.getId())); + message.setPusherName(user.getName()); + message.setAccount(String.valueOf(user.getId())); + message.setCreateUser(user.getId()); + messageClient.sendMessage(message); + }); + + } diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/SystemAlarmServiceImpl.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/SystemAlarmServiceImpl.java index 3cb379c..d03e051 100644 --- a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/SystemAlarmServiceImpl.java +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/SystemAlarmServiceImpl.java @@ -3,13 +3,12 @@ package com.hnac.hzims.alarm.show.service.impl; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.hnac.hzims.alarm.constants.AlarmConstants; import com.hnac.hzims.alarm.entity.AlarmEntity; -import com.hnac.hzims.alarm.entity.SystemAlarmEntity; import com.hnac.hzims.alarm.show.service.AlarmService; +import com.hnac.hzims.alarm.show.service.MessageService; import com.hnac.hzims.alarm.show.service.SystemAlarmService; -import com.hnac.hzims.equipment.feign.IEmInfoClient; -import com.hnac.hzims.message.MessageConstants; -import com.hnac.hzims.message.dto.MessagePushRecordDto; +import com.hnac.hzims.alarm.vo.SystemAlarmVo; import com.hnac.hzims.message.fegin.IMessageClient; import com.hnac.hzims.operational.alert.constants.AbnormalAlarmConstant; import com.hnac.hzims.operational.station.entity.StationEntity; @@ -20,49 +19,50 @@ import org.springblade.core.log.exception.ServiceException; import org.springblade.core.tool.api.R; import org.springblade.core.tool.utils.CollectionUtil; import org.springblade.core.tool.utils.DateUtil; -import org.springblade.core.tool.utils.StringUtil; import org.springblade.system.feign.ISysClient; -import org.springblade.system.user.entity.User; import org.springblade.system.user.feign.IUserClient; import org.springframework.stereotype.Service; -import java.util.*; +import java.util.Date; +import java.util.List; +import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; /** - * 等级告警实现类 - * @author ysj + * 华自3000告警实现类 + * + * @author ty */ @Slf4j @Service @RequiredArgsConstructor public class SystemAlarmServiceImpl implements SystemAlarmService { - - - private final IEmInfoClient deviceClient; + private final MessageService messageService; private final IUserClient userClient; private final ISysClient sysClient; private final IMessageClient messageClient; private final AlarmService alarmService; - private final IStationClient stationClient; + private final IStationClient stationClient; + /** * 定时发送消息内容 + * * @return String */ @Override public String sendMessage() { R> listAll = stationClient.getListAll(); - if (!listAll.isSuccess()||CollectionUtil.isEmpty(listAll.getData())){ - throw new ServiceException("level send message is null"); + if (!listAll.isSuccess() || CollectionUtil.isEmpty(listAll.getData())) { + throw new ServiceException("systemAlarm send message is null"); } List stations = listAll.getData(); - Map map = new ConcurrentHashMap<>(); - map.put("stations",stations.stream().map(StationEntity::getCode).collect(Collectors.joining(","))); + Map map = new ConcurrentHashMap<>(); + map.put("stations", stations.stream().map(StationEntity::getCode).collect(Collectors.joining(","))); map.put("soe_type", AbnormalAlarmConstant.SYSTEM_TYPE_LIST); return JSONObject.toJSONString(map); } @@ -75,38 +75,43 @@ public class SystemAlarmServiceImpl implements SystemAlarmService { @Override public void receiveMessage(String message) { // 对象转换 - List alarms = JSONObject.parseArray(message, SystemAlarmEntity.class); - if(CollectionUtil.isEmpty(alarms)){ + List alarms = JSONObject.parseArray(message, SystemAlarmVo.class); + if (CollectionUtil.isEmpty(alarms)) { return; } R> listAll = stationClient.getListAll(); - if (!listAll.isSuccess()||CollectionUtil.isEmpty(listAll.getData())){ - throw new ServiceException("level send message is null"); + if (!listAll.isSuccess() || CollectionUtil.isEmpty(listAll.getData())) { + throw new ServiceException("systemAlarm send message is null"); } List stations = listAll.getData(); // 查询当天已经记录的告警 List historys = alarmService.list(Wrappers.lambdaQuery(). - ge(AlarmEntity::getCreateTime, DateUtil.format(new Date(), DateUtil.PATTERN_DATE) + " 00:00:00")); - + ge(AlarmEntity::getCreateTime, DateUtil.format(new Date(), DateUtil.PATTERN_DATE) + " 00:00:00") + .eq(AlarmEntity::getAlarmSource, AlarmConstants.HZ3000_ALARM)); // 数据过滤 List entitys = alarms.stream() .filter(alarm -> CollectionUtil.isNotEmpty(historys) || !historys.stream().map(AlarmEntity::getAlarmId).collect(Collectors.toList()).contains(alarm.getId())) - .map(item->{ - AlarmEntity entity = getAlarmEntity(item); - return entity; - }).collect(Collectors.toList()); - if(CollectionUtil.isEmpty(entitys)){ + .map(item -> { + AlarmEntity entity = getAlarmEntity(item); + if (ObjectUtils.isNotEmpty(entity)){ + // 短信 + CompletableFuture.runAsync(() -> messageService.message(entity)); + // web/app消息推送 + CompletableFuture.runAsync(() -> messageService.webAppMessage(entity)); + // 微信公众号推送 + CompletableFuture.runAsync(() -> messageService.weChatMessage(entity)); + } + return entity; + }).collect(Collectors.toList()); + if (CollectionUtil.isEmpty(entitys)) { return; } // 批量保存 alarmService.saveBatch(entitys); - - // 消息推送 - CompletableFuture.runAsync(()->this.sendAlarmMessage(entitys,stations.stream().filter(station -> entitys.stream().filter(stationId -> !StringUtil.isEmpty(stationId)).map(AlarmEntity::getStationId).collect(Collectors.toList()).contains(station.getCode())).collect(Collectors.toList()))); } - private AlarmEntity getAlarmEntity(SystemAlarmEntity item) { + private AlarmEntity getAlarmEntity(SystemAlarmVo item) { AlarmEntity entity = new AlarmEntity(); entity.setAlarmId(item.getId()); entity.setAlarmTime(DateUtil.parse(item.getTs(), "yyyy-MM-dd HH:mm:ss.s")); @@ -115,107 +120,11 @@ public class SystemAlarmServiceImpl implements SystemAlarmService { entity.setAlarmStatus(item.getSoeStatus()); entity.setAlarmValue(item.getOptionvals()); entity.setStationId(item.getStation()); + entity.setAlarmSource(AlarmConstants.HZ3000_ALARM); R stationByCode = stationClient.getStationByCode(item.getStation()); - if (stationByCode.isSuccess()&& ObjectUtils.isNotEmpty(stationByCode.getData())){ + if (stationByCode.isSuccess() && ObjectUtils.isNotEmpty(stationByCode.getData())) { entity.setCreateDept(stationByCode.getData().getCreateDept()); } return entity; } - - - /** - * 告警消息推送 - * @param entitys - */ - private void sendAlarmMessage(List entitys,List stations) { - if(CollectionUtil.isEmpty(entitys)){ - return; - } -// // 告警等级 :一级、二级告警发送通知 -// List alarms = entitys.stream().filter(entity -> AbnormalAlarmConstant.LEVEL_LIST.contains(entity.getAlarmLevel())).collect(Collectors.toList()); -// if(CollectionUtil.isEmpty(alarms)){ -// return; -// } - // 查询站点用户 - entitys.forEach(entity->{ - if(StringUtil.isEmpty(entity.getStationId())){ - return; - } - List depts = stations.stream().filter(station -> station.getCode().equals(entity.getStationId())).map(StationEntity::getRefDept).collect(Collectors.toList()); - if(CollectionUtil.isEmpty(depts)){ - return; - } - // app,web消息推送 - this.sendMessageByWebApp(depts.get(0),entity); - // 短信推送 - //this.message(depts.get(0),entity); - }); - } - - private void sendMessageByWebApp(Long dept,AlarmEntity entity) { - // 获取站点用户 - R> result = userClient.userListByDeptId(dept); - if(!result.isSuccess() || CollectionUtil.isEmpty(result.getData())){ - return; - } - MessagePushRecordDto message = new MessagePushRecordDto(); - message.setBusinessClassify("warning"); - message.setBusinessKey(MessageConstants.BusinessClassifyEnum.WARNING.getKey()); - message.setSubject(MessageConstants.BusinessClassifyEnum.WARNING.getDescription()); - message.setTaskId(entity.getId()); - message.setTenantId("200000"); - message.setContent(entity.getAlarmContext()); - message.setTypes(Arrays.asList(MessageConstants.APP_PUSH, MessageConstants.WS_PUSH)); - message.setPushType(MessageConstants.IMMEDIATELY); - message.setDeptId(dept); - message.setCreateDept(dept); - R deptName = sysClient.getDeptName(dept); - if (deptName.isSuccess()) { - message.setDeptName(deptName.getData()); - } - message.setCreateDept(dept); - result.getData().forEach(user->{ - message.setPusher(String.valueOf(user.getId())); - message.setPusherName(user.getName()); - message.setAccount(String.valueOf(user.getId())); - message.setCreateUser(user.getId()); - messageClient.sendMessage(message); - }); - } - - /** - * 发送短信 - * @param dept - * @param entity - */ - private void message(Long dept,AlarmEntity entity) { - // 获取站点用户 - R> result = userClient.relationUserListByRoleAlias("200000",dept,"projectManager"); - if(!result.isSuccess() || CollectionUtil.isEmpty(result.getData())){ - return; - } - MessagePushRecordDto message = new MessagePushRecordDto(); - message.setBusinessClassify("warning"); - message.setBusinessKey(MessageConstants.BusinessClassifyEnum.WARNING.getKey()); - message.setSubject(MessageConstants.BusinessClassifyEnum.WARNING.getDescription()); - message.setTaskId(entity.getId()); - message.setTenantId("200000"); - message.setContent(entity.getAlarmContext()); - message.setTypes(Collections.singletonList(MessageConstants.SMS_PUSH)); - message.setPushType(MessageConstants.IMMEDIATELY); - message.setDeptId(dept); - R deptName = sysClient.getDeptName(dept); - if (deptName.isSuccess()) { - message.setDeptName(deptName.getData()); - } - User admin = userClient.userByAccount("200000", "admin").getData(); - message.setCreateDept(admin.getCreateDept()); - message.setCreateUser(admin.getId()); - result.getData().forEach(user->{ - message.setPusher(String.valueOf(user.getId())); - message.setPusherName(user.getName()); - message.setAccount(String.valueOf(user.getId())); - messageClient.sendMessage(message); - }); - } } diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/fdp/FdpAlarmRegular.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/fdp/FdpAlarmRegular.java new file mode 100644 index 0000000..cff203f --- /dev/null +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/fdp/FdpAlarmRegular.java @@ -0,0 +1,51 @@ +package com.hnac.hzims.alarm.ws.fdp; + +import com.hnac.hzims.alarm.show.service.FdpAlarmService; +import lombok.extern.slf4j.Slf4j; +import org.springblade.core.tool.utils.ObjectUtil; +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 ty + */ +@Slf4j +@Component +@EnableScheduling +public class FdpAlarmRegular { + + @Value("${hzims.fdp.alarm-url}") + private String fdp_alarm_url; + + private FdpAlarmWebSocket client; + + @Autowired + private FdpAlarmService fdpAlarmService; + + // 定时发送消息 + @Scheduled(cron = "0 0/30 * * * ?") + private void regular(){ + // 检查链接存活状态 + if(ObjectUtil.isNotEmpty(client) && client.isOpen()){ + client.send(fdpAlarmService.sendMessage()); + }else { + this.createClient(); + } + } + + // 创建websocket链接 + private void createClient() { + try{ + client = new FdpAlarmWebSocket(new URI(fdp_alarm_url)); + client.connectBlocking(); + }catch (Exception e){ + log.error("FdpAlarm 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/fdp/FdpAlarmWebSocket.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/fdp/FdpAlarmWebSocket.java new file mode 100644 index 0000000..319a3b5 --- /dev/null +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/fdp/FdpAlarmWebSocket.java @@ -0,0 +1,125 @@ +package com.hnac.hzims.alarm.ws.fdp; + +import com.hnac.hzims.alarm.show.service.SystemAlarmService; +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 ty + */ +@Slf4j +public class FdpAlarmWebSocket extends WebSocketClient { + + private final SystemAlarmService systemAlarmService; + + /** + * 构造等级告警websocket + * @param uri + */ + public FdpAlarmWebSocket(URI uri) { + super(uri); + systemAlarmService = SpringUtil.getBean(SystemAlarmService.class); + connection(this); + } + + // 链接到服务器回调接口 + @Override + public void onOpen(ServerHandshake handshakedata) { + log.error("FdpAlarm websocket open"); + } + + // 接收到服务器消息回调接口 + @Override + public void onMessage(String message) { + if(StringUtil.isEmpty(message)){ + log.error("FdpAlarm on message is null"); + }else{ + // 等级告警数据处理 + systemAlarmService.receiveMessage(message); + } + } + + // 与服务器链接中断回调接口 + @Override + public void onClose(int code, String reason, boolean remote) { + log.error("FdpAlarm websocket close"); + } + + // 与服务器通讯异常触发 + @Override + public void onError(Exception e) { + log.error("FdpAlarm websocket error : {}",e.getMessage()); + } + + /** + * 建立链接 + * @param webSocket + */ + private void connection(FdpAlarmWebSocket 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("FdpAlarm 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("FdpAlarm 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..1333e59 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 @@ -45,7 +45,7 @@ public class SystemAlarmRegular { client = new SystemAlarmWebSocket(new URI(system_alarm_url)); client.connectBlocking(); }catch (Exception e){ - log.error("level create error : {}",e.getMessage()); + log.error("systemAlarm create error : {}",e.getMessage()); } } } \ No newline at end of file