|
@@ -1,220 +1,220 @@
|
|
|
-package com.persagy.client;
|
|
|
-
|
|
|
-import cn.hutool.core.collection.CollectionUtil;
|
|
|
-import cn.hutool.core.date.DateUtil;
|
|
|
-import cn.hutool.core.date.TimeInterval;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.alibaba.fastjson.TypeReference;
|
|
|
-import com.persagy.cache.AlarmInfoCache;
|
|
|
-import com.persagy.entity.AlarmDefine;
|
|
|
-import com.persagy.entity.AlarmState;
|
|
|
-import com.persagy.entity.NettyMessage;
|
|
|
-import com.persagy.entity.ZktAlarmRecordDO;
|
|
|
-import com.persagy.job.NettyMessageQueue;
|
|
|
-import com.persagy.repository.AlarmRecordRepository;
|
|
|
-import com.persagy.service.AlarmDefineService;
|
|
|
-import com.persagy.utils.LockUtil;
|
|
|
-import com.persagy.utils.StringUtil;
|
|
|
-import io.netty.channel.ChannelHandlerContext;
|
|
|
-import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
|
-import io.netty.handler.timeout.IdleState;
|
|
|
-import io.netty.handler.timeout.IdleStateEvent;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-
|
|
|
-
|
|
|
- * @author lqshi
|
|
|
- * @ClassName: EchoClientHandler
|
|
|
- * @Description: 客户端处理类(处理云端数据)
|
|
|
- * @date 2020年8月29日 下午8:31:59
|
|
|
- */
|
|
|
-
|
|
|
-@Slf4j
|
|
|
-public class GroupNettyClientHandler extends ChannelInboundHandlerAdapter {
|
|
|
-
|
|
|
- static final int RECONNECT_DELAY = Integer.parseInt(System.getProperty("reconnectDelay", "5"));
|
|
|
-
|
|
|
- private static final int READ_TIMEOUT = Integer.parseInt(System.getProperty("readTimeout", "10"));
|
|
|
-
|
|
|
- private AlarmDefineService alarmDefineService;
|
|
|
- private GroupNettyClient groupNettyClient;
|
|
|
- private AlarmRecordRepository alarmRecordRepository;
|
|
|
-
|
|
|
-
|
|
|
- public GroupNettyClientHandler(GroupNettyClient groupNettyClient, AlarmDefineService alarmDefineService, AlarmRecordRepository alarmRecordRepository) {
|
|
|
- this.alarmDefineService = alarmDefineService;
|
|
|
- this.groupNettyClient = groupNettyClient;
|
|
|
- this.alarmRecordRepository = alarmRecordRepository;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 在到服务器的连接已经建立之后将被调用
|
|
|
- *
|
|
|
- * @param ctx
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- @Override
|
|
|
- public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
|
|
- log.info("Connected to: " + ctx.channel().remoteAddress());
|
|
|
- ctx.channel().writeAndFlush(new NettyMessage<>(200, groupNettyClient.getProjectId(),groupNettyClient.getGroupCode()).toString());
|
|
|
-
|
|
|
-
|
|
|
- NettyMessage nettyMessage = new NettyMessage(4, groupNettyClient.getProjectId(),groupNettyClient.getGroupCode());
|
|
|
- nettyMessage.setRemark("连接已经建立;");
|
|
|
- JSONObject content = new JSONObject();
|
|
|
- content.put("groupCode", groupNettyClient.getGroupCode());
|
|
|
- content.put("projectId", groupNettyClient.getProjectId());
|
|
|
- nettyMessage.setContent(Arrays.asList(content));
|
|
|
- log.info(nettyMessage.toString());
|
|
|
- ctx.channel().writeAndFlush(nettyMessage.toString());
|
|
|
- iniAlarmResult(ctx);
|
|
|
- }
|
|
|
-
|
|
|
- private void iniAlarmResult(ChannelHandlerContext ctx) {
|
|
|
- try {
|
|
|
- log.info("--initAlarmResult--");
|
|
|
- TimeInterval timer = DateUtil.timer();
|
|
|
- while (NettyMessageQueue.getNettyMessageQueue().size() > 0 && timer.interval() < 10000) {
|
|
|
- String msg = NettyMessageQueue.getNettyMessageQueue().consume();
|
|
|
- log.info("剩余报警消息令总数:{}", NettyMessageQueue.getNettyMessageQueue().size());
|
|
|
- ctx.writeAndFlush(msg);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("发送报警消息失败", e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 当从服务器接收到一个消息时被调用
|
|
|
- *
|
|
|
- * @param ctx
|
|
|
- * @param msg
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- @Override
|
|
|
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
|
|
- log.info("Client received: {}", msg);
|
|
|
- try {
|
|
|
- TimeInterval timer = DateUtil.timer();
|
|
|
- handlerMsg(ctx, msg);
|
|
|
- log.info("处理消息时间[{}]", timer.interval());
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("channelRead", e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void handlerMsg(ChannelHandlerContext channelHandlerContext, Object msg) throws Exception {
|
|
|
- if (StringUtil.isJSONObject((String) msg)) {
|
|
|
- NettyMessage message = StringUtil.tranferItemToDTO((String) msg, NettyMessage.class);
|
|
|
- if (message.getOpCode() == 7) {
|
|
|
- NettyMessage<AlarmDefine> AlarmDefineMessage = JSONObject.parseObject(String.valueOf(msg), new TypeReference<NettyMessage<AlarmDefine>>() {
|
|
|
- });
|
|
|
- List<AlarmDefine> definesList = AlarmDefineMessage.getContent();
|
|
|
- if (CollectionUtil.isNotEmpty(definesList)) {
|
|
|
- alarmDefineService.listSomeAlarmDefine(definesList);
|
|
|
- }
|
|
|
- } else if (message.getOpCode() == 8) {
|
|
|
- log.info("-----报警记录id推送----[{}]", message);
|
|
|
-
|
|
|
- List content = message.getContent();
|
|
|
- if (CollectionUtil.isNotEmpty(content)) {
|
|
|
- JSONObject parseObject = JSONObject.parseObject(JSONObject.toJSONString(content.get(0)));
|
|
|
- String defineId = parseObject.getString("itemCode") + "-" + parseObject.getString("objId") + "-" + parseObject.getString("targetId");
|
|
|
- ZktAlarmRecordDO zktAlarmRecordDO = alarmRecordRepository.findById(defineId).orElse(new ZktAlarmRecordDO());
|
|
|
- zktAlarmRecordDO.setDefinitionId(defineId);
|
|
|
- zktAlarmRecordDO.setItemCode(parseObject.getString("itemCode"));
|
|
|
- zktAlarmRecordDO.setObjId(parseObject.getString("objId"));
|
|
|
- zktAlarmRecordDO.setTargetId(parseObject.getString("targetId"));
|
|
|
- zktAlarmRecordDO.setAlarmId(parseObject.getString("id"));
|
|
|
- alarmRecordRepository.save(zktAlarmRecordDO);
|
|
|
- }
|
|
|
- } else if (message.getOpCode() == 9) {
|
|
|
- NettyMessage<AlarmDefine> AlarmDefineMessage = JSONObject.parseObject(String.valueOf(msg), new TypeReference<NettyMessage<AlarmDefine>>() {
|
|
|
- });
|
|
|
- List<AlarmDefine> definesList = AlarmDefineMessage.getContent();
|
|
|
- if (CollectionUtil.isNotEmpty(definesList)) {
|
|
|
- try {
|
|
|
- LockUtil.getInstance().lock.lock();
|
|
|
- LockUtil.getInstance().setExecute(false);
|
|
|
-
|
|
|
- Thread.sleep(4000);
|
|
|
- alarmDefineService.listAllAlarmDefine(definesList);
|
|
|
- LockUtil.getInstance().setExecute(true);
|
|
|
- LockUtil.getInstance().condition.signalAll();
|
|
|
- } catch (InterruptedException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- LockUtil.getInstance().lock.unlock();
|
|
|
- }
|
|
|
- }
|
|
|
- } else if (message.getOpCode() == 10) {
|
|
|
- NettyMessage<AlarmDefine> AlarmDefineMessage = JSONObject.parseObject(String.valueOf(msg), new TypeReference<NettyMessage<AlarmDefine>>() {
|
|
|
- });
|
|
|
- List<AlarmDefine> definesList = AlarmDefineMessage.getContent();
|
|
|
- if (CollectionUtil.isNotEmpty(definesList)) {
|
|
|
- alarmDefineService.deleteAlarmDefine(definesList);
|
|
|
- }
|
|
|
- } else if (message.getOpCode() == 11) {
|
|
|
-
|
|
|
- NettyMessage<String> AlarmDefineMessage = JSONObject.parseObject(String.valueOf(msg), new TypeReference<NettyMessage<String>>() {
|
|
|
- });
|
|
|
- List<String> isolationSystemList = AlarmDefineMessage.getContent();
|
|
|
- if (CollectionUtil.isNotEmpty(isolationSystemList)) {
|
|
|
- AlarmInfoCache.isolationSystemList = isolationSystemList;
|
|
|
- }
|
|
|
- } else if (message.getOpCode() == 12) {
|
|
|
-
|
|
|
- NettyMessage<JSONObject> AlarmStateMessage = JSONObject.parseObject(String.valueOf(msg), new TypeReference<NettyMessage<JSONObject>>() {
|
|
|
- });
|
|
|
- List<JSONObject> stateList = AlarmStateMessage.getContent();
|
|
|
- alarmDefineService.updateAlarmDefine(stateList);
|
|
|
- }
|
|
|
- NettyMessage response = new NettyMessage(3, groupNettyClient.getProjectId(),groupNettyClient.getGroupCode());
|
|
|
- response.setRemark("已经收到消息");
|
|
|
- channelHandlerContext.write(response.toString());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void channelReadComplete(ChannelHandlerContext ctx) {
|
|
|
- ctx.flush();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 在处理过程中引发异常时被调用
|
|
|
- *
|
|
|
- * @param ctx
|
|
|
- * @param cause
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- @Override
|
|
|
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
- log.info("exceptionCaught",cause);
|
|
|
- ctx.close();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 通道处于非活跃状态动作,该方法只会在失效时调用一次
|
|
|
- */
|
|
|
- @Override
|
|
|
- public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
|
|
-
|
|
|
- log.info("Disconnected from: " + ctx.channel().remoteAddress());
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void channelUnregistered(final ChannelHandlerContext ctx) throws Exception {
|
|
|
- log.info("Sleeping for: " + RECONNECT_DELAY + "s,Reconnecting to: " + groupNettyClient.getHost() + ':' + groupNettyClient.getPort());
|
|
|
- ctx.channel().eventLoop().schedule(new Runnable() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- log.info("Reconn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