| package com.zy.asrs.wcs.core.action; | 
|   | 
| import com.alibaba.fastjson.JSON; | 
| import com.zy.asrs.wcs.core.model.command.*; | 
| import com.zy.asrs.wcs.core.model.enums.LiftCommandModeType; | 
| import com.zy.asrs.wcs.core.utils.RedisUtil; | 
| import com.zy.asrs.wcs.rcs.News; | 
| import com.zy.asrs.wcs.rcs.cache.SlaveConnection; | 
| import com.zy.asrs.wcs.rcs.constant.DeviceRedisConstant; | 
| import com.zy.asrs.wcs.rcs.entity.Device; | 
| import com.zy.asrs.wcs.rcs.model.enums.ShuttleProtocolStatusType; | 
| import com.zy.asrs.wcs.rcs.model.enums.SlaveType; | 
| import com.zy.asrs.wcs.rcs.model.protocol.LiftProtocol; | 
| import com.zy.asrs.wcs.rcs.thread.LiftThread; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Component; | 
|   | 
| import java.util.List; | 
|   | 
| @Component | 
| public class LiftAction { | 
|   | 
|     @Autowired | 
|     private RedisUtil redisUtil; | 
|   | 
|     public synchronized boolean assignWork(Device device, LiftAssignCommand assignCommand) { | 
|         LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, device.getId().intValue()); | 
|         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(DeviceRedisConstant.LIFT_WORK_FLAG + assignCommand.getTaskNo(), JSON.toJSONString(redisCommand))) { | 
|             liftProtocol.setTaskNo(assignCommand.getTaskNo()); | 
|             return true; | 
|         } | 
|         return false; | 
|     } | 
|   | 
|     public synchronized boolean executeWork(Device device, Integer taskNo) { | 
|         Object obj = redisUtil.get(DeviceRedisConstant.LIFT_WORK_FLAG + 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, device.getId().intValue()); | 
|         if (liftThread == null) { | 
|             return false; | 
|         } | 
|   | 
|         LiftProtocol liftProtocol = liftThread.getStatus(); | 
|         if (liftProtocol == null) { | 
|             return false; | 
|         } | 
|   | 
|         //判断提升机是否空闲 | 
|         if (!liftThread.isIdle()) { | 
|             return false; | 
|         } | 
|   | 
|         List<LiftCommand> 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 - 1); | 
|   | 
|             //目前没有判断,直接判定上一条指令完成 | 
|             command.setComplete(true); | 
|   | 
|             // 更新redis数据 | 
|             redisUtil.set(DeviceRedisConstant.LIFT_WORK_FLAG + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand)); | 
|   | 
|             if (!command.getComplete()) { | 
|                 return false; | 
|             } | 
|   | 
|   | 
|             //判断是否为最后一条命令且命令执行完成,抛出等待确认状态 | 
|             LiftCommand endCommand = commands.get(commands.size() - 1); | 
|             if (endCommand.getComplete()) { | 
|                 News.info("提升机任务执行下发完成等待执行结束,提升机号={},任务数据={}", liftProtocol.getLiftNo(), JSON.toJSON(commands)); | 
|                 return false;//禁止再下发命令 | 
|             } | 
|         } | 
|   | 
|         //取出命令 | 
|         LiftCommand command = commands.get(commandStep); | 
|   | 
|         boolean result = write(command, device); | 
|         if (!result) { | 
|             News.error("提升机命令下发失败,提升机号={},任务数据={}", command.getLiftNo(), JSON.toJSON(command)); | 
|             return false; | 
|         } else { | 
|             News.info("提升机命令下发成功,提升机号={},任务数据={}", command.getLiftNo(), JSON.toJSON(command)); | 
|         } | 
|   | 
|         redisUtil.del(DeviceRedisConstant.LIFT_WORK_FLAG + command.getTaskNo()); | 
|         return true; | 
|     } | 
|   | 
|     private synchronized boolean write(LiftCommand command, Device device) { | 
|         if (null == command) { | 
|             News.error("提升机写入命令为空"); | 
|             return false; | 
|         } | 
|   | 
|         LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, device.getId().intValue()); | 
|         if (liftThread == null) { | 
|             return false; | 
|         } | 
|   | 
|         boolean result = false; | 
|         if (command.getMode() == LiftCommandModeType.MOVE.id) { | 
|             result = liftThread.move(command); | 
|         } else if (command.getMode() == LiftCommandModeType.PALLET_INOUT.id) { | 
|             result = liftThread.palletInOut(command); | 
|         } else if (command.getMode() == LiftCommandModeType.LOCK.id) { | 
|             result = liftThread.lock(command); | 
|         } else if (command.getMode() == LiftCommandModeType.UNLOCK.id) { | 
|             result = liftThread.unlock(command); | 
|         } else if (command.getMode() == LiftCommandModeType.RESET.id) { | 
|             result = liftThread.reset(command); | 
|         } | 
|         return result; | 
|     } | 
|   | 
| } |