package com.tsi.comm.server.repository; import com.tsi.app.common.utils.HexString; import com.tsi.app.common.xnet.NettyUtils; import com.tsi.comm.server.vo.TsiAlarmConfigVo; import io.netty.channel.Channel; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.slf4j.MDC; import org.springframework.stereotype.Component; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @Slf4j @RequiredArgsConstructor @Component public class TsiAlarmManager { private final ConcurrentHashMap tsiAlarmConfigMap = new ConcurrentHashMap<>(); private final Set UNKNOWN_NODE_SET = ConcurrentHashMap.newKeySet(); private final Set UNKNOWN_NODE_BUF_SET = ConcurrentHashMap.newKeySet(); public TsiAlarmConfigVo get(String code) { return this.tsiAlarmConfigMap.get(code); } public void put(String code, TsiAlarmConfigVo vo) { this.tsiAlarmConfigMap.put(code, vo); } public int size() { return this.tsiAlarmConfigMap.size(); } public boolean checkAlarm(String code) { TsiAlarmConfigVo vo = get(code); return (vo != null && vo.isUseYn()); } // public boolean containsKey(String key) { // return this.tsiAlarmConfigMap.containsKey(key); // } // public boolean isAlarm(String code, int value) { // TsiAlarmConfigVo vo = get(code); // if (vo != null && vo.isUseYn()) { // return (value > vo.getValue()); // } // return false; // } public void loggingUnknownNode(long nodeId, Channel channel) { final String remoteIpAddress = NettyUtils.getRemoteIpAddress(channel); if (this.UNKNOWN_NODE_SET.contains(remoteIpAddress)) { return; // 이미 등록된 IP 주소는 무시 } this.UNKNOWN_NODE_SET.add(remoteIpAddress); final String fileName = "unknown_node"; MDC.put("report", fileName); log.info("The node ID of the first received packet is not registered: {}, {}", nodeId, remoteIpAddress); MDC.remove(fileName); MDC.clear(); } public void loggingUnknownNodePacket(long nodeId, Channel channel, byte[] buf) { final String remoteIpAddress = NettyUtils.getRemoteIpAddress(channel); if (this.UNKNOWN_NODE_BUF_SET.contains(remoteIpAddress)) { return; // 이미 등록된 IP 주소는 무시 } this.UNKNOWN_NODE_BUF_SET.add(remoteIpAddress); final String fileName = "unknown_node"; MDC.put("report", fileName); log.info("Node: {}, {}, [{}]", nodeId, remoteIpAddress, HexString.fromBytes(buf)); MDC.remove(fileName); MDC.clear(); } public void loggingAddUnknownNode(long nodeId, Channel channel) { final String fileName = "unknown_node"; MDC.put("report", fileName); log.info("Unknown node id registered: {}, {}", nodeId, channel); MDC.remove(fileName); MDC.clear(); } }