package com.zy.acs.manager.core.service; import com.alibaba.fastjson.JSON; import com.zy.acs.framework.common.Cools; import com.zy.acs.common.constant.RedisConstant; import com.zy.acs.common.domain.AgvProtocol; import com.zy.acs.common.domain.protocol.*; import com.zy.acs.common.utils.RedisSupport; import com.zy.acs.manager.common.domain.WebsocketDto; import com.zy.acs.manager.manager.entity.Agv; 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.Date; /** * 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; Agv agv = agvService.selectByUuid(protocol.getAgvNo()); if (null == agv) { log.warn("Agv [{}] 尚未鉴权 !!!", protocol.getAgvNo()); return; } AgvDetail detail = agvDetailService.selectByAgvId(agv.getId()); if (null == detail) { detail = new AgvDetail(); detail.setAgvId(agv.getId()); 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 threadPoolRegulator.getInstance().execute(() -> { // avoidWaveCalculator.syncWaveBySingleVeh(protocol.getAgvNo(), agv_12_up.getQrCode()); mapService.unlockPath(protocol.getAgvNo(), agv_12_up.getQrCode()); // mapService.unlockPath(protocol.getAgvNo(), Utils.singletonList(agv_12_up.getQrCode())); }); // finish jam threadPoolRegulator.getInstance().execute(() -> jamService.checkIfFinish(agv.getId(), agv_12_up.getQrCode())); websocketService.agvInfoBroadcastOnPos(new WebsocketDto("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", agv.getUuid(), agv_13_up)); this.sync(agv_13_up, detail); // redis.setObject(RedisConstant.DIGITAL_AGV_FLAG, agv.getUuid(), agv_13_up); // todo 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; log.info("Agv [{}] 故障数据包 ===>> {}", protocol.getAgvNo(), JSON.toJSONString(agv_04_up)); // faultProcessor.execute(new VehicleFaultDto()); } if (!agvDetailService.updateById(detail)) { log.error("Agv [{}] 详情更新失败 !!!", protocol.getAgvNo()); } } // 有码实时数据包 private void sync(AGV_12_UP mesBody, AgvDetail detail) { 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.setStatus(mesBody.getStatus()); // 地面码偏移 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()); } // 无码实时数据包 private void sync(AGV_13_UP mesBody, AgvDetail detail) { Code code = codeService.selectByData(mesBody.getQrCode()); if (null != code) { detail.setCode(null); detail.setLastCode(code.getId()); detail.setPos(0); // if (!agvDetailService.updateUnPosCode(detail.getId(), code.getId())) { // log.error("Agv [{}] 更新定位数据失败 !!!", agvService.getById(detail.getAgvId()).getUuid()); // } } 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()); } // 心跳包 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) { } }