| | |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | @Slf4j |
| | | @SuppressWarnings("all") |
| | |
| | | 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 boolean fake = false; |
| | | |
| | |
| | | break; |
| | | } |
| | | |
| | | handleClient(fakeSocket, device); |
| | | handleClient(device); |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | }); |
| | | fakeThread.start(); |
| | | |
| | | 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);; |
| | |
| | | } |
| | | } |
| | | |
| | | private void handleClient(Socket socket, DeviceConfig device) throws IOException { |
| | | private void excuteFakeCommand(String key) { |
| | | if (!fakeCommandMap.containsKey(key)) { |
| | | return; |
| | | } |
| | | JSONObject command = fakeCommandMap.get(key); |
| | | fakeCommandMap.remove(key); |
| | | new Thread(() -> { |
| | | try { |
| | | JSONObject fakeStatus = fakeStatusMap.get(key); |
| | | String commandType = command.getString("commandType"); |
| | | if (commandType.equals("liftUp")) { |
| | | fakeStatus.put("deviceStatus", 0);//设备忙碌 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | |
| | | //delay |
| | | Thread.sleep(2000); |
| | | fakeStatus.put("hasLift", true); |
| | | fakeStatus.put("deviceStatus", 1);//设备空闲 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | }else if (commandType.equals("liftDown")) { |
| | | fakeStatus.put("deviceStatus", 0);//设备忙碌 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | |
| | | //delay |
| | | Thread.sleep(2000); |
| | | fakeStatus.put("hasLift", false); |
| | | fakeStatus.put("deviceStatus", 1);//设备空闲 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | }else if (commandType.equals("charge")) { |
| | | fakeStatus.put("deviceStatus", 0);//设备忙碌 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | |
| | | //delay |
| | | Thread.sleep(2000); |
| | | fakeStatus.put("hasCharge", true); |
| | | fakeStatus.put("deviceStatus", 1);//设备空闲 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | }else if (commandType.equals("stopCharge")) { |
| | | fakeStatus.put("deviceStatus", 0);//设备忙碌 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | |
| | | //delay |
| | | Thread.sleep(2000); |
| | | fakeStatus.put("hasCharge", false); |
| | | fakeStatus.put("deviceStatus", 1);//设备空闲 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | }else if (commandType.equals("updateFloor")) { |
| | | fakeStatus.put("deviceStatus", 0);//设备忙碌 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | |
| | | //delay |
| | | Thread.sleep(2000); |
| | | |
| | | JSONObject body = command.getJSONObject("commandBody"); |
| | | String currentCode = fakeStatus.getString("currentCode"); |
| | | JSONObject point = JSON.parseObject(currentCode); |
| | | point.put("z", body.getInteger("z")); |
| | | fakeStatus.put("currentCode", JSON.toJSONString(point)); |
| | | fakeStatus.put("deviceStatus", 1);//设备空闲 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | }else if (commandType.equals("move")) { |
| | | fakeStatus.put("deviceStatus", 0);//设备忙碌 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | Thread.sleep(2000); |
| | | |
| | | JSONObject body = command.getJSONObject("commandBody"); |
| | | String pathList = body.getString("path"); |
| | | List<JSONObject> list = JSON.parseArray(pathList, JSONObject.class); |
| | | for (JSONObject path : list) { |
| | | String currentCode = fakeStatus.getString("currentCode"); |
| | | JSONObject point = JSON.parseObject(currentCode); |
| | | point.put("x", path.getInteger("xp")); |
| | | point.put("y", path.getInteger("yp")); |
| | | point.put("z", path.getInteger("z")); |
| | | fakeStatus.put("currentCode", JSON.toJSONString(point)); |
| | | fakeStatus.put("deviceStatus", 0);//设备忙碌 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | Thread.sleep(2000); |
| | | } |
| | | |
| | | fakeStatus.put("deviceStatus", 1);//设备空闲 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | }).start(); |
| | | } |
| | | |
| | | private void handleClient(DeviceConfig device) throws IOException { |
| | | try { |
| | | Socket socket = fakeServerMap.get(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo()); |
| | | // 获取输入流 |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
| | | // 读取服务器的响应 |
| | |
| | | } |
| | | |
| | | JSONObject result = JSON.parseObject(sb.toString()); |
| | | log.info("收到Client Data: {}", JSON.toJSONString(result)); |
| | | // log.info("收到Client Data: {}", JSON.toJSONString(result)); |
| | | processCommand(result, device); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | |
| | | Integer requestId = header.getInteger("requestId"); |
| | | String requestType = body.getString("requestType"); |
| | | Integer taskId = body.getInteger("taskId"); |
| | | |
| | | JSONObject fakeCommand = new JSONObject(); |
| | | fakeCommand.put("deviceNo", device.getDeviceNo()); |
| | | fakeCommand.put("deviceType", device.getDeviceType()); |
| | | fakeCommand.put("commandType", requestType); |
| | | fakeCommand.put("commandBody", body); |
| | | if (requestType.equals("liftUp")) { |
| | | fakeStatus.put("hasLift", true); |
| | | fakeCommandMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeCommand); |
| | | response = genereateFakeCommandResponse(requestId, taskId, requestType); |
| | | } else if (requestType.equals("liftDown")) { |
| | | fakeStatus.put("hasLift", false); |
| | | fakeCommandMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeCommand); |
| | | response = genereateFakeCommandResponse(requestId, taskId, requestType); |
| | | } else if (requestType.equals("charge")) { |
| | | fakeStatus.put("hasCharge", true); |
| | | fakeCommandMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeCommand); |
| | | response = genereateFakeCommandResponse(requestId, taskId, requestType); |
| | | } else if (requestType.equals("stopCharge")) { |
| | | fakeStatus.put("hasCharge", false); |
| | | fakeCommandMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeCommand); |
| | | response = genereateFakeCommandResponse(requestId, taskId, requestType); |
| | | } else if (requestType.equals("updateFloor")) { |
| | | String currentCode = fakeStatus.getString("currentCode"); |
| | | JSONObject point = JSON.parseObject(currentCode); |
| | | point.put("z", body.getInteger("z")); |
| | | fakeStatus.put("currentCode", JSON.toJSONString(point)); |
| | | fakeCommandMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeCommand); |
| | | response = genereateFakeCommandResponse(requestId, taskId, requestType); |
| | | } else if (requestType.equals("move")) { |
| | | String pathList = body.getString("path"); |
| | | List<JSONObject> list = JSON.parseArray(pathList, JSONObject.class); |
| | | for (JSONObject path : list) { |
| | | String currentCode = fakeStatus.getString("currentCode"); |
| | | JSONObject point = JSON.parseObject(currentCode); |
| | | point.put("x", path.getInteger("xp")); |
| | | point.put("y", path.getInteger("yp")); |
| | | point.put("z", path.getInteger("z")); |
| | | fakeStatus.put("currentCode", JSON.toJSONString(point)); |
| | | try { |
| | | Thread.sleep(100); |
| | | } 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); |
| | | } |