|
@@ -1,11 +1,10 @@
|
|
|
package com.persagy.dmp.starter.alarm.communication.netty;
|
|
|
|
|
|
-import cn.hutool.core.collection.CollectionUtil;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.persagy.dmp.starter.alarm.communication.mq.DmpMessage;
|
|
|
import com.persagy.dmp.starter.alarm.service.NettyAlarmService;
|
|
|
-import com.persagy.zkt.utils.StringUtil;
|
|
|
+import com.persagy.dmp.starter.alarm.util.StringUtil;
|
|
|
import io.netty.channel.Channel;
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
@@ -13,6 +12,9 @@ import io.netty.channel.group.ChannelGroup;
|
|
|
import io.netty.channel.group.DefaultChannelGroup;
|
|
|
import io.netty.util.concurrent.GlobalEventExecutor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.net.SocketAddress;
|
|
|
import java.util.ArrayList;
|
|
@@ -31,22 +33,39 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
|
@Slf4j
|
|
|
public class NettyAlarmMsgBaseHandler extends ChannelInboundHandlerAdapter {
|
|
|
|
|
|
+ public static final String allProjects = "allProjects";
|
|
|
+
|
|
|
+ @Autowired
|
|
|
private NettyAlarmService nettyAlarmService;
|
|
|
/**
|
|
|
* 装每个客户端的地址及对应的管道
|
|
|
*/
|
|
|
public Map<String, Channel> socketChannelMap = new ConcurrentHashMap<>();
|
|
|
|
|
|
+ /**
|
|
|
+ * @description: 根据项目id获取对应的通信通道
|
|
|
+ * @param: projectId
|
|
|
+ * @return: io.netty.channel.Channel
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/12/3 3:03 下午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ private Channel getChannel(String projectId) {
|
|
|
+ // 项目上的消息只推送给一个边缘端来处理
|
|
|
+ Channel channel = socketChannelMap.get(projectId);
|
|
|
+ if (channel == null) {
|
|
|
+ channel = socketChannelMap.get(allProjects);
|
|
|
+ }
|
|
|
+ return channel;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 保留所有与服务器建立连接的channel对象
|
|
|
*/
|
|
|
public static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
|
|
|
|
|
|
- public NettyAlarmMsgBaseHandler(NettyAlarmService alarmService) {
|
|
|
- this.nettyAlarmService = alarmService;
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
|
|
|
SocketAddress socketAddress = ctx.channel().remoteAddress();
|
|
@@ -67,7 +86,15 @@ public class NettyAlarmMsgBaseHandler extends ChannelInboundHandlerAdapter {
|
|
|
*/
|
|
|
private void connected(NettyAlarmMessage nettyMessage, ChannelHandlerContext channelHandlerContext) {
|
|
|
String source = nettyMessage.getSource();
|
|
|
- socketChannelMap.put(source, channelHandlerContext.channel());
|
|
|
+ if (StringUtils.isEmpty(source)) {
|
|
|
+ socketChannelMap.put(allProjects, channelHandlerContext.channel());
|
|
|
+ } else {
|
|
|
+ String[] projectIds = source.split(",");
|
|
|
+ // 一个项目只能对应一个channel
|
|
|
+ for (String projectId : projectIds) {
|
|
|
+ socketChannelMap.put(projectId, channelHandlerContext.channel());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -80,7 +107,7 @@ public class NettyAlarmMsgBaseHandler extends ChannelInboundHandlerAdapter {
|
|
|
* @since: 2020/11/30 3:26 下午
|
|
|
* @version: V1.0
|
|
|
*/
|
|
|
- private void sendAllAlarmConfigs(NettyAlarmMessage nettyMessage) throws Exception {
|
|
|
+ public void sendAllAlarmConfigs(NettyAlarmMessage nettyMessage) throws Exception {
|
|
|
List<JSONObject> dataList = nettyMessage.getContent();
|
|
|
JSONObject data = dataList.get(0);
|
|
|
data.put("userId", "system");
|
|
@@ -103,7 +130,7 @@ public class NettyAlarmMsgBaseHandler extends ChannelInboundHandlerAdapter {
|
|
|
* @since: 2020/11/30 3:33 下午
|
|
|
* @version: V1.0
|
|
|
*/
|
|
|
- private void createAlarmRecordAndSendRecordId(NettyAlarmMessage nettyMessage) throws Exception {
|
|
|
+ public void createAlarmRecordAndSendRecordId(NettyAlarmMessage nettyMessage) throws Exception {
|
|
|
List<JSONObject> dataList = nettyMessage.getContent();
|
|
|
JSONObject data = dataList.get(0);
|
|
|
data.put("userId", "system");
|
|
@@ -129,7 +156,7 @@ public class NettyAlarmMsgBaseHandler extends ChannelInboundHandlerAdapter {
|
|
|
* @since: 2020/11/30 4:13 下午
|
|
|
* @version: V1.0
|
|
|
*/
|
|
|
- private void updateAlarmRecord(NettyAlarmMessage message) throws Exception {
|
|
|
+ public void updateAlarmRecord(NettyAlarmMessage message) throws Exception {
|
|
|
List<JSONObject> dataList = message.getContent();
|
|
|
JSONObject data = dataList.get(0);
|
|
|
data.put("userId", "system");
|
|
@@ -150,11 +177,11 @@ public class NettyAlarmMsgBaseHandler extends ChannelInboundHandlerAdapter {
|
|
|
Map<String, JSONArray> changedAlarmConfigs = nettyAlarmService.queryChangedAlarmConfigs(dmpMessage);
|
|
|
JSONArray createdConfigUniques = changedAlarmConfigs.get("createdConfigUniques");
|
|
|
JSONArray deletedConfigUniques = changedAlarmConfigs.get("deletedConfigUniques");
|
|
|
- if (CollectionUtil.isNotEmpty(deletedConfigUniques)) {
|
|
|
+ if (!CollectionUtils.isEmpty(deletedConfigUniques)) {
|
|
|
// 通过netty发送给边缘端 10-云端推送删除的报警定义给边缘端(增量删除报警定义)
|
|
|
sendMessage(dmpMessage.getProjectId(), new NettyAlarmMessage(10, deletedConfigUniques).toString());
|
|
|
}
|
|
|
- if (CollectionUtil.isNotEmpty(createdConfigUniques)) {
|
|
|
+ if (!CollectionUtils.isEmpty(createdConfigUniques)) {
|
|
|
// 通过netty发送给边缘端 7-云端推送修改的报警定义给边缘端(增量新增修改报警定义)
|
|
|
sendMessage(dmpMessage.getProjectId(), new NettyAlarmMessage(7, createdConfigUniques).toString());
|
|
|
}
|
|
@@ -175,7 +202,7 @@ public class NettyAlarmMsgBaseHandler extends ChannelInboundHandlerAdapter {
|
|
|
public void channelRead(ChannelHandlerContext channelHandlerContext, Object msg) throws Exception {
|
|
|
log.info("收到[" + channelHandlerContext.channel().remoteAddress() + "]消息:" + msg);
|
|
|
try {
|
|
|
- NettyAlarmMessage nettyMessage = StringUtil.tranferItemToDTO(msg.toString(), NettyAlarmMessage.class);
|
|
|
+ NettyAlarmMessage nettyMessage = StringUtil.transferItemToDTO(msg.toString(), NettyAlarmMessage.class);
|
|
|
/* 操作类型:1-请求、2 -响应、3-通知、
|
|
|
* 4-边缘端获取报警定义、
|
|
|
* 5-边缘端主动推送报警记录、
|
|
@@ -294,8 +321,9 @@ public class NettyAlarmMsgBaseHandler extends ChannelInboundHandlerAdapter {
|
|
|
* @Description: 服务端给某个客户端发送消息
|
|
|
*/
|
|
|
public void sendMessage(String projectId, String msg) {
|
|
|
- if (socketChannelMap.containsKey(projectId)) {
|
|
|
- socketChannelMap.get(projectId).writeAndFlush(msg);
|
|
|
+ Channel channel = getChannel(projectId);
|
|
|
+ if (channel != null) {
|
|
|
+ channel.writeAndFlush(msg);
|
|
|
} else {
|
|
|
log.info("...projectId[{}]未建立连接,无法发送!", projectId);
|
|
|
}
|