123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package com.gzlh.bus.thread;
- import cn.hutool.core.util.StrUtil;
- import cn.hutool.core.util.XmlUtil;
- import cn.hutool.extra.spring.SpringUtil;
- import cn.hutool.json.JSONUtil;
- import com.gzlh.bus.ChannelCacheManager;
- import com.gzlh.bus.EventBus;
- import com.gzlh.bus.EventDataManager;
- import com.gzlh.bus.SysConfig;
- import com.gzlh.config.ModuleEnum;
- import com.gzlh.config.SystemObject;
- import com.gzlh.config.dto.ChannelSetting;
- import com.gzlh.config.parser81.Xml81Data;
- import com.gzlh.config.parser81.XmlPlaceholderFiller;
- import com.gzlh.device.led.utils.LedOptions;
- import com.gzlh.device.plc.event.PLCEvent;
- import com.gzlh.entity.ReqBO;
- import lombok.extern.slf4j.Slf4j;
- import org.w3c.dom.Document;
- import javax.xml.xpath.XPathConstants;
- import java.io.*;
- import java.net.Socket;
- import java.util.Date;
- @Slf4j
- public class Sub81Thread implements Runnable {
- private static final String ENCODING = "GB2312";
- private static final String END_TAG = "</GATHER_FEEDBACK>";
- @Override
- public void run() {
- ReqBO reqBO = EventDataManager.getCacheData();
- if (!ChannelCacheManager.stateEnable(SysConfig.channelSetting.getChannelCode())) {
- log.error("通道已停用:{}", JSONUtil.toJsonStr(reqBO));
- return;
- }
- ChannelSetting channelSetting = SysConfig.channelSetting;
- Xml81Data xml81Data = new Xml81Data();
- String img = reqBO.getCarImg();
- ReqBO.Box box = reqBO.getBox();
- if (box != null) {
- String boxFImg = box.getBoxFImg();
- img = img + "," + boxFImg;
- String boxBImg = box.getBoxBImg();
- if (StrUtil.isNotEmpty(boxBImg)) {
- img = img + "," + boxBImg;
- }
- }
- LedOptions ledOptions = new LedOptions();
- ledOptions.setLine("02");
- xml81Data.setOPERATE_TIME(new Date())
- .setCHNL_NO(channelSetting.getChannelCode())
- .setI_E_FLAG(channelSetting.getIeFlag())
- .setAREA_ID(channelSetting.getPlaceCode())
- .setVE_LICENSE_NO(reqBO.getCarNo())
- .setGROSS_WT(reqBO.getWeight() + "")
- .setRFID_ID(reqBO.getEri())
- .setPHOTO_PERSPECTIVE(img);
- try (Socket socket = new Socket(SysConfig.socketSetting.getHost(), SysConfig.socketSetting.getPort());
- OutputStream os = socket.getOutputStream();
- OutputStreamWriter osw = new OutputStreamWriter(os, ENCODING);
- BufferedWriter bw = new BufferedWriter(osw);
- InputStream is = socket.getInputStream();
- InputStreamReader isr = new InputStreamReader(is, ENCODING);
- BufferedReader br = new BufferedReader(isr)) {
- socket.setSoTimeout(10000);
- String xmlContent = XmlPlaceholderFiller.fillXmlFromTemplate("81.xml", xml81Data);
- // 发送XML
- bw.write(xmlContent);
- bw.newLine(); // 确保换行符,与服务器协议匹配
- bw.flush();
- log.info("已发送XML到服务器:\n{}", xmlContent);
- ByteArrayOutputStream responseBuffer = new ByteArrayOutputStream();
- byte[] buffer = new byte[4096];
- int bytesRead;
- // 接收响应
- // 持续读取数据直到超时或结束
- while ((bytesRead = is.read(buffer)) != -1) {
- responseBuffer.write(buffer, 0, bytesRead);
- // 检查是否包含结束标签
- String responseStr = responseBuffer.toString(ENCODING);
- log.info(responseStr);
- if (responseStr.contains(END_TAG)) {
- break;
- }
- }
- String response = responseBuffer.toString(ENCODING);
- Document document = XmlUtil.parseXml(response);
- String SESSION_ID_DATA = xml81Data.getSESSION_ID();
- String sessionId = StrUtil.toString(XmlUtil.getByXPath("//GATHER_FEEDBACK//SESSION_ID", document, XPathConstants.STRING));
- if (!StrUtil.equals(sessionId, SESSION_ID_DATA)) {
- log.error("-----异常会话-----");
- return;
- }
- String checkResult = StrUtil.toString(XmlUtil.getByXPath("//GATHER_FEEDBACK//CHECK_RESULT", document, XPathConstants.STRING));
- String ledMsg = StrUtil.toString(XmlUtil.getByXPath("//GATHER_FEEDBACK//LED_HINT", document, XPathConstants.STRING));
- SystemObject.ledFactory.handler(SysConfig.serialSetting.getLed().getBrand()).sendMsg(ledMsg, ledOptions);
- if (StrUtil.equalsIgnoreCase(checkResult, "Y")) {
- EventBus eventBus = SpringUtil.getBean(EventBus.class);
- eventBus.startEvent(ModuleEnum.PLC_MODULE.getModuleEn() + "." + PLCEvent.RAILING_RISE);
- }
- } catch (Exception e) {
- log.error("send msg error:{}", e.getMessage());
- SystemObject.ledFactory.handler(SysConfig.serialSetting.getLed().getBrand()).sendMsg(e.getMessage(), ledOptions);
- e.printStackTrace();
- }
- }
- }
|