package com.zy.core.utils; import com.alibaba.fastjson.JSON; import com.zy.common.R; import com.zy.common.exception.CoolException; import com.zy.common.utils.RedisUtil; 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.properties.DeviceConfig; import com.zy.core.thread.ForkLiftThread; import com.zy.core.thread.ShuttleThread; import com.zy.core.thread.impl.LfdZyForkLiftMasterThread; import com.zy.core.thread.impl.NyShuttleThread; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; @Component public class FakeDeviceUtils { @Autowired private RedisUtil redisUtil; @Autowired private DeviceMsgUtils deviceMsgUtils; public String getFakeDeviceConfig() { Object obj = redisUtil.get(RedisKeyType.FAKE_DEVICE_CONFIG.key); if(null == obj){ return null; } return obj.toString(); } public void addFakeDevice(DeviceConfig deviceConfig) { if (deviceConfig.getDeviceType().equals(String.valueOf(SlaveType.Shuttle))) { addShuttleFakeDevice(deviceConfig); } else if (deviceConfig.getDeviceType().equals(String.valueOf(SlaveType.ForkLift))) { addForkLiftFakeDevice(deviceConfig); } } public void deleteFakeDevice(Integer deviceNo, String deviceType) { SlaveType slaveType = SlaveType.findInstance(deviceType); if(slaveType == null){ throw new CoolException("设备类型不存在"); } if (slaveType.equals(SlaveType.Shuttle)) { ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(slaveType, deviceNo); if(shuttleThread == null){ throw new CoolException("设备线程不存在"); } if (!shuttleThread.isFake()) { throw new CoolException("不允许删除真实设备线程"); } shuttleThread.stopThread(); } SlaveConnection.remove(slaveType, deviceNo); delFakeDeviceToRedis(deviceNo, deviceType); } public void addShuttleFakeDevice(DeviceConfig deviceConfig) { ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, deviceConfig.getDeviceNo()); if (shuttleThread != null) { throw new CoolException("设备已存在"); } ThreadHandler thread = null; if (deviceConfig.getThreadImpl().equals("NyShuttleThread")) { thread = new NyShuttleThread(deviceConfig, redisUtil); } else { throw new CoolException("未知的线程实现"); } new Thread(thread).start(); SlaveConnection.put(SlaveType.Shuttle, deviceConfig.getDeviceNo(), thread); addFakeDeviceToRedis(deviceConfig); } public void addForkLiftFakeDevice(DeviceConfig deviceConfig) { ForkLiftThread forkLiftThread = (ForkLiftThread) SlaveConnection.get(SlaveType.ForkLift, deviceConfig.getDeviceNo()); if (forkLiftThread != null) { throw new CoolException("设备已存在"); } ThreadHandler thread = null; if (deviceConfig.getThreadImpl().equals("LfdZyForkLiftMasterThread")) { thread = new LfdZyForkLiftMasterThread(deviceConfig, redisUtil); } else { throw new CoolException("未知的线程实现"); } new Thread(thread).start(); SlaveConnection.put(SlaveType.ForkLiftMaster, deviceConfig.getDeviceNo(), thread); addFakeDeviceToRedis(deviceConfig); } public void addFakeDeviceToRedis(DeviceConfig deviceConfig) { Object object = redisUtil.get(RedisKeyType.FAKE_DEVICE_CONFIG.key); List list = new ArrayList<>(); if (object != null) { list = JSON.parseArray(object.toString(), DeviceConfig.class); } list.add(deviceConfig); redisUtil.set(RedisKeyType.FAKE_DEVICE_CONFIG.key, JSON.toJSONString(list)); } public void delFakeDeviceToRedis(Integer deviceNo, String deviceType) { Object object = redisUtil.get(RedisKeyType.FAKE_DEVICE_CONFIG.key); if (object == null) { return; } List newList = new ArrayList<>(); List list = JSON.parseArray(object.toString(), DeviceConfig.class); for (DeviceConfig config : list) { if(config.getDeviceNo().equals(deviceNo) && config.getDeviceType().equals(deviceType)){ continue; } newList.add(config); } redisUtil.set(RedisKeyType.FAKE_DEVICE_CONFIG.key, JSON.toJSONString(newList)); } }