CenterCommServerReceiver.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package com.its.api.xnetudp.thread;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.its.api.its.model.dto.common.NotifyDto;
  5. import com.its.api.utils.ItsUtils;
  6. import com.its.api.websocket.ItsWebSocketMessage;
  7. import com.its.api.websocket.ItsWebSocketSessionManager;
  8. import com.its.api.xnetudp.protocol.CENTER_COMM_DEFINE;
  9. import com.its.api.xnetudp.protocol.CENTER_COMM_MESSAGE;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.springframework.scheduling.annotation.Async;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.web.socket.TextMessage;
  14. import javax.annotation.PostConstruct;
  15. import java.nio.ByteBuffer;
  16. import java.nio.ByteOrder;
  17. @Slf4j
  18. @Service
  19. public class CenterCommServerReceiver {
  20. private ObjectMapper mapper;
  21. @PostConstruct
  22. private void init() {
  23. this.mapper = new ObjectMapper();
  24. }
  25. @Async("centerCommExecutor")
  26. public void run(CENTER_COMM_MESSAGE data) {
  27. if (data == null) {
  28. log.error("CenterCommServerReceiver: RECV Data Packet NULL");
  29. return;
  30. }
  31. if (CENTER_COMM_DEFINE.INT_ID_TRAFFIC_SERVER == data.getSendId()) {
  32. if (CENTER_COMM_DEFINE.INT_OP_TRAFFIC_CHANGE == data.getOpCode()) {
  33. log.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ RECV Traffic server job completed.");
  34. NotifyDto notifyDto = NotifyDto.builder()
  35. .notify("traffic")
  36. .notifyTm(ItsUtils.getCurrFiveMinString())
  37. .notifyCount(0)
  38. .notifyMsg("traffic")
  39. .build();
  40. ItsWebSocketMessage socketMessage = new ItsWebSocketMessage("traffic", notifyDto);
  41. try {
  42. String jsonData = this.mapper.writeValueAsString(socketMessage);
  43. ItsWebSocketSessionManager.getInstance().sendBroadcastMessage(socketMessage.getCommand(), new TextMessage(jsonData));
  44. }
  45. catch(JsonProcessingException e){
  46. log.error("CenterCommServerReceiver NotifyDto Json parsing Exception: {}, {}", notifyDto, e.getMessage());
  47. }
  48. }
  49. return;
  50. }
  51. // VMS 통신 서버는 Sender, Receiver 위치가 바뀌었음. ==> 서버 수정했음.
  52. //if (CENTER_COMM_DEFINE.INT_ID_VMS_OPER == data.getSendId() && CENTER_COMM_DEFINE.INT_ID_VMS_SERVER == data.getRecvId()) {
  53. if (CENTER_COMM_DEFINE.INT_ID_VMS_SERVER == data.getSendId()) {
  54. log.info("RECV VMS Server Message[0xFF]: Length: {} Bytes.", data.getLength());
  55. //log.error("VMS PACKET: {}", SysUtils.byteArrayToHex(data.getBody()));
  56. if (data.getLength() > 0) {
  57. ByteBuffer byteBuffer = ByteBuffer.wrap(data.getBody());
  58. byteBuffer.order(ByteOrder.BIG_ENDIAN);
  59. byte[] vmsId;
  60. byte count;
  61. byte opCode = byteBuffer.get();
  62. byte msgSeq = byteBuffer.get();
  63. log.info("OP CODE: {}, MsgSeq: {}", opCode, msgSeq);
  64. switch(opCode) {
  65. case CENTER_COMM_DEFINE.INT_OP_VMS_FORM_SAVE:
  66. byte[] saveDt = new byte[CENTER_COMM_DEFINE.INT_VMS_MAX_DATETIME];
  67. byteBuffer.get(saveDt);
  68. // VMS 폼을 데이터베이스에 저장(다운로드 결과는 알수 없음)
  69. log.info("INT_OP_VMS_FORM_SAVE: SaveTime: {}", new String(saveDt));
  70. NotifyDto notifyDto1 = NotifyDto.builder()
  71. .notify("form-save")
  72. .notifyTm(new String(saveDt))
  73. .notifyCount(0)
  74. .notifyMsg("form-save")
  75. .build();
  76. ItsWebSocketMessage socketMessage1 = new ItsWebSocketMessage("form-save", notifyDto1);
  77. try {
  78. String jsonData = this.mapper.writeValueAsString(socketMessage1);
  79. ItsWebSocketSessionManager.getInstance().sendBroadcastMessage(socketMessage1.getCommand(), new TextMessage(jsonData));
  80. }
  81. catch(JsonProcessingException e){
  82. log.error("CenterCommServerReceiver NotifyDto Json parsing Exception: {}, {}", notifyDto1, e.getMessage());
  83. }
  84. break;
  85. case CENTER_COMM_DEFINE.INT_OP_VMS_FORM_DOWNLOAD:
  86. count = byteBuffer.get();
  87. // VMS 폼 다운로드 결과 데이터베이스 저장
  88. log.info("INT_OP_VMS_FORM_DOWNLOAD: Count: {} EA", count);
  89. NotifyDto notifyDto2 = NotifyDto.builder()
  90. .notify("form-download")
  91. .notifyTm(ItsUtils.getSysTime())
  92. .notifyCount(0)
  93. .notifyMsg("form-download")
  94. .build();
  95. ItsWebSocketMessage socketMessage2 = new ItsWebSocketMessage("form-download", notifyDto2);
  96. try {
  97. String jsonData = this.mapper.writeValueAsString(socketMessage2);
  98. ItsWebSocketSessionManager.getInstance().sendBroadcastMessage(socketMessage2.getCommand(), new TextMessage(jsonData));
  99. }
  100. catch(JsonProcessingException e){
  101. log.error("CenterCommServerReceiver NotifyDto Json parsing Exception: {}, {}", notifyDto2, e.getMessage());
  102. }
  103. /* for (int ii = 0; ii < count; ii++) {
  104. vmsId = new byte[CENTER_COMM_DEFINE.INT_VMS_MAX_ID];
  105. byte[] downloadDt = new byte[CENTER_COMM_DEFINE.INT_VMS_MAX_DATETIME];
  106. byteBuffer.get(vmsId);
  107. byteBuffer.get(downloadDt);
  108. byte result = byteBuffer.get();
  109. for (int jj = 0; jj < vmsId.length; jj++) {
  110. if (vmsId[jj] < '0' || vmsId[jj] > '9') {
  111. vmsId[jj] = ' ';
  112. }
  113. }
  114. //log.error("{}, {}, {}, {}", ii+1, new String(vmsId).trim(), new String(downloadDt), result);
  115. }*/
  116. break;
  117. case CENTER_COMM_DEFINE.INT_OP_VMS_STATE_RES:
  118. short total = byteBuffer.getShort();
  119. short error = byteBuffer.getShort();
  120. short normal = byteBuffer.getShort();
  121. short module = byteBuffer.getShort();
  122. count = byteBuffer.get();
  123. log.info("INT_OP_VMS_STATE_RES: total({}), error({}), normal({}), module({}), count: {} EA.", total, error, normal, module, count);
  124. for (int ii = 0; ii < count; ii++) {
  125. vmsId = new byte[CENTER_COMM_DEFINE.INT_VMS_MAX_ID];
  126. byteBuffer.get(vmsId);
  127. byte OprMode = byteBuffer.get(); /* VMS운영모드, 0:auto, 1:Fix */
  128. byte Comm = byteBuffer.get(); /* 유선통신상태, 0:정상 1:장애 */
  129. byte Wcomm = byteBuffer.get(); /* 무선통신상태, 0:정상 1:장애 */
  130. byte DoorStatus = byteBuffer.get(); /* 도어상태정보코드, 0:열림 1:닫힘 2:알수없음 */
  131. byte ModulePowerStatus = byteBuffer.get(); /* 모듈전원상태정보코드, 0:켜짐 1:꺼짐 2:알수없음 */
  132. short BodyTemp = byteBuffer.getShort(); /* 함체온도값(℃), 범위(-128~127) */
  133. byte LuminanceStatus = byteBuffer.get(); /* 화면의 밝기값 (최대 휘도값을 100으로 했을 때의 백분율 값), 범위(0~100) */
  134. byte FanStatus = byteBuffer.get(); /* Fan 동작상태정보코드, 0:켜짐, 1:꺼짐 2:알수없음 */
  135. byte HeaterStatus = byteBuffer.get(); /* Heater 동작상태정보코드, 0:켜짐, 1:꺼짐 2:알수없음 */
  136. byte ExternalLightStatus = byteBuffer.get(); /* 선택 외부조명 동작상태정보코드 0:켜짐, 1:꺼짐, 2:자동(공단은 미사용) */
  137. byte AlarmLightStatus = byteBuffer.get(); /* 선택 경광등 동작상태정보코드 0:켜짐, 1:꺼짐 */
  138. byte SpeakerStatus = byteBuffer.get(); /* 선택 스피커 동작상태정보코드 0:켜짐, 1:꺼짐 */
  139. byte[] ControllerCurrentTime = new byte[CENTER_COMM_DEFINE.INT_VMS_MAX_DATETIME];
  140. byteBuffer.get(ControllerCurrentTime);
  141. byte Voltage = byteBuffer.get(); /* 전압, 범위(0~255), 사용안함 */
  142. byte ModuleState = byteBuffer.get(); /* 모듈 상태, 0:정상 1:장애 2:알수없음 */
  143. byte ModuleHorizontal = byteBuffer.get(); /* 가로 모듈수 */
  144. byte ModuleVertical = byteBuffer.get(); /* 세로 모듈수 */
  145. byte[] ModuleStatus = new byte[CENTER_COMM_DEFINE.INT_VMS_MAX_MODULE_BIT]; /* 모듈 개별 상태, 0:정상 1:장애 2:알수없음 */
  146. byteBuffer.get(ModuleStatus);
  147. byte PowerCount = byteBuffer.get(); /* 전원 갯수 */
  148. byte[] PowerStatus = new byte[CENTER_COMM_DEFINE.INT_VMS_MAX_POWER_BIT]; /* 전원 개별 상태, 0:켜짐 1:꺼짐 2:알수없음 */
  149. byteBuffer.get(PowerStatus);
  150. byte[] Dummy = new byte[CENTER_COMM_DEFINE.INT_VMS_STATUE_DUMMY]; /* protocol dummy bytes */
  151. byteBuffer.get(Dummy);
  152. short ScheduledMessageOperatingTime = byteBuffer.getShort(); /* 필수 계획된 메시지의 동작시간(초) */
  153. short ModuleOperatingTemperature = byteBuffer.getShort(); /* 필수 모듈 전원이 꺼지는 온도값(℃) */
  154. short FanOperatingTemperature = byteBuffer.getShort(); /* 필수 Fan 동작 기준 온도값(℃) */
  155. short HeaterOperatingTemperature = byteBuffer.getShort(); /* 필수 Heater 동작 기준 온도값(℃) */
  156. short ExternalLightOperatingLuminance = byteBuffer.getShort(); /* 선택 외부전등 동작 기준 휘도값 */
  157. short ModuleBasicFailureRate = byteBuffer.getShort(); /* 선택 모듈 장애율 (한 개의 모듈을 장애로 처리하기 위한 픽셀의 백분율값) */
  158. short MaximumRetry = byteBuffer.getShort(); /* 선택 최대 재시도 횟수(회) */
  159. short ResponseTimeOut = byteBuffer.getShort(); /* 선택 최대응답대기시간 (초) */
  160. short BlinkingCycleTime = byteBuffer.getShort(); /* 선택 점멸시간 주기 ( 1/10초단위) */
  161. for (int jj = 0; jj < vmsId.length; jj++) {
  162. if (vmsId[jj] < '0' || vmsId[jj] > '9') {
  163. vmsId[jj] = ' ';
  164. }
  165. }
  166. //log.info("{}, VmsId: {}, ModuleHor: {}, ModuleVer: {}", ii+1, new String(vmsId).trim(), ModuleHorizontal, ModuleVertical);
  167. }
  168. break;
  169. default:
  170. log.info("Other opCode: {}", opCode);
  171. break;
  172. }
  173. }
  174. }
  175. String reqIpAddr = data.getSenderIp();
  176. int reqPort = 4603;
  177. /*UnitSystService unitSystService = (UnitSystService) AppUtils.getBean(UnitSystService.class);
  178. ConcurrentHashMap<String, voUnitSyst> untiSystMap = unitSystService.getUntiSystMap();
  179. for (Map.Entry<String, voUnitSyst> e : untiSystMap.entrySet()) {
  180. if (reqIpAddr.equals(e.getValue().getSYST_IP_1())) {
  181. reqPort = e.getValue().getPRGM_PORT();
  182. break;
  183. }
  184. }*/
  185. /*byte opCode = data.getOpCode();
  186. if (opCode == CENTER_COMM_DEFINE.INT_OP_DSRC_CONTROL_REQ) {
  187. CENTER_DSRC_REQ_CONTROL req = new CENTER_DSRC_REQ_CONTROL(data.getBody());
  188. log.info("DSRC REQ CONTROL: [{}] [{},{},{},{}], {}",
  189. req.getCtlrNmbr(), req.getOperId(), req.getCmdTime(), req.getDeviceType(), req.getControlType(), Thread.currentThread().getName());
  190. int devcType = req.getDeviceType();
  191. int cntlType = req.getControlType();
  192. voDsrcCtlrCntl cntl = new voDsrcCtlrCntl();
  193. cntl.setReq(req);
  194. cntl.setReqIpAddress(data.getSender().getAddress().getHostAddress());
  195. cntl.setReqPort(data.getSender().getPort());
  196. cntl.setID(req.getCtlrNmbr());
  197. cntl.setCNTL_DT(SysUtils.getSysTime());
  198. cntl.setDEVC_TYPE(String.valueOf(devcType));
  199. cntl.setCNTL_TYPE(String.valueOf(cntlType));
  200. voDsrcCtlr dsrcCtlr = AppRepository.getInstance().getCtlrMap().get(String.valueOf(req.getCtlrNmbr()).toString());
  201. if (dsrcCtlr == null) {
  202. log.info("DSRC REQ CONTROL Unknown DSRC ID: [{}] [{},{},{},{}]",
  203. req.getCtlrNmbr(), req.getOperId(), req.getCmdTime(), req.getDeviceType(), req.getControlType());
  204. responseOperator(null, cntl, (byte)0x01);
  205. return;
  206. }
  207. if (dsrcCtlr.getChannel() == null || dsrcCtlr.getNetState() != NET.LOGINED) {
  208. // 여기 들어오면 안된다. 운영단말에서 통신이 정상인 것만 제어를 내릴수 있도록 한다.
  209. log.error("DSRC REQ CONTROL Not Connect: [{}] [{},{},{},{}]",
  210. req.getCtlrNmbr(), req.getOperId(), req.getCmdTime(), req.getDeviceType(), req.getControlType());
  211. responseOperator(dsrcCtlr, cntl, (byte)0x01);
  212. return;
  213. }
  214. // 제어요청 패킷을 만든다
  215. eControlDeviceId controlDeviceId = eControlDeviceId.getByValue(devcType);
  216. eControlCommand commandType = eControlCommand.getByValue(cntlType);
  217. if (controlDeviceId == null || commandType == null) {
  218. log.error("DSRC REQ CONTROL Value Error: [{}] [{},{},{},{}]",
  219. req.getCtlrNmbr(), req.getOperId(), req.getCmdTime(), req.getDeviceType(), req.getControlType());
  220. responseOperator(dsrcCtlr, cntl, (byte)0x01);
  221. return;
  222. }
  223. log.warn("{}, {}, controlDeviceId: {}, commandType: {}", dsrcCtlr.getID(), dsrcCtlr.getRSE_ID(), controlDeviceId.toString(), commandType.toString());
  224. boolean res = ControlDeviceService.getInstance().requestSubscriptionDeviceCommand(true, dsrcCtlr, dsrcCtlr.getChannel(), controlDeviceId.getValue(), commandType.getValue());
  225. if (res) {
  226. log.info("DSRC REQ CONTROL SEND OK: [{}] [{},{},{},{}]",
  227. req.getCtlrNmbr(), req.getOperId(), req.getCmdTime(), req.getDeviceType(), req.getControlType());
  228. responseOperator(dsrcCtlr, cntl, (byte)0x00);
  229. } else {
  230. log.error("DSRC REQ CONTROL SEND Error: [{}] [{},{},{},{}]",
  231. req.getCtlrNmbr(), req.getOperId(), req.getCmdTime(), req.getDeviceType(), req.getControlType());
  232. responseOperator(dsrcCtlr, cntl, (byte)0x01);
  233. }
  234. }
  235. else {
  236. log.error("UDP RECV UNKNOWN MESSAGE: {}", opCode);
  237. }*/
  238. }
  239. /*
  240. private int responseOperator(voDsrcCtlr obj, voDsrcCtlrCntl cntl, byte error) {
  241. CenterCommResponseService responseService = (CenterCommResponseService) AppUtils.getBean(CenterCommResponseService.class);
  242. return responseService.response(obj, cntl, error);
  243. }*/
  244. }