| | |
| | | import java.net.ServerSocket; |
| | | import java.net.Socket; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | |
| | | public void run() { |
| | | News.info("Fake Server is Started"); |
| | | |
| | | acceptorThread(); |
| | | fakeCommandThread(); |
| | | |
| | | while (true) { |
| | | try { |
| | | initFakeDeviceServer(); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private void acceptorThread() { |
| | | Thread acceptorThread = new Thread(() -> { |
| | | log.info("{}:acceptorThread is start"); |
| | | try { |
| | | while (true) { |
| | | try { |
| | | if (serverSocket == null) { |
| | | serverSocket = new ServerSocket(gatewayPort); |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | if(serverSocket == null){ |
| | | continue; |
| | | } |
| | | |
| | | Socket fakeSocket = serverSocket.accept(); |
| | | InputStream inputStream = fakeSocket.getInputStream(); |
| | | if(inputStream == null){ |
| | | continue; |
| | | } |
| | | // 获取输入流 |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); |
| | | // 读取服务器的响应 |
| | | StringBuffer sb = new StringBuffer(); |
| | | char[] chars = new char[2048];//缓冲区 |
| | | while (true) { |
| | | reader.read(chars); |
| | | String trim = new String(chars); |
| | | sb.append(trim); |
| | | if (trim.lastIndexOf("\r\n") != -1) { |
| | | break; |
| | | } |
| | | } |
| | | |
| | | // log.info("acceptorThread is end:{}", sb.toString()); |
| | | JSONObject result = JSON.parseObject(sb.toString()); |
| | | if(result == null){ |
| | | continue; |
| | | } |
| | | |
| | | Object msgType = result.get("msgType"); |
| | | if (msgType != null) { |
| | | if ("fakeDeviceFirstConnect".equals(msgType)) { |
| | | DeviceConfig device = JSON.parseObject(JSON.toJSONString(result.get("deviceConfig")), DeviceConfig.class); |
| | | log.info("{}:device is start,devices:{}", device.getDeviceNo(),JSON.toJSONString(fakeServerMap)); |
| | | |
| | | String fakeStatus = JSON.toJSONString(fakeStatusDemo); |
| | | if (!Cools.isEmpty(device.getFakeInitStatus())) { |
| | | fakeStatus = device.getFakeInitStatus(); |
| | | } |
| | | |
| | | fakeServerMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeSocket); |
| | | fakeStatusMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), JSON.parseObject(fakeStatus)); |
| | | |
| | | HashMap<String, Object> map = new HashMap<>(); |
| | | map.put("deviceNo", device.getDeviceNo()); |
| | | map.put("status", "success"); |
| | | // 获取输出流 |
| | | OutputStreamWriter writer = new OutputStreamWriter(fakeSocket.getOutputStream()); |
| | | writer.write(JSON.toJSONString(map) + "\r\n"); |
| | | writer.flush(); |
| | | } |
| | | } |
| | | |
| | | Thread.sleep(1000); |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | }); |
| | | acceptorThread.start(); |
| | | } |
| | | |
| | | private void fakeCommandThread() { |
| | | Thread fakeCommandThread = new Thread(() -> { |
| | | while (true) { |
| | | try { |
| | |
| | | } |
| | | }); |
| | | fakeCommandThread.start(); |
| | | |
| | | while (true) { |
| | | try { |
| | | try { |
| | | if (serverSocket == null) { |
| | | serverSocket = new ServerSocket(gatewayPort); |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | if(serverSocket == null){ |
| | | continue; |
| | | } |
| | | |
| | | initFakeDeviceServer(); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private synchronized void initFakeDeviceServer() { |
| | |
| | | log.info("{}:device is start handle client", device.getDeviceNo()); |
| | | try { |
| | | while (true) { |
| | | if(!fakeServerMap.containsKey(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo())){ |
| | | log.info("{}:device is start,devices:{}", device.getDeviceNo(),JSON.toJSONString(fakeServerMap)); |
| | | Socket fakeSocket = serverSocket.accept(); |
| | | String fakeStatus = JSON.toJSONString(fakeStatusDemo); |
| | | if (!Cools.isEmpty(device.getFakeInitStatus())) { |
| | | fakeStatus = device.getFakeInitStatus(); |
| | | } |
| | | |
| | | fakeServerMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeSocket); |
| | | fakeStatusMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), JSON.parseObject(fakeStatus)); |
| | | } |
| | | |
| | | if(fakeServerMap.containsKey(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo())){ |
| | | handleClient(device); |
| | | } |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | |
| | | fakeCommandMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeCommand); |
| | | response = genereateFakeCommandResponse(requestId, taskId, requestType); |
| | | } else if (requestType.equals("readState")) { |
| | | response = genereateFakeStatusResponse(requestId, fakeStatus); |
| | | response = genereateFakeStatusResponse(requestId, fakeStatus, device.getDeviceNo()); |
| | | } |
| | | |
| | | fakeStatusMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeStatus); |
| | |
| | | } |
| | | } |
| | | |
| | | public String genereateFakeStatusResponse(Integer taskId, JSONObject fakeStatus) { |
| | | public String genereateFakeStatusResponse(Integer taskId, JSONObject fakeStatus, Integer deviceNo) { |
| | | JSONObject result = new JSONObject(); |
| | | JSONObject response = new JSONObject(); |
| | | result.put("msgType", "responseMsg"); |
| | | result.put("robotId", 5001); |
| | | result.put("robotId", deviceNo); |
| | | result.put("response", response); |
| | | |
| | | JSONObject header = new JSONObject(); |
| | |
| | | body.put("point", fakeStatus.getString("currentCode")); |
| | | body.put("powerPercent", fakeStatus.getString("batteryPower")); |
| | | body.put("voltage", fakeStatus.getInteger("batteryVoltage")); |
| | | body.put("errCode", new ArrayList<Integer>(){{add(fakeStatus.getInteger("errorCode"));add(0);}}); |
| | | body.put("errCode", new ArrayList<Integer>() {{ |
| | | add(fakeStatus.getInteger("errorCode")); |
| | | add(0); |
| | | }}); |
| | | body.put("liftPosition", fakeStatus.getBoolean("hasLift") == true ? 2 : 1); |
| | | body.put("loadState", fakeStatus.getBoolean("hasPallet") == true ? 1 : 0); |
| | | body.put("runDir", fakeStatus.getString("runDirection")); |