AciService.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package com.ruoyi.ams.agv.ndc;
  2. import cn.hutool.core.date.DateUnit;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.thread.ThreadUtil;
  5. import com.alibaba.fastjson.JSON;
  6. import com.ruoyi.ams.agv.ndc.common.Aci;
  7. import com.ruoyi.ams.agv.ndc.common.ByteUtil;
  8. import com.ruoyi.ams.agv.ndc.domain.AmsHexdefineDetail;
  9. import com.ruoyi.ams.agv.ndc.domain.AmsTask;
  10. import com.ruoyi.ams.agv.ndc.entity.Order;
  11. import com.ruoyi.ams.agv.ndc.service.IAmsHexdefineDetailService;
  12. import com.ruoyi.ams.agv.ndc.service.IAmsTaskService;
  13. import com.ruoyi.ams.agv.ndc.service.SocketBufferService;
  14. import com.ruoyi.base.constant.Constant;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.stereotype.Service;
  18. import java.io.IOException;
  19. import java.net.InetSocketAddress;
  20. import java.net.SocketAddress;
  21. import java.nio.ByteBuffer;
  22. import java.nio.channels.SocketChannel;
  23. import java.util.Date;
  24. import java.util.List;
  25. /**
  26. * Created by IntelliJ IDEA.
  27. * User: andy.qu
  28. * Date: 2022/8/5
  29. */
  30. @Slf4j
  31. @Service
  32. public class AciService {
  33. private static final int TIMEOUT = 3000;
  34. private static final int BUFF_SIZE = 2048;
  35. private static final String IP_ADDR = "10.95.2.66";
  36. private static final int IP_PORT = 50001;
  37. @Autowired
  38. private SocketBufferService socketBufferService;
  39. @Autowired
  40. private IAmsTaskService amsTaskService;
  41. @Autowired
  42. private IAmsHexdefineDetailService amsHexdefineDetailService;
  43. @SuppressWarnings("InfiniteLoopStatement")
  44. public void start() throws IOException {
  45. String ip_addr = IP_ADDR; //redisUtils.get(RedisEnum.NDC_IP.getMessage());
  46. int ip_port = IP_PORT; //redisUtils.getInt(RedisEnum.NDC_PORT.getMessage());
  47. SocketChannel socketChannel = SocketChannel.open();
  48. socketChannel.configureBlocking(false); // 解决了IO阻塞
  49. while (true) {
  50. try {
  51. //链接服务端Socket
  52. if (!socketChannel.isConnected()) {
  53. if (!socketChannel.isOpen()) {
  54. socketChannel = SocketChannel.open();
  55. socketChannel.configureBlocking(false); // 解决了IO阻塞
  56. }
  57. // 非阻塞模式下 这段代码并不是等到连接建立之后再往下执行
  58. SocketAddress socketAddress = new InetSocketAddress(ip_addr, ip_port);
  59. socketChannel.connect(socketAddress);
  60. Thread.sleep(2000);
  61. if (socketChannel.isConnectionPending()) {
  62. socketChannel.finishConnect();
  63. }
  64. }
  65. ByteBuffer buffer = ByteBuffer.allocate(BUFF_SIZE);
  66. buffer.clear();
  67. //删除指令
  68. AmsTask taskBean = new AmsTask();
  69. taskBean.setIsDelete(1);
  70. taskBean.setAciAccept(0);
  71. taskBean = amsTaskService.selectAmsTaskByModel(taskBean);
  72. String s = "87CD000800060001006E0002%04X";
  73. if (taskBean != null) {
  74. if (taskBean.getAciIndex() == null) {
  75. //还没有提交给aci系统
  76. taskBean.setAciAccept(1);
  77. amsTaskService.updateAmsTask(taskBean);
  78. } else {
  79. //已经提交给aci系统
  80. s = String.format(s, taskBean.getAciIndex());
  81. buffer.put(ByteUtil.hexString2ByteArray(s));
  82. buffer.flip();
  83. socketChannel.write(buffer);
  84. buffer.clear();
  85. log.info("cancelOrder: " + s);
  86. }
  87. if (DateUtil.between(taskBean.getAddtime(), new Date(), DateUnit.MINUTE) > 1) {
  88. taskBean.setAciAccept(2);
  89. taskBean.setRemark("下发超时1分钟未接收自动取消");
  90. amsTaskService.updateAmsTask(taskBean);
  91. }
  92. }
  93. //充电指令
  94. taskBean = new AmsTask();
  95. taskBean.setIsDelete(0);
  96. taskBean.setAciAccept(0);
  97. taskBean.setBusinessType(Constant.TASK_BUSINESS_TYPE.TASK_POWER.getValue());
  98. taskBean = amsTaskService.selectAmsTaskByModel(taskBean);
  99. s = "87CD0008000C00010071000663010001%04X";
  100. if (taskBean != null) {
  101. s = String.format(s, Integer.parseInt(taskBean.getDeviceName()));
  102. buffer.put(ByteUtil.hexString2ByteArray(s));
  103. buffer.flip();
  104. socketChannel.write(buffer);
  105. buffer.clear();
  106. log.info("AGV-Power: " + s);
  107. taskBean.setServerAck(0);
  108. taskBean.setAciAccept(1);
  109. taskBean.setIsDelete(null);
  110. amsTaskService.updateAmsTask(taskBean);
  111. }
  112. //添加指令
  113. List<AmsTask> taskBeans = amsTaskService.selectTaskNotIssued();
  114. if (taskBeans != null && taskBeans.size() > 0) {
  115. for (AmsTask t : taskBeans) {
  116. t.init();
  117. List<AmsHexdefineDetail> basTask = amsHexdefineDetailService.selectAmsHexdefineDetailByBusinessType(t.getBusinessType());
  118. if (basTask == null || basTask.size() == 0) {
  119. t.setIsDelete(1);
  120. t.setRemark("该任务类型未定义");
  121. log.info("任务类型未定义------" + JSON.toJSONString(t));
  122. continue;
  123. }
  124. // try {
  125. String order = amsHexdefineDetailService.concatOrder(t, basTask);
  126. log.info("writeOrder:" + order);
  127. if (t.getBusinessType().equals("01")) {
  128. buffer.put(ByteUtil.hexString2ByteArray(order));
  129. buffer.flip();
  130. socketChannel.write(buffer);
  131. buffer.clear();
  132. if (DateUtil.between(t.getAddtime(), new Date(), DateUnit.MINUTE) > 2) {
  133. t.setAciAccept(2);
  134. t.setRoadway("下发超时2分钟未接收自动取消");
  135. amsTaskService.updateAmsTask(t);
  136. }
  137. } else {
  138. buffer.put(ByteUtil.hexString2ByteArray(order));
  139. buffer.flip();
  140. socketChannel.write(buffer);
  141. buffer.clear();
  142. // 修改已发送
  143. t.setAciAccept(1);
  144. t.setRoadway("TS请求反馈任务发送成功!");
  145. amsTaskService.updateAmsTask(t);
  146. }
  147. // } catch (Exception e) {
  148. // e.printStackTrace();
  149. // t.setServerAck(0);
  150. // t.setAciAccept(1);
  151. // t.setRoadway("程序异常自动ack");
  152. // t.setIsDelete(null);
  153. // amsTaskService.updateAmsTask(t);
  154. // }
  155. }
  156. }
  157. //从服务端读取
  158. buffer.clear();
  159. //因为设置了异步 这边不会阻塞 测试了一下 服务器断开这边会抛出异常 但是写的时候不会抛出异常 可能是设置了异步的原因
  160. int readLenth = socketChannel.read(buffer);
  161. if (readLenth == -1) {
  162. // throw new IOException();
  163. }
  164. if (readLenth > 0) {
  165. buffer.flip();
  166. byte[] bytes = new byte[readLenth];
  167. buffer.get(bytes);
  168. List<Order> bufferSeparators = socketBufferService.bufferSeparator(bytes);
  169. for (Order order : bufferSeparators) {
  170. // log.info("消息类型:" + order.getMessageType() + ",消息参数:" + ByteUtil.byteArray2HexString(order.getParameters()));
  171. switch (order.getMessageType()) {
  172. case Aci.MSG_ORDER_STATUS://车体状态信息、位置信息
  173. socketBufferService.handleMsgOrderStatus(order.getParameters());
  174. break;
  175. case Aci.MSG_EVENT://任务反馈、自定义消息
  176. socketBufferService.handleMsgEvent(order.getParameters());
  177. break;
  178. case Aci.MSG_STOP://交通管制暂停
  179. socketBufferService.handleMsgStopStart(order.getParameters(), true);
  180. break;
  181. case Aci.MSG_START://启动信息
  182. socketBufferService.handleMsgStopStart(order.getParameters(), false);
  183. break;
  184. case Aci.MSG_ORDER_ACK://信息接收反馈
  185. socketBufferService.handleMsgOrderAck(order.getParameters());
  186. break;
  187. }
  188. }
  189. }
  190. buffer.clear();
  191. try {
  192. Thread.sleep(500);
  193. } catch (InterruptedException e) {
  194. e.printStackTrace();
  195. }
  196. } catch (IOException i) {
  197. log.error("NDC通讯异常!", i);
  198. try {
  199. socketChannel.close();
  200. } catch (Exception exception) {
  201. i.printStackTrace();
  202. }
  203. } catch (Exception e) {
  204. e.printStackTrace();
  205. try {
  206. socketChannel.close();
  207. } catch (Exception exception) {
  208. e.printStackTrace();
  209. }
  210. }
  211. }
  212. }
  213. }