| | |
| | | |
| | | import java.io.*; |
| | | import java.net.InetAddress; |
| | | import java.net.ServerSocket; |
| | | import java.net.Socket; |
| | | import java.text.MessageFormat; |
| | | import java.util.*; |
| | |
| | | private DeviceConfig deviceConfig; |
| | | private RedisUtil redisUtil; |
| | | private Socket socket; |
| | | private ServerSocket serverSocket; |
| | | |
| | | private boolean fake = false; |
| | | private boolean stopThread = false; |
| | | |
| | | public NyShuttleThread(DeviceConfig deviceConfig, RedisUtil redisUtil) { |
| | | this.deviceConfig = deviceConfig; |
| | | this.redisUtil = redisUtil; |
| | | this.fake = deviceConfig.getFake(); |
| | | } |
| | | |
| | | @Override |
| | | public void run() { |
| | | News.info("{}号四向车线程启动", deviceConfig.getDeviceNo()); |
| | | this.connect(); |
| | | |
| | | //监听消息 |
| | | Thread innerThread = new Thread(() -> { |
| | |
| | | break; |
| | | } |
| | | |
| | | System.out.println("read"); |
| | | |
| | | try { |
| | | this.connect(); |
| | | Thread.sleep(200); |
| | | listenSocketMessage(); |
| | | } catch (Exception e) { |
| | |
| | | break; |
| | | } |
| | | |
| | | System.out.println("executeThread"); |
| | | try { |
| | | DeviceMsgUtils deviceMsgUtils = null; |
| | | try { |
| | |
| | | } |
| | | }); |
| | | executeThread.start(); |
| | | |
| | | if (this.fake) { |
| | | Thread fakeThread = new Thread(() -> { |
| | | try { |
| | | serverSocket = new ServerSocket(deviceConfig.getPort()); |
| | | |
| | | while (true) { |
| | | if(stopThread) { |
| | | break; |
| | | } |
| | | |
| | | System.out.println("fakeThread"); |
| | | Socket accept = serverSocket.accept(); |
| | | handleClient(accept); |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | }); |
| | | fakeThread.start(); |
| | | } |
| | | } |
| | | |
| | | private void handleClient(Socket socket) throws IOException { |
| | | InputStream inputStream = socket.getInputStream(); |
| | | OutputStream outputStream = socket.getOutputStream(); |
| | | outputStream.write("test".getBytes()); |
| | | outputStream.flush(); |
| | | socket.close(); |
| | | } |
| | | |
| | | private void executeCommand(DeviceCommandMsgModel deviceCommandMsg) { |
| | |
| | | DeviceMsgUtils deviceMsgUtils = null; |
| | | try { |
| | | deviceMsgUtils = SpringUtils.getBean(DeviceMsgUtils.class); |
| | | }catch (Exception e){ |
| | | } catch (Exception e) { |
| | | } |
| | | if(deviceMsgUtils == null) { |
| | | if (deviceMsgUtils == null) { |
| | | return; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | JSONObject result = JSON.parseObject(sb.toString());//得到响应结果集 |
| | | log.info("收到Server Data: {}", JSON.toJSONString(result)); |
| | | |
| | | String msgType = result.getString("msgType"); |
| | | if ("responseMsg".equals(msgType)) { |
| | |
| | | deviceMsgModel.setDeviceOriginMsg(sb.toString()); |
| | | deviceMsgUtils.sendDeviceMsg(SlaveType.Shuttle, deviceConfig.getDeviceNo(), deviceMsgModel); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | // e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | |
| | | @Override |
| | | public boolean connect() { |
| | | try { |
| | | if(this.socket != null) { |
| | | return true; |
| | | } |
| | | |
| | | InetAddress address = InetAddress.getByName(deviceConfig.getIp()); |
| | | if (address.isReachable(10000)) { |
| | | Socket socket = new Socket(deviceConfig.getIp(), deviceConfig.getPort()); |
| | |
| | | this.stopThread = true; |
| | | } |
| | | |
| | | @Override |
| | | public boolean isFake() { |
| | | return this.fake; |
| | | } |
| | | } |