#
zy
2025-07-21 bcdf41a43daff31d342e6cd2a39fbd28bb00b61b
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.zy.core.controller;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zy.common.R;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.param.AddFakeDeviceParam;
import com.zy.core.model.param.DeleteDeviceParam;
import com.zy.core.model.param.GetFakeThreadStatusParam;
import com.zy.core.model.param.UpdateFakeThreadStatusParam;
import com.zy.core.properties.DeviceConfig;
import com.zy.core.thread.FakeThread;
import com.zy.core.thread.ForkLiftThread;
import com.zy.core.thread.ShuttleThread;
import com.zy.core.utils.DeviceMsgUtils;
import com.zy.core.utils.FakeDeviceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
 
@RestController
@RequestMapping("/open")
public class OpenController {
 
    @Value("${deviceMsgConfig.gatewayId}")
    private Integer gatewayId;
    @Value("${deviceMsgConfig.gatewayPort}")
    private Integer gatewayPort;
 
    @Autowired
    private DeviceMsgUtils deviceMsgUtils;
    @Autowired
    private FakeDeviceUtils fakeDeviceUtils;
 
    @GetMapping("/getSystemInfo")
    public R getSystemInfo() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("gatewayId", gatewayId);
        map.put("gatewayPort", gatewayPort);
        return R.ok().add(map);
    }
 
    @PostMapping("/updateFakeThreadStatus")
    public R updateFakeThreadStatus(@RequestBody UpdateFakeThreadStatusParam param) {
        FakeThread fakeThread = null;
        if (param.getDeviceType().equals(String.valueOf(SlaveType.Shuttle))) {
            fakeThread = (FakeThread) SlaveConnection.get(SlaveType.FakeThread, 1);
        } else if (param.getDeviceType().equals(String.valueOf(SlaveType.ForkLift))) {
            fakeThread = (FakeThread) SlaveConnection.get(SlaveType.FakeThread, 1);
        }
 
        if (fakeThread == null) {
            return R.error("找不到设备线程");
        }
 
        boolean result = fakeThread.updateFakeStatus(param);
        if (result) {
            return R.ok();
        }
        return R.error("更新失败");
    }
 
    @PostMapping("/getFakeThreadStatus")
    public R getFakeThreadStatus(@RequestBody GetFakeThreadStatusParam param) {
        FakeThread fakeThread = null;
        if (param.getDeviceType().equals(String.valueOf(SlaveType.Shuttle))) {
            fakeThread = (FakeThread) SlaveConnection.get(SlaveType.FakeThread, 1);
        } else if (param.getDeviceType().equals(String.valueOf(SlaveType.ForkLift))) {
            fakeThread = (FakeThread) SlaveConnection.get(SlaveType.FakeThread, 1);
        }
 
        if (fakeThread == null) {
            return R.error("找不到设备线程");
        }
 
        ConcurrentHashMap<String, JSONObject> fakeStatusMap = fakeThread.getFakeStatusMap();
 
        String key = param.getDeviceType() + param.getDeviceNo();
        JSONObject result = fakeStatusMap.get(key);
        if (result == null) {
            return R.error("数据不存在");
        }
        return R.ok().add(result);
    }
 
    @GetMapping("/getFakeThreadStatusList")
    public R getFakeThreadStatusList() {
        FakeThread fakeThread1 = (FakeThread) SlaveConnection.get(SlaveType.FakeThread, 1);
        FakeThread fakeThread2 = (FakeThread) SlaveConnection.get(SlaveType.FakeThread, 2);
 
        HashMap<String, Object> map = new HashMap<>();
        map.put("fakeNyShuttleStatusMap", fakeThread1.getFakeStatusMap());
        map.put("fakeZyForkLiftStatusMap", fakeThread2.getFakeStatusMap());
        return R.ok().add(map);
    }
 
    @GetMapping("/getDeviceList")
    public R getDeviceList() {
        List<DeviceConfig> deviceList = new ArrayList<>();
 
        List<DeviceConfig> configList = new ArrayList<>();
 
        List<DeviceConfig> deviceConfigs = deviceMsgUtils.getDeviceConfig();
        configList.addAll(deviceConfigs);
 
        List<DeviceConfig> fakeDeviceConfig = fakeDeviceUtils.getFakeDeviceConfig();
        configList.addAll(fakeDeviceConfig);
 
        for (DeviceConfig config : configList) {
            SlaveType slaveType = SlaveType.findInstance(config.getDeviceType());
            if(slaveType == null){
                continue;
            }
 
            if(slaveType.equals(SlaveType.Shuttle)) {
                ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(slaveType, config.getDeviceNo());
                if(shuttleThread == null){
                    continue;
                }
                deviceList.add(shuttleThread.getDeviceConfig());
            } else if (slaveType.equals(SlaveType.ForkLift)) {
                ForkLiftThread forkLiftThread = (ForkLiftThread) SlaveConnection.get(slaveType, config.getDeviceNo());
                if(forkLiftThread == null){
                    continue;
                }
                deviceList.add(forkLiftThread.getDeviceConfig());
            }
        }
 
        return R.ok().add(deviceList);
    }
 
    @PostMapping("/addFakeDevice")
    public R addFakeDevice(@RequestBody AddFakeDeviceParam param) {
        DeviceConfig deviceConfig = new DeviceConfig();
        deviceConfig.setDeviceNo(param.getDeviceNo());
        deviceConfig.setIp(param.getIp());
        deviceConfig.setPort(param.getPort());
        deviceConfig.setDeviceType(param.getDeviceType());
        deviceConfig.setThreadImpl(param.getThreadImpl());
        deviceConfig.setFake(true);
 
        fakeDeviceUtils.addFakeDevice(deviceConfig);
        return R.ok();
    }
 
    @PostMapping("/deleteFakeDevice")
    public R deleteFakeDevice(@RequestBody DeleteDeviceParam param) {
        fakeDeviceUtils.deleteFakeDevice(param.getDeviceNo(), param.getDeviceType());
        return R.ok();
    }
 
}