| | |
| | | |
| | | 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; |
| | | private HashMap<Integer, String> resultKeyMap = new HashMap<Integer, String>(); |
| | | |
| | | 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) { |
| | |
| | | return; |
| | | } |
| | | |
| | | Object command = deviceCommandMsg.getCommand(); |
| | | String command = JSON.toJSONString(deviceCommandMsg.getCommand()); |
| | | JSONObject commandObj = JSON.parseObject(command); |
| | | JSONObject request = commandObj.getJSONObject("request"); |
| | | JSONObject header = request.getJSONObject("header"); |
| | | Integer requestId = header.getInteger("requestId"); |
| | | resultKeyMap.put(requestId, deviceCommandMsg.getResultKey()); |
| | | |
| | | // 获取输出流 |
| | | OutputStreamWriter writer = new OutputStreamWriter(this.socket.getOutputStream()); |
| | | writer.write(JSON.toJSONString(command) + "\r\n"); |
| | | writer.write(command + "\r\n"); |
| | | writer.flush(); |
| | | // System.out.println("Sent message to server: " + JSON.toJSONString(httpCommand)); |
| | | }catch (Exception e) { |
| | |
| | | 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());//得到响应结果集 |
| | | JSONObject response = result.getJSONObject("response"); |
| | | JSONObject header = response.getJSONObject("header"); |
| | | JSONObject body = response.getJSONObject("body"); |
| | | |
| | | String msgType = result.getString("msgType"); |
| | | |
| | | // log.info("收到Server Data: {}", JSON.toJSONString(result)); |
| | | if ("responseMsg".equals(msgType)) { |
| | | JSONObject response = result.getJSONObject("response"); |
| | | JSONObject body = response.getJSONObject("body"); |
| | | if (body.containsKey("workingMode")) { |
| | | Integer responseId = header.getInteger("responseId"); |
| | | String resultKey = resultKeyMap.get(responseId); |
| | | |
| | | String responseType = body.getString("responseType"); |
| | | if (responseType.equals("state")) { |
| | | //read |
| | | JSONObject data = parseSocketResult(body); |
| | | |
| | |
| | | deviceMsgModel.setDeviceMsgType("status"); |
| | | deviceMsgModel.setDeviceMsg(data); |
| | | deviceMsgModel.setDeviceOriginMsg(sb.toString()); |
| | | deviceMsgModel.setResultKey(resultKey); |
| | | deviceMsgUtils.sendDeviceMsg(SlaveType.Shuttle, deviceConfig.getDeviceNo(), deviceMsgModel); |
| | | return; |
| | | }else { |
| | | log.info("收到Server Command Data: {}", JSON.toJSONString(result)); |
| | | DeviceMsgModel deviceMsgModel = new DeviceMsgModel(); |
| | | deviceMsgModel.setDeviceId(deviceConfig.getDeviceNo()); |
| | | deviceMsgModel.setDeviceMsgType("command"); |
| | | deviceMsgModel.setDeviceMsg(result); |
| | | deviceMsgModel.setDeviceOriginMsg(sb.toString()); |
| | | deviceMsgModel.setResultKey(resultKey); |
| | | deviceMsgUtils.sendDeviceMsg(SlaveType.Shuttle, deviceConfig.getDeviceNo(), deviceMsgModel); |
| | | } |
| | | } else if ("requestMsg".equals(msgType)) { |
| | | log.info("收到Server Init Data: {}", JSON.toJSONString(result)); |
| | | String requestType = body.getString("requestType"); |
| | | if (requestType.equals("init")) { |
| | | DeviceMsgModel deviceMsgModel = new DeviceMsgModel(); |
| | | deviceMsgModel.setDeviceId(deviceConfig.getDeviceNo()); |
| | | deviceMsgModel.setDeviceMsgType("shuttleInit"); |
| | | deviceMsgModel.setDeviceMsg(result); |
| | | deviceMsgModel.setDeviceOriginMsg(sb.toString()); |
| | | deviceMsgUtils.sendDeviceMsg(SlaveType.Shuttle, deviceConfig.getDeviceNo(), deviceMsgModel); |
| | | } |
| | | } |
| | | |
| | | DeviceMsgModel deviceMsgModel = new DeviceMsgModel(); |
| | | deviceMsgModel.setDeviceId(deviceConfig.getDeviceNo()); |
| | | deviceMsgModel.setDeviceMsgType("command"); |
| | | deviceMsgModel.setDeviceMsg(result); |
| | | 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; |
| | | } |
| | | } |