|
@@ -0,0 +1,124 @@
|
|
|
+package com.ecnu.platform.service
|
|
|
+
|
|
|
+import com.ecnu.alg.pojo.path.data.AlgPathStrategyInputData
|
|
|
+import com.ecnu.alg.pojo.path.data.AlgPathStrategyOutputData
|
|
|
+import com.ecnu.alg.pojo.path.inputs.AlgVehicleTask
|
|
|
+import com.ecnu.alg.pojo.path.results.AlgRePathResult
|
|
|
+import com.three.common.constants.RedisKeyPrefix
|
|
|
+import com.three.common.exception.ParamException
|
|
|
+import com.three.common.utils.GroovyUtil
|
|
|
+import com.three.common.utils.GsonUtil
|
|
|
+import com.three.common.utils.StringUtil
|
|
|
+import com.three.data_api.dm.annos.BizApiMethod
|
|
|
+import com.three.data_api.dm.annos.BizRequestPm
|
|
|
+import com.three.data_api.dm.constants.ThreeConstant
|
|
|
+import com.three.data_api.dm.context.LoginUserUtil
|
|
|
+import com.three.data_api.dm.service.BeanService
|
|
|
+import com.three.data_api.dm.service.DaoService
|
|
|
+import com.three.redis.utils.RedisUtil
|
|
|
+import org.apache.commons.lang3.exception.ExceptionUtils
|
|
|
+import org.slf4j.Logger
|
|
|
+import org.slf4j.LoggerFactory
|
|
|
+import org.springframework.beans.factory.annotation.Autowired
|
|
|
+import org.springframework.http.HttpEntity
|
|
|
+import org.springframework.http.HttpHeaders
|
|
|
+import org.springframework.http.HttpMethod
|
|
|
+import org.springframework.http.ResponseEntity
|
|
|
+import org.springframework.web.client.RestTemplate
|
|
|
+
|
|
|
+import java.lang.reflect.Method
|
|
|
+
|
|
|
+class AlgExecutionService {
|
|
|
+
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(AlgExecutionService.class)
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DaoService daoService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisUtil redisUtil;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BeanService beanService;
|
|
|
+
|
|
|
+ @BizApiMethod(matchedApis = ThreeConstant.BIZ_API_NAME, desc = "执行路线规划算法")
|
|
|
+ void exePathStrategy(@BizRequestPm(key = "mapId", nullable = false, desc = "底图ID") String mapId,
|
|
|
+ @BizRequestPm(key = "versionGkey", nullable = false, desc = "算法版本主键") String versionGkey) {
|
|
|
+ // 查询算法版本信息
|
|
|
+ Object algVersionObj = daoService.findOneByGkey(AlgVersionVoService.EN_AlgVersion, versionGkey)
|
|
|
+ if (algVersionObj == null) {
|
|
|
+ throw new ParamException("算法版本不存在")
|
|
|
+ }
|
|
|
+ // 得到请求地址和接口
|
|
|
+ Integer isCall = GroovyUtil.getProperty(algVersionObj, "isCall") as Integer
|
|
|
+ String serviceAddress = GroovyUtil.getProperty(algVersionObj, "serviceAddress")
|
|
|
+ String interfaceName = GroovyUtil.getProperty(algVersionObj, "interfaceName")
|
|
|
+ if (Objects.equals(isCall, 0)) {
|
|
|
+ throw new ParamException("算法是不可调用状态")
|
|
|
+ }
|
|
|
+ if (StringUtil.isBlank(serviceAddress)) {
|
|
|
+ throw new ParamException("算法版本服务地址信息为空")
|
|
|
+ }
|
|
|
+ if (StringUtil.isBlank(interfaceName)) {
|
|
|
+ throw new ParamException("算法版本接口名称为空")
|
|
|
+ }
|
|
|
+ // 根据底图ID,查询路线规划算法需要的数据
|
|
|
+ Object pathStrategyDataService = beanService.getBeanById("PathStrategyDataService");
|
|
|
+ Method method = pathStrategyDataService.getClass().getMethod("queryAll", String.class)
|
|
|
+ AlgPathStrategyInputData algPathStrategyInputData = method.invoke(pathStrategyDataService, mapId) as AlgPathStrategyInputData
|
|
|
+ String inDataStr = GsonUtil.toJson(algPathStrategyInputData)
|
|
|
+ // 调用算法接口
|
|
|
+ Map<String, Object> valueMap = new HashMap<>()
|
|
|
+ valueMap.put("versionGkey", versionGkey)
|
|
|
+ valueMap.put("shipId", mapId)
|
|
|
+ valueMap.put("runVersion", GroovyUtil.getProperty(algVersionObj, "version"))
|
|
|
+ long st = System.currentTimeMillis()
|
|
|
+ try {
|
|
|
+ logger.info("路线规划算法开始执行。。。")
|
|
|
+ HttpHeaders headers = new HttpHeaders()
|
|
|
+ // 获取账号token
|
|
|
+ String reToken = (String) redisUtil.get(RedisKeyPrefix.AUTH_KEY_PREFIX_USER_TOKEN + LoginUserUtil.getLoginUsername() + RedisKeyPrefix.tokenKeyCon + "web");
|
|
|
+ headers.add("Authorization", "Bearer " + reToken)
|
|
|
+ HttpEntity<AlgPathStrategyInputData> request = new HttpEntity<>(algPathStrategyInputData, headers)
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ ResponseEntity<AlgPathStrategyOutputData> result = restTemplate.exchange(serviceAddress + "/" + interfaceName, HttpMethod.POST, request, AlgPathStrategyOutputData.class)
|
|
|
+ AlgPathStrategyOutputData algPathStrategyOutputData = result.getBody()
|
|
|
+ String outDataStr = GsonUtil.toJson(algPathStrategyOutputData);
|
|
|
+ valueMap.put("outData", outDataStr)
|
|
|
+ logger.info("路线规划算法返回结果:{}", outDataStr)
|
|
|
+ // 记录算法日志
|
|
|
+ if (algPathStrategyOutputData.getAlgReMessage() != null) {
|
|
|
+ valueMap.put("exeLog", algPathStrategyOutputData.getAlgReMessage().getExeLog())
|
|
|
+ valueMap.put("errorLog", algPathStrategyOutputData.getAlgReMessage().getErrorLog())
|
|
|
+ }
|
|
|
+ valueMap.put("exeState", "OK")
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.info("路线规划算法执行异常:", e)
|
|
|
+ valueMap.put("exeState", "ERROR")
|
|
|
+ valueMap.put("errorLog", ExceptionUtils.getStackTrace(e))
|
|
|
+ }
|
|
|
+ long et = System.currentTimeMillis()
|
|
|
+ valueMap.put("costTime", et - st)
|
|
|
+ valueMap.put("callSource", "test")
|
|
|
+ valueMap.put("inData", inDataStr)
|
|
|
+ // 记录算法执行日志
|
|
|
+ daoService.insertMetaEntityDefault(AlgExecutionLogService.EN_AlgExecutionLog, valueMap)
|
|
|
+ }
|
|
|
+
|
|
|
+ AlgPathStrategyOutputData doPathStrategy(AlgPathStrategyInputData algPathStrategyInputData) {
|
|
|
+ AlgPathStrategyOutputData algPathStrategyOutputData = new AlgPathStrategyOutputData()
|
|
|
+ algPathStrategyOutputData.setAlgRePathResultList(new ArrayList<AlgRePathResult>())
|
|
|
+ for (AlgVehicleTask algVehicleTask : algPathStrategyInputData.getAlgVehicleTaskList()) {
|
|
|
+ AlgRePathResult algRePathResult = new AlgRePathResult()
|
|
|
+ algRePathResult.setVehicleNo(algVehicleTask.getVehicleNo())
|
|
|
+ algRePathResult.setPosition(algVehicleTask.getStartPosition())
|
|
|
+ algPathStrategyOutputData.getAlgRePathResultList().add(algRePathResult);
|
|
|
+ //
|
|
|
+ AlgRePathResult algRePathResult1 = new AlgRePathResult()
|
|
|
+ algRePathResult1.setVehicleNo(algVehicleTask.getVehicleNo())
|
|
|
+ algRePathResult1.setPosition(algVehicleTask.getEndPosition())
|
|
|
+ algPathStrategyOutputData.getAlgRePathResultList().add(algRePathResult1);
|
|
|
+ }
|
|
|
+ return algPathStrategyOutputData
|
|
|
+ }
|
|
|
+}
|