package com.zy.asrs.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.core.annotations.ManagerAuth; import com.core.common.BaseRes; import com.core.common.Cools; import com.core.common.R; import com.core.exception.CoolException; import com.zy.asrs.domain.param.ShuttleOperatorParam; import com.zy.asrs.domain.vo.*; import com.zy.asrs.entity.BasShuttle; import com.zy.asrs.entity.WrkMast; import com.zy.asrs.service.BasShuttleService; import com.zy.asrs.service.LocMastService; import com.zy.asrs.service.WrkMastService; import com.zy.asrs.utils.Utils; import com.zy.common.model.enums.NavigationMapType; import com.zy.common.service.CommonService; import com.zy.common.utils.NavigateMapUtils; import com.zy.common.utils.RedisUtil; import com.zy.common.utils.ShuttleOperaUtils; import com.zy.core.News; import com.zy.core.action.ShuttleAction; import com.zy.core.cache.OutputQueue; import com.zy.core.cache.SlaveConnection; import com.zy.core.dispatcher.ShuttleDispatchUtils; import com.zy.core.enums.*; import com.zy.core.model.ShuttleSlave; import com.zy.core.model.command.ShuttleAssignCommand; import com.zy.core.model.command.ShuttleCommand; import com.zy.core.model.command.ShuttleRedisCommand; import com.zy.core.model.param.ShuttleMoveLocParam; import com.zy.core.model.protocol.ShuttleProtocol; import com.zy.core.properties.SlaveProperties; import com.zy.core.thread.ShuttleThread; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; /** * 四向穿梭车接口 */ @Slf4j @RestController @RequestMapping("/shuttle") public class ShuttleController { @Autowired private SlaveProperties slaveProperties; @Autowired private BasShuttleService basShuttleService; @Autowired private WrkMastService wrkMastService; @Autowired private LocMastService locMastService; @Autowired private CommonService commonService; @Autowired private RedisUtil redisUtil; @Autowired private ShuttleDispatchUtils shuttleDispatchUtils; @Autowired private ShuttleOperaUtils shuttleOperaUtils; @Autowired private ShuttleAction shuttleAction; @Autowired private NavigateMapUtils navigateMapUtils; @PostMapping("/table/shuttle/state") @ManagerAuth(memo = "四向穿梭车信息表") public R shuttleStateTable(){ ArrayList list = new ArrayList<>(); for (ShuttleSlave slave : slaveProperties.getShuttle()) { // 表格行 JSONObject baseObj = new JSONObject(); baseObj.put("shuttleNo", slave.getId()); list.add(baseObj); // 获取四向穿梭车信息 ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, slave.getId()); if (shuttleThread == null) { continue; } ShuttleProtocol shuttleProtocol = shuttleThread.getStatus(); JSONObject shuttleData = JSON.parseObject(JSON.toJSONString(shuttleProtocol)); if (shuttleProtocol == null || shuttleProtocol.getShuttleNo()==null) { if (shuttleData != null) { baseObj.putAll(shuttleData); } continue; } shuttleData.put("moveAdvancePath", null);//穿梭车预计路径 if (shuttleProtocol.getTaskNo() != 0) { //存在任务,获取指令 Object object = redisUtil.get(RedisKeyType.SHUTTLE_WORK_FLAG.key + shuttleProtocol.getTaskNo()); if (object != null) { ShuttleRedisCommand redisCommand = JSON.parseObject(object.toString(), ShuttleRedisCommand.class); shuttleData.put("moveAdvancePath", redisCommand.getAssignCommand().getNodes());//穿梭车预计路径 } } Integer chargeLine = basShuttleService.selectById(slave.getId()).getChargeLine(); String lowerPower = "N"; if (chargeLine != null && shuttleProtocol.getBatteryPower() != null) { lowerPower = Integer.parseInt(shuttleProtocol.getBatteryPower()) <= chargeLine ? "Y" : "N"; } shuttleData.put("lowerPower", lowerPower);//是否低电量 baseObj.putAll(shuttleData); } return R.ok().add(list); } @PostMapping("/table/shuttle/msg") @ManagerAuth(memo = "四向穿梭车数据表") public R steMsgTable(){ List list = new ArrayList<>(); for (ShuttleSlave slave : slaveProperties.getShuttle()) { // 表格行 ShuttleMsgTableVo vo = new ShuttleMsgTableVo(); vo.setShuttleNo(slave.getId()); // 四向穿梭车号 list.add(vo); // 获取穿梭车信息 ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, slave.getId()); if (shuttleThread == null) { continue; } ShuttleProtocol shuttleProtocol = shuttleThread.getStatus(); if (shuttleProtocol == null) { continue; } if (shuttleProtocol.getShuttleNo() == null) { continue; } vo.setWorkNo(shuttleProtocol.getTaskNo());//任务号 if (shuttleProtocol.getTaskNo() > 0) { WrkMast wrkMast = wrkMastService.selectById(shuttleProtocol.getTaskNo()); if (wrkMast != null) { vo.setSourceLocNo(wrkMast.getSourceLocNo());//源库位 vo.setDistLocNo(wrkMast.getLocNo());//目标库位 } } if (shuttleProtocol.getProtocolStatusType() != null) { vo.setStatus(shuttleProtocol.getProtocolStatusType().desc);//状态 } vo.setPakMk(shuttleProtocol.getPakMk()?"Y" : "N"); // 作业标记 vo.setLocNo(shuttleProtocol.getLocNo());//四向穿梭车当前库位号 vo.setLev(shuttleProtocol.getLocNo() == null ? 0 : Utils.getLev(shuttleProtocol.getLocNo()));//四向穿梭车当前层高 BasShuttle basShuttle = basShuttleService.selectById(slave.getId()); if (basShuttle != null) { vo.setChargeLine(basShuttle.getChargeLine());//充电阈值 } } return R.ok().add(list); } @PostMapping("/latest/data/shuttle") @ManagerAuth(memo = "四向穿梭车实时数据") public R shuttleLatestData(){ return R.ok(); } @PostMapping("/output/shuttle") @ManagerAuth public R steOutput(){ StringBuilder str = new StringBuilder(); String s; int i = 0; while((s = OutputQueue.SHUTTLE.poll()) != null && i <=10) { str.append("\n").append(s); i++; } return R.ok().add(str.toString()); } @RequestMapping(value = "/command/query") public R shuttleCommandQuery(@RequestParam("wrkNo") Integer wrkNo) { Object o = redisUtil.get(RedisKeyType.SHUTTLE_WORK_FLAG.key + wrkNo); if (o == null) { return R.error("指令不存在"); } ShuttleRedisCommand redisCommand = JSON.parseObject(o.toString(), ShuttleRedisCommand.class); ShuttleAssignCommand assignCommand = redisCommand.getAssignCommand(); List commands = assignCommand.getCommands(); HashMap map = new HashMap<>(); map.put("commands", commands); map.put("shuttleNo", redisCommand.getShuttleNo()); map.put("commandStep", redisCommand.getCommandStep()); map.put("wrkNo", redisCommand.getWrkNo()); return R.ok().add(map); } //回退命令 @RequestMapping(value = "/command/rollback") public R shuttleCommandRollback(@RequestParam("wrkNo") Integer wrkNo , @RequestParam("commandStep") Integer commandStep) { Object o = redisUtil.get(RedisKeyType.SHUTTLE_WORK_FLAG.key + wrkNo); if (o == null) { return R.error(); } ShuttleRedisCommand redisCommand = JSON.parseObject(o.toString(), ShuttleRedisCommand.class); redisCommand.setCommandStep(commandStep); redisUtil.set(RedisKeyType.SHUTTLE_WORK_FLAG.key + wrkNo, JSON.toJSONString(redisCommand)); return R.ok(); } //命令完成状态切换 @RequestMapping(value = "/command/completeSwitch") public R shuttleCommandCompleteSwitch(@RequestParam("wrkNo") Integer wrkNo , @RequestParam("commandStep") Integer commandStep , @RequestParam("complete") Integer complete) { Object o = redisUtil.get(RedisKeyType.SHUTTLE_WORK_FLAG.key + wrkNo); if (o == null) { return R.error(); } ShuttleRedisCommand redisCommand = JSON.parseObject(o.toString(), ShuttleRedisCommand.class); ShuttleAssignCommand assignCommand = redisCommand.getAssignCommand(); List commands = assignCommand.getCommands(); ShuttleCommand command = commands.get(commandStep); command.setComplete(complete != 0); redisUtil.set(RedisKeyType.SHUTTLE_WORK_FLAG.key + wrkNo, JSON.toJSONString(redisCommand)); return R.ok(); } //重启任务(命令) @RequestMapping(value = "/command/restart") public R shuttleCommandCompleteSwitch(@RequestParam("wrkNo") Integer wrkNo) { Object o = redisUtil.get(RedisKeyType.SHUTTLE_WORK_FLAG.key + wrkNo); if (o == null) { return R.error(); } ShuttleRedisCommand redisCommand = JSON.parseObject(o.toString(), ShuttleRedisCommand.class); Integer shuttleNo = redisCommand.getShuttleNo(); ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleNo); if (shuttleThread == null) { return R.error(); } if (!shuttleThread.isIdle()) { return R.error(); } //四向穿梭车处于空闲状态,进行任务的恢复 shuttleThread.setTaskNo(redisCommand.getWrkNo());//将四向穿梭车线程分配任务号 shuttleThread.setProtocolStatus(ShuttleProtocolStatusType.WORKING);//工作状态 return R.ok(); } //删除任务(命令) @RequestMapping(value = "/command/del") public R liftCommandDel(@RequestParam("wrkNo") Integer wrkNo) { Object o = redisUtil.get(RedisKeyType.SHUTTLE_WORK_FLAG.key + wrkNo); if (o == null) { return R.error(); } redisUtil.del(RedisKeyType.SHUTTLE_WORK_FLAG.key + wrkNo); return R.ok(); } /****************************************************************/ /************************** 手动操作 ******************************/ /****************************************************************/ @ManagerAuth(memo = "手动操作") @PostMapping("/operator/shuttle") public R shuttleOperator(ShuttleOperatorParam param){ if (Cools.isEmpty(param.getShuttleNo(), param.getShuttleTaskMode())) { return R.parse(BaseRes.PARAM); } ShuttleAssignCommand assignCommand = new ShuttleAssignCommand(); if (param.getShuttleTaskMode().intValue() == ShuttleTaskModeType.MOVE_LOC_NO.id || param.getShuttleTaskMode().intValue() == ShuttleTaskModeType.MOVE_LOC_NO_TASK.id || param.getShuttleTaskMode().intValue() == ShuttleTaskModeType.TRANSPORT.id) { if (Cools.isEmpty(param.getSourceLocNo(), param.getDistLocNo())) { return R.parse(BaseRes.PARAM); } assignCommand.setSourceLocNo(param.getSourceLocNo()); assignCommand.setLocNo(param.getDistLocNo()); } ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, param.getShuttleNo()); if (shuttleThread == null) { throw new CoolException("四向穿梭车不在线"); } ShuttleProtocol shuttleProtocol = shuttleThread.getStatus(); if (shuttleProtocol == null) { throw new CoolException("四向穿梭车不在线"); } ShuttleTaskModeType shuttleTaskModeType = ShuttleTaskModeType.get(param.getShuttleTaskMode().intValue()); assignCommand.setShuttleNo(param.getShuttleNo()); // 四向穿梭车编号 assignCommand.setTaskMode(shuttleTaskModeType.id); assignCommand.setTaskNo(commonService.getWorkNo(99));//获取任务号 assignCommand.setAuto(false);//手动模式 if (shuttleTaskModeType == ShuttleTaskModeType.MOVE_LOC_NO) { //移动到目标库位 Integer mapType = NavigationMapType.NORMAL.id; if (shuttleProtocol.getHasLift()) { mapType = NavigationMapType.DFX.id; } List commands = shuttleOperaUtils.getStartToTargetCommands(shuttleProtocol.getCurrentLocNo(), param.getDistLocNo(), mapType, assignCommand, shuttleThread); if (commands == null) { throw new CoolException("路径计算失败"); } assignCommand.setCommands(commands); //尝试锁定目标站路径 boolean result2 = navigateMapUtils.writeNavigateNodeToRedisMap(Utils.getLev(param.getDistLocNo()), shuttleProtocol.getShuttleNo(), assignCommand.getNodesDeepCopy(), true);//所使用的路径进行锁定禁用 if (!result2) { News.info("{}任务,{}小车,路径锁定失败,禁止派发", param.getDistLocNo(), shuttleProtocol.getShuttleNo()); throw new CoolException("路径锁定失败"); } } else if (shuttleTaskModeType == ShuttleTaskModeType.PALLET_LIFT) { //托盘顶升 List commands = shuttleOperaUtils.getShuttleLiftCommand(assignCommand, shuttleThread, true); assignCommand.setCommands(commands); } else if (shuttleTaskModeType == ShuttleTaskModeType.PALLET_DOWN) { //托盘下降 List commands = shuttleOperaUtils.getShuttleLiftCommand(assignCommand, shuttleThread, false); assignCommand.setCommands(commands); } else if (shuttleTaskModeType == ShuttleTaskModeType.CHARGE) { //充电开关 boolean charge = false; if (shuttleProtocol.getHasCharge()) { //已充电,关闭充电 charge = false; }else { //开启充电 charge = true; } List commands = shuttleOperaUtils.getShuttleChargeCommand(assignCommand, shuttleThread, charge); assignCommand.setCommands(commands); } else if (shuttleTaskModeType == ShuttleTaskModeType.RESET) { //复位 shuttleThread.setSyncTaskNo(0);//工作号清空 shuttleThread.setProtocolStatus(ShuttleProtocolStatusType.IDLE);//任务状态-空闲 shuttleThread.setPakMk(true);//作业标记复位 shuttleThread.enableMoveLoc(null, false); return R.ok(); } else if (shuttleTaskModeType == ShuttleTaskModeType.MOVE_LOC_NO_TASK) { //移动到目标库位(生成移动任务) shuttleDispatchUtils.dispatchShuttle(commonService.getWorkNo(3), param.getDistLocNo(), param.getShuttleNo()); return R.ok(); } else if (shuttleTaskModeType == ShuttleTaskModeType.SHUTTLE_DEMO_OPEN) { //演示模式-开 shuttleThread.enableDemo(true); return R.ok(); } else if (shuttleTaskModeType == ShuttleTaskModeType.SHUTTLE_DEMO_CLOSE) { //演示模式-关 shuttleThread.enableDemo(false); return R.ok(); } else { throw new CoolException("未知命令"); } shuttleAction.assignWork(shuttleProtocol.getShuttleNo(), assignCommand); return R.ok(); } @PostMapping("/detl/update") @ManagerAuth(memo = "修改数据") public R shuttleUpdate(@RequestParam Integer shuttleNo, @RequestParam Integer workNo, @RequestParam String pakMk) { ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleNo); if (shuttleThread == null) { return R.error("plc已掉线"); } if (workNo != null) { shuttleThread.setSyncTaskNo(workNo); } if (pakMk != null) { shuttleThread.setPakMk(pakMk.equals("Y")); } return R.ok(); } @PostMapping("/runSpeed/{shuttleNo}") @ManagerAuth public R setRunSpeed(@PathVariable("shuttleNo") String shuttleNo, @RequestParam("runSpeed") Integer runSpeed, @RequestParam("chargeLine") Integer chargeLine) { BasShuttle basShuttle = basShuttleService.selectById(shuttleNo); if (basShuttle == null) { return R.error("四向穿梭车不存在"); } basShuttle.setRunSpeed(runSpeed); basShuttle.setChargeLine(chargeLine); if (!basShuttleService.updateById(basShuttle)) { return R.error(); } return R.ok(); } /** * 跑库程序 */ @PostMapping("/moveLoc") @ManagerAuth public R moveLoc(@RequestParam Integer shuttleNo, @RequestParam Integer moveType, @RequestParam Integer xStart, @RequestParam Integer xTarget, @RequestParam Integer yStart, @RequestParam Integer yTarget) { ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleNo); if (shuttleThread == null) { return R.error(); } ShuttleProtocol shuttleProtocol = shuttleThread.getStatus(); if (shuttleProtocol == null) { return R.error(); } if (!shuttleThread.isIdle()) { return R.error("小车忙碌中"); } ShuttleMoveLocParam param = new ShuttleMoveLocParam(); param.setShuttleNo(shuttleNo); param.setMoveType(moveType); param.setStartX(xStart); param.setTargetX(xTarget); param.setStartY(yStart); param.setTargetY(yTarget); shuttleThread.enableMoveLoc(param, true); return R.ok(); } }