Browse Source

华自3000集中监控告警代码提交

zhongwei
tyty 1 year ago
parent
commit
a8863c45ac
  1. 4
      hzims-service/hzims-alarm/pom.xml
  2. 12
      hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/FdpAlarmService.java
  3. 126
      hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/FdpAlarmServiceImpl.java
  4. 56
      hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/MessageServiceImpl.java
  5. 145
      hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/show/service/impl/SystemAlarmServiceImpl.java
  6. 51
      hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/fdp/FdpAlarmRegular.java
  7. 125
      hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/fdp/FdpAlarmWebSocket.java
  8. 2
      hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/hz3000/SystemAlarmRegular.java

4
hzims-service/hzims-alarm/pom.xml

@ -134,6 +134,10 @@
<groupId>com.hnac.hzims</groupId> <groupId>com.hnac.hzims</groupId>
<artifactId>message-api</artifactId> <artifactId>message-api</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

12
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);
}

126
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<List<StationEntity>> listAll = stationClient.getListAll();
if (!listAll.isSuccess()||CollectionUtil.isEmpty(listAll.getData())){
throw new ServiceException("FdpAlarm send message is null");
}
List<StationEntity> stations = listAll.getData();
Map<String,String> 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<FdpAlarmVo> alarms = JSONObject.parseArray(message, FdpAlarmVo.class);
if(CollectionUtil.isEmpty(alarms)){
return;
}
R<List<StationEntity>> listAll = stationClient.getListAll();
if (!listAll.isSuccess()||CollectionUtil.isEmpty(listAll.getData())){
throw new ServiceException("FdpAlarm send message is null");
}
List<StationEntity> stations = listAll.getData();
// 查询当天已经记录的告警
List<AlarmEntity> historys = alarmService.list(Wrappers.<AlarmEntity>lambdaQuery().
ge(AlarmEntity::getCreateTime, DateUtil.format(new Date(), DateUtil.PATTERN_DATE) + " 00:00:00")
.eq(AlarmEntity::getAlarmSource, EARLY_WARNING));
// 数据过滤
List<AlarmEntity> 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<StationEntity> stationByCode = stationClient.getStationByCode(item.getStation());
if (stationByCode.isSuccess()&& ObjectUtils.isNotEmpty(stationByCode.getData())){
entity.setCreateDept(stationByCode.getData().getCreateDept());
}
return entity;
}
}

56
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; 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.entity.AlarmEntity;
import com.hnac.hzims.alarm.show.service.MessageService; 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.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; 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 org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
/** /**
* 消息推送实现类 * 消息推送实现类
* @author ysj * @author ysj
@ -14,7 +28,10 @@ import org.springframework.stereotype.Service;
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
public class MessageServiceImpl implements MessageService { public class MessageServiceImpl implements MessageService {
private final ISysClient sysClient;
private final IStationClient stationClient;
private final IUserClient userClient;
private final IMessageClient messageClient;
/** /**
* 短信推送 * 短信推送
* @param entity : 告警对象 * @param entity : 告警对象
@ -30,6 +47,43 @@ public class MessageServiceImpl implements MessageService {
*/ */
@Override @Override
public void webAppMessage(AlarmEntity entity) { public void webAppMessage(AlarmEntity entity) {
R<StationEntity> stationByCode = stationClient.getStationByCode(entity.getStationId());
if (!stationByCode.isSuccess() && ObjectUtils.isEmpty(stationByCode.getData())) {
log.error("消息发送失败:站点code获取失败",entity);
return;
}
Long dept = stationByCode.getData().getCreateDept();
// 获取站点用户
R<List<User>> 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<String> 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);
});
} }

145
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.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; 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.AlarmEntity;
import com.hnac.hzims.alarm.entity.SystemAlarmEntity;
import com.hnac.hzims.alarm.show.service.AlarmService; 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.alarm.show.service.SystemAlarmService;
import com.hnac.hzims.equipment.feign.IEmInfoClient; import com.hnac.hzims.alarm.vo.SystemAlarmVo;
import com.hnac.hzims.message.MessageConstants;
import com.hnac.hzims.message.dto.MessagePushRecordDto;
import com.hnac.hzims.message.fegin.IMessageClient; import com.hnac.hzims.message.fegin.IMessageClient;
import com.hnac.hzims.operational.alert.constants.AbnormalAlarmConstant; import com.hnac.hzims.operational.alert.constants.AbnormalAlarmConstant;
import com.hnac.hzims.operational.station.entity.StationEntity; import com.hnac.hzims.operational.station.entity.StationEntity;
@ -20,29 +19,28 @@ import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R; import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.CollectionUtil; import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.DateUtil; import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.system.feign.ISysClient; import org.springblade.system.feign.ISysClient;
import org.springblade.system.user.entity.User;
import org.springblade.system.user.feign.IUserClient; import org.springblade.system.user.feign.IUserClient;
import org.springframework.stereotype.Service; 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.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
* 等级告警实现类 * 华自3000告警实现类
* @author ysj *
* @author ty
*/ */
@Slf4j @Slf4j
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
public class SystemAlarmServiceImpl implements SystemAlarmService { public class SystemAlarmServiceImpl implements SystemAlarmService {
private final MessageService messageService;
private final IEmInfoClient deviceClient;
private final IUserClient userClient; private final IUserClient userClient;
private final ISysClient sysClient; private final ISysClient sysClient;
@ -50,15 +48,17 @@ public class SystemAlarmServiceImpl implements SystemAlarmService {
private final IMessageClient messageClient; private final IMessageClient messageClient;
private final AlarmService alarmService; private final AlarmService alarmService;
private final IStationClient stationClient; private final IStationClient stationClient;
/** /**
* 定时发送消息内容 * 定时发送消息内容
*
* @return String * @return String
*/ */
@Override @Override
public String sendMessage() { public String sendMessage() {
R<List<StationEntity>> listAll = stationClient.getListAll(); R<List<StationEntity>> listAll = stationClient.getListAll();
if (!listAll.isSuccess() || CollectionUtil.isEmpty(listAll.getData())) { if (!listAll.isSuccess() || CollectionUtil.isEmpty(listAll.getData())) {
throw new ServiceException("level send message is null"); throw new ServiceException("systemAlarm send message is null");
} }
List<StationEntity> stations = listAll.getData(); List<StationEntity> stations = listAll.getData();
Map<String, String> map = new ConcurrentHashMap<>(); Map<String, String> map = new ConcurrentHashMap<>();
@ -75,25 +75,33 @@ public class SystemAlarmServiceImpl implements SystemAlarmService {
@Override @Override
public void receiveMessage(String message) { public void receiveMessage(String message) {
// 对象转换 // 对象转换
List<SystemAlarmEntity> alarms = JSONObject.parseArray(message, SystemAlarmEntity.class); List<SystemAlarmVo> alarms = JSONObject.parseArray(message, SystemAlarmVo.class);
if (CollectionUtil.isEmpty(alarms)) { if (CollectionUtil.isEmpty(alarms)) {
return; return;
} }
R<List<StationEntity>> listAll = stationClient.getListAll(); R<List<StationEntity>> listAll = stationClient.getListAll();
if (!listAll.isSuccess() || CollectionUtil.isEmpty(listAll.getData())) { if (!listAll.isSuccess() || CollectionUtil.isEmpty(listAll.getData())) {
throw new ServiceException("level send message is null"); throw new ServiceException("systemAlarm send message is null");
} }
List<StationEntity> stations = listAll.getData(); List<StationEntity> stations = listAll.getData();
// 查询当天已经记录的告警 // 查询当天已经记录的告警
List<AlarmEntity> historys = alarmService.list(Wrappers.<AlarmEntity>lambdaQuery(). List<AlarmEntity> historys = alarmService.list(Wrappers.<AlarmEntity>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<AlarmEntity> entitys = alarms.stream() List<AlarmEntity> entitys = alarms.stream()
.filter(alarm -> CollectionUtil.isNotEmpty(historys) || !historys.stream().map(AlarmEntity::getAlarmId).collect(Collectors.toList()).contains(alarm.getId())) .filter(alarm -> CollectionUtil.isNotEmpty(historys) || !historys.stream().map(AlarmEntity::getAlarmId).collect(Collectors.toList()).contains(alarm.getId()))
.map(item -> { .map(item -> {
AlarmEntity entity = getAlarmEntity(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; return entity;
}).collect(Collectors.toList()); }).collect(Collectors.toList());
if (CollectionUtil.isEmpty(entitys)) { if (CollectionUtil.isEmpty(entitys)) {
@ -101,12 +109,9 @@ public class SystemAlarmServiceImpl implements SystemAlarmService {
} }
// 批量保存 // 批量保存
alarmService.saveBatch(entitys); 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(); AlarmEntity entity = new AlarmEntity();
entity.setAlarmId(item.getId()); entity.setAlarmId(item.getId());
entity.setAlarmTime(DateUtil.parse(item.getTs(), "yyyy-MM-dd HH:mm:ss.s")); 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.setAlarmStatus(item.getSoeStatus());
entity.setAlarmValue(item.getOptionvals()); entity.setAlarmValue(item.getOptionvals());
entity.setStationId(item.getStation()); entity.setStationId(item.getStation());
entity.setAlarmSource(AlarmConstants.HZ3000_ALARM);
R<StationEntity> stationByCode = stationClient.getStationByCode(item.getStation()); R<StationEntity> stationByCode = stationClient.getStationByCode(item.getStation());
if (stationByCode.isSuccess() && ObjectUtils.isNotEmpty(stationByCode.getData())) { if (stationByCode.isSuccess() && ObjectUtils.isNotEmpty(stationByCode.getData())) {
entity.setCreateDept(stationByCode.getData().getCreateDept()); entity.setCreateDept(stationByCode.getData().getCreateDept());
} }
return entity; return entity;
} }
/**
* 告警消息推送
* @param entitys
*/
private void sendAlarmMessage(List<AlarmEntity> entitys,List<StationEntity> stations) {
if(CollectionUtil.isEmpty(entitys)){
return;
}
// // 告警等级 :一级、二级告警发送通知
// List<HistoryLevelAlarmEntity> 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<Long> 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<List<User>> 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<String> 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<List<User>> 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<String> 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);
});
}
} }

51
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());
}
}
}

125
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;
}
}

2
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 = new SystemAlarmWebSocket(new URI(system_alarm_url));
client.connectBlocking(); client.connectBlocking();
}catch (Exception e){ }catch (Exception e){
log.error("level create error : {}",e.getMessage()); log.error("systemAlarm create error : {}",e.getMessage());
} }
} }
} }
Loading…
Cancel
Save