#
DELL
2025-09-18 53a71007458bdded764dc52a39d596f8c4ca28db
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package com.zy.core.thread.fake;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zy.common.Cools;
import com.zy.common.SpringUtils;
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.DeviceCommandMsgModel;
import com.zy.core.model.DeviceMsgModel;
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.NyLiftThread;
import com.zy.core.utils.DeviceMsgUtils;
import com.zy.core.utils.FakeDeviceUtils;
import lombok.extern.slf4j.Slf4j;
 
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
 
@Slf4j
@SuppressWarnings("all")
public class FakeNyLiftThread implements FakeThread {
 
    private RedisUtil redisUtil;
    private JSONObject fakeStatusDemo = JSONObject.parseObject("{\"model\":2,\"plcTaskNo\":0,\"lev\":1,\"deviceStatus\":0,\"taskMode\":0,\"pick\":0,\"put\":0,\"iOMode\":0,\"errorCode\":0,\"hasTray\":0,\"hasCar\":0,\"stationList\":[{\"siteId\":101,\"model\":1,\"busy\":0,\"hasTray\":0,\"deviceError\":0,\"taskNo\":0,\"staNo\":0,\"allowShuttleTake\":0,\"allowShuttlePut\":0,\"barcode\":\"\"},{\"siteId\":102,\"model\":1,\"busy\":0,\"hasTray\":0,\"deviceError\":0,\"taskNo\":0,\"staNo\":0,\"allowShuttleTake\":0,\"allowShuttlePut\":0,\"barcode\":\"\"},{\"siteId\":103,\"model\":1,\"busy\":0,\"hasTray\":0,\"deviceError\":0,\"taskNo\":0,\"staNo\":0,\"allowShuttleTake\":0,\"allowShuttlePut\":0,\"barcode\":\"\"},{\"siteId\":1001,\"model\":1,\"busy\":0,\"hasTray\":0,\"deviceError\":0,\"taskNo\":0,\"staNo\":0,\"allowShuttleTake\":0,\"allowShuttlePut\":0,\"barcode\":\"\"},{\"siteId\":1002,\"model\":1,\"busy\":0,\"hasTray\":0,\"deviceError\":0,\"taskNo\":0,\"staNo\":0,\"allowShuttleTake\":0,\"allowShuttlePut\":0,\"barcode\":\"\"},{\"siteId\":1003,\"model\":1,\"busy\":0,\"hasTray\":0,\"deviceError\":0,\"taskNo\":0,\"staNo\":0,\"allowShuttleTake\":0,\"allowShuttlePut\":0,\"barcode\":\"\"}],\"extend\":{\"frontOverrun\":false,\"backOverrun\":false,\"leftOverrun\":false,\"rightOverrun\":false,\"overHeight\":false,\"overWeight\":false}}");
    private ConcurrentHashMap<String, Thread> fakeThreadMap = new ConcurrentHashMap();
    private ConcurrentHashMap<String, JSONObject> fakeStatusMap = new ConcurrentHashMap();
    private ConcurrentHashMap<String, JSONObject> fakeCommandMap = new ConcurrentHashMap();
 
    private boolean fake = false;
 
    public FakeNyLiftThread(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }
 
    @Override
    public void run() {
        News.info("Fake Server is Started");
 
        Thread fakeCommandThread = new Thread(() -> {
            while (true) {
                try {
                    FakeDeviceUtils fakeDeviceUtils = null;
                    try {
                        fakeDeviceUtils = SpringUtils.getBean(FakeDeviceUtils.class);
                    }catch (Exception e){}
                    if(fakeDeviceUtils == null){
                        continue;
                    }
 
                    FakeThread fakeThread1 = (FakeThread) SlaveConnection.get(SlaveType.FakeThread, 1);
 
                    List<DeviceConfig> deviceConfigs = fakeDeviceUtils.getFakeDeviceConfig();
                    for (DeviceConfig device : deviceConfigs) {
                        if (!device.getDeviceType().equals(String.valueOf(SlaveType.Lift))) {
                            continue;
                        }
 
                        String key = String.valueOf(SlaveType.Lift) + device.getDeviceNo();
 
                        if(fakeThread1 != null){
                            int hasCar = 0;
                            ConcurrentHashMap<String, JSONObject> shuttleFakeStatusMap1 = fakeThread1.getFakeStatusMap();
                            for (Map.Entry<String, JSONObject> entry : shuttleFakeStatusMap1.entrySet()) {
                                JSONObject statusData = entry.getValue();
                                String currentCode = statusData.getString("currentCode");
                                JSONObject point = JSON.parseObject(currentCode);
                                String currentPoint = point.getInteger("x") + "-" + point.getInteger("y");
                                if (device.getOtherData().toString().equals(currentPoint)) {
                                    hasCar = 1;
                                    break;
                                }
                            }
 
                            JSONObject fakeStatus = fakeStatusMap.get(key);
                            fakeStatus.put("hasCar", hasCar);
                            fakeStatusMap.put(key, fakeStatus);
                        }
 
                        excuteFakeCommand(key);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        fakeCommandThread.start();
 
        while (true) {
            try {
                initFakeDeviceServer();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
 
    private synchronized void initFakeDeviceServer() {
        FakeDeviceUtils fakeDeviceUtils = null;
        try {
            fakeDeviceUtils = SpringUtils.getBean(FakeDeviceUtils.class);
        }catch (Exception e){}
        if(fakeDeviceUtils == null){
            return;
        }
        List<DeviceConfig> deviceConfigs = fakeDeviceUtils.getFakeDeviceConfig();
        for (DeviceConfig device : deviceConfigs) {
            if (!device.getDeviceType().equals(String.valueOf(SlaveType.Lift))) {
                continue;
            }
 
            if (!device.getThreadImpl().equals("NyLiftThread")) {
                continue;
            }
 
            if (fakeThreadMap.containsKey(String.valueOf(SlaveType.Lift) + device.getDeviceNo())) {
                continue;
            }
 
            Thread fakeThread = new Thread(() -> {
                log.info("{}:device is start handle client", device.getDeviceNo());
                try {
                    while (true) {
                        handleClient(device);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            });
            fakeThread.start();
 
            String fakeStatus = JSON.toJSONString(fakeStatusDemo);
            if (!Cools.isEmpty(device.getFakeInitStatus())) {
                fakeStatus = device.getFakeInitStatus();
            }
            fakeThreadMap.put(String.valueOf(SlaveType.Lift) + device.getDeviceNo(), fakeThread);
            fakeStatusMap.put(String.valueOf(SlaveType.Lift) + device.getDeviceNo(), JSON.parseObject(fakeStatus));
 
 
            // init
            NyLiftThread thread = new NyLiftThread(device, redisUtil);;
            new Thread(thread).start();
            SlaveConnection.put(SlaveType.Lift, device.getDeviceNo(), thread);
        }
    }
 
    private void excuteFakeCommand(String key) {
        if (!fakeCommandMap.containsKey(key)) {
            return;
        }
        JSONObject command = fakeCommandMap.get(key);
        JSONObject commandBody = command.getJSONObject("commandBody");
        fakeCommandMap.remove(key);
        new Thread(() -> {
            try {
                JSONObject fakeStatus = fakeStatusMap.get(key);
                Integer commandType = command.getInteger("commandType");
                if (commandType == null) {
                    return;
                }
 
                if (commandType == 1) {
                    //取放货
                    fakeStatus.put("plcTaskNo", commandBody.getInteger("taskNo"));//任务号
                    fakeStatus.put("deviceStatus", 1);//取货中
                    fakeStatus.put("taskMode", commandType);//任务类型
                    fakeStatus.put("pick", commandBody.getInteger("pick"));//取货数据
                    fakeStatus.put("put", commandBody.getInteger("put"));//放货数据
                    fakeStatusMap.put(key, fakeStatus);
 
                    //delay
                    Thread.sleep(5000);
 
                    Integer put = commandBody.getInteger("put");
                    int lev = put % 100;
 
                    fakeStatus.put("deviceStatus", 0);
                    fakeStatus.put("taskMode", 0);//任务类型
                    fakeStatus.put("pick", 0);//取货数据
                    fakeStatus.put("put", 0);//放货数据
                    fakeStatus.put("lev", lev);
                    fakeStatusMap.put(key, fakeStatus);
                } else if (commandType == 2) {
                    //小车换层
                    fakeStatus.put("plcTaskNo", commandBody.getInteger("taskNo"));//任务号
                    fakeStatus.put("deviceStatus", 1);//取货中
                    fakeStatus.put("taskMode", commandType);//任务类型
                    fakeStatus.put("pick", commandBody.getInteger("pick"));//取货数据
                    fakeStatus.put("put", commandBody.getInteger("put"));//放货数据
                    fakeStatusMap.put(key, fakeStatus);
 
                    //delay
                    Thread.sleep(5000);
 
                    Integer put = commandBody.getInteger("put");
                    int lev = put % 100;
 
                    fakeStatus.put("deviceStatus", 0);
                    fakeStatus.put("taskMode", 0);//任务类型
                    fakeStatus.put("pick", 0);//取货数据
                    fakeStatus.put("put", 0);//放货数据
                    fakeStatus.put("lev", lev);
                    fakeStatusMap.put(key, fakeStatus);
                } else if (commandType == 3) {
                    //提升机移动
                    fakeStatus.put("plcTaskNo", commandBody.getInteger("taskNo"));//任务号
                    fakeStatus.put("deviceStatus", 1);//移动中
                    fakeStatus.put("taskMode", commandType);//任务类型
                    fakeStatus.put("pick", commandBody.getInteger("pick"));//取货数据
                    fakeStatus.put("put", commandBody.getInteger("put"));//放货数据
                    fakeStatusMap.put(key, fakeStatus);
 
                    //delay
                    Thread.sleep(5000);
 
                    Integer put = commandBody.getInteger("put");
                    int lev = put % 100;
 
                    fakeStatus.put("deviceStatus", 0);
                    fakeStatus.put("taskMode", 0);//任务类型
                    fakeStatus.put("pick", 0);//取货数据
                    fakeStatus.put("put", 0);//放货数据
                    fakeStatus.put("lev", lev);
                    fakeStatusMap.put(key, fakeStatus);
                } else if (commandType == 9996) {
                    //复位
                    fakeStatus.put("plcTaskNo", 0);//任务号
                    fakeStatus.put("deviceStatus", 0);
                    fakeStatus.put("taskMode", 0);//任务类型
                    fakeStatus.put("pick", 0);//取货数据
                    fakeStatus.put("put", 0);//放货数据
                    fakeStatusMap.put(key, fakeStatus);
                } else if (commandType == 9997) {
                    //切换入库模式
                    fakeStatus.put("iOMode", 1);//出入库模式
                    fakeStatusMap.put(key, fakeStatus);
                } else if (commandType == 9998) {
                    //切换出库模式
                    fakeStatus.put("iOMode", 2);//出入库模式
                    fakeStatusMap.put(key, fakeStatus);
                } else if (commandType == 10000) {
                    //写入输送线数据
                    Integer siteId = commandBody.getInteger("siteId");
                    Integer staNo = commandBody.getInteger("staNo");
                    Integer taskNo = commandBody.getInteger("taskNo");
                    String address = commandBody.getString("address");
 
                    short[] array = new short[2];
                    array[0] = staNo.shortValue();//目标站
                    array[1] = taskNo.shortValue();//任务号
 
                    JSONArray stationList = fakeStatus.getJSONArray("stationList");
                    for (int i = 0; i < stationList.size(); i++) {
                        JSONObject staObj = stationList.getJSONObject(i);
                        if (staObj.getInteger("siteId").equals(siteId)) {
                            staObj.put("taskNo", taskNo);
                            staObj.put("staNo", staNo);
                        }
                    }
 
                    fakeStatusMap.put(key, fakeStatus);
                }
 
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }
 
    private void handleClient(DeviceConfig device) throws IOException {
        try {
            Set<String> keys = redisUtil.searchKeys(RedisKeyType.FAKE_DEVICE_LIFT_COMMAND_MSG_KEY.key + device.getDeviceNo());
            if (keys.isEmpty()) {
                return;
            }
 
            TreeSet<String> treeSet = new TreeSet<>();
            for (String key : keys) {
                treeSet.add(key);
            }
 
            String first = treeSet.first();
            DeviceCommandMsgModel deviceCommandMsgModel = (DeviceCommandMsgModel) redisUtil.get(first);
            redisUtil.del(first);
 
//            log.info("收到Client Data: {}", JSON.toJSONString(result));
            processCommand(deviceCommandMsgModel, device);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private void removeFake(DeviceConfig device) {
        Thread thread = fakeThreadMap.get(String.valueOf(SlaveType.Lift) + device.getDeviceNo());
        if (thread != null) {
            thread.interrupt();
        }
 
        fakeThreadMap.remove(String.valueOf(SlaveType.Lift) + device.getDeviceNo());
        fakeStatusMap.remove(String.valueOf(SlaveType.Lift) + device.getDeviceNo());
        fakeCommandMap.remove(String.valueOf(SlaveType.Lift) + device.getDeviceNo());
    }
 
    public void processCommand(DeviceCommandMsgModel deviceCommandMsgModel, DeviceConfig device) throws IOException {
        DeviceMsgUtils deviceMsgUtils = SpringUtils.getBean(DeviceMsgUtils.class);
        if (deviceMsgUtils == null) {
            return;
        }
 
        HashMap<String, Object> response = null;
        JSONObject fakeStatus = fakeStatusMap.get(String.valueOf(SlaveType.Lift) + device.getDeviceNo());
 
        String command = JSON.toJSONString(deviceCommandMsgModel.getCommand());
        JSONObject commandObj = JSON.parseObject(command);
        Integer mode = commandObj.getInteger("mode");
 
        String deviceMsgType = "command";
        JSONObject fakeCommand = new JSONObject();
        fakeCommand.put("deviceNo", device.getDeviceNo());
        fakeCommand.put("deviceType", device.getDeviceType());
        fakeCommand.put("commandType", commandObj.getInteger("mode"));
        fakeCommand.put("commandBody", command);
        if (mode == 1) {
            //取放货
            fakeCommandMap.put(String.valueOf(SlaveType.Lift) + device.getDeviceNo(), fakeCommand);
            response = genereateFakeCommandResponse();
        } else if (mode == 2) {
            //小车换层
            fakeCommandMap.put(String.valueOf(SlaveType.Lift) + device.getDeviceNo(), fakeCommand);
            response = genereateFakeCommandResponse();
        } else if (mode == 3) {
            //提升机移动
            fakeCommandMap.put(String.valueOf(SlaveType.Lift) + device.getDeviceNo(), fakeCommand);
            response = genereateFakeCommandResponse();
        } else if (mode == 9996) {
            //复位
            fakeCommandMap.put(String.valueOf(SlaveType.Lift) + device.getDeviceNo(), fakeCommand);
            response = genereateFakeCommandResponse();
        } else if (mode == 9997) {
            //切换入库模式
            fakeCommandMap.put(String.valueOf(SlaveType.Lift) + device.getDeviceNo(), fakeCommand);
            response = genereateFakeCommandResponse();
        } else if (mode == 9998) {
            //切换出库模式
            fakeCommandMap.put(String.valueOf(SlaveType.Lift) + device.getDeviceNo(), fakeCommand);
            response = genereateFakeCommandResponse();
        } else if (mode == 9999) {
            //读取状态
            response = genereateFakeStatusResponse(fakeStatus);
            deviceMsgType = "status";
        } else if (mode == 10000) {
            //写入输送线数据
            fakeCommandMap.put(String.valueOf(SlaveType.Lift) + device.getDeviceNo(), fakeCommand);
            response = genereateFakeCommandResponse();
        }
 
        fakeStatusMap.put(String.valueOf(SlaveType.Lift) + device.getDeviceNo(), fakeStatus);
 
        if (deviceMsgType.equals("command")) {
            log.info("收到Rcs Lift Command Data: {}", JSON.toJSONString(deviceCommandMsgModel));
        }
        DeviceMsgModel deviceMsgModel = new DeviceMsgModel();
        deviceMsgModel.setDeviceId(device.getDeviceNo());
        deviceMsgModel.setDeviceMsgType(deviceMsgType);
        deviceMsgModel.setDeviceMsg(response);
        deviceMsgModel.setDeviceOriginMsg(JSON.toJSONString(response));
        deviceMsgModel.setResultKey(deviceCommandMsgModel.getResultKey());
        deviceMsgUtils.sendDeviceMsg(SlaveType.Lift, device.getDeviceNo(), deviceMsgModel);
    }
 
    public HashMap<String, Object> genereateFakeStatusResponse(JSONObject fakeStatus) {
        JSONObject device = new JSONObject();
        HashMap<String, Object> resultData = new HashMap<>();
        resultData.put("result", "success");
        resultData.put("deviceStatus", device);
 
 
        device.put("model", fakeStatus.getInteger("model"));
        device.put("plcTaskNo", fakeStatus.getInteger("plcTaskNo"));
        device.put("deviceStatus", fakeStatus.getInteger("deviceStatus"));
        device.put("taskMode", fakeStatus.getInteger("taskMode"));
        device.put("pick", fakeStatus.getInteger("pick"));
        device.put("put", fakeStatus.getInteger("put"));
        device.put("iOMode", fakeStatus.getInteger("iOMode"));
        device.put("errorCode", fakeStatus.getInteger("errorCode"));
        device.put("hasTray", fakeStatus.getInteger("hasTray"));
        device.put("hasCar", fakeStatus.getInteger("hasCar"));
        device.put("lev", fakeStatus.getInteger("lev"));
 
        JSONArray stationList = fakeStatus.getJSONArray("stationList");
        device.put("stationList", stationList);
 
        JSONObject extend = fakeStatus.getJSONObject("extend");
        device.put("frontOverrun", extend.getBoolean("frontOverrun"));
        device.put("backOverrun", extend.getInteger("backOverrun"));
        device.put("leftOverrun", extend.getInteger("leftOverrun"));
        device.put("rightOverrun", extend.getInteger("rightOverrun"));
        device.put("overHeight", extend.getInteger("overHeight"));
        device.put("overWeight", extend.getInteger("overWeight"));
        return resultData;
    }
 
    public HashMap<String, Object> genereateFakeCommandResponse() {
        HashMap<String, Object> resultData = new HashMap<>();
        resultData.put("result", "success");
        return resultData;
    }
 
    @Override
    public boolean connect() {
        return true;
    }
 
    @Override
    public void close() {
 
    }
 
    @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;
    }
}