|  |  | 
 |  |  | 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.BasShuttleOpt; | 
 |  |  | import com.zy.asrs.wcs.rcs.entity.Device; | 
 |  |  | import com.zy.asrs.wcs.rcs.model.CommandResponse; | 
 |  |  | 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.ShuttleProtocol; | 
 |  |  | import com.zy.asrs.wcs.rcs.service.BasShuttleOptService; | 
 |  |  | import com.zy.asrs.wcs.rcs.thread.ShuttleThread; | 
 |  |  | import com.zy.asrs.wcs.system.entity.Dict; | 
 |  |  | import com.zy.asrs.wcs.system.service.DictService; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.stereotype.Component; | 
 |  |  |  | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Objects; | 
 |  |  | import java.util.*; | 
 |  |  |  | 
 |  |  | @Component | 
 |  |  | public class ShuttleAction { | 
 |  |  | 
 |  |  |     private DictService dictService; | 
 |  |  |     @Autowired | 
 |  |  |     private ConveyorDispatcher conveyorDispatcher; | 
 |  |  |     @Autowired | 
 |  |  |     private BasShuttleOptService basShuttleOptService; | 
 |  |  |  | 
 |  |  |     public synchronized boolean assignWork(Device device, ShuttleAssignCommand assignCommand) { | 
 |  |  |         ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, device.getId().intValue()); | 
 |  |  | 
 |  |  |         redisCommand.setAssignCommand(assignCommand);//命令 | 
 |  |  |         //任务数据保存到redis | 
 |  |  |         if (redisUtil.set(DeviceRedisConstant.SHUTTLE_WORK_FLAG + assignCommand.getTaskNo(), JSON.toJSONString(redisCommand, SerializerFeature.DisableCircularReferenceDetect))) { | 
 |  |  |             if (assignCommand.getTaskMode() == ShuttleTaskModeType.PAK_IN.id | 
 |  |  |                     || assignCommand.getTaskMode() == ShuttleTaskModeType.PAK_OUT.id | 
 |  |  |                     || assignCommand.getTaskMode() == ShuttleTaskModeType.MOVE_LOC_NO.id | 
 |  |  |                     || assignCommand.getTaskMode() == ShuttleTaskModeType.SHUTTLE_MOVE_LOC_NO.id) { | 
 |  |  |             if (assignCommand.getTaskMode() == ShuttleTaskModeType.SHUTTLE_MOVE.id | 
 |  |  |                     || assignCommand.getTaskMode() == ShuttleTaskModeType.TRANSPORT.id | 
 |  |  |                     || assignCommand.getTaskMode() == ShuttleTaskModeType.MOVE_PALLET_LIFT.id | 
 |  |  |                     || assignCommand.getTaskMode() == ShuttleTaskModeType.MOVE_PALLET_DOWN.id) { | 
 |  |  |                 //下发行驶路径 | 
 |  |  |                 boolean result = shuttleThread.movePath(assignCommand.getNodes(), assignCommand.getDeviceTaskNo()); | 
 |  |  |                 CommandResponse response = shuttleThread.movePath(assignCommand.getNodes(), assignCommand.getDeviceTaskNo()); | 
 |  |  |                 Boolean result = response.getResult(); | 
 |  |  |                 if (!result) { | 
 |  |  |                     return false; | 
 |  |  |                 } | 
 |  |  | 
 |  |  |         ShuttleAssignCommand assignCommand = redisCommand.getAssignCommand(); | 
 |  |  |         int commandStep = redisCommand.getCommandStep(); | 
 |  |  |  | 
 |  |  |         Integer mode = -1; | 
 |  |  |         if(commandStep < commands.size()) { | 
 |  |  |             //取出命令 | 
 |  |  |             ShuttleCommand currentCommand = commands.get(commandStep); | 
 |  |  |             mode = currentCommand.getMode(); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         //判断设备是否空闲 | 
 |  |  |         Integer finalMode = mode; | 
 |  |  |         if (!shuttleThread.isDeviceIdle(new ExecuteSupport() { | 
 |  |  |             @Override | 
 |  |  |             public Boolean judgement() { | 
 |  |  |                 if (ShuttleCommandModeType.CHARGE_CLOSE.id.equals(finalMode)) {//关闭充电motion | 
 |  |  |                     return false;//不需要判断状态 | 
 |  |  |                 } | 
 |  |  |                 return true;//需要判断状态 | 
 |  |  |             } | 
 |  |  |         })) { | 
 |  |  |             return false; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 完结上一条命令 | 
 |  |  |         boolean updateCommand = false; | 
 |  |  |         if (commandStep != 0) { | 
 |  |  |             ShuttleCommand command = commands.get(commandStep - 1); | 
 |  |  |             if (command.getMode() == ShuttleCommandModeType.MOVE.id) { | 
 |  |  |                 // 正常移动 | 
 |  |  |                 if (command.getTargetLocNo().equals(shuttleProtocol.getCurrentLocNo())) { | 
 |  |  |                     command.setComplete(true); | 
 |  |  |                     updateCommand = true; | 
 |  |  |                     //解锁锁定路径,上一条路径 | 
 |  |  |                     List<NavigateNode> nodes = null; | 
 |  |  |                     try { | 
 |  |  | 
 |  |  |                 //判断是否顶升到位 | 
 |  |  |                 if (shuttleProtocol.getHasLift()) { | 
 |  |  |                     command.setComplete(true); | 
 |  |  |                     updateCommand = true; | 
 |  |  | //                    //判断是否有物 | 
 |  |  | //                    if (shuttleProtocol.getHasPallet()) { | 
 |  |  | //                        command.setComplete(true); | 
 |  |  | 
 |  |  |                 // 判断是否下降到位 | 
 |  |  |                 if (!shuttleProtocol.getHasLift()) { | 
 |  |  |                     command.setComplete(true); | 
 |  |  |                     updateCommand = true; | 
 |  |  |                 } | 
 |  |  |             } else if (command.getMode() == ShuttleCommandModeType.CHARGE_OPEN.id) { | 
 |  |  |                 // 充电开 | 
 |  |  |                 //判断小车充电状态 | 
 |  |  |                 if (shuttleProtocol.getHasCharge()) { | 
 |  |  |                     command.setComplete(true); | 
 |  |  |                     updateCommand = true; | 
 |  |  |                 } | 
 |  |  |             }else { | 
 |  |  |                 command.setComplete(true);//其他命令默认认为完成 | 
 |  |  |                 updateCommand = true; | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             // 更新redis数据 | 
 |  |  |             redisUtil.set(DeviceRedisConstant.SHUTTLE_WORK_FLAG + redisCommand.getTaskNo(), JSON.toJSONString(redisCommand, SerializerFeature.DisableCircularReferenceDetect)); | 
 |  |  |             if(updateCommand) { | 
 |  |  |                 // 更新redis数据 | 
 |  |  |                 redisUtil.set(DeviceRedisConstant.SHUTTLE_WORK_FLAG + redisCommand.getTaskNo(), JSON.toJSONString(redisCommand, SerializerFeature.DisableCircularReferenceDetect)); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             if (!command.getComplete()) { | 
 |  |  |                 return false; | 
 |  |  | 
 |  |  |         //取出命令 | 
 |  |  |         ShuttleCommand command = commands.get(commandStep); | 
 |  |  |  | 
 |  |  |         Integer mode = command.getMode(); | 
 |  |  |         //判断设备是否空闲 | 
 |  |  |         if (!shuttleThread.isDeviceIdle(new ExecuteSupport() { | 
 |  |  |             @Override | 
 |  |  |             public Boolean judgement() { | 
 |  |  |                 if (ShuttleCommandModeType.CHARGE_CLOSE.id.equals(mode)) {//关闭充电motion | 
 |  |  |                     return false;//不需要判断状态 | 
 |  |  |                 } | 
 |  |  |                 return true;//需要判断状态 | 
 |  |  |             } | 
 |  |  |         })) { | 
 |  |  |             return false; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 下发命令 | 
 |  |  |         if (!write(command, device)) { | 
 |  |  |         CommandResponse response = write(command, device); | 
 |  |  |  | 
 |  |  |         //保存命令日志 | 
 |  |  |         BasShuttleOpt basShuttleOpt = new BasShuttleOpt(); | 
 |  |  |         basShuttleOpt.setTaskNo(redisCommand.getTaskNo()); | 
 |  |  |         basShuttleOpt.setShuttleNo(Integer.parseInt(device.getDeviceNo())); | 
 |  |  |  | 
 |  |  |         basShuttleOpt.setMode(ShuttleTaskModeType.get(assignCommand.getTaskMode()).desc); | 
 |  |  |         basShuttleOpt.setSourceLocNo(assignCommand.getSourceLocNo()); | 
 |  |  |         basShuttleOpt.setDistLocNo(assignCommand.getLocNo()); | 
 |  |  |         basShuttleOpt.setCommand(JSON.toJSONString(command)); | 
 |  |  |         basShuttleOpt.setSystemStatus(JSON.toJSONString(shuttleProtocol)); | 
 |  |  |         basShuttleOpt.setDeviceId(device.getId()); | 
 |  |  |         basShuttleOpt.setDeviceWrk(String.valueOf(command.getTaskNo())); | 
 |  |  |         basShuttleOpt.setResponse(response.getMessage());//获取响应 | 
 |  |  |         basShuttleOpt.setSendTime(new Date());//指令下发时间 | 
 |  |  |         basShuttleOpt.setSend(response.getResult() ? 1 : 0); | 
 |  |  |         basShuttleOpt.setHostId(device.getHostId()); | 
 |  |  |         //保存命令流水 | 
 |  |  |         basShuttleOptService.save(basShuttleOpt); | 
 |  |  |  | 
 |  |  |         if (!response.getResult()) { | 
 |  |  |             News.error("四向穿梭车命令下发失败,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command)); | 
 |  |  |             return false; | 
 |  |  |         } | 
 |  |  | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private synchronized boolean write(ShuttleCommand command, Device device) { | 
 |  |  |     private synchronized CommandResponse write(ShuttleCommand command, Device device) { | 
 |  |  |         CommandResponse response = new CommandResponse(false); | 
 |  |  |         if (null == command) { | 
 |  |  |             News.error("四向穿梭车写入命令为空"); | 
 |  |  |             return false; | 
 |  |  |             response.setMessage("四向穿梭车写入命令为空"); | 
 |  |  |             return response; | 
 |  |  |         } | 
 |  |  |         ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, device.getId().intValue()); | 
 |  |  |         boolean result = false; | 
 |  |  |         if (command.getMode() == ShuttleCommandModeType.MOVE.id | 
 |  |  |                 || command.getMode() == ShuttleCommandModeType.IN_LIFT.id | 
 |  |  |                 || command.getMode() == ShuttleCommandModeType.OUT_LIFT.id) {//移动 | 
 |  |  |             result = shuttleThread.move(command); | 
 |  |  |             response = shuttleThread.move(command); | 
 |  |  |         } else if (command.getMode() == ShuttleCommandModeType.PALLET_LIFT.id | 
 |  |  |                 || command.getMode() == ShuttleCommandModeType.PALLET_DOWN.id) {//顶升 | 
 |  |  |             result = shuttleThread.lift(command); | 
 |  |  |             response = shuttleThread.lift(command); | 
 |  |  |         } else if (command.getMode() == ShuttleCommandModeType.CHARGE_OPEN.id | 
 |  |  |                 || command.getMode() == ShuttleCommandModeType.CHARGE_CLOSE.id) {//充电 | 
 |  |  |             result = shuttleThread.charge(command); | 
 |  |  |             response = shuttleThread.charge(command); | 
 |  |  |         } else if (command.getMode() == ShuttleCommandModeType.RESET.id) {//复位 | 
 |  |  |             result = shuttleThread.reset(command); | 
 |  |  |             response = shuttleThread.reset(command); | 
 |  |  |         } else if (command.getMode() == ShuttleCommandModeType.UPDATE_LOCATION.id) {//更新坐标 | 
 |  |  |             result = shuttleThread.updateLocation(command); | 
 |  |  |             response = shuttleThread.updateLocation(command); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         News.info("四向穿梭车命令下发,穿梭车号={},任务数据={}", device.getDeviceNo(), JSON.toJSON(command)); | 
 |  |  |         return result; | 
 |  |  | //        News.info("四向穿梭车命令下发,穿梭车号={},任务数据={}", device.getDeviceNo(), JSON.toJSON(command)); | 
 |  |  |         return response; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |