TbPisInfr.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package com.its.pis.entity;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.its.app.utils.SysUtils;
  5. import com.its.pis.domain.NET;
  6. import com.its.pis.ui.MainUI;
  7. import com.its.pis.websocket.C2F.message.C2FMessage;
  8. import com.its.pis.websocket.C2F.message.C2FMessageData;
  9. import com.its.pis.websocket.C2F.message.C2FMessageRequest;
  10. import com.its.pis.websocket.C2F.message.PrkPlceRlTimeResponseInfo;
  11. import com.its.pis.websocket.PisWebSocketSession;
  12. import io.netty.channel.Channel;
  13. import lombok.Getter;
  14. import lombok.Setter;
  15. import lombok.ToString;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.springframework.web.socket.TextMessage;
  18. import java.io.IOException;
  19. import java.util.Collections;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. @Slf4j
  23. @Getter
  24. @Setter
  25. @ToString
  26. public class TbPisInfr {
  27. private int index;
  28. private String PIS_NMBR;
  29. private String PIS_NM;
  30. private String PIS_TP;
  31. private String PIS_IP;
  32. private int PIS_PORT;
  33. private String PIS_END_POINT;
  34. private String PIS_ID;
  35. private String PIS_TOKEN;
  36. private int PIS_CYCLE;
  37. private String DEL_YN;
  38. private Map<String, TbPrltCtlr> rltnPrltMap;
  39. private int netState;
  40. private boolean isDupCon;
  41. private boolean isDupLogin;
  42. private TbPisInfrStts stts;
  43. private PisWebSocketSession session;
  44. private Channel dupChannel;
  45. private long syncTime;
  46. private C2FMessage<PrkPlceRlTimeResponseInfo> rlTimeInfo;
  47. private int connectCount;
  48. private String connectTm;
  49. private String disConnectTm;
  50. private boolean dump = false;
  51. public Integer getId() {
  52. return Integer.parseInt(this.PIS_NMBR);
  53. }
  54. public TbPisInfr() {
  55. this.rltnPrltMap = Collections.synchronizedMap(new HashMap<String, TbPrltCtlr>());
  56. this.stts = new TbPisInfrStts();
  57. this.connectCount = 0;
  58. this.connectTm = "";
  59. this.disConnectTm = "";
  60. initNet();
  61. }
  62. public String getLogKey() {
  63. return this.PIS_END_POINT;
  64. }
  65. public void resetConnectCount() {
  66. if (this.netState == NET.CLOSED)
  67. this.connectCount = 0;
  68. else
  69. this.connectCount = 1;
  70. }
  71. public void setConnectTm() {
  72. this.connectTm = SysUtils.getSysTimeStrMMDD();
  73. this.connectCount++;
  74. getStts().setCONN_DT(SysUtils.getSysTime());
  75. }
  76. public void setDisConnectTm() {
  77. this.disConnectTm = SysUtils.getSysTimeStrMMDD();
  78. }
  79. void initNet() {
  80. this.netState = NET.CLOSED;
  81. this.isDupCon = false;
  82. this.isDupLogin = false;
  83. this.session = null;
  84. this.dupChannel = null;
  85. this.syncTime = 0;
  86. }
  87. public synchronized void channelOpen(PisWebSocketSession session) {
  88. this.netState = NET.LOGIN_REQ;
  89. this.session = session;
  90. getStts().initStts(true);
  91. setConnectTm();
  92. MainUI mainUI = MainUI.getInstance();
  93. if (mainUI != null) {
  94. mainUI.updateCtlrStts(this);
  95. }
  96. }
  97. public synchronized void channelLogin(PisWebSocketSession session) {
  98. this.netState = NET.LOGINED;
  99. this.session = session;
  100. getStts().initStts(true);
  101. setConnectTm();
  102. MainUI mainUI = MainUI.getInstance();
  103. if (mainUI != null) {
  104. mainUI.updateCtlrStts(this);
  105. }
  106. }
  107. public synchronized void channelClosed() {
  108. if (this.netState != NET.CLOSED) {
  109. setDisConnectTm();
  110. }
  111. getStts().setDIS_CONN_DT(SysUtils.getSysTime());
  112. this.netState = NET.CLOSED;
  113. this.session = null;
  114. getStts().initStts(false);
  115. MainUI mainUI = MainUI.getInstance();
  116. if (mainUI != null) {
  117. mainUI.updateCtlrStts(this);
  118. }
  119. }
  120. public boolean channelClose() {
  121. if (getSession() == null || getNetState() == NET.CLOSED) {
  122. log.error("Close Request: channel not connected: [{}]", this);
  123. return false;
  124. }
  125. try {
  126. getSession().getSession().close();
  127. } catch (IOException e) {
  128. throw new RuntimeException(e);
  129. }
  130. MainUI mainUI = MainUI.getInstance();
  131. if (mainUI != null) {
  132. mainUI.updateCtlrStts(this);
  133. }
  134. return true;
  135. }
  136. public boolean requestRlTimeInfo() {
  137. if (getSession() == null || getNetState() == NET.CLOSED) {
  138. log.error("Reset Request: channel not connected: [{}]", this);
  139. return false;
  140. }
  141. C2FMessageRequest request = C2FMessageRequest.builder()
  142. .prk_tkn(this.PIS_TOKEN)
  143. .event_name("prk_plce_rl_time_request_info")
  144. .event_type("request")
  145. .build();
  146. C2FMessage<Object> message = C2FMessage.builder()
  147. .command("message")
  148. .data(C2FMessageData.builder()
  149. .payload(request)
  150. .build())
  151. .build();
  152. ObjectMapper mapper = new ObjectMapper();
  153. try {
  154. String strMessage = mapper.writeValueAsString(message);
  155. log.error("{}", strMessage);
  156. TextMessage txtMessage = new TextMessage(strMessage);
  157. getSession().sendMessage(request.getEvent_name(), txtMessage);
  158. } catch (JsonProcessingException e) {
  159. throw new RuntimeException(e);
  160. }
  161. return true;
  162. }
  163. }