#
Junjie
2025-07-05 a757961fc5b8f5ee5b79cc30615bd22d321d0d72
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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<DeviceConfig> 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<DeviceConfig> newList = new ArrayList<>();
        List<DeviceConfig> 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));
    }
 
}