#
zjj
2024-06-22 8a830f3e5f9ff3bca3161b5bf800abeb1a64e866
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
package com.zy.asrs.wcs.core.map.websocket;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.framework.common.Cools;
import com.zy.asrs.wcs.core.domain.dto.MapLockPathDto;
import com.zy.asrs.wcs.core.entity.BasConveyorSta;
import com.zy.asrs.wcs.core.entity.BasShuttle;
import com.zy.asrs.wcs.core.map.entity.MapWsShuttleVo;
import com.zy.asrs.wcs.core.map.entity.MapWsVo;
import com.zy.asrs.wcs.core.model.NavigateNode;
import com.zy.asrs.wcs.core.model.enums.DeviceCtgType;
import com.zy.asrs.wcs.core.service.BasConveyorStaService;
import com.zy.asrs.wcs.core.service.BasShuttleService;
import com.zy.asrs.wcs.core.utils.NavigateMapUtils;
import com.zy.asrs.wcs.core.utils.Utils;
import com.zy.asrs.wcs.rcs.cache.SlaveConnection;
import com.zy.asrs.wcs.rcs.entity.Device;
import com.zy.asrs.wcs.rcs.model.enums.SlaveType;
import com.zy.asrs.wcs.rcs.model.protocol.ShuttleProtocol;
import com.zy.asrs.wcs.rcs.service.DeviceService;
import com.zy.asrs.wcs.rcs.service.DeviceTypeService;
import com.zy.asrs.wcs.rcs.thread.ShuttleThread;
import com.zy.asrs.wcs.system.entity.Dict;
import com.zy.asrs.wcs.system.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
 
/**
 * Created by vincent on 4/11/2024
 */
@Component
public class MapRealTimeDataScheduler {
 
    @Autowired
    private DeviceTypeService deviceTypeService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private BasShuttleService basShuttleService;
    @Autowired
    private DictService dictService;
    @Autowired
    private NavigateMapUtils navigateMapUtils;
    @Autowired
    private BasConveyorStaService basConveyorStaService;
 
    @Scheduled(cron = "0/1 * * * * ? ")
    public void sync() {
        MapWsVo wsVo = new MapWsVo();
        // shuttle
        wsVo.setShuttleVos(syncShuttle());
        wsVo.setLockPath(getMapLocPath());
        wsVo.setConveyorSta(getMapConveyorSta());
        MapWebSocket.broadcast(JSON.toJSONString(wsVo));
    }
 
    private List<MapWsShuttleVo> syncShuttle() {
        List<MapWsShuttleVo> shuttleVos = new ArrayList<>();
 
        List<Device> deviceList = deviceService.list(new LambdaQueryWrapper<Device>().eq(Device::getDeviceType, DeviceCtgType.SHUTTLE.val()));
        for (Device device : deviceList) {
            MapWsShuttleVo shuttleVo = new MapWsShuttleVo();
            shuttleVos.add(shuttleVo);
 
            shuttleVo.setShuttleNo(device.getDeviceNo());
 
            Consumer<Device> consumer = new Consumer<Device>() {
                @Override
                public void accept(Device device) {
                    BasShuttle basShuttle = basShuttleService.getOne(new LambdaQueryWrapper<BasShuttle>().eq(BasShuttle::getDeviceId, device.getId()));
                    if (null != basShuttle && !Cools.isEmpty(basShuttle.getProtocol())) {
                        ShuttleProtocol protocol = JSON.parseObject(basShuttle.getProtocol(), ShuttleProtocol.class);
                        shuttleVo.setCurLocNo(protocol.getCurrentLocNo());
                    }
                }
            };
 
            ShuttleThread thread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, device.getId().intValue());
            if (null == thread) {
                consumer.accept(device);
                continue;
            }
            ShuttleProtocol protocol = thread.getStatus();
            if (null == protocol) {
                consumer.accept(device);
                continue;
            }
 
            shuttleVo.setCurLocNo(protocol.getCurrentLocNo());
 
            List<NavigateNode> moveAdvancePath = thread.getMoveAdvancePath();
            if (!Cools.isEmpty(moveAdvancePath)) {
                shuttleVo.setTravelPath(moveAdvancePath.stream()
                        .map(path -> Utils.getLocNo(path.getX(), path.getY(), path.getZ()))
                        .collect(Collectors.toList()));
            }
        }
 
        return shuttleVos;
    }
 
    private List<MapLockPathDto> getMapLocPath() {
        List<MapLockPathDto> list = new ArrayList<>();
        Dict dict = dictService.getOne(new LambdaQueryWrapper<Dict>()
                .eq(Dict::getFlag, "floor-list")
                .eq(Dict::getStatus, 1));
        if (dict != null) {
            for (Object o : JSON.parseArray(dict.getValue())) {
                JSONObject jsonObject = JSON.parseObject(o.toString());
                Integer lev = jsonObject.getInteger("value");
                List<NavigateNode> path = navigateMapUtils.getLockPath(lev);
 
                MapLockPathDto lockPathDto = new MapLockPathDto();
                lockPathDto.setPath(path);
                lockPathDto.setLev(lev);
                list.add(lockPathDto);
            }
        }
        return list;
    }
 
    private List<BasConveyorSta> getMapConveyorSta() {
        return basConveyorStaService.list();
    }
 
}