| | |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | @Slf4j |
| | | @SuppressWarnings("all") |
| | |
| | | |
| | | private RedisUtil redisUtil; |
| | | private JSONObject fakeStatusDemo = JSONObject.parseObject("{\"mode\":1,\"extend\":{\"countQuantity\":400,\"suspendState\":0,\"minCellVoltage\":3279,\"chargeCycleTimes\":0,\"maxCellVoltage\":3281,\"surplusQuantity\":204,\"voltage\":5248},\"hasLift\":false,\"hasPallet\":false,\"batteryVoltage\":5248,\"runDirection\":\"2\",\"currentCode\":\"{\\\"x\\\":19,\\\"y\\\":11,\\\"z\\\":2}\",\"errorCode\":\"0\",\"hasCharge\":false,\"batteryPower\":\"51\",\"speed\":0,\"deviceStatus\":1}"); |
| | | private ServerSocket serverSocket; |
| | | private HashMap<String, Thread> fakeThreadMap = new HashMap(); |
| | | private HashMap<String, Socket> fakeServerMap = new HashMap(); |
| | | private HashMap<String, JSONObject> fakeStatusMap = new HashMap(); |
| | | private HashMap<String, JSONObject> fakeCommandMap = new HashMap(); |
| | |
| | | @Override |
| | | public void run() { |
| | | News.info("Fake Server is Started"); |
| | | |
| | | Thread fakeCommandThread = new Thread(() -> { |
| | | while (true) { |
| | | try { |
| | | FakeDeviceUtils fakeDeviceUtils = null; |
| | | try { |
| | | fakeDeviceUtils = SpringUtils.getBean(FakeDeviceUtils.class); |
| | | }catch (Exception e){} |
| | | if(fakeDeviceUtils == null){ |
| | | continue; |
| | | } |
| | | String fakeDeviceConfig = fakeDeviceUtils.getFakeDeviceConfig(); |
| | | if(null == fakeDeviceConfig) { |
| | | continue; |
| | | } |
| | | List<DeviceConfig> deviceConfigs = JSON.parseArray(fakeDeviceConfig, DeviceConfig.class); |
| | | for (DeviceConfig device : deviceConfigs) { |
| | | excuteFakeCommand(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo()); |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | }); |
| | | fakeCommandThread.start(); |
| | | |
| | | while (true) { |
| | | try { |
| | | try { |
| | | if (serverSocket == null) { |
| | | serverSocket = new ServerSocket(8888); |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | if(serverSocket == null){ |
| | | continue; |
| | | } |
| | | |
| | | initFakeDeviceServer(); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | |
| | | } |
| | | } |
| | | |
| | | private void initFakeDeviceServer() { |
| | | private synchronized void initFakeDeviceServer() { |
| | | FakeDeviceUtils fakeDeviceUtils = null; |
| | | try { |
| | | fakeDeviceUtils = SpringUtils.getBean(FakeDeviceUtils.class); |
| | |
| | | continue; |
| | | } |
| | | |
| | | if (fakeServerMap.containsKey(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo())) { |
| | | if (fakeThreadMap.containsKey(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo())) { |
| | | continue; |
| | | } |
| | | |
| | | log.info("{}:device is run,devices:{}", device.getDeviceNo(),JSON.toJSONString(fakeServerMap)); |
| | | |
| | | Thread fakeThread = new Thread(() -> { |
| | | log.info("{}:device is start handle client", device.getDeviceNo()); |
| | | try { |
| | | ServerSocket serverSocket = new ServerSocket(device.getPort()); |
| | | 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)); |
| | | while (true) { |
| | | if(fakeSocket == null) { |
| | | fakeServerMap.remove(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo()); |
| | | break; |
| | | 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)); |
| | | } |
| | | |
| | | handleClient(device); |
| | |
| | | } |
| | | }); |
| | | fakeThread.start(); |
| | | |
| | | Thread fakeCommandThread = new Thread(() -> { |
| | | while (true) { |
| | | try { |
| | | excuteFakeCommand(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo()); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | }); |
| | | fakeCommandThread.start(); |
| | | fakeThreadMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeThread); |
| | | |
| | | // init |
| | | ThreadHandler thread = new NyShuttleThread(device, redisUtil);; |
| | |
| | | } |
| | | |
| | | private void removeFake(DeviceConfig device) { |
| | | Thread thread = fakeThreadMap.get(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo()); |
| | | if (thread != null) { |
| | | thread.interrupt(); |
| | | } |
| | | |
| | | fakeThreadMap.remove(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo()); |
| | | fakeServerMap.remove(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo()); |
| | | fakeStatusMap.remove(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo()); |
| | | fakeCommandMap.remove(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo()); |