| | |
| | | import com.zy.common.SpringUtils; |
| | | 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.RedisKeyType; |
| | | import com.zy.core.enums.SlaveType; |
| | | import com.zy.core.model.param.UpdateFakeThreadStatusParam; |
| | | import com.zy.core.properties.DeviceConfig; |
| | | import com.zy.core.thread.FakeThread; |
| | | 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; |
| | | import java.util.*; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | @Slf4j |
| | | @SuppressWarnings("all") |
| | | public class FakeNyShuttleThread implements ThreadHandler { |
| | | public class FakeNyShuttleThread implements FakeThread { |
| | | |
| | | 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}"); |
| | |
| | | } |
| | | List<DeviceConfig> deviceConfigs = fakeDeviceUtils.getFakeDeviceConfig(); |
| | | for (DeviceConfig device : deviceConfigs) { |
| | | excuteFakeCommand(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo()); |
| | | if (!device.getDeviceType().equals(String.valueOf(SlaveType.Shuttle))) { |
| | | continue; |
| | | } |
| | | |
| | | excuteFakeCommand(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), device); |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | |
| | | } |
| | | } |
| | | |
| | | private void excuteFakeCommand(String key) { |
| | | private void excuteFakeCommand(String key, DeviceConfig deviceConfig) { |
| | | if (!fakeCommandMap.containsKey(key)) { |
| | | return; |
| | | } |
| | |
| | | fakeCommandMap.remove(key); |
| | | new Thread(() -> { |
| | | try { |
| | | log.info("Fake Shuttle Command Running: {}", JSON.toJSONString(command)); |
| | | JSONObject fakeStatus = fakeStatusMap.get(key); |
| | | String commandType = command.getString("commandType"); |
| | | if (commandType == null) { |
| | |
| | | fakeStatus.put("deviceStatus", 0);//设备忙碌 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | |
| | | //delay |
| | | Thread.sleep(2000); |
| | | long startTime = System.currentTimeMillis(); |
| | | while (true) { |
| | | if((System.currentTimeMillis() - startTime) < 1000 * 2) { |
| | | continue; |
| | | } |
| | | break; |
| | | } |
| | | fakeStatus.put("hasLift", true); |
| | | fakeStatus.put("deviceStatus", 1);//设备空闲 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | }else if (commandType.equals("liftDown")) { |
| | | } else if (commandType.equals("liftDown")) { |
| | | fakeStatus.put("deviceStatus", 0);//设备忙碌 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | |
| | | //delay |
| | | Thread.sleep(2000); |
| | | long startTime = System.currentTimeMillis(); |
| | | while (true) { |
| | | if((System.currentTimeMillis() - startTime) < 1000 * 2) { |
| | | continue; |
| | | } |
| | | break; |
| | | } |
| | | fakeStatus.put("hasLift", false); |
| | | fakeStatus.put("deviceStatus", 1);//设备空闲 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | }else if (commandType.equals("charge")) { |
| | | } else if (commandType.equals("charge")) { |
| | | fakeStatus.put("deviceStatus", 0);//设备忙碌 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | |
| | | //delay |
| | | Thread.sleep(2000); |
| | | long startTime = System.currentTimeMillis(); |
| | | while (true) { |
| | | if((System.currentTimeMillis() - startTime) < 1000 * 2) { |
| | | continue; |
| | | } |
| | | break; |
| | | } |
| | | fakeStatus.put("hasCharge", true); |
| | | |
| | | Integer batteryPower = Integer.parseInt(fakeStatus.getString("batteryPower")); |
| | | while (true) { |
| | | Thread.sleep(10000); |
| | | if((System.currentTimeMillis() - startTime) < 1000 * 10) { |
| | | continue; |
| | | } |
| | | batteryPower = batteryPower + 1; |
| | | fakeStatus.put("batteryPower", batteryPower); |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | if(batteryPower >= 100) { |
| | | startTime = System.currentTimeMillis(); |
| | | if (batteryPower >= 100) { |
| | | break; |
| | | } |
| | | } |
| | | |
| | | fakeStatus.put("deviceStatus", 1);//设备空闲 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | }else if (commandType.equals("stopCharge")) { |
| | | } else if (commandType.equals("stopCharge")) { |
| | | fakeStatus.put("deviceStatus", 0);//设备忙碌 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | |
| | | //delay |
| | | Thread.sleep(2000); |
| | | long startTime = System.currentTimeMillis(); |
| | | while (true) { |
| | | if((System.currentTimeMillis() - startTime) < 1000 * 2) { |
| | | continue; |
| | | } |
| | | break; |
| | | } |
| | | fakeStatus.put("hasCharge", false); |
| | | fakeStatus.put("deviceStatus", 1);//设备空闲 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | }else if (commandType.equals("updateFloor")) { |
| | | } else if (commandType.equals("updateFloor")) { |
| | | fakeStatus.put("deviceStatus", 0);//设备忙碌 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | |
| | | //delay |
| | | Thread.sleep(2000); |
| | | long startTime = System.currentTimeMillis(); |
| | | while (true) { |
| | | if((System.currentTimeMillis() - startTime) < 1000 * 2) { |
| | | continue; |
| | | } |
| | | break; |
| | | } |
| | | |
| | | JSONObject body = command.getJSONObject("commandBody"); |
| | | String currentCode = fakeStatus.getString("currentCode"); |
| | |
| | | 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); |
| | | } else if (commandType.equals("move")) { |
| | | String baseCommandKey = RedisKeyType.FAKE_SHUTTLE_MOVE_EXECUTE_COMMAND_LIST.key + deviceConfig.getDeviceNo() + "_"; |
| | | while (true) { |
| | | Set<String> keys = redisUtil.searchKeys(baseCommandKey); |
| | | if (keys.isEmpty()) { |
| | | break; |
| | | } |
| | | } |
| | | |
| | | String finalKey = baseCommandKey + System.currentTimeMillis(); |
| | | redisUtil.set(finalKey, true, 60 * 2); |
| | | |
| | | JSONObject body = command.getJSONObject("commandBody"); |
| | | String requestType = body.getString("requestType"); |
| | | String pathList = body.getString("path"); |
| | | List<JSONObject> list = JSON.parseArray(pathList, JSONObject.class); |
| | | for (JSONObject path : list) { |
| | | List<JSONObject> executePathList = JSON.parseArray(pathList, JSONObject.class); |
| | | |
| | | fakeStatus.put("deviceStatus", 0);//设备忙碌 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | long startTime = System.currentTimeMillis(); |
| | | while (true) { |
| | | if((System.currentTimeMillis() - startTime) < 1000 * 2) { |
| | | continue; |
| | | } |
| | | break; |
| | | } |
| | | |
| | | for (JSONObject path : executePathList) { |
| | | while (true) { |
| | | JSONObject realFakeStatus = fakeStatusMap.get(key); |
| | | Integer errorCode = realFakeStatus.getInteger("errorCode"); |
| | | if (errorCode > 0) { |
| | | continue; |
| | | } |
| | | break; |
| | | } |
| | | |
| | | while (true) { |
| | | if((System.currentTimeMillis() - startTime) < 1000 * 1) { |
| | | continue; |
| | | } |
| | | break; |
| | | } |
| | | |
| | | String currentCode = fakeStatus.getString("currentCode"); |
| | | JSONObject point = JSON.parseObject(currentCode); |
| | | point.put("x", path.getInteger("xp")); |
| | |
| | | fakeStatus.put("currentCode", JSON.toJSONString(point)); |
| | | fakeStatus.put("deviceStatus", 0);//设备忙碌 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | Thread.sleep(2000); |
| | | startTime = System.currentTimeMillis(); |
| | | } |
| | | |
| | | fakeStatus.put("deviceStatus", 1);//设备空闲 |
| | | fakeStatusMap.put(key, fakeStatus); |
| | | redisUtil.del(finalKey); |
| | | } |
| | | }catch (Exception e){ |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | }).start(); |
| | | } |
| | | |
| | | private void handleClient(DeviceConfig device) throws IOException { |
| | | StringBuffer sb = new StringBuffer(); |
| | | try { |
| | | Socket socket = fakeServerMap.get(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo()); |
| | | if (socket == null) { |
| | |
| | | // 获取输入流 |
| | | 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) { |
| | | int lastIndexOf = trim.lastIndexOf("\r\n"); |
| | | if (lastIndexOf != -1) { |
| | | trim = trim.substring(0, lastIndexOf); |
| | | sb.append(trim); |
| | | break; |
| | | } |
| | | } |
| | |
| | | // log.info("收到Client Data: {}", JSON.toJSONString(result)); |
| | | processCommand(result, device); |
| | | } catch (Exception e) { |
| | | log.error("handleClient deviceNo:{},recevie:{}", device.getDeviceNo(), sb); |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public ConcurrentHashMap<String, Thread> getFakeThreadMap() { |
| | | return this.fakeThreadMap; |
| | | } |
| | | |
| | | @Override |
| | | public ConcurrentHashMap<String, JSONObject> getFakeStatusMap() { |
| | | return this.fakeStatusMap; |
| | | } |
| | | |
| | | @Override |
| | | public ConcurrentHashMap<String, JSONObject> getFakeCommandMap() { |
| | | return this.fakeCommandMap; |
| | | } |
| | | |
| | | @Override |
| | | public boolean updateFakeStatus(UpdateFakeThreadStatusParam param) { |
| | | String key = param.getDeviceType() + param.getDeviceNo(); |
| | | JSONObject result = fakeStatusMap.get(key); |
| | | if (result == null) { |
| | | return false; |
| | | } |
| | | |
| | | JSONObject newData = JSON.parseObject(param.getData()); |
| | | this.fakeStatusMap.put(key, newData); |
| | | return true; |
| | | } |
| | | } |