| | |
| | | package com.zy.core.thread; |
| | | |
| | | import HslCommunication.Core.Types.OperateResult; |
| | | import HslCommunication.Core.Types.OperateResultExOne; |
| | | import HslCommunication.Profinet.Siemens.SiemensPLCS; |
| | | import HslCommunication.Profinet.Siemens.SiemensS7Net; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.core.common.DateUtils; |
| | | import com.core.common.SpringUtils; |
| | | import com.core.exception.CoolException; |
| | | import com.zy.core.ThreadHandler; |
| | | import com.zy.core.cache.MessageQueue; |
| | | import com.zy.core.cache.OutputQueue; |
| | | import com.zy.core.enums.SlaveType; |
| | | import com.zy.core.model.SocketSlave; |
| | | import com.zy.core.model.Task; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.text.MessageFormat; |
| | | import java.util.Date; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.net.InetAddress; |
| | | import java.net.ServerSocket; |
| | | import java.net.Socket; |
| | | |
| | | /** |
| | | * shuttle 穿梭车线程 |
| | | * Created by vincent on 2020/8/4 |
| | | * 不能有 槽号 和 机架号 |
| | | * 消防socket线程 |
| | | * Created on 2022/7/30 |
| | | */ |
| | | @Data |
| | | @Slf4j |
| | | public class SocketThread implements Runnable, ThreadHandler { |
| | | |
| | | private SiemensS7Net siemensS7Net; |
| | | private SocketSlave slave; |
| | | private SteProtocol steProtocol; |
| | | private short heartBeatVal = 1; |
| | | private boolean resetFlag = false; |
| | | // private String barcode; |
| | | // private Socket socket; |
| | | // private DataOutputStream dataOutputStream; |
| | | // private DataInputStream dataInputStream; |
| | | |
| | | private Integer lastRow; |
| | | private Integer lastBay; |
| | | private Integer lastLev; |
| | | private byte[] byteData = new byte[1024]; |
| | | |
| | | public SocketThread(SocketSlave slave) { |
| | | this.slave = slave; |
| | |
| | | @Override |
| | | @SuppressWarnings("InfiniteLoopStatement") |
| | | public void run() { |
| | | this.connect(); |
| | | // this.connect(); |
| | | while (true) { |
| | | try { |
| | | int step = 1; |
| | | Task task = MessageQueue.poll(SlaveType.Socket, slave.getId()); |
| | | if (task != null) { |
| | | step = task.getStep(); |
| | | } |
| | | switch (step) { |
| | | // 读数据 |
| | | case 1: |
| | | // readStatus(); |
| | | break; |
| | | // 写入数据 |
| | | case 2: |
| | | // write((StaProtocol)task.getData()); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | // 心跳 |
| | | // heartbeat(); |
| | | Thread.sleep(500); |
| | | // byte[] read = read(8, 200); |
| | | // if (null != read) { |
| | | // String s = new String(read); |
| | | //// if (!Cools.isEmpty(s)) { |
| | | //// barcode = new String(read); |
| | | //// log.info("{}号条码器,检索数据:{}", slave.getId(), this.barcode); |
| | | //// JSONObject jsonObject = new JSONObject(); |
| | | //// jsonObject.put("time", DateUtils.convert(new Date(), DateUtils.yyyyMMddHHmmss_F)); |
| | | //// jsonObject.put("barcode", barcode); |
| | | //// if (OutputQueue.BARCODE.size() >= 32) { |
| | | //// OutputQueue.BARCODE.poll(); |
| | | //// } |
| | | //// OutputQueue.BARCODE.offer(jsonObject); |
| | | //// } |
| | | // } |
| | | // Thread.sleep(50); |
| | | // } catch (SocketTimeoutException ignore) { |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | // e.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | public byte[] getByteData() { |
| | | return byteData; |
| | | } |
| | | |
| | | public void setByteData(byte[] byteData) { |
| | | this.byteData = byteData; |
| | | } |
| | | |
| | | @Override |
| | | public boolean connect() { |
| | | boolean result = false; |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 读取状态 |
| | | */ |
| | | private void readStatus(){ |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 写入数据 |
| | | */ |
| | | private boolean write(){ |
| | | |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | | public void close() { |
| | | // try { |
| | | // if (null != dataOutputStream) { |
| | | // dataOutputStream.close(); |
| | | // } |
| | | // if (null != dataInputStream) { |
| | | // dataInputStream.close(); |
| | | // } |
| | | //// if (null != socket && !socket.isClosed()) { |
| | | //// socket.close(); |
| | | //// } |
| | | // if (null != socket){ |
| | | // socket.close(); |
| | | // } |
| | | // socket = null; |
| | | // } catch (IOException e) { |
| | | // log.error("SocketClient close Exception:" + e.getMessage()); |
| | | // } |
| | | } |
| | | |
| | | /** |
| | | * 心跳 |
| | | */ |
| | | private void heartbeat(){ |
| | | // public void write(byte[] msg, int len) throws IOException { |
| | | // if (null != dataInputStream) |
| | | // { |
| | | // dataOutputStream.write(msg, 0, len); |
| | | // dataOutputStream.flush(); |
| | | // } |
| | | // } |
| | | // |
| | | // public byte[] read(int bufferSize, int timeOut) throws IOException { |
| | | // if (socket == null || !socket.isConnected() || socket.isClosed()) { |
| | | // connect(); |
| | | // } |
| | | //// connect(); |
| | | // socket.setSoTimeout(timeOut); |
| | | // byte[] bytes = new byte[bufferSize]; |
| | | // int len = dataInputStream.read(bytes); |
| | | // byte[] tempBytes = null; |
| | | // if (len > 0) { |
| | | // tempBytes = new byte[len]; |
| | | // System.arraycopy(bytes, 0, tempBytes, 0, len); |
| | | // } else { |
| | | // connect(); |
| | | // } |
| | | // return tempBytes; |
| | | // } |
| | | |
| | | } |
| | | |
| | | // public boolean valid() throws Exception { |
| | | // if (null == socket || socket.isClosed() || socket.isInputShutdown() || socket.isOutputShutdown()) { |
| | | // if (dataInputStream != null) { |
| | | // dataInputStream.close(); |
| | | // } |
| | | // if (dataOutputStream != null) { |
| | | // dataOutputStream.close(); |
| | | // } |
| | | // if (socket != null) { |
| | | // socket.close(); |
| | | // } |
| | | // return false; |
| | | // } |
| | | // return true; |
| | | // } |
| | | |
| | | /******************************************************************************************/ |
| | | /**************************************** 测试专用 *****************************************/ |
| | | /*****************************************************************************************/ |
| | | public static void main(String[] args) throws InterruptedException { |
| | | try{ |
| | | ServerSocket socket = new ServerSocket(8802); |
| | | System.out.println("套接字创建成功,等待连接..."); |
| | | |
| | | while (true){ |
| | | final Socket con = socket.accept(); |
| | | InetAddress addr = con.getInetAddress(); |
| | | System.out.println("客户端接入===>>" + addr.getHostAddress() + ":" + con.getPort()); |
| | | |
| | | new Thread((new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | InputStream in; |
| | | |
| | | while (true){ |
| | | try{ |
| | | in = con.getInputStream(); |
| | | int len = 0; |
| | | byte[] data = new byte[128]; |
| | | len = in.read(data); |
| | | if(len>0) { |
| | | System.out.println("读到消息===>>" + new String(data, 0, len)); |
| | | } else { |
| | | // System.out.println("客户端断开===>>"); |
| | | } |
| | | }catch (IOException e){ |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | })).start(); |
| | | } |
| | | }catch (IOException e){ |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | } |