|  |  |  | 
|---|
|  |  |  | import com.zy.core.enums.*; | 
|---|
|  |  |  | import com.zy.core.model.LiftSlave; | 
|---|
|  |  |  | import com.zy.core.model.Task; | 
|---|
|  |  |  | import com.zy.core.model.command.LiftAssignCommand; | 
|---|
|  |  |  | import com.zy.core.model.command.LiftCommand; | 
|---|
|  |  |  | import com.zy.core.model.command.ShuttleAssignCommand; | 
|---|
|  |  |  | import com.zy.core.model.command.ShuttleCommand; | 
|---|
|  |  |  | import com.zy.core.model.command.*; | 
|---|
|  |  |  | import com.zy.core.model.protocol.LiftProtocol; | 
|---|
|  |  |  | import lombok.Data; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | 
|---|
|  |  |  | import java.text.MessageFormat; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | && !liftProtocol.getRunning() | 
|---|
|  |  |  | && liftProtocol.getReady() | 
|---|
|  |  |  | && liftProtocol.getPakMk() | 
|---|
|  |  |  | && liftProtocol.getTaskNo() != 0 | 
|---|
|  |  |  | && liftProtocol.getAssignCommand() != null) { | 
|---|
|  |  |  | && liftProtocol.getTaskNo() != 0) { | 
|---|
|  |  |  | //还有未完成的命令 | 
|---|
|  |  |  | executeWork(liftProtocol.getAssignCommand()); | 
|---|
|  |  |  | executeWork(liftProtocol.getTaskNo()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //将提升机状态保存至数据库 | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //分配任务 | 
|---|
|  |  |  | private void assignWork(LiftAssignCommand assignCommand) { | 
|---|
|  |  |  | //将此map存入redis中 | 
|---|
|  |  |  | HashMap<String, Object> map = new HashMap<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //手动模式指令 | 
|---|
|  |  |  | if (!assignCommand.getAuto()) { | 
|---|
|  |  |  | LiftCommand command = new LiftCommand(); | 
|---|
|  |  |  | 
|---|
|  |  |  | liftProtocol.setTaskNo(assignCommand.getTaskNo()); | 
|---|
|  |  |  | switch (assignCommand.getTaskMode()) { | 
|---|
|  |  |  | case 1://上升一层 | 
|---|
|  |  |  | command.setRun((short) 1);//升降 | 
|---|
|  |  |  | command.setDistPosition(++lev); | 
|---|
|  |  |  | command = getLiftUpDownCommand(++lev);//获取提升机上升下降命令 | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case 2://下降一层 | 
|---|
|  |  |  | command.setRun((short) 1);//下降 | 
|---|
|  |  |  | command.setDistPosition(--lev); | 
|---|
|  |  |  | command = getLiftUpDownCommand(--lev);//获取提升机上升下降命令 | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case 3://有货正转 | 
|---|
|  |  |  | command.setRun((short) 6); | 
|---|
|  |  |  | command = getLiftTurnCommand(1);//获取提升机转动命令 | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case 4://有货反转 | 
|---|
|  |  |  | command.setRun((short) 3); | 
|---|
|  |  |  | command = getLiftTurnCommand(2);//获取提升机转动命令 | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case 5://复位 | 
|---|
|  |  |  | command.setRun((short) 0); | 
|---|
|  |  |  | command.setLiftLock(false); | 
|---|
|  |  |  | command = getResetCommand();//获取复位命令 | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | default: | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | assignCommand.setCommands(commands); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //提升机号 | 
|---|
|  |  |  | map.put("lift_no", assignCommand.getLiftNo()); | 
|---|
|  |  |  | //工作号 | 
|---|
|  |  |  | map.put("wrk_no", assignCommand.getTaskNo()); | 
|---|
|  |  |  | //命令执行步序 | 
|---|
|  |  |  | map.put("commandStep", 0); | 
|---|
|  |  |  | //命令 | 
|---|
|  |  |  | map.put("assignCommand", assignCommand); | 
|---|
|  |  |  | LiftRedisCommand redisCommand = new LiftRedisCommand(); | 
|---|
|  |  |  | redisCommand.setLiftNo(assignCommand.getLiftNo());//提升机号 | 
|---|
|  |  |  | redisCommand.setWrkNo(assignCommand.getTaskNo());//工作号 | 
|---|
|  |  |  | redisCommand.setCommandStep(0);//命令执行步序 | 
|---|
|  |  |  | redisCommand.setAssignCommand(assignCommand);//命令 | 
|---|
|  |  |  | //任务数据保存到redis | 
|---|
|  |  |  | redisUtil.set("lift_wrk_no_" + assignCommand.getTaskNo(), JSON.toJSONString(map)); | 
|---|
|  |  |  | redisUtil.set("lift_wrk_no_" + assignCommand.getTaskNo(), JSON.toJSONString(redisCommand)); | 
|---|
|  |  |  | liftProtocol.setAssignCommand(assignCommand); | 
|---|
|  |  |  | liftProtocol.setProtocolStatus(LiftProtocolStatusType.WORKING); | 
|---|
|  |  |  | //执行下发任务 | 
|---|
|  |  |  | executeWork(assignCommand); | 
|---|
|  |  |  | executeWork(assignCommand.getTaskNo()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //执行任务 | 
|---|
|  |  |  | private boolean executeWork(LiftAssignCommand assignCommand) { | 
|---|
|  |  |  | private boolean executeWork(Short wrkNo) { | 
|---|
|  |  |  | //读取redis数据 | 
|---|
|  |  |  | if (assignCommand == null) { | 
|---|
|  |  |  | if (wrkNo == null) { | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //将标记置为false(防止重发) | 
|---|
|  |  |  | liftProtocol.setPakMk(false); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Object o = redisUtil.get("lift_wrk_no_" + assignCommand.getTaskNo()); | 
|---|
|  |  |  | Object o = redisUtil.get("lift_wrk_no_" + wrkNo); | 
|---|
|  |  |  | if (o == null) { | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | HashMap map = JSON.parseObject(o.toString(), HashMap.class); | 
|---|
|  |  |  | List<LiftCommand> commands = assignCommand.getCommands(); | 
|---|
|  |  |  | LiftRedisCommand redisCommand = JSON.parseObject(o.toString(), LiftRedisCommand.class); | 
|---|
|  |  |  | List<LiftCommand> commands = redisCommand.getAssignCommand().getCommands(); | 
|---|
|  |  |  | //当前步序 | 
|---|
|  |  |  | int commandStep = Integer.parseInt(map.get("commandStep").toString()); | 
|---|
|  |  |  | int commandStep = redisCommand.getCommandStep(); | 
|---|
|  |  |  | //总步序 | 
|---|
|  |  |  | int size = commands.size(); | 
|---|
|  |  |  | LiftAssignCommand assignCommand = redisCommand.getAssignCommand(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //取出命令 | 
|---|
|  |  |  | LiftCommand command = commands.get(commandStep); | 
|---|
|  |  |  | 
|---|
|  |  |  | //更新redis数据 | 
|---|
|  |  |  | //步序增加 | 
|---|
|  |  |  | commandStep++; | 
|---|
|  |  |  | map.put("commandStep", commandStep); | 
|---|
|  |  |  | redisCommand.setCommandStep(commandStep); | 
|---|
|  |  |  | //任务数据保存到redis | 
|---|
|  |  |  | redisUtil.set("lift_wrk_no_" + map.get("wrk_no").toString(), JSON.toJSONString(map)); | 
|---|
|  |  |  | redisUtil.set("lift_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand)); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | //已执行完成 | 
|---|
|  |  |  | //保存数据到数据库做流水 | 
|---|
|  |  |  | BasLiftOptService liftOptService = SpringUtils.getBean(BasLiftOptService.class); | 
|---|
|  |  |  | if (liftOptService != null) { | 
|---|
|  |  |  | BasLiftOpt opt = new BasLiftOpt( | 
|---|
|  |  |  | assignCommand.getTaskNo().intValue(), | 
|---|
|  |  |  | assignCommand.getLiftNo().intValue(), | 
|---|
|  |  |  | redisCommand.getWrkNo().intValue(), | 
|---|
|  |  |  | redisCommand.getLiftNo().intValue(), | 
|---|
|  |  |  | new Date(), | 
|---|
|  |  |  | null, | 
|---|
|  |  |  | null, | 
|---|
|  |  |  | 
|---|
|  |  |  | liftOptService.insert(opt); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //删除redis | 
|---|
|  |  |  | redisUtil.del("lift_wrk_no_" + map.get("wrk_no").toString()); | 
|---|
|  |  |  | redisUtil.del("lift_wrk_no_" + redisCommand.getWrkNo()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (assignCommand.getAuto()) { | 
|---|
|  |  |  | //对主线程抛出等待确认状态waiting | 
|---|
|  |  |  | 
|---|
|  |  |  | News.info("提升机任务执行完成等待确认中,提升机号={},任务数据={}", command.getLiftNo(), JSON.toJSON(command)); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | //手动模式不抛出等待状态 | 
|---|
|  |  |  | if (assignCommand.getTaskMode() == 5) { | 
|---|
|  |  |  | liftProtocol.setTaskNo((short) 0); | 
|---|
|  |  |  | liftProtocol.setShuttleNo((short) 0); | 
|---|
|  |  |  | liftProtocol.setProtocolStatus(LiftProtocolStatusType.IDLE); | 
|---|
|  |  |  | liftProtocol.setPakMk(true); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | liftProtocol.setTaskNo((short) 0); | 
|---|
|  |  |  | liftProtocol.setShuttleNo((short) 0); | 
|---|
|  |  |  | liftProtocol.setProtocolStatus(LiftProtocolStatusType.IDLE); | 
|---|
|  |  |  | liftProtocol.setPakMk(true); | 
|---|
|  |  |  | liftProtocol.setSecurityMk(false); | 
|---|
|  |  |  | News.info("提升机手动任务执行完成,提升机号={},任务数据={}", command.getLiftNo(), JSON.toJSON(command)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //下发一条提升机解锁命令 | 
|---|
|  |  |  | LiftCommand liftCommand = new LiftCommand(); | 
|---|
|  |  |  | liftCommand.setRun((short) 0); | 
|---|
|  |  |  | liftCommand.setLiftNo(command.getLiftNo()); | 
|---|
|  |  |  | liftCommand.setLiftLock(false); | 
|---|
|  |  |  | if (write(liftCommand)) { | 
|---|
|  |  |  | LiftCommand unlockCommand = getUnlockCommand(command.getLiftNo()); | 
|---|
|  |  |  | if (write(unlockCommand)) { | 
|---|
|  |  |  | News.info("提升机状态已解锁,提升机号={},任务数据={}", command.getLiftNo(), JSON.toJSON(command)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取提升机解锁命令 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public LiftCommand getUnlockCommand(Short liftNo) { | 
|---|
|  |  |  | LiftCommand command = new LiftCommand(); | 
|---|
|  |  |  | command.setRun((short) 0); | 
|---|
|  |  |  | command.setLiftNo(liftNo); | 
|---|
|  |  |  | command.setLiftLock(false); | 
|---|
|  |  |  | return command; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取复位命令 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public LiftCommand getResetCommand() { | 
|---|
|  |  |  | LiftCommand command = new LiftCommand(); | 
|---|
|  |  |  | command.setRun((short) 0); | 
|---|
|  |  |  | command.setLiftLock(false); | 
|---|
|  |  |  | return command; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取提升机上升下降命令 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public LiftCommand getLiftUpDownCommand(Short lev) { | 
|---|
|  |  |  | LiftCommand command = new LiftCommand(); | 
|---|
|  |  |  | command.setRun((short) 1);//升降 | 
|---|
|  |  |  | command.setDistPosition(lev); | 
|---|
|  |  |  | return command; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取提升机上升下降命令 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public LiftCommand getLiftUpDownCommand(Short liftNo, Short taskNo, Short lev) { | 
|---|
|  |  |  | LiftCommand command = new LiftCommand(); | 
|---|
|  |  |  | command.setRun((short) 1);//升降 | 
|---|
|  |  |  | command.setLiftNo(liftNo);//提升机号 | 
|---|
|  |  |  | command.setTaskNo(taskNo);//任务号 | 
|---|
|  |  |  | command.setDistPosition(lev);//目标楼层1层 | 
|---|
|  |  |  | command.setLiftLock(true);//锁定提升机 | 
|---|
|  |  |  | return command; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取提升机转动命令,direction:1=》有货正转,2=》有货反转 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public LiftCommand getLiftTurnCommand(Integer direction) { | 
|---|
|  |  |  | LiftCommand command = new LiftCommand(); | 
|---|
|  |  |  | command.setRun(direction == 1 ? (short) 6 : (short) 3); | 
|---|
|  |  |  | return command; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取提升机转动命令,direction:1=》有货正转,2=》有货反转 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public LiftCommand getLiftTurnCommand(Short liftNo, Short taskNo, Integer direction) { | 
|---|
|  |  |  | LiftCommand command = new LiftCommand(); | 
|---|
|  |  |  | command.setRun(direction == 1 ? (short) 6 : (short) 3); | 
|---|
|  |  |  | command.setLiftNo(liftNo);//提升机号 | 
|---|
|  |  |  | command.setTaskNo(taskNo);//任务号 | 
|---|
|  |  |  | command.setLiftLock(true);//锁定提升机 | 
|---|
|  |  |  | return command; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 初始化提升机 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private void initLift() { | 
|---|