TbVdsCtlrDto.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package com.its.vds.dto;
  2. import com.its.app.utils.SysUtils;
  3. import com.its.vds.domain.NET;
  4. import com.its.vds.domain.VdsDebug;
  5. import com.its.vds.domain.VdsFrameSequence;
  6. import com.its.vds.domain.task.VdsCommandTimeoutTask;
  7. import com.its.vds.entity.TbVdsCtlr;
  8. import com.its.vds.entity.TbVdsCtlrStts;
  9. import com.its.vds.entity.TbVdsCtrlHs;
  10. import com.its.vds.entity.TbVdsDtct;
  11. import com.its.vds.xnettcp.vds.handler.VdsServerIdleStateHandler;
  12. import com.its.vds.xnettcp.vds.process.TcpServerSendData;
  13. import com.its.vds.xnettcp.vds.process.TcpServerSendDataProcess;
  14. import com.its.vds.xnettcp.vds.protocol.VdsReqFramePacket;
  15. import com.its.vds.xnettcp.vds.protocol.enums.eVdsOpCode;
  16. import com.its.vds.xnettcp.vds.protocol.impl.VdsReqDataLength;
  17. import io.netty.channel.Channel;
  18. import io.netty.channel.ChannelFuture;
  19. import io.netty.channel.ChannelHandlerContext;
  20. import lombok.*;
  21. import lombok.extern.slf4j.Slf4j;
  22. import java.io.Serializable;
  23. import java.net.InetSocketAddress;
  24. import java.nio.ByteBuffer;
  25. import java.text.SimpleDateFormat;
  26. import java.util.Map;
  27. import java.util.Timer;
  28. import java.util.concurrent.ConcurrentHashMap;
  29. @Slf4j
  30. @Getter
  31. @Setter
  32. @ToString
  33. @Builder
  34. @NoArgsConstructor//(access = AccessLevel.PROTECTED)
  35. @AllArgsConstructor
  36. public class TbVdsCtlrDto implements Serializable {
  37. private static final long serialVersionUID = 1L;
  38. private int index;
  39. private Long vdsCtlrNmbr;
  40. private String vdsCtlrId;
  41. private String vdsNm;
  42. private String vdsCtlrIp;
  43. private Integer vdsCtlrPort;
  44. private Integer vdsCtlrLocalNo;
  45. private String vdsTypeCd;
  46. private String istlLctnNm;
  47. private Integer groupNo;
  48. private Integer fanMode;
  49. private Integer fanRunTmpr;
  50. private Integer hetrMode;
  51. private Integer hetrRunTmpr;
  52. private Integer detectLanes;
  53. private Integer trafClctCycl;
  54. private Integer sttsClctCycl;
  55. private String istlLctnAddr;
  56. private String validYn;
  57. private String delYn;
  58. private Map<String, TbVdsDtct> vdsDtctMap;
  59. private TbVdsCtlrStts stts;
  60. private int netState;
  61. private boolean isDupCon;
  62. private boolean isDupLogin;
  63. private String dstIpAddr;
  64. private Channel channel = null;
  65. private Channel dupChannel = null;
  66. private InetSocketAddress cliReq;
  67. private long syncTime;
  68. private VdsFrameSequence seq;
  69. private ConcurrentHashMap<Short, Timer> registeredCommandTimer = null;
  70. private ConcurrentHashMap<Short, VdsCommandTimeoutTask> registeredCommand = null;
  71. private ConcurrentHashMap<Long, TbVdsCtrlHs> userCommands = null;
  72. private int connectCount;
  73. private String connectTm;
  74. private String disConnectTm;
  75. private long lastRecvTime;
  76. private VdsDebug debug;
  77. private TcpServerSendDataProcess dataProcess; // 제어기로 데이터를 전송할 전송 전송 큐를 처리할 스레드
  78. public void init() {
  79. this.debug = new VdsDebug();
  80. this.stts = new TbVdsCtlrStts(this.vdsCtlrNmbr);
  81. this.seq = new VdsFrameSequence();
  82. this.registeredCommandTimer = new ConcurrentHashMap<>();
  83. this.registeredCommand = new ConcurrentHashMap<>();
  84. this.userCommands = new ConcurrentHashMap<>();
  85. this.connectCount = 0;
  86. this.connectTm = "-";
  87. this.disConnectTm = "-";
  88. this.lastRecvTime = 0;
  89. initNet();
  90. this.dataProcess = new TcpServerSendDataProcess(this);
  91. this.dataProcess.run();
  92. }
  93. public String getLogKey() {
  94. return this.vdsCtlrId;
  95. }
  96. /**
  97. * 제어기 전송 패킷의 패킷 번호를 구한다.
  98. * @return
  99. */
  100. public short getSeqNext() {
  101. return this.seq.nextValue();
  102. }
  103. /**
  104. * 제어기 전송 스레드 타스크 큐에 명령을 전송
  105. * @param data
  106. * @return
  107. */
  108. public synchronized boolean addRequestData(TcpServerSendData data) {
  109. return this.dataProcess.add(data);
  110. }
  111. /**
  112. * 제어기 네트워크 최초 접속시 발생 이벤트
  113. * @param ctx
  114. */
  115. public synchronized void connected(ChannelHandlerContext ctx) {
  116. this.netState = NET.LOGIN_REQ;
  117. this.connectTm = SysUtils.getSysTimeStr();
  118. this.channel = ctx.channel();
  119. this.lastRecvTime = System.currentTimeMillis();
  120. this.connectCount++;
  121. this.stts.initNormal();
  122. clearRegisteredCommandTimer();
  123. }
  124. /**
  125. * 제어기 네트워크 연결 종료 이벤트
  126. */
  127. public synchronized void disconnected() {
  128. clearRegisteredCommandTimer();
  129. this.netState = NET.CLOSED;
  130. this.channel = null;
  131. this.disConnectTm = SysUtils.getSysTimeStr();
  132. this.stts.initError();
  133. }
  134. public String getLastRecvTimeFmt() {
  135. SimpleDateFormat sdfDate = new SimpleDateFormat("MM-dd HH:mm:ss");
  136. return sdfDate.format(this.lastRecvTime);
  137. }
  138. public void resetConnectCount() {
  139. if (this.netState == NET.CLOSED)
  140. this.connectCount = 0;
  141. else
  142. this.connectCount = 1;
  143. }
  144. void initNet() {
  145. this.netState = NET.CLOSED;
  146. this.isDupCon = false;
  147. this.isDupLogin = false;
  148. this.dstIpAddr = "";
  149. this.channel = null;
  150. this.dupChannel = null;
  151. this.cliReq = null;
  152. this.syncTime = 0;
  153. }
  154. /**
  155. * 제어기 명령 타임아웃 타스크 클리어
  156. */
  157. public synchronized void clearRegisteredCommandTimer() {
  158. this.registeredCommand.clear();
  159. for (Map.Entry<Short, Timer> e : this.registeredCommandTimer.entrySet()) {
  160. Timer task = e.getValue();
  161. task.cancel();
  162. }
  163. this.registeredCommandTimer.clear();
  164. }
  165. /**
  166. * 제어기 명령 타임아웃 타스크 추가
  167. * @param cmdTimeoutTask
  168. * @return
  169. */
  170. public synchronized void addRegisteredCommandsTimer(VdsCommandTimeoutTask cmdTimeoutTask) {
  171. long timeoutSec = 1000L * 5;
  172. Timer timer = new Timer();
  173. timer.schedule(cmdTimeoutTask, timeoutSec);
  174. this.registeredCommandTimer.put(cmdTimeoutTask.getPacketNmbr(), timer);
  175. this.registeredCommand.put(cmdTimeoutTask.getPacketNmbr(), cmdTimeoutTask);
  176. // log.info("addRegisteredCommandsTimer: VDS {}, Task {} EA, OpCode {}.",
  177. // cmdTimeoutTask.getCtlr().getVdsCtlrNmbr(), this.registeredCommandTimer.size(), cmdTimeoutTask.getPacket().getOpCode());
  178. }
  179. /**
  180. * 제어기 명령 타임아웃 타스크 제거
  181. * @param opCode
  182. */
  183. public synchronized void removeRegisteredCommandsTimer(eVdsOpCode opCode) {
  184. short packetNmbr = (short)opCode.getValue();
  185. Timer timer = this.registeredCommandTimer.get(packetNmbr);
  186. if (timer != null) {
  187. timer.cancel();
  188. this.registeredCommandTimer.remove(packetNmbr);
  189. }
  190. VdsCommandTimeoutTask cmdTimeoutTask = this.registeredCommand.get(packetNmbr);
  191. if (cmdTimeoutTask != null) {
  192. this.registeredCommand.remove(packetNmbr);
  193. // log.info("removeRegisteredCommandsTimer: VDS {}, Task {} EA, OpCode {}.",
  194. // cmdTimeoutTask.getCtlr().getVdsCtlrNmbr(), this.registeredCommandTimer.size(), cmdTimeoutTask.getPacket().getOpCode());
  195. }
  196. }
  197. /**
  198. * Channel Send Data
  199. * @param packet
  200. * @param retryCnt
  201. * @return
  202. */
  203. public synchronized boolean sendData(VdsReqFramePacket packet, int retryCnt) {
  204. boolean result = false;
  205. ByteBuffer sendBuffer = packet.getByteBuffer();
  206. String packetDesc = packet.getOpCodeDesc();
  207. log.info("~REQUEST-{}. VDS {}, {} Bytes.", packetDesc, this.vdsCtlrNmbr, sendBuffer.capacity());
  208. if (this.channel != null) {
  209. ChannelFuture f = this.channel.writeAndFlush(sendBuffer);
  210. f.awaitUninterruptibly();
  211. if (f.isDone() || f.isSuccess()) {
  212. result = true;
  213. } else {
  214. log.error("~REQUEST-{}. VDS {}, sendData Failed. {} Bytes.", packetDesc, this.vdsCtlrNmbr, sendBuffer.array().length);
  215. }
  216. } else {
  217. log.error("~REQUEST-{}. VDS {}, sendData Failed. Not Connected. {} Bytes.", packetDesc, this.vdsCtlrNmbr, sendBuffer.array().length);
  218. }
  219. if (result) {
  220. VdsCommandTimeoutTask cmdTimeoutTask = new VdsCommandTimeoutTask(this, packet, retryCnt);
  221. addRegisteredCommandsTimer(cmdTimeoutTask);
  222. }
  223. return result;
  224. }
  225. public boolean channelClose() {
  226. if (getChannel() == null || getNetState() == NET.CLOSED) {
  227. log.error("Close Request: channel not connected: [{}]", this);
  228. return false;
  229. }
  230. VdsServerIdleStateHandler.disconnectChannel(this, getChannel());
  231. return true;
  232. }
  233. public boolean reset() {
  234. if (getChannel() == null || getNetState() == NET.CLOSED) {
  235. log.error("Reset Request: channel not connected: [{}]", this);
  236. return false;
  237. }
  238. VdsReqDataLength resetControl = new VdsReqDataLength(this);
  239. // resetControl.controlReset();
  240. return addRequestData(new TcpServerSendData(eVdsOpCode.OP_VDS_DATA_LENGTH, resetControl));
  241. }
  242. public synchronized boolean removeUserCommands(Long packetNmbr) {
  243. TbVdsCtrlHs command = this.userCommands.get(packetNmbr);
  244. if (command != null) {
  245. this.userCommands.remove(packetNmbr);
  246. return true;
  247. }
  248. return false;
  249. }
  250. public synchronized TbVdsCtrlHs getUserCommands(Long packetNmbr) {
  251. TbVdsCtrlHs command = this.userCommands.get(packetNmbr);
  252. return command;
  253. }
  254. public TbVdsCtlr toEntity() {
  255. return TbVdsCtlr.builder()
  256. .vdsCtlrNmbr(this.vdsCtlrNmbr)
  257. .vdsCtlrId(this.vdsCtlrId)
  258. .vdsNm(this.vdsNm)
  259. .vdsCtlrIp(this.vdsCtlrIp)
  260. .vdsCtlrPort(this.vdsCtlrPort)
  261. .vdsCtlrLocalNo(this.vdsCtlrLocalNo)
  262. .vdsTypeCd(this.vdsTypeCd)
  263. .istlLctnNm(this.istlLctnNm)
  264. .groupNo(this.groupNo)
  265. .fanMode(this.fanMode)
  266. .fanRunTmpr(this.fanRunTmpr)
  267. .hetrMode(this.hetrMode)
  268. .hetrRunTmpr(this.hetrRunTmpr)
  269. .detectLanes(this.detectLanes)
  270. .trafClctCycl(this.trafClctCycl)
  271. .sttsClctCycl(this.sttsClctCycl)
  272. .istlLctnAddr(this.istlLctnAddr)
  273. .validYn(this.validYn)
  274. .delYn(this.delYn)
  275. .build();
  276. }
  277. }