Sub81Thread.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package com.gzlh.bus.thread;
  2. import cn.hutool.core.util.StrUtil;
  3. import cn.hutool.core.util.XmlUtil;
  4. import cn.hutool.extra.spring.SpringUtil;
  5. import cn.hutool.json.JSONUtil;
  6. import com.gzlh.bus.ChannelCacheManager;
  7. import com.gzlh.bus.EventBus;
  8. import com.gzlh.bus.EventDataManager;
  9. import com.gzlh.bus.SysConfig;
  10. import com.gzlh.config.ModuleEnum;
  11. import com.gzlh.config.SystemObject;
  12. import com.gzlh.config.dto.ChannelSetting;
  13. import com.gzlh.config.parser81.Xml81Data;
  14. import com.gzlh.config.parser81.XmlPlaceholderFiller;
  15. import com.gzlh.device.led.utils.LedOptions;
  16. import com.gzlh.device.plc.event.PLCEvent;
  17. import com.gzlh.entity.ReqBO;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.w3c.dom.Document;
  20. import javax.xml.xpath.XPathConstants;
  21. import java.io.*;
  22. import java.net.Socket;
  23. import java.util.Date;
  24. @Slf4j
  25. public class Sub81Thread implements Runnable {
  26. private static final String ENCODING = "GB2312";
  27. private static final String END_TAG = "</GATHER_FEEDBACK>";
  28. @Override
  29. public void run() {
  30. ReqBO reqBO = EventDataManager.getCacheData();
  31. if (!ChannelCacheManager.stateEnable(SysConfig.channelSetting.getChannelCode())) {
  32. log.error("通道已停用:{}", JSONUtil.toJsonStr(reqBO));
  33. return;
  34. }
  35. ChannelSetting channelSetting = SysConfig.channelSetting;
  36. Xml81Data xml81Data = new Xml81Data();
  37. String img = reqBO.getCarImg();
  38. ReqBO.Box box = reqBO.getBox();
  39. if (box != null) {
  40. String boxFImg = box.getBoxFImg();
  41. img = img + "," + boxFImg;
  42. String boxBImg = box.getBoxBImg();
  43. if (StrUtil.isNotEmpty(boxBImg)) {
  44. img = img + "," + boxBImg;
  45. }
  46. }
  47. LedOptions ledOptions = new LedOptions();
  48. ledOptions.setLine("02");
  49. xml81Data.setOPERATE_TIME(new Date())
  50. .setCHNL_NO(channelSetting.getChannelCode())
  51. .setI_E_FLAG(channelSetting.getIeFlag())
  52. .setAREA_ID(channelSetting.getPlaceCode())
  53. .setVE_LICENSE_NO(reqBO.getCarNo())
  54. .setGROSS_WT(reqBO.getWeight() + "")
  55. .setRFID_ID(reqBO.getEri())
  56. .setPHOTO_PERSPECTIVE(img);
  57. try (Socket socket = new Socket(SysConfig.socketSetting.getHost(), SysConfig.socketSetting.getPort());
  58. OutputStream os = socket.getOutputStream();
  59. OutputStreamWriter osw = new OutputStreamWriter(os, ENCODING);
  60. BufferedWriter bw = new BufferedWriter(osw);
  61. InputStream is = socket.getInputStream();
  62. InputStreamReader isr = new InputStreamReader(is, ENCODING);
  63. BufferedReader br = new BufferedReader(isr)) {
  64. socket.setSoTimeout(10000);
  65. String xmlContent = XmlPlaceholderFiller.fillXmlFromTemplate("81.xml", xml81Data);
  66. // 发送XML
  67. bw.write(xmlContent);
  68. bw.newLine(); // 确保换行符,与服务器协议匹配
  69. bw.flush();
  70. log.info("已发送XML到服务器:\n{}", xmlContent);
  71. ByteArrayOutputStream responseBuffer = new ByteArrayOutputStream();
  72. byte[] buffer = new byte[4096];
  73. int bytesRead;
  74. // 接收响应
  75. // 持续读取数据直到超时或结束
  76. while ((bytesRead = is.read(buffer)) != -1) {
  77. responseBuffer.write(buffer, 0, bytesRead);
  78. // 检查是否包含结束标签
  79. String responseStr = responseBuffer.toString(ENCODING);
  80. log.info(responseStr);
  81. if (responseStr.contains(END_TAG)) {
  82. break;
  83. }
  84. }
  85. String response = responseBuffer.toString(ENCODING);
  86. Document document = XmlUtil.parseXml(response);
  87. String SESSION_ID_DATA = xml81Data.getSESSION_ID();
  88. String sessionId = StrUtil.toString(XmlUtil.getByXPath("//GATHER_FEEDBACK//SESSION_ID", document, XPathConstants.STRING));
  89. if (!StrUtil.equals(sessionId, SESSION_ID_DATA)) {
  90. log.error("-----异常会话-----");
  91. return;
  92. }
  93. String checkResult = StrUtil.toString(XmlUtil.getByXPath("//GATHER_FEEDBACK//CHECK_RESULT", document, XPathConstants.STRING));
  94. String ledMsg = StrUtil.toString(XmlUtil.getByXPath("//GATHER_FEEDBACK//LED_HINT", document, XPathConstants.STRING));
  95. SystemObject.ledFactory.handler(SysConfig.serialSetting.getLed().getBrand()).sendMsg(ledMsg, ledOptions);
  96. if (StrUtil.equalsIgnoreCase(checkResult, "Y")) {
  97. EventBus eventBus = SpringUtil.getBean(EventBus.class);
  98. eventBus.startEvent(ModuleEnum.PLC_MODULE.getModuleEn() + "." + PLCEvent.RAILING_RISE);
  99. }
  100. } catch (Exception e) {
  101. log.error("send msg error:{}", e.getMessage());
  102. SystemObject.ledFactory.handler(SysConfig.serialSetting.getLed().getBrand()).sendMsg(e.getMessage(), ledOptions);
  103. e.printStackTrace();
  104. }
  105. }
  106. }