#
Junjie
5 天以前 82c36e2b434fa7a1c16b0448aa4cf1483107f79c
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
package com.zy.asrs.controller;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.core.annotations.ManagerAuth;
import com.core.common.Cools;
import com.core.common.R;
import com.zy.asrs.domain.enums.CrnStatusType;
import com.zy.asrs.domain.enums.StationStatusType;
import com.zy.asrs.domain.param.SystemSwitchParam;
import com.zy.asrs.domain.vo.CrnDetailVo;
import com.zy.asrs.domain.vo.CrnLatestDataVo;
import com.zy.asrs.domain.vo.StationLatestDataVo;
import com.zy.asrs.domain.vo.RgvLatestDataVo;
import com.zy.asrs.entity.*;
import com.zy.asrs.service.*;
import com.zy.common.CodeRes;
import com.zy.common.utils.RedisUtil;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.CrnModeType;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.enums.RgvStatusType;
import com.zy.core.enums.SlaveType;
import com.zy.core.enums.WrkIoType;
import com.zy.core.model.protocol.CrnProtocol;
import com.zy.core.model.protocol.StationProtocol;
import com.zy.core.properties.SystemProperties;
import com.zy.core.thread.CrnThread;
import com.zy.core.thread.StationThread;
import com.zy.core.thread.RgvThread;
import com.zy.core.model.protocol.RgvProtocol;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.util.*;
 
/**
 * 主控图接口
 */
@Slf4j
@RestController
@RequestMapping("/console")
public class ConsoleController {
 
    @Autowired
    private DeviceConfigService deviceConfigService;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private BasCrnpErrService basCrnpErrService;
    @Autowired
    private WrkLastnoService wrkLastnoService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private LocMastService locMastService;
 
    @PostMapping("/system/running/status")
    @ManagerAuth(memo = "系统运行状态")
    public R systemRunningStatus() {
        return R.ok().add(Cools.add("status", SystemProperties.WCS_RUNNING_STATUS.get()));
    }
 
    @PostMapping("/system/switch")
    @ManagerAuth(memo = "系统运行开关操作")
    public R systemSwitch(SystemSwitchParam param) throws InterruptedException {
        if (Cools.isEmpty(param.getOperatorType())) {
            return R.error();
        }
        if (param.getOperatorType() == 0) {
            if (Cools.isEmpty(param.getPassword())) {
                return R.error("请输入口令");
            }
            if (!param.getPassword().equals(SystemProperties.WCS_PASSWORD)) {
                return R.error("口令错误");
            }
        }
        Thread.sleep(200L);
        SystemProperties.WCS_RUNNING_STATUS.set(param.getOperatorType() == 1 ? Boolean.TRUE : Boolean.FALSE);
        return R.ok().add(Cools.add("status", SystemProperties.WCS_RUNNING_STATUS.get()));
    }
 
    @PostMapping("/latest/data/station")
    @ManagerAuth(memo = "站点实时数据")
    public R stationLatestData() {
        List<StationLatestDataVo> vos = new ArrayList<>();
 
        WrkLastno inTaskRange = wrkLastnoService.selectById(WrkIoType.IN.id);
        WrkLastno outTaskRange = wrkLastnoService.selectById(WrkIoType.OUT.id);
 
        List<DeviceConfig> devpList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
                .eq("device_type", String.valueOf(SlaveType.Devp)));
        for (DeviceConfig deviceConfig : devpList) {
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp,
                    deviceConfig.getDeviceNo());
            if (stationThread == null) {
                continue;
            }
 
            List<StationProtocol> statusList = stationThread.getStatus();
            if (statusList == null || statusList.isEmpty()) {
                continue;
            }
            for (StationProtocol stationProtocol : statusList) {
                StationLatestDataVo vo = new StationLatestDataVo();
                vo.setStationId(stationProtocol.getStationId()); // 站点编号
                vo.setTaskNo(stationProtocol.getTaskNo()); // 任务号
                vo.setTargetStaNo(stationProtocol.getTargetStaNo()); // 目标站点
                vo.setAutoing(stationProtocol.isAutoing()); // 是否自动
                vo.setLoading(stationProtocol.isLoading()); // 是否有物
                vo.setInEnable(stationProtocol.isInEnable()); // 是否可入
                vo.setOutEnable(stationProtocol.isOutEnable()); // 是否可出
                vo.setEmptyMk(stationProtocol.isEmptyMk()); // 是否空板
                vo.setFullPlt(stationProtocol.isFullPlt()); // 是否满板
                vo.setPalletHeight(stationProtocol.getPalletHeight()); // 托盘高度
                vo.setError(stationProtocol.getError()); // 错误码
                vo.setBarcode(stationProtocol.getBarcode()); // 条码
                String stationStatus = StationStatusType.process(stationProtocol).toString().toLowerCase().replaceAll("_", "-");
                if (stationProtocol.isAutoing() && stationProtocol.isLoading() && stationProtocol.getTaskNo() > 0) {
                    String taskClass = getStationTaskClass(stationProtocol.getTaskNo(), inTaskRange, outTaskRange);
                    if (taskClass != null) {
                        stationStatus = taskClass;
                    }
                }
                vo.setStationStatus(stationStatus);
                vos.add(vo);
            }
 
        }
        return R.ok().add(vos);
    }
 
    private String getStationTaskClass(Integer taskNo, WrkLastno inTaskRange, WrkLastno outTaskRange) {
        if (taskNo == null || taskNo <= 0) {
            return null;
        }
        if (isInRange(taskNo, inTaskRange)) {
            return "machine-pakin";
        }
        if (isInRange(taskNo, outTaskRange)) {
            return "machine-pakout";
        }
        return null;
    }
 
    private boolean isInRange(Integer taskNo, WrkLastno taskRange) {
        if (taskRange == null || taskRange.getSNo() == null || taskRange.getENo() == null) {
            return false;
        }
        return taskNo >= taskRange.getSNo() && taskNo <= taskRange.getENo();
    }
 
    @PostMapping("/latest/data/crn")
    @ManagerAuth(memo = "堆垛机实时数据")
    public R crnLatestData() {
        List<CrnLatestDataVo> vos = new ArrayList<>();
 
        List<DeviceConfig> crnList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
                .eq("device_type", String.valueOf(SlaveType.Crn)));
        for (DeviceConfig deviceConfig : crnList) {
            // 获取堆垛机信息
            CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, deviceConfig.getDeviceNo());
            if (crnThread == null) {
                continue;
            }
 
            CrnProtocol crnProtocol = crnThread.getStatus();
            if (crnProtocol == null) {
                continue;
            }
            CrnLatestDataVo vo = new CrnLatestDataVo();
            vo.setCrnId(crnProtocol.getCrnNo()); // 堆垛机编号
 
            // 使用实际列值作为偏移参考,避免随机坐标导致界面漂移
            vo.setOffset(Double.valueOf(crnProtocol.getBay()));
            vo.setBay(crnProtocol.getBay()); // 当前列
            vo.setTaskNo(crnProtocol.getTaskNo());
            /**
             * 堆垛机状态判断
             */
            if (crnProtocol.getAlarm() > 0) {
                vo.setCrnStatus(CrnStatusType.MACHINE_ERROR);
            } else {
                if (crnProtocol.getTaskNo() > 0) {
                    WrkMast wrkMast = wrkMastService.selectById(crnProtocol.getTaskNo());
                    if (wrkMast != null) {
                        vo.setCrnStatus(CrnStatusType.process(wrkMast.getIoType()));
                    } else {
                        vo.setCrnStatus(crnProtocol.modeType.equals(CrnModeType.AUTO) ? CrnStatusType.MACHINE_AUTO
                                : CrnStatusType.MACHINE_UN_AUTO);
                    }
                } else {
                    vo.setCrnStatus(crnProtocol.modeType.equals(CrnModeType.AUTO) ? CrnStatusType.MACHINE_AUTO
                            : CrnStatusType.MACHINE_UN_AUTO);
                }
            }
            vos.add(vo);
        }
        return R.ok().add(vos);
    }
 
    @PostMapping("/latest/data/rgv")
    @ManagerAuth(memo = "RGV实时数据")
    public R rgvLatestData(){
        List<RgvLatestDataVo> vos = new ArrayList<>();
        List<DeviceConfig> rgvList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
                .eq("device_type", String.valueOf(SlaveType.Rgv)));
        for (DeviceConfig deviceConfig : rgvList) {
            RgvThread rgvThread = (RgvThread) SlaveConnection.get(SlaveType.Rgv, deviceConfig.getDeviceNo());
            if (rgvThread == null) {
                continue;
            }
            RgvProtocol rgvProtocol = rgvThread.getStatus();
            if (rgvProtocol == null) {
                continue;
            }
            RgvLatestDataVo vo = new RgvLatestDataVo();
            vo.setRgvNo(rgvProtocol.getRgvNo());
            vo.setTaskNo(rgvProtocol.getTaskNo());
            vo.setTrackSiteNo(String.valueOf(rgvProtocol.getRgvPos()));
            vo.setRgvStatus(RgvStatusType.get(rgvProtocol.getStatus()));
            vos.add(vo);
        }
        return R.ok().add(vos);
    }
 
    // @PostMapping("/latest/data/barcode")
    // @ManagerAuth(memo = "条码扫描仪实时数据")
    // public R barcodeLatestData(){
    // List<BarcodeDataVo> list = new ArrayList<>();
    // for (Slave barcode : slaveProperties.getBarcode()) {
    // BarcodeThread barcodeThread = (BarcodeThread)
    // SlaveConnection.get(SlaveType.Barcode, barcode.getId());
    // if (null == barcodeThread) {
    // continue;
    // }
    // BarcodeDataVo vo = new BarcodeDataVo();
    // vo.setBarcodeId(barcode.getId());
    // vo.setCodeValue(barcodeThread.getBarcode());
    // list.add(vo);
    // }
    // return R.ok().add(list);
    // }
 
    @PostMapping("/crn/detail")
    @ManagerAuth(memo = "堆垛机设备数据详情")
    public R crnDetail(@RequestParam Integer crnNo) {
        if (Cools.isEmpty(crnNo)) {
            return R.parse(CodeRes.EMPTY);
        }
        CrnDetailVo vo = new CrnDetailVo();
 
        DeviceConfig deviceConfig = deviceConfigService.selectOne(new EntityWrapper<DeviceConfig>()
                .eq("device_type", String.valueOf(SlaveType.Crn))
                .eq("device_no", crnNo));
 
        if (deviceConfig == null) {
            return R.error("设备不存在");
        }
 
        CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crnNo);
        CrnProtocol crnProtocol = crnThread.getStatus();
        vo.setCrnNo(crnNo);
        vo.setWorkNo(crnProtocol.getTaskNo());
        vo.setCrnStatus(crnProtocol.getStatusType().desc);
 
        if (crnProtocol.getAlarm() > 0) {
            BasCrnpErr crnError = basCrnpErrService.selectById(crnProtocol.getAlarm());
            vo.setError(crnError == null ? "未知异常" : crnError.getErrName());
        }
 
        if (crnProtocol.getTaskNo() > 0) {
            WrkMast wrkMast = wrkMastService.selectById(crnProtocol.getTaskNo());
            if (wrkMast != null) {
                vo.setSourceStaNo(String.valueOf(wrkMast.getSourceStaNo()));
                vo.setStaNo(String.valueOf(wrkMast.getStaNo()));
                vo.setWrkSts(wrkMast.getWrkSts$()); // 工作状态
                vo.setIoType(wrkMast.getIoType$()); // 入出库类型
                vo.setSourceLocNo(wrkMast.getSourceLocNo$());
                vo.setLocNo(wrkMast.getLocNo$());
                vo.setCrnStatus(crnProtocol.getStatusType().desc);
                vo.setError(""); // todo
            }
        }
        return R.ok().add(vo);
    }
 
    /**
     * 获取地图楼层数据
     */
    @GetMapping("/map/lev/list")
    @ManagerAuth
    public R getMapLevList() {
        List<Integer> levList = locMastService.getLevList();
        return R.ok().add(levList);
    }
 
    /**
     * 获取地图数据
     */
    @GetMapping("/map/{lev}/auth")
    public R getLocMap(@PathVariable Integer lev) {
        Object object = redisUtil.get(RedisKeyType.LOC_MAP_BASE.key);
        if (object == null) {
            return R.error("地图未初始化");
        }
        List<List<HashMap<String, Object>>> mapNodeList = (List<List<HashMap<String, Object>>>) object;
        List<LocMast> locMastList = locMastService.selectLocByLev(lev);
        for (LocMast locMast : locMastList) {
            String[] locType = locMast.getLocType().split("-");
            HashMap<String, Object> mapNode = mapNodeList.get(Integer.parseInt(locType[0])).get(Integer.parseInt(locType[1]));
            mapNode.put("locSts", locMast.getLocSts());
            mapNode.put("locNo", locMast.getLocNo());
        }
        return R.ok().add(mapNodeList);
    }
 
    @RequestMapping(value = "/map/locList")
    public R mapLocList() {
        Object object = redisUtil.get(RedisKeyType.LOC_MAST_MAP_LIST.key);
        if (object != null) {
            JSONArray data = JSON.parseArray(object.toString());
            return R.ok().add(data);
        }
        EntityWrapper<LocMast> wrapper = new EntityWrapper<>();
        wrapper.eq("lev1", 1);
        List<LocMast> locMasts = locMastService.selectList(wrapper);
        redisUtil.set(RedisKeyType.LOC_MAST_MAP_LIST.key, JSON.toJSONString(locMasts), 60 * 60 * 24);
        return R.ok().add(locMasts);
    }
 
}