TbVdsCtlr.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package com.its.vds.entity;
  2. import com.its.app.utils.SysUtils;
  3. import com.its.vds.domain.NET;
  4. import com.its.vds.xnettcp.vds.handler.VdsTcpClientIdleHandler;
  5. import com.its.vds.xnettcp.vds.protocol.*;
  6. import com.its.vds.xnetudp.protocol.voVdsState;
  7. import io.netty.channel.Channel;
  8. import io.netty.channel.ChannelFuture;
  9. import lombok.Getter;
  10. import lombok.Setter;
  11. import lombok.ToString;
  12. import lombok.extern.slf4j.Slf4j;
  13. import java.net.InetSocketAddress;
  14. import java.nio.ByteBuffer;
  15. import java.util.*;
  16. @Slf4j
  17. @Getter
  18. @Setter
  19. @ToString
  20. public class TbVdsCtlr {
  21. private int index;
  22. private String VDS_CTLR_NMBR;
  23. private String VDS_CTLR_ID;
  24. private String VDS_NM;
  25. private String VDS_TYPE_CD;
  26. private String VDS_CTLR_IP;
  27. private int VDS_CTLR_PORT;
  28. private int GROUP_NO;
  29. private int VDS_CTLR_LOCAL_NO;
  30. private int FAN_MODE;
  31. private int FAN_RUN_TMPR;
  32. private int HETR_MODE;
  33. private int HETR_RUN_TMPR;
  34. private int DETECT_LANES;
  35. private int TRAF_CLCT_CYCL;
  36. private int STTS_CLCT_CYCL;
  37. private String VALD_YN;
  38. private String DEL_YN;
  39. private Map<String, TbVdsDtct> vdsDtctMap;
  40. private int netState;
  41. private boolean isDupCon;
  42. private boolean isDupLogin;
  43. private String dstIpAddr;
  44. private TbVdsCtlrStts stts;
  45. private Channel channel;
  46. private Channel dupChannel;
  47. private long syncTime;
  48. private InetSocketAddress cliReq;
  49. private int connectCount;
  50. private String connectTm;
  51. private String disConnectTm;
  52. private boolean dump = false;
  53. private VdsReqSynchronize reqSynchronize = null;
  54. private VdsReqData reqData = null;
  55. private VdsReqTemperature reqTemperature = null;
  56. private VdsReqTraffic reqTraffic = null;
  57. private boolean requestStopImage = false;
  58. private List<Channel> requestImageList = new ArrayList<>();
  59. private int frameNo;
  60. private int cameraNo;
  61. private int imageSize;
  62. private ByteBuffer imageBuffer;
  63. public Integer getId() {
  64. return Integer.parseInt(this.VDS_CTLR_NMBR);
  65. }
  66. public void initDtctClct() {
  67. for (Map.Entry<String, TbVdsDtct> e : this.vdsDtctMap.entrySet()) {
  68. TbVdsDtct tmp = e.getValue();
  69. tmp.getClct().initVal();
  70. }
  71. }
  72. public void addImageData(byte[] imageData) {
  73. if (imageData == null) {
  74. return;
  75. }
  76. if (this.imageBuffer == null) {
  77. this.imageBuffer = ByteBuffer.wrap(imageData);
  78. imageBuffer.order(VdsProtocol.byteOrder);
  79. }
  80. else {
  81. byte[] prevImage = this.imageBuffer.array();
  82. this.imageBuffer = ByteBuffer.allocate(prevImage.length + imageData.length);
  83. this.imageBuffer.put(prevImage);
  84. this.imageBuffer.put(imageData);
  85. }
  86. }
  87. public ByteBuffer getImageData() {
  88. return this.imageBuffer;
  89. }
  90. public void addImageSize(int imageSize) {
  91. this.imageSize += imageSize;
  92. }
  93. public void setImageSize(int imageSize) {
  94. this.imageSize = imageSize;
  95. }
  96. public int getImageSize() {
  97. return this.imageSize;
  98. }
  99. public int getFrameNo() {
  100. return this.frameNo;
  101. }
  102. public int getCameraNo() {
  103. return this.cameraNo;
  104. }
  105. public List<Channel> getRequestImageList() {
  106. return this.requestImageList;
  107. }
  108. public void setStopImageRequest(Channel channel, int cameraNo, int frameNo) {
  109. if (!this.requestStopImage) {
  110. this.requestImageList.clear();
  111. this.requestImageList = new ArrayList<>();
  112. //this.cameraNo = cameraNo;
  113. //this.frameNo = frameNo;
  114. //this.imageSize = 0;
  115. }
  116. // 마지막 요청을 유지하자
  117. this.imageBuffer = null;
  118. this.imageSize = 0;
  119. this.cameraNo = cameraNo;
  120. this.frameNo = frameNo;
  121. this.requestImageList.add(channel);
  122. this.requestStopImage = true;
  123. }
  124. public void setStopImageResponse() {
  125. this.imageBuffer = null;
  126. this.imageSize = 0;
  127. this.requestStopImage = false;
  128. this.cameraNo = 0;
  129. this.frameNo = 0;
  130. this.requestImageList.clear();
  131. this.requestImageList = new ArrayList<>();
  132. }
  133. public TbVdsCtlr() {
  134. this.vdsDtctMap = Collections.synchronizedMap(new HashMap<String, TbVdsDtct>());
  135. this.stts = new TbVdsCtlrStts();
  136. this.connectCount = 0;
  137. this.connectTm = "";
  138. this.disConnectTm = "";
  139. initNet();
  140. }
  141. public String getLogKey() {
  142. return this.VDS_CTLR_ID;
  143. }
  144. public void resetConnectCount() {
  145. if (this.netState == NET.CLOSED)
  146. this.connectCount = 0;
  147. else
  148. this.connectCount = 1;
  149. }
  150. public void setConnectTm() {
  151. this.connectTm = SysUtils.getSysTimeStrMMDD();
  152. this.connectCount++;
  153. }
  154. public void setDisConnectTm() {
  155. this.disConnectTm = SysUtils.getSysTimeStrMMDD();
  156. }
  157. void initNet() {
  158. this.netState = NET.CLOSED;
  159. this.isDupCon = false;
  160. this.isDupLogin = false;
  161. this.dstIpAddr = "";
  162. this.channel = null;
  163. this.dupChannel = null;
  164. this.cliReq = null;
  165. this.syncTime = 0;
  166. }
  167. public synchronized void channelOpen(Channel channel) {
  168. this.netState = NET.LOGIN_REQ;
  169. this.channel = channel;
  170. getStts().initStts(true);
  171. setConnectTm();
  172. }
  173. public synchronized void channelLogin(Channel channel) {
  174. this.netState = NET.LOGINED;
  175. this.channel = channel;
  176. getStts().initStts(true);
  177. setConnectTm();
  178. }
  179. public synchronized void channelClosed() {
  180. if (this.netState != NET.CLOSED) {
  181. setDisConnectTm();
  182. }
  183. this.netState = NET.CLOSED;
  184. this.channel = null;
  185. getStts().initStts(false);
  186. }
  187. /**
  188. * 통신 패킷 초기화
  189. */
  190. public void initReqPacket() {
  191. if (this.reqSynchronize == null) {
  192. this.reqSynchronize = new VdsReqSynchronize((short)this.GROUP_NO, (short)this.VDS_CTLR_LOCAL_NO);
  193. //this.reqSynchronize = new VdsReqSynchronize((short)0xFFFF, (short)0xFFFF);
  194. this.reqSynchronize.makeCRC();
  195. }
  196. if (this.reqData == null) {
  197. this.reqData = new VdsReqData((short)this.GROUP_NO, (short)this.VDS_CTLR_LOCAL_NO);
  198. this.reqData.makeCRC();
  199. }
  200. if (this.reqTemperature == null) {
  201. this.reqTemperature = new VdsReqTemperature((short)this.GROUP_NO, (short)this.VDS_CTLR_LOCAL_NO);
  202. this.reqTemperature.makeCRC();
  203. }
  204. if (this.reqTraffic == null) {
  205. this.reqTraffic = new VdsReqTraffic((short)this.GROUP_NO, (short)this.VDS_CTLR_LOCAL_NO);
  206. this.reqTraffic.makeCRC();
  207. }
  208. }
  209. /**
  210. * Channel Send Data
  211. * @param sendBuffer
  212. * @param delayMilliSeconds
  213. * @param packetDesc
  214. * @return
  215. */
  216. public boolean sendData(ByteBuffer sendBuffer, int delayMilliSeconds, String packetDesc) {
  217. boolean result = false;
  218. if (this.channel != null) {
  219. ChannelFuture f = this.channel.writeAndFlush(sendBuffer);
  220. f.awaitUninterruptibly();
  221. if (f.isDone() || f.isSuccess()) {
  222. result = true;
  223. //log.info("[{}]. sendData: OK. {}, {} Bytes.", this.VDS_CTLR_ID, packetDesc, sendBuffer.array().length);
  224. if (delayMilliSeconds > 0) {
  225. VdsProtocol.sleep(delayMilliSeconds);
  226. }
  227. } else {
  228. log.error("[{}]. sendData: Failed. {}, {} Bytes.", this.VDS_CTLR_ID, packetDesc, sendBuffer.array().length);
  229. }
  230. } else {
  231. log.error("[{}]. sendData: Failed. Not Connected. {}, {} Bytes.", this.VDS_CTLR_ID, packetDesc, sendBuffer.array().length);
  232. }
  233. return result;
  234. }
  235. public voVdsState getVdsStts() {
  236. voVdsState ss = new voVdsState();
  237. ss.setCTLR_NMBR(getVDS_CTLR_NMBR());
  238. ss.setComm(getStts().getSttsComm()); //Comm
  239. ss.setVideoInput(getStts().getSttsVideoInput()); //VideoInput
  240. ss.setHeater(getStts().getSttsHeater()); //Heater
  241. ss.setFan(getStts().getSttsFan()); //Fan
  242. ss.setBackDoorOpen(getStts().getSttsBackDoor()); //BackDoorOpen
  243. ss.setFrontDoorOpen(getStts().getSttsFrontDoor()); //FrontDoorOpen
  244. ss.setTemperature(getStts().getSttsTemp()); //Temperature
  245. ss.setFanMode(getStts().getSttsFanMode()); //FanMode
  246. ss.setFanRunTemp(getStts().getSttsFanRunTemp()); //FanRunTemp
  247. ss.setHetrMode(getStts().getSttsHetrMode()); //HetrMode
  248. ss.setHetrRunTemp(getStts().getSttsHetrRunTemp()); //HetrRunTemp
  249. return ss;
  250. }
  251. public boolean channelClose() {
  252. if (getChannel() == null || getNetState() == NET.CLOSED) {
  253. log.error("Close Request: channel not connected: [{}]", this);
  254. return false;
  255. }
  256. VdsTcpClientIdleHandler.disconnectChannel(getChannel());
  257. return true;
  258. }
  259. public boolean reset() {
  260. if (getChannel() == null || getNetState() == NET.CLOSED) {
  261. log.error("Reset Request: channel not connected: [{}]", this);
  262. return false;
  263. }
  264. VdsReqReset vdsReq = new VdsReqReset((short)getGROUP_NO(), (short)getVDS_CTLR_LOCAL_NO());
  265. vdsReq.makeCRC();
  266. ByteBuffer sendBuffer = vdsReq.getByteBuffer();
  267. if (!sendData(sendBuffer, 0, "vds_Reset")) {
  268. log.error("Reset Data Send Failed: [{}]", this);
  269. return false;
  270. } else {
  271. log.error("Reset Data Send Failed: [{}]", this);
  272. return true;
  273. }
  274. }
  275. public boolean initialize() {
  276. if (getChannel() == null || getNetState() == NET.CLOSED) {
  277. log.error("Initialize Request: channel not connected: [{}]", this);
  278. return false;
  279. }
  280. VdsReqInitialize vdsReq = new VdsReqInitialize((short)getGROUP_NO(), (short)getVDS_CTLR_LOCAL_NO());
  281. vdsReq.makeCRC();
  282. ByteBuffer sendBuffer = vdsReq.getByteBuffer();
  283. if (!sendData(sendBuffer, 0, "vds_Initialize")) {
  284. log.error("Initialize Data Send Failed: [{}]", this);
  285. return false;
  286. } else {
  287. log.error("Initialize Data Send Failed: [{}]", this);
  288. return true;
  289. }
  290. }
  291. public boolean stopImage(byte cameraNo) {
  292. if (getChannel() == null || getNetState() == NET.CLOSED) {
  293. log.error("StopImage Request: channel not connected: [{}]", this);
  294. return false;
  295. }
  296. int frameNo = 0; // first image request
  297. setStopImageRequest(getChannel(), cameraNo, frameNo);
  298. VdsReqImage reqImage = new VdsReqImage((short)getGROUP_NO(), (short)getVDS_CTLR_LOCAL_NO());
  299. reqImage.setCameraNo(cameraNo);
  300. reqImage.setFrameNo(frameNo);
  301. reqImage.makeCRC();
  302. ByteBuffer sendBuffer = reqImage.getByteBuffer();
  303. if (!sendData(sendBuffer, 0, "vds_Image")) {
  304. setStopImageResponse();
  305. log.error("StopImage Data Send Failed: [{}]", this);
  306. return false;
  307. } else {
  308. log.error("StopImage Data Send Failed: [{}]", this);
  309. return true;
  310. }
  311. }
  312. }