package com.zy.core.action; import com.alibaba.fastjson.JSON; import com.zy.asrs.entity.BasLiftOpt; import com.zy.asrs.entity.WrkMast; import com.zy.asrs.service.BasLiftOptService; import com.zy.asrs.service.WrkMastService; import com.zy.common.utils.RedisUtil; import com.zy.core.News; import com.zy.core.cache.SlaveConnection; import com.zy.core.enums.LiftProtocolStatusType; import com.zy.core.enums.LiftTaskModeType; import com.zy.core.enums.RedisKeyType; import com.zy.core.enums.SlaveType; import com.zy.core.model.CommandResponse; import com.zy.core.model.command.LiftAssignCommand; import com.zy.core.model.command.LiftCommand; import com.zy.core.model.command.LiftRedisCommand; import com.zy.core.model.protocol.LiftProtocol; import com.zy.core.thread.LiftThread; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; @Component public class LiftAction { @Autowired private RedisUtil redisUtil; @Autowired private WrkMastService wrkMastService; @Autowired private BasLiftOptService basLiftOptService; public synchronized boolean assignWork(Integer liftNo, LiftAssignCommand assignCommand) { LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, liftNo); if (liftThread == null) { return false; } LiftProtocol liftProtocol = liftThread.getStatus(); if (liftProtocol == null) { return false; } LiftRedisCommand redisCommand = new LiftRedisCommand(); redisCommand.setLiftNo(assignCommand.getLiftNo());//提升机号 redisCommand.setWrkNo(assignCommand.getTaskNo());//工作号 redisCommand.setCommandStep(0);//命令执行步序 redisCommand.setAssignCommand(assignCommand);//命令 //任务数据保存到redis if (redisUtil.set(RedisKeyType.LIFT_WORK_FLAG.key + assignCommand.getTaskNo(), JSON.toJSONString(redisCommand))) { liftThread.setSyncTaskNo(assignCommand.getTaskNo()); return true; } return false; } public synchronized boolean executeWork(Integer liftNo, Integer taskNo) { Object obj = redisUtil.get(RedisKeyType.LIFT_WORK_FLAG.key + taskNo); if (obj == null) { return false; } LiftRedisCommand redisCommand = JSON.parseObject(obj.toString(), LiftRedisCommand.class); if (redisCommand == null) { return false; } LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, liftNo); if (liftThread == null) { return false; } LiftProtocol liftProtocol = liftThread.getStatus(); if (liftProtocol == null) { return false; } List commands = redisCommand.getAssignCommand().getCommands(); if (commands.isEmpty()) { return false; } LiftAssignCommand assignCommand = redisCommand.getAssignCommand(); int commandStep = redisCommand.getCommandStep(); if (commandStep == 0) { //取出命令 LiftCommand command = commands.get(commandStep); //判断提升机是否空闲 if (!liftThread.isDeviceIdle()) { return false; } // 下发命令 CommandResponse response = write(command, liftNo); //保存命令日志 BasLiftOpt basLiftOpt = new BasLiftOpt(); basLiftOpt.setWrkNo(taskNo); basLiftOpt.setLiftNo(liftNo); basLiftOpt.setCommand(JSON.toJSONString(command)); basLiftOpt.setSystemStatus(JSON.toJSONString(liftProtocol)); basLiftOpt.setDeviceWrk(String.valueOf(command.getTaskNo())); basLiftOpt.setSendTime(new Date());//指令下发时间 //保存命令流水 basLiftOptService.insert(basLiftOpt); if (!response.getResult()) { News.error("提升机命令下发失败,提升机号={},任务数据={}", command.getLiftNo(), JSON.toJSON(command)); return false; } else { News.info("提升机命令下发成功,提升机号={},任务数据={}", command.getLiftNo(), JSON.toJSON(command)); } commandStep++; //更新redis数据 redisCommand.setCommandStep(commandStep); // 更新redis数据 redisUtil.set(RedisKeyType.LIFT_WORK_FLAG.key + taskNo, JSON.toJSONString(redisCommand)); }else { if (!liftThread.isDeviceIdle()) { return false; } LiftCommand lastCommand = commands.get(commandStep - 1); if (lastCommand.getMode() == LiftTaskModeType.PICK_PUT.id) { if (liftProtocol.getLev() == lastCommand.getPut()) { lastCommand.setComplete(true); } } else if (lastCommand.getMode() == LiftTaskModeType.SHUTTLE_SWITCH.id) { if (liftProtocol.getLev() == lastCommand.getPut()) { lastCommand.setComplete(true); } } else if (lastCommand.getMode() == LiftTaskModeType.MOVE.id) { if (liftProtocol.getLev() == lastCommand.getPut()) { lastCommand.setComplete(true); } } //任务数据保存到redis redisUtil.set(RedisKeyType.LIFT_WORK_FLAG.key + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand)); if (!lastCommand.getComplete()) { //上一条任务未完成,禁止下发命令 return false; } //判断是否为最后一条命令且命令执行完成,抛出等待确认状态 LiftCommand endCommand = commands.get(commands.size() - 1); if (endCommand.getComplete()) { //已执行完成 //删除redis redisUtil.del(RedisKeyType.LIFT_WORK_FLAG.key + redisCommand.getWrkNo()); //对主线程抛出等待确认状态waiting liftThread.setProtocolStatus(LiftProtocolStatusType.WAITING); News.info("提升机任务执行下发完成执行结束,提升机号={},任务数据={}", redisCommand.getLiftNo(), JSON.toJSON(redisCommand)); } } return true; } private synchronized CommandResponse write(LiftCommand command, Integer liftNo) { CommandResponse response = new CommandResponse(false); if (null == command) { News.error("提升机写入命令为空"); response.setMessage("提升机写入命令为空"); return response; } LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, liftNo); if (liftThread == null) { return response; } if (command.getMode() == LiftTaskModeType.PICK_PUT.id) { response = liftThread.pickAndPut(command); } else if (command.getMode() == LiftTaskModeType.SHUTTLE_SWITCH.id) { response = liftThread.shuttleSwitch(command); } else if (command.getMode() == LiftTaskModeType.MOVE.id) { response = liftThread.move(command); } return response; } //申请提升机资源 public synchronized boolean applyLift(Integer liftNo, Integer waitBindTaskNo) { LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, liftNo); if (liftThread == null) { return false; } LiftProtocol liftProtocol = liftThread.getStatus(); if (liftProtocol == null) { return false; } if (!liftThread.isIdle()) { return false; } List wrkMasts = wrkMastService.selectLiftWrkMast(liftNo); if (!wrkMasts.isEmpty()) { return false; } if (waitBindTaskNo != null) { WrkMast wrkMast = wrkMastService.selectByWorkNo(waitBindTaskNo); if (wrkMast == null) { return false; } wrkMast.setLiftNo(liftNo); wrkMast.setModiTime(new Date()); wrkMastService.updateById(wrkMast); } return true; } }