|
@@ -1,278 +0,0 @@
|
|
|
-package com.persagy.service.impl;
|
|
|
-
|
|
|
-import cn.hutool.core.collection.CollectionUtil;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.alibaba.fastjson.TypeReference;
|
|
|
-import com.persagy.cache.AlarmInfoCache;
|
|
|
-import com.persagy.cache.AlarmLastTimeCache;
|
|
|
-import com.persagy.cache.CreatedAlarmIdsCache;
|
|
|
-import com.persagy.client.GroupNettyClient;
|
|
|
-import com.persagy.entity.NettyMessage;
|
|
|
-import com.persagy.entity.v2.AlarmCondition;
|
|
|
-import com.persagy.entity.v2.ObjConditionRel;
|
|
|
-import com.persagy.enumeration.NettyMsgTypeEnum;
|
|
|
-import com.persagy.utils.LockUtil;
|
|
|
-import io.netty.channel.ChannelHandlerContext;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.util.CollectionUtils;
|
|
|
-
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
-/**
|
|
|
- * Netty消息处理类
|
|
|
- *
|
|
|
- * @author lixing
|
|
|
- * @version V1.0 2021/10/25 8:34 下午
|
|
|
- **/
|
|
|
-@Service
|
|
|
-@Slf4j
|
|
|
-public class NettyMsgHandler {
|
|
|
- @Autowired
|
|
|
- public AlarmConditionServiceImpl alarmConditionService;
|
|
|
- @Autowired
|
|
|
- public GroupNettyClient groupNettyClient;
|
|
|
- @Autowired
|
|
|
- public CreatedAlarmIdsCache createdAlarmIdsCache;
|
|
|
- @Autowired
|
|
|
- public AlarmLastTimeCache alarmLastTimeCache;
|
|
|
- @Autowired
|
|
|
- public AlarmInfoCache alarmInfoCache;
|
|
|
- @Value("${logging.level.com.persagy}")
|
|
|
- private String logLevel;
|
|
|
-
|
|
|
- /**
|
|
|
- * 已接收回执
|
|
|
- *
|
|
|
- * @param channelHandlerContext 通道上下文
|
|
|
- * @author lixing
|
|
|
- * @version V1.0 2021/10/25 8:32 下午
|
|
|
- */
|
|
|
- public void acceptedReply(ChannelHandlerContext channelHandlerContext) {
|
|
|
- NettyMessage response = new NettyMessage(groupNettyClient.projectId);
|
|
|
- response.setOpCode(NettyMsgTypeEnum.ACCEPTED);
|
|
|
- response.setRemark("已经收到消息");
|
|
|
- channelHandlerContext.write(response.toString());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 缓存报警记录id
|
|
|
- *
|
|
|
- * @param message netty消息
|
|
|
- * @author lixing
|
|
|
- * @version V1.0 2021/10/25 8:27 下午
|
|
|
- */
|
|
|
- public void cacheRecordId(NettyMessage message) {
|
|
|
- log.debug("云端完成报警记录创建");
|
|
|
- // {"id":"","objId":"","itemCode":""} id为报警记录ID
|
|
|
- log.debug("返回报警记录id[{}]", message);
|
|
|
- List content = message.getContent();
|
|
|
- if (CollectionUtil.isNotEmpty(content)) {
|
|
|
- JSONObject parseObject = JSONObject.parseObject(JSONObject.toJSONString(content.get(0)));
|
|
|
- String alarmId = parseObject.getString("id");
|
|
|
- // 将alarmId放入缓存中,用于后续判断报警是否完成创建
|
|
|
- createdAlarmIdsCache.put(alarmId);
|
|
|
- alarmLastTimeCache.setAlarmHasCreated(alarmId);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 缓存设备与报警条件的关联
|
|
|
- *
|
|
|
- * @param msg netty消息
|
|
|
- * @author lixing
|
|
|
- * @version V1.0 2021/10/25 8:27 下午
|
|
|
- */
|
|
|
- public void cacheAllObjConditionRel(Object msg) {
|
|
|
- List<ObjConditionRel> content = getObjConditionRelList(msg);
|
|
|
- if (CollectionUtils.isEmpty(content)) {
|
|
|
- log.error("接收到的消息中报警条件与设备的关联关系为空");
|
|
|
- return;
|
|
|
- }
|
|
|
- log.info("正在同步报警条件与设备的关联关系 -> 项目id:[{}], 同步条数[{}]",
|
|
|
- content.get(0).getProjectId(), content.size());
|
|
|
- try {
|
|
|
- LockUtil.getInstance().lock.lock();
|
|
|
- LockUtil.getInstance().setExecute(false);
|
|
|
- alarmConditionService.cacheObjConditionRelList(content);
|
|
|
- LockUtil.getInstance().setExecute(true);
|
|
|
- LockUtil.getInstance().condition.signalAll();
|
|
|
- log.info("同步报警条件与设备的关联关系完成 -> 项目id:[{}]",
|
|
|
- content.get(0).getProjectId());
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("同步设备与报警条件关联关系发生异常", e);
|
|
|
- } finally {
|
|
|
- LockUtil.getInstance().lock.unlock();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 将消息解析为设备与报警条件关联关系列表
|
|
|
- *
|
|
|
- * @param msg netty消息
|
|
|
- * @return 设备与报警条件关联关系列表
|
|
|
- * @author lixing
|
|
|
- * @version V1.0 2021/10/25 9:05 下午
|
|
|
- */
|
|
|
- private List<ObjConditionRel> getObjConditionRelList(Object msg) {
|
|
|
- NettyMessage<ObjConditionRel> objConditionRelNettyMessage = JSONObject.parseObject(
|
|
|
- String.valueOf(msg),
|
|
|
- new TypeReference<NettyMessage<ObjConditionRel>>() {
|
|
|
- });
|
|
|
-
|
|
|
- return objConditionRelNettyMessage.getContent();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 日志记录当前缓存中的数据数量
|
|
|
- *
|
|
|
- * @author lixing
|
|
|
- * @version V1.0 2021/10/28 9:58 上午
|
|
|
- */
|
|
|
- public void logCacheInfo() {
|
|
|
- // 只在debug日志等级下才获取数据数量,避免预期之外的统计计算
|
|
|
- if (!"debug".equals(logLevel)) {
|
|
|
- return;
|
|
|
- }
|
|
|
- log.debug("当前缓存中报警条件数量:[{}]", alarmInfoCache.getCachedConditionCount());
|
|
|
- log.debug("当前缓存中设备数量:[{}]", alarmInfoCache.getCachedObjCount());
|
|
|
- log.debug("当前缓存中设备与报警条件关联关系数量:[{}]", alarmInfoCache.getCachedObjConditionRelCount());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 缓存报警条件
|
|
|
- *
|
|
|
- * @param msg 全量报警条件消息
|
|
|
- * @author lixing
|
|
|
- * @version V1.0 2021/10/22 3:21 下午
|
|
|
- */
|
|
|
- public void cacheAllAlarmConditions(Object msg) {
|
|
|
- log.info("开始全量同步报警条件");
|
|
|
- List<AlarmCondition> conditionList = getAlarmConditions(msg);
|
|
|
- if (CollectionUtil.isNotEmpty(conditionList)) {
|
|
|
- try {
|
|
|
- LockUtil.getInstance().lock.lock();
|
|
|
- LockUtil.getInstance().setExecute(false);
|
|
|
- //加个等待,保证正在执行的逻辑执行成功
|
|
|
- Thread.sleep(4000);
|
|
|
- alarmConditionService.cacheAllConditions(conditionList);
|
|
|
- log.info("全量同步报警条件完成");
|
|
|
- LockUtil.getInstance().setExecute(true);
|
|
|
- LockUtil.getInstance().condition.signalAll();
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("全量同步报警条件发生异常", e);
|
|
|
- } finally {
|
|
|
- LockUtil.getInstance().lock.unlock();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 将消息解析为报警条件列表
|
|
|
- *
|
|
|
- * @param msg netty消息
|
|
|
- * @return 报警条件列表
|
|
|
- * @author lixing
|
|
|
- * @version V1.0 2021/10/25 8:54 下午
|
|
|
- */
|
|
|
- private List<AlarmCondition> getAlarmConditions(Object msg) {
|
|
|
- NettyMessage<AlarmCondition> alarmConditionNettyMessage = JSONObject.parseObject(
|
|
|
- String.valueOf(msg),
|
|
|
- new TypeReference<NettyMessage<AlarmCondition>>() {
|
|
|
- });
|
|
|
- return alarmConditionNettyMessage.getContent();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 将消息解析为报警条件
|
|
|
- *
|
|
|
- * @param msg netty消息
|
|
|
- * @return 报警条件
|
|
|
- * @author lixing
|
|
|
- * @version V1.0 2021/10/25 8:54 下午
|
|
|
- */
|
|
|
- private AlarmCondition getAlarmCondition(Object msg) {
|
|
|
- NettyMessage<AlarmCondition> alarmConditionNettyMessage = JSONObject.parseObject(
|
|
|
- String.valueOf(msg),
|
|
|
- new TypeReference<NettyMessage<AlarmCondition>>() {
|
|
|
- });
|
|
|
- List<AlarmCondition> content = alarmConditionNettyMessage.getContent();
|
|
|
- if (CollectionUtils.isEmpty(content)) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- return content.get(0);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 缓存新增的报警条件
|
|
|
- *
|
|
|
- * @param msg 新增的报警条件
|
|
|
- * @author lixing
|
|
|
- * @version V1.0 2021/10/22 3:21 下午
|
|
|
- */
|
|
|
- public void cacheNewCondition(Object msg) {
|
|
|
- AlarmCondition alarmCondition = getAlarmCondition(msg);
|
|
|
- alarmConditionService.cacheNewCondition(alarmCondition);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 缓存更新的报警条件
|
|
|
- *
|
|
|
- * @param msg 更新的报警条件
|
|
|
- * @author lixing
|
|
|
- * @version V1.0 2021/10/22 3:21 下午
|
|
|
- */
|
|
|
- public void cacheUpdatedCondition(Object msg) {
|
|
|
- AlarmCondition alarmCondition = getAlarmCondition(msg);
|
|
|
- alarmConditionService.cacheUpdatedCondition(alarmCondition);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 移除缓存中的报警条件
|
|
|
- *
|
|
|
- * @param msg 要移除的报警条件
|
|
|
- * @author lixing
|
|
|
- * @version V1.0 2021/10/22 3:21 下午
|
|
|
- */
|
|
|
- public void removeCachedCondition(Object msg) {
|
|
|
- AlarmCondition alarmCondition = getAlarmCondition(msg);
|
|
|
- alarmConditionService.removeCachedCondition(alarmCondition);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 缓存新增的设备与报警条件关联关系
|
|
|
- *
|
|
|
- * @param msg 新增的设备与报警条件关联关系
|
|
|
- * @author lixing
|
|
|
- * @version V1.0 2021/10/22 3:21 下午
|
|
|
- */
|
|
|
- public void cacheNewObjConditionRel(Object msg) {
|
|
|
- List<ObjConditionRel> objConditionRelList = getObjConditionRelList(msg);
|
|
|
- if (CollectionUtils.isEmpty(objConditionRelList)) {
|
|
|
- return;
|
|
|
- }
|
|
|
- for (ObjConditionRel objConditionRel : objConditionRelList) {
|
|
|
- alarmConditionService.cacheNewObjConditionRel(objConditionRel);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 移除缓存中的设备与报警条件关联关系
|
|
|
- *
|
|
|
- * @param msg 要移除的关联
|
|
|
- * @author lixing
|
|
|
- * @version V1.0 2021/10/22 3:21 下午
|
|
|
- */
|
|
|
- public void removeCachedObjConditionRel(Object msg) {
|
|
|
- List<ObjConditionRel> objConditionRelList = getObjConditionRelList(msg);
|
|
|
- if (CollectionUtils.isEmpty(objConditionRelList)) {
|
|
|
- return;
|
|
|
- }
|
|
|
- for (ObjConditionRel objConditionRel : objConditionRelList) {
|
|
|
- alarmConditionService.removeCachedObjConditionRel(objConditionRel);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|