| | |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | @Slf4j |
| | | @SuppressWarnings("all") |
| | | public class FakeNyShuttleThread implements ThreadHandler { |
| | | |
| | | |
| | | 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 HashMap<String, Socket> fakeServerMap = new HashMap(); |
| | | private HashMap<String, JSONObject> fakeStatusMap = new HashMap(); |
| | | private HashMap<String, JSONObject> fakeCommandMap = new HashMap(); |
| | | private ServerSocket serverSocket; |
| | | private Integer gatewayPort; |
| | | private boolean enableFakeAndDeviceThread; |
| | | private ConcurrentHashMap<String, Thread> fakeThreadMap = new ConcurrentHashMap(); |
| | | private ConcurrentHashMap<String, Socket> fakeServerMap = new ConcurrentHashMap(); |
| | | private ConcurrentHashMap<String, JSONObject> fakeStatusMap = new ConcurrentHashMap(); |
| | | private ConcurrentHashMap<String, JSONObject> fakeCommandMap = new ConcurrentHashMap(); |
| | | |
| | | private boolean fake = false; |
| | | |
| | | public FakeNyShuttleThread(RedisUtil redisUtil) { |
| | | public FakeNyShuttleThread(RedisUtil redisUtil, Integer gatewayPort, boolean enableFakeAndDeviceThread) { |
| | | this.redisUtil = redisUtil; |
| | | this.gatewayPort = gatewayPort; |
| | | this.enableFakeAndDeviceThread = enableFakeAndDeviceThread; |
| | | } |
| | | |
| | | @Override |
| | | public void run() { |
| | | News.info("Fake Server is Started"); |
| | | |
| | | acceptorThread(); |
| | | fakeCommandThread(); |
| | | |
| | | while (true) { |
| | | try { |
| | | initFakeDeviceServer(); |
| | |
| | | } |
| | | } |
| | | |
| | | private void initFakeDeviceServer() { |
| | | 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 = null; |
| | | try { |
| | | result = JSON.parseObject(sb.toString()); |
| | | }catch (Exception e) { |
| | | log.error("error body:{}", sb.toString()); |
| | | e.printStackTrace(); |
| | | } |
| | | 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 { |
| | | FakeDeviceUtils fakeDeviceUtils = null; |
| | | try { |
| | | fakeDeviceUtils = SpringUtils.getBean(FakeDeviceUtils.class); |
| | | }catch (Exception e){} |
| | | if(fakeDeviceUtils == null){ |
| | | continue; |
| | | } |
| | | List<DeviceConfig> deviceConfigs = fakeDeviceUtils.getFakeDeviceConfig(); |
| | | for (DeviceConfig device : deviceConfigs) { |
| | | excuteFakeCommand(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo()); |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | }); |
| | | fakeCommandThread.start(); |
| | | } |
| | | |
| | | private synchronized void initFakeDeviceServer() { |
| | | FakeDeviceUtils fakeDeviceUtils = null; |
| | | try { |
| | | fakeDeviceUtils = SpringUtils.getBean(FakeDeviceUtils.class); |
| | |
| | | if(fakeDeviceUtils == null){ |
| | | return; |
| | | } |
| | | String fakeDeviceConfig = fakeDeviceUtils.getFakeDeviceConfig(); |
| | | if(null != fakeDeviceConfig){ |
| | | List<DeviceConfig> deviceConfigs = JSON.parseArray(fakeDeviceConfig, DeviceConfig.class); |
| | | for (DeviceConfig device : deviceConfigs) { |
| | | if (!device.getDeviceType().equals(String.valueOf(SlaveType.Shuttle))) { |
| | | continue; |
| | | List<DeviceConfig> deviceConfigs = fakeDeviceUtils.getFakeDeviceConfig(); |
| | | for (DeviceConfig device : deviceConfigs) { |
| | | if (!device.getDeviceType().equals(String.valueOf(SlaveType.Shuttle))) { |
| | | continue; |
| | | } |
| | | |
| | | if (!device.getThreadImpl().equals("NyShuttleThread")) { |
| | | continue; |
| | | } |
| | | |
| | | if (fakeThreadMap.containsKey(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo())) { |
| | | continue; |
| | | } |
| | | |
| | | if (enableFakeAndDeviceThread) { |
| | | NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, device.getDeviceNo()); |
| | | if (shuttleThread == null) { |
| | | // init |
| | | NyShuttleThread thread = new NyShuttleThread(device, redisUtil);; |
| | | new Thread(thread).start(); |
| | | SlaveConnection.put(SlaveType.Shuttle, device.getDeviceNo(), thread); |
| | | }else { |
| | | Socket socket = shuttleThread.getSocket(); |
| | | if(socket == null){ |
| | | continue; |
| | | } |
| | | } |
| | | } |
| | | |
| | | if (!device.getThreadImpl().equals("NyShuttleThread")) { |
| | | continue; |
| | | } |
| | | log.info("{}:device is run,devices:{}", device.getDeviceNo(),JSON.toJSONString(fakeServerMap)); |
| | | |
| | | if (fakeServerMap.containsKey(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo())) { |
| | | continue; |
| | | } |
| | | |
| | | Thread fakeThread = new Thread(() -> { |
| | | 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; |
| | | } |
| | | |
| | | Thread fakeThread = new Thread(() -> { |
| | | log.info("{}:device is start handle client", device.getDeviceNo()); |
| | | try { |
| | | while (true) { |
| | | if(fakeServerMap.containsKey(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo())){ |
| | | handleClient(device); |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | }); |
| | | fakeThread.start(); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | }); |
| | | fakeThread.start(); |
| | | fakeThreadMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeThread); |
| | | |
| | | Thread fakeCommandThread = new Thread(() -> { |
| | | while (true) { |
| | | try { |
| | | excuteFakeCommand(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo()); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | }); |
| | | fakeCommandThread.start(); |
| | | |
| | | // init |
| | | ThreadHandler thread = new NyShuttleThread(device, redisUtil);; |
| | | new Thread(thread).start(); |
| | | SlaveConnection.put(SlaveType.Shuttle, device.getDeviceNo(), thread); |
| | | } |
| | | try { |
| | | Thread.sleep(2000); |
| | | }catch (Exception e){} |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | 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()); |
| | |
| | | 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")); |