#
luxiaotao1123
2025-01-07 e7cc0981a18fa322642dadddf7043623c83d676f
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
package com.zy.acs.manager.core.service;
 
import com.alibaba.fastjson.JSON;
import com.zy.acs.common.domain.AgvProtocol;
import com.zy.acs.common.domain.protocol.*;
import com.zy.acs.common.enums.AgvStatusType;
import com.zy.acs.common.enums.error.AgvErrorType;
import com.zy.acs.common.utils.GsonUtils;
import com.zy.acs.common.utils.RedisSupport;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.framework.common.DateUtils;
import com.zy.acs.manager.core.constant.MapDataConstant;
import com.zy.acs.manager.core.domain.BackpackDto;
import com.zy.acs.manager.core.domain.VehicleFaultDto;
import com.zy.acs.manager.manager.entity.AgvDetail;
import com.zy.acs.manager.manager.entity.Code;
import com.zy.acs.manager.manager.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * Created by vincent on 2023/6/16
 */
@Slf4j
@Service
public class AgvDataService {
 
    private final static Boolean PRINT_LOG = Boolean.FALSE;
 
    private final RedisSupport redis = RedisSupport.defaultRedisSupport;
 
    @Autowired
    private AgvService agvService;
    @Autowired
    private AgvDetailService agvDetailService;
    @Autowired
    private CodeService codeService;
    @Autowired
    private WebsocketService websocketService;
    @Autowired
    private TrafficService trafficService;
    @Autowired
    private MapService mapService;
    @Autowired
    private ThreadPoolRegulator threadPoolRegulator;
    @Autowired
    private JamService jamService;
    @Autowired
    private AvoidWaveCalculator avoidWaveCalculator;
    @Autowired
    private FaultProcessor faultProcessor;
 
    @Async
    public void dataProcess(AgvProtocol protocol) {
        Date now = new Date();
 
        IMessageBody msgBody = protocol.getMessageBody();
        assert msgBody != null;
 
        Long agvId = agvService.getAgvId(protocol.getAgvNo());
        if (null == agvId) {
            log.warn("Agv [{}] 尚未鉴权 !!!", protocol.getAgvNo());
            return;
        }
        AgvDetail detail = agvDetailService.selectByAgvId(agvId);
        if (null == detail) {
            detail = new AgvDetail();
            detail.setAgvId(agvId);
            if (!agvDetailService.save(detail)) {
                log.error("Agv [{}] 详情更新失败 !!!", protocol.getAgvNo());
                return;
            }
        }
        detail.setUpdateTime(now);
 
        // 有码实时数据包
        if (msgBody instanceof AGV_12_UP) {
            AGV_12_UP agv_12_up = (AGV_12_UP) msgBody;
 
            // unlock path
            mapService.unlockPath(protocol.getAgvNo(), agv_12_up.getQrCode());
//            threadPoolRegulator.getInstance().execute(() -> {
//
//            });
            // finish jam
            threadPoolRegulator.getInstance().execute(() -> jamService.checkIfFinish(agvId, agv_12_up.getQrCode()));
 
//            websocketService.agvInfoBroadcastOnPos(new WebsocketDto<AGV_12_UP>("agv", agv.getUuid(), agv_12_up));
            this.sync(agv_12_up, detail);
//            redis.setObject(RedisConstant.DIGITAL_AGV_FLAG, agv.getUuid(), agv_12_up);
            if (PRINT_LOG) {
                log.info("Agv [{}] 有码实时数据包 ===>> {}", protocol.getAgvNo(), JSON.toJSONString(agv_12_up));
            }
        }
        // 无码实时数据包
        if (msgBody instanceof AGV_13_UP) {
            AGV_13_UP agv_13_up = (AGV_13_UP) msgBody;
//            websocketService.agvInfoBroadcastWithOutPos(new WebsocketDto<AGV_13_UP>("agv", agv.getUuid(), agv_13_up));
            this.sync(agv_13_up, detail);
//            redis.setObject(RedisConstant.DIGITAL_AGV_FLAG, agv.getUuid(), agv_13_up);
            if (PRINT_LOG) {
                log.info("Agv [{}] 无码实时数据包 ===>> {}", protocol.getAgvNo(), JSON.toJSONString(agv_13_up));
            }
        }
        // 心跳包
        if (msgBody instanceof AGV_03_UP) {
            AGV_03_UP agv_03_up = (AGV_03_UP) msgBody;
            this.sync(agv_03_up, detail);
            if (PRINT_LOG) {
                log.info("Agv [{}] 心跳包 ===>> {}", protocol.getAgvNo(), JSON.toJSONString(agv_03_up));
            }
        }
        // 登陆包
        if (msgBody instanceof AGV_F0_UP) {
            AGV_F0_UP agv_f0_up = (AGV_F0_UP) msgBody;
            this.sync(agv_f0_up, detail);
            log.info("Agv [{}] 登陆包 ===>> {}", protocol.getAgvNo(), JSON.toJSONString(agv_f0_up));
        }
        // 故障数据包
        if (msgBody instanceof AGV_04_UP) {
            AGV_04_UP agv_04_up = (AGV_04_UP) msgBody;
            AgvErrorType errorType = AgvErrorType.query(agv_04_up.getHexFaultId());
            log.info("Agv [{}] 故障数据包 ===>> {}", protocol.getAgvNo(), JSON.toJSONString(agv_04_up));
 
            // save
            faultProcessor.execute(new VehicleFaultDto<>(protocol.getAgvNo(), protocol.getTimestamp(), agv_04_up));
 
            // detail
            String errorMSg = errorType != null ? errorType.getMsg() : agv_04_up.getHexFaultId();
            detail.setError(errorMSg);
            detail.setErrorTime(new Date());
        }
 
        // 故障复位
        if (!Cools.isEmpty(detail.realError())) {
            assert detail.getErrorTime() != null;
            if (DateUtils.diffToSeconds(detail.getErrorTime(), new Date()) > 20) {
                detail.setError(MapDataConstant.EMPTY_OF_ERROR);
            }
        }
        if (!agvDetailService.updateById(detail)) {
            log.error("Agv [{}] 详情更新失败 !!!", protocol.getAgvNo());
        }
 
    }
 
    // 有码实时数据包
    private void sync(AGV_12_UP mesBody, AgvDetail detail) {
        detail.setStatus(mesBody.getStatus());
 
        Code code = codeService.selectByData(mesBody.getQrCode());
        if (null != code) {
            detail.setCode(code.getId());
            detail.setLastCode(null);
            detail.setPos(1);
//            if (!agvDetailService.updatePosCode(detail.getId(), code.getId())) {
//                log.error("Agv [{}] 更新定位数据失败 !!!", agvService.getById(detail.getAgvId()).getUuid());
//            }
        } else {
            log.error("Agv [{}] 更新定位数据失败 !!!上传条码:{}", agvService.getById(detail.getAgvId()).getUuid(), mesBody.getQrCode());
        }
 
        // 地面码偏移
        detail.setCodeOffsert(JSON.toJSONString(Cools
                .add("offsetX", mesBody.getOffsetX())
                .add("offsetY", mesBody.getOffsetX())
                .add("groundCodeOffset0", mesBody.getGroundCodeOffset0())
        ));
 
 
        detail.setStraightVal((double) mesBody.getStraightDirectionPosition());
 
        detail.setAgvAngle((double) mesBody.getAGVCurrentAngle());
 
        detail.setGyroAngle((double) mesBody.getGyroAngle());
 
        detail.setEncoderAngle((double) mesBody.getEncoderAngle());
 
        detail.setHigh(mesBody.getCurrentAltitude());
 
        detail.setSensorSts((long) mesBody.getSensorStatusFlags());
 
        List<BackpackDto> backpackDtoList = new ArrayList<>();
        backpackDtoList.add(new BackpackDto(1, mesBody.isTempLoc1()));
        backpackDtoList.add(new BackpackDto(2, mesBody.isTempLoc2()));
        backpackDtoList.add(new BackpackDto(3, mesBody.isTempLoc3()));
        backpackDtoList.add(new BackpackDto(4, mesBody.isTempLoc4()));
        backpackDtoList.add(new BackpackDto(5, mesBody.isTempLoc5()));
        backpackDtoList.add(new BackpackDto(6, mesBody.isTempLoc6()));
        backpackDtoList.add(new BackpackDto(7, mesBody.isTempLoc7()));
        detail.setBackpack(GsonUtils.toJson(backpackDtoList));
 
    }
 
    // 无码实时数据包
    private void sync(AGV_13_UP mesBody, AgvDetail detail) {
        detail.setStatus(mesBody.getStatus());
 
        Code code = codeService.selectByData(mesBody.getQrCode());
        if (null != code) {
 
            if (!detail.getAgvStatus().equals(AgvStatusType.CHARGE)) {
                detail.setCode(null);
                detail.setLastCode(code.getId());
                detail.setPos(0);
            } else {
                detail.setCode(code.getId());
                detail.setLastCode(null);
                detail.setPos(1);
            }
 
 
        } else {
            log.error("Agv [{}] 更新定位数据失败 !!!上传条码:{}", agvService.getById(detail.getAgvId()).getUuid(), mesBody.getQrCode());
        }
 
        detail.setStraightVal((double) mesBody.getStraightDirectionPosition());
 
        detail.setAgvAngle((double) mesBody.getAGVCurrentAngle());
 
        detail.setGyroAngle((double) mesBody.getGyroAngle());
 
        detail.setEncoderAngle((double) mesBody.getEncoderAngle());
 
        detail.setHigh(mesBody.getCurrentAltitude());
 
        detail.setSensorSts((long) mesBody.getSensorStatusFlags());
 
        List<BackpackDto> backpackDtoList = new ArrayList<>();
        backpackDtoList.add(new BackpackDto(1, mesBody.isTempLoc1()));
        backpackDtoList.add(new BackpackDto(2, mesBody.isTempLoc2()));
        backpackDtoList.add(new BackpackDto(3, mesBody.isTempLoc3()));
        backpackDtoList.add(new BackpackDto(4, mesBody.isTempLoc4()));
        backpackDtoList.add(new BackpackDto(5, mesBody.isTempLoc5()));
        backpackDtoList.add(new BackpackDto(6, mesBody.isTempLoc6()));
        backpackDtoList.add(new BackpackDto(7, mesBody.isTempLoc7()));
        detail.setBackpack(GsonUtils.toJson(backpackDtoList));
 
    }
 
    // 心跳包
    private void sync(AGV_03_UP mesBody, AgvDetail detail) {
 
        detail.setVol(mesBody.getVoltage());
        detail.setSoc(mesBody.getSoc());
        detail.setSoh(mesBody.getSoh());
        detail.setBatteryFail(mesBody.getBatterFailSign());
 
        detail.setTempe(JSON.toJSONString(Cools
                .add("batteryTempe", mesBody.getBatteryTempe())
                .add("envirTempe", mesBody.getEnvirTempe())
                .add("leftMotorTempe", mesBody.getLeftMotorTempe())
                .add("rightMotorTempe", mesBody.getRightMotorTempe())
                .add("raiseMotorTempe", mesBody.getRaiseMotorTempe())
                .add("telescopicMotorTempe", mesBody.getTelescopicMotorTempe())
                .add("rotatingMotorTempe", mesBody.getRotatingMotorTempe())
        ));
 
        detail.setMotorFail(JSON.toJSONString(Cools
                .add("leftMotorFailSign", mesBody.getLeftMotorFailSign())
                .add("rightMotorFailSign", mesBody.getRightMotorFailSign())
                .add("raiseMotorFailSign", mesBody.getRaiseMotorFailSign())
                .add("telescopicMotorFailSign", mesBody.getTelescopicMotorFailSign())
                .add("rotatingMotorFailSign", mesBody.getRotatingMotorFailSign())
        ));
 
        detail.setFailSign(String.valueOf(mesBody.getFailSign()));
 
        detail.setBootTime(mesBody.getBootTime());
 
    }
 
    // 登陆包
    private void sync(AGV_F0_UP mesBody, AgvDetail detail) {
 
    }
 
}