| package com.zy.acs.manager.core.service; | 
|   | 
| import com.alibaba.fastjson.JSON; | 
| import com.zy.acs.framework.common.SnowflakeIdWorker; | 
| import com.zy.acs.common.constant.RedisConstant; | 
| import com.zy.acs.common.domain.AgvAction; | 
| import com.zy.acs.common.domain.AgvCommand; | 
| import com.zy.acs.common.domain.AgvProtocol; | 
| import com.zy.acs.common.domain.BaseResult; | 
| import com.zy.acs.common.domain.protocol.AGV_01_UP; | 
| import com.zy.acs.common.domain.protocol.AGV_02_UP; | 
| import com.zy.acs.common.domain.protocol.IMessageBody; | 
| import com.zy.acs.common.utils.RedisSupport; | 
| import com.zy.acs.common.utils.RequestSupport; | 
| import com.zy.acs.manager.system.service.ConfigService; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Service; | 
|   | 
| import java.util.concurrent.TimeoutException; | 
|   | 
| /** | 
|  * Created by vincent on 2023/3/22 | 
|  */ | 
| @Slf4j | 
| @Service | 
| public class AgvCmdService { | 
|   | 
|     private final RedisSupport redis = RedisSupport.defaultRedisSupport; | 
|   | 
|     @Autowired | 
|     private SnowflakeIdWorker snowflakeIdWorker; | 
|     @Autowired | 
|     private ConfigService configService; | 
|   | 
|     public BaseResult<?> executeAgvActionCmd(AgvAction agvAction) { | 
|   | 
|         // todo 1: 记录日志 2:发送任务  3:同步响应 | 
| //        log.info(JSON.toJSONString(agvAction)); | 
|   | 
|         String serialNo = agvAction.getSerialNo(); | 
|   | 
|         AgvProtocol protocol = AgvProtocol.build(agvAction.getAgvNo()).setMessageBody(agvAction.beMesBody(serialNo)); | 
|   | 
|         return this.requestProcess(serialNo, protocol | 
|                 , (RequestSupport<AGV_01_UP>) result -> result.getSerialNo().equals(serialNo)); | 
|   | 
|     } | 
|   | 
|     public BaseResult<?> executeAgvCommand(AgvCommand agvCommand) { | 
|   | 
|         // todo 1: 记录日志 2:发送任务  3:同步响应 | 
|         log.info(JSON.toJSONString(agvCommand)); | 
|   | 
|         String serialNo = agvCommand.getSerialNo(); | 
|   | 
|         AgvProtocol protocol = AgvProtocol.build(agvCommand.getAgvNo()).setMessageBody(agvCommand.beMesBody(serialNo)); | 
|   | 
|         return this.requestProcess(serialNo, protocol | 
|                 , (RequestSupport<AGV_02_UP>) result -> result.getSerialNo().equals(serialNo)); | 
|     } | 
|   | 
|     @SuppressWarnings("all") | 
|     private <T> BaseResult<?> requestProcess(String serialNo, AgvProtocol protocol, RequestSupport<T> support){ | 
|         try { | 
|             IMessageBody messageBody = executeRequest(serialNo, protocol); | 
|             if(support.success((T) messageBody)) { | 
|                 return BaseResult.ok(); | 
|             } else { | 
|                 return BaseResult.error(); | 
|             } | 
|         } catch (TimeoutException e){ | 
|             log.error("AgvCmdService.requestProcess", e); | 
|             return BaseResult.error(); | 
|         } | 
|     } | 
|   | 
|     @SuppressWarnings("all") | 
|     private IMessageBody executeRequest(String serialNo, AgvProtocol protocol) throws TimeoutException { | 
|   | 
|         redis.push(RedisConstant.AGV_PATH_DOWN_FLAG, protocol); | 
|   | 
|         IMessageBody messageBody = null; | 
|   | 
|         String redisKey = protocol.getAgvNo() + "_" + serialNo; | 
|         // 获取响应 | 
|         long startTime = System.currentTimeMillis(); | 
|         while ((System.currentTimeMillis() - startTime) < RedisConstant.CMD_TIMEOUT_LIMIT) { | 
|             if ((messageBody = redis.getObject(RedisConstant.AGV_PATH_UP_FLAG, redisKey)) != null){ | 
|                 redis.deleteObject(RedisConstant.AGV_PATH_UP_FLAG, redisKey); | 
|                 return messageBody; | 
|             } | 
|             try{ | 
|                 Thread.sleep(100); | 
|             }catch(Exception ex){ | 
|                 ex.printStackTrace(); | 
|             } | 
|         } | 
|         throw new TimeoutException(); | 
|     } | 
|   | 
| } |