| New file |
| | |
| | | package com.zy.core.thread.fake; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.zy.common.SpringUtils; |
| | | import com.zy.common.exception.CoolException; |
| | | import com.zy.common.utils.RedisUtil; |
| | | import com.zy.core.News; |
| | | import com.zy.core.ThreadHandler; |
| | | import com.zy.core.cache.SlaveConnection; |
| | | import com.zy.core.enums.SlaveType; |
| | | import com.zy.core.properties.DeviceConfig; |
| | | import com.zy.core.thread.impl.NyShuttleThread; |
| | | import com.zy.core.utils.FakeDeviceUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.io.*; |
| | | import java.net.ServerSocket; |
| | | import java.net.Socket; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | |
| | | @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\\\":12,\\\"y\\\":37,\\\"z\\\":3}\",\"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 boolean fake = false; |
| | | |
| | | public FakeNyShuttleThread(RedisUtil redisUtil) { |
| | | this.redisUtil = redisUtil; |
| | | } |
| | | |
| | | @Override |
| | | public void run() { |
| | | News.info("Fake Server is Started"); |
| | | while (true) { |
| | | try { |
| | | initFakeDeviceServer(); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private void initFakeDeviceServer() { |
| | | FakeDeviceUtils fakeDeviceUtils = SpringUtils.getBean(FakeDeviceUtils.class); |
| | | 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; |
| | | } |
| | | |
| | | if (!device.getThreadImpl().equals("NyShuttleThread")) { |
| | | continue; |
| | | } |
| | | |
| | | 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(); |
| | | fakeServerMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeSocket); |
| | | fakeStatusMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), JSON.parseObject(JSON.toJSONString(fakeStatusDemo))); |
| | | while (true) { |
| | | if(fakeSocket == null) { |
| | | fakeServerMap.remove(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo()); |
| | | break; |
| | | } |
| | | |
| | | if(fakeSocket.isClosed()) { |
| | | fakeSocket = null; |
| | | continue; |
| | | } |
| | | |
| | | handleClient(fakeSocket, device); |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | }); |
| | | fakeThread.start(); |
| | | |
| | | // init |
| | | ThreadHandler thread = new NyShuttleThread(device, redisUtil);; |
| | | new Thread(thread).start(); |
| | | SlaveConnection.put(SlaveType.Shuttle, device.getDeviceNo(), thread); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private void handleClient(Socket socket, DeviceConfig device) throws IOException { |
| | | try { |
| | | // 获取输入流 |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
| | | // 读取服务器的响应 |
| | | 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; |
| | | } |
| | | } |
| | | |
| | | JSONObject result = JSON.parseObject(sb.toString()); |
| | | log.info("收到Client Data: {}", JSON.toJSONString(result)); |
| | | processCommand(result, device); |
| | | } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | socket.close(); |
| | | fakeServerMap.remove(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo()); |
| | | } |
| | | } |
| | | |
| | | public void processCommand(JSONObject result, DeviceConfig device) throws IOException { |
| | | String response = null; |
| | | JSONObject fakeStatus = fakeStatusMap.get(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo()); |
| | | |
| | | JSONObject request = result.getJSONObject("request"); |
| | | JSONObject body = request.getJSONObject("body"); |
| | | String requestType = body.getString("requestType"); |
| | | Integer taskId = body.getInteger("taskId"); |
| | | if (requestType.equals("liftUp")) { |
| | | fakeStatus.put("hasLift", true); |
| | | } else if (requestType.equals("liftDown")) { |
| | | fakeStatus.put("hasLift", false); |
| | | } else if (requestType.equals("charge")) { |
| | | fakeStatus.put("hasCharge", true); |
| | | } else if (requestType.equals("stopCharge")) { |
| | | fakeStatus.put("hasCharge", false); |
| | | } 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)); |
| | | } 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(1); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } else if (requestType.equals("readState")) { |
| | | response = genereateFakeStatusResponse(taskId, fakeStatus); |
| | | } |
| | | |
| | | fakeStatusMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeStatus); |
| | | if (response != null) { |
| | | Socket fakeSocket = fakeServerMap.get(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo()); |
| | | OutputStreamWriter writer = new OutputStreamWriter(fakeSocket.getOutputStream()); |
| | | writer.write(response + "\r\n"); |
| | | writer.flush(); |
| | | } |
| | | } |
| | | |
| | | public String genereateFakeStatusResponse(Integer taskId, JSONObject fakeStatus) { |
| | | JSONObject result = new JSONObject(); |
| | | JSONObject response = new JSONObject(); |
| | | result.put("msgType", "responseMsg"); |
| | | result.put("robotId", 5001); |
| | | result.put("response", response); |
| | | |
| | | JSONObject header = new JSONObject(); |
| | | JSONObject body = new JSONObject(); |
| | | response.put("header", header); |
| | | response.put("body", body); |
| | | |
| | | header.put("responseId", taskId); |
| | | header.put("version", "GV-APP-F427-N24036-1112"); |
| | | |
| | | body.put("responseType", "state"); |
| | | body.put("workingMode", fakeStatus.getInteger("mode")); |
| | | body.put("free", fakeStatus.getInteger("deviceStatus")); |
| | | 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("liftPosition", fakeStatus.getBoolean("hasLift") == true ? 2 : 1); |
| | | body.put("loadState", fakeStatus.getBoolean("hasPallet") == true ? 1 : 0); |
| | | body.put("runDir", fakeStatus.getString("runDirection")); |
| | | body.put("chargState", fakeStatus.getBoolean("hasCharge") == true ? 1 : 0); |
| | | body.put("speed", fakeStatus.getInteger("speed")); |
| | | |
| | | JSONObject extend = fakeStatus.getJSONObject("extend"); |
| | | body.put("suspendState", extend.getInteger("suspendState")); |
| | | body.put("maxCellVoltage", extend.getInteger("maxCellVoltage")); |
| | | body.put("minCellVoltage", extend.getInteger("minCellVoltage")); |
| | | body.put("voltage", extend.getInteger("voltage")); |
| | | body.put("chargeCycleTimes", extend.getInteger("chargeCycleTimes")); |
| | | body.put("surplusQuantity", extend.getInteger("surplusQuantity")); |
| | | body.put("countQuantity", extend.getInteger("countQuantity")); |
| | | return JSON.toJSONString(result); |
| | | } |
| | | |
| | | @Override |
| | | public boolean connect() { |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public void close() { |
| | | |
| | | } |
| | | |
| | | } |