#
Junjie
1 天以前 6d29b4cb573525c1092a67ef37aacf7ef2233723
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
package com.zy.asrs.controller;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.asrs.domain.param.StationCommandBarcodeParam;
import com.zy.asrs.entity.BasDevp;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.asrs.service.BasDevpService;
import com.zy.asrs.service.DeviceConfigService;
import com.zy.common.utils.RedisUtil;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.enums.StationCommandType;
import com.zy.core.model.StationObjModel;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import com.core.common.Cools;
import com.core.common.R;
import com.zy.asrs.domain.param.StationCommandMoveParam;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.Task;
import com.zy.core.model.command.StationCommand;
import com.zy.core.thread.StationThread;
 
import java.util.List;
 
@RestController
@RequestMapping("/station")
public class StationController {
 
    @Value("${mainProcessPlugin}")
    private String mainProcessPlugin;
    @Autowired
    private BasDevpService basDevpService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ConfigService configService;
    @Autowired
    private DeviceConfigService deviceConfigService;
 
    @PostMapping("/command/move")
    public R commandMove(@RequestBody StationCommandMoveParam param) {
        if (Cools.isEmpty(param)) {
            return R.error("缺少参数");
        }
 
        Integer stationId = param.getStationId();
        Integer taskNo = param.getTaskNo();
        Integer targetStationId = param.getTargetStationId();
 
        StationObjModel finalStation = findStation(stationId);
 
        if(finalStation == null){
            return R.error("站点不存在");
        }
 
        Integer devpNo = finalStation.getDeviceNo();
 
        StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, devpNo);
        if (stationThread == null) {
            return R.error("线程不存在");
        }
 
        StationCommand command = stationThread.getCommand(StationCommandType.MOVE, taskNo, stationId, targetStationId, 0);
        MessageQueue.offer(SlaveType.Devp, devpNo, new Task(2, command));
        return R.ok();
    }
 
    @PostMapping("/command/barcode")
    public R commandBarcode(@RequestBody StationCommandBarcodeParam param) {
        if (Cools.isEmpty(param) || Cools.isEmpty(param.getStationId())) {
            return R.error("缺少参数");
        }
 
        if (!mainProcessPlugin.contains("Fake")) {
            return R.error("当前系统未启用仿真插件");
        }
 
        Config enableFakeConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "enableFake"));
        if (enableFakeConfig == null || !"Y".equals(enableFakeConfig.getValue())) {
            return R.error("当前非仿真运行模式,禁止修改条码");
        }
 
        Integer stationId = param.getStationId();
        StationObjModel finalStation = findStation(stationId);
        if (finalStation == null) {
            return R.error("站点不存在");
        }
 
        Integer devpNo = finalStation.getDeviceNo();
        DeviceConfig deviceConfig = deviceConfigService.selectOne(new EntityWrapper<DeviceConfig>()
                .eq("device_no", devpNo)
                .eq("device_type", String.valueOf(SlaveType.Devp)));
        if (deviceConfig == null || deviceConfig.getFake() == null || deviceConfig.getFake() != 1) {
            return R.error("当前站点设备未启用仿真,禁止修改条码");
        }
 
        StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, devpNo);
        if (stationThread == null) {
            return R.error("线程不存在");
        }
 
        String barcode = param.getBarcode();
        if (barcode == null) {
            barcode = "";
        }
 
        StationCommand command = stationThread.getCommand(StationCommandType.WRITE_INFO, 9997, stationId, stationId, 0);
        command.setBarcode(barcode.trim());
        MessageQueue.offer(SlaveType.Devp, devpNo, new Task(2, command));
        return R.ok();
    }
 
    @PostMapping("/command/reset")
    public R commandReset(@RequestBody StationCommandMoveParam param) {
        if (Cools.isEmpty(param)) {
            return R.error("缺少参数");
        }
 
        Integer taskNo = param.getTaskNo();
        redisUtil.set(RedisKeyType.DEVICE_STATION_MOVE_RESET.key + taskNo, "cancel", 3);
        return R.ok();
    }
 
    private StationObjModel findStation(Integer stationId) {
        if (Cools.isEmpty(stationId)) {
            return null;
        }
 
        List<BasDevp> basDevps = basDevpService.selectList(new EntityWrapper<BasDevp>());
        for (BasDevp basDevp : basDevps) {
            List<StationObjModel> list = basDevp.getStationList$();
            for (StationObjModel entity : list) {
                if (entity.getStationId().equals(stationId)) {
                    return entity;
                }
            }
        }
        return null;
    }
 
}