|
@@ -1,14 +1,20 @@
|
|
|
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.ecnu.alg.pojo.path.data.AlgPathStrategyResultData
|
|
|
+import com.ecnu.alg.pojo.path.results.AlgReMessage
|
|
|
+import com.ecnu.alg.pojo.port.datas.AlgCwpInputData
|
|
|
+import com.ecnu.alg.pojo.port.datas.AlgCwpResultData
|
|
|
+import com.ecnu.alg.pojo.port.results.AlgReCwp
|
|
|
+import com.ecnu.alg.pojo.port.results.AlgReCwpBlock
|
|
|
+import com.ecnu.platform.cwp.CwpDataService
|
|
|
+import com.ecnu.platform.path.PathStrategyDataService
|
|
|
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.common.vo.ResponseData
|
|
|
import com.three.data_api.dm.annos.BizApiMethod
|
|
|
import com.three.data_api.dm.annos.BizRequestPm
|
|
|
import com.three.data_api.dm.constants.ThreeConstant
|
|
@@ -26,10 +32,11 @@ import org.springframework.http.HttpMethod
|
|
|
import org.springframework.http.ResponseEntity
|
|
|
import org.springframework.web.client.RestTemplate
|
|
|
|
|
|
-import java.lang.reflect.Method
|
|
|
|
|
|
class AlgExecutionService {
|
|
|
|
|
|
+ public static final String AlgReCwpBlockKey = "port:AlgReCwpBlock:"
|
|
|
+
|
|
|
private static Logger logger = LoggerFactory.getLogger(AlgExecutionService.class)
|
|
|
|
|
|
@Autowired
|
|
@@ -41,11 +48,11 @@ class AlgExecutionService {
|
|
|
@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) {
|
|
|
+ @BizApiMethod(matchedApis = ThreeConstant.BIZ_API_NAME, desc = "执行算法")
|
|
|
+ void exeAlg(@BizRequestPm(key = "id", nullable = false, desc = "底图ID/船舶航次主键(多个用英文逗号隔开)") String id,
|
|
|
+ @BizRequestPm(key = "versionGkey", nullable = false, desc = "算法版本主键") String versionGkey) {
|
|
|
// 查询算法版本信息
|
|
|
- Object algVersionObj = daoService.findOneByGkey(AlgVersionVoService.EN_AlgVersion, versionGkey)
|
|
|
+ Object algVersionObj = daoService.findOneByGkey(AlgVersionVoService.EN_AlgVersionVo, versionGkey)
|
|
|
if (algVersionObj == null) {
|
|
|
throw new ParamException("算法版本不存在")
|
|
|
}
|
|
@@ -62,53 +69,87 @@ class AlgExecutionService {
|
|
|
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("shipId", id)
|
|
|
valueMap.put("runVersion", GroovyUtil.getProperty(algVersionObj, "version"))
|
|
|
long st = System.currentTimeMillis()
|
|
|
try {
|
|
|
- logger.info("路线规划算法开始执行。。。")
|
|
|
- HttpHeaders headers = new HttpHeaders()
|
|
|
+ logger.info("算法[{}:{}]执行开始。。。", GroovyUtil.getProperty(algVersionObj, "algDefId"), GroovyUtil.getProperty(algVersionObj, "version"))
|
|
|
// 获取账号token
|
|
|
+ HttpHeaders headers = new HttpHeaders()
|
|
|
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())
|
|
|
+ // restTemplate
|
|
|
+ RestTemplate restTemplate = new RestTemplate()
|
|
|
+ if ("PathStrategy".equals(GroovyUtil.getProperty(algVersionObj, "algDefId"))) {
|
|
|
+ // 根据ID,查询算法输入数据
|
|
|
+ PathStrategyDataService pathStrategyDataService = beanService.getBeanById(PathStrategyDataService.class.getSimpleName());
|
|
|
+ AlgPathStrategyInputData algPathStrategyInputData = pathStrategyDataService.queryAll(id)
|
|
|
+ String inDataStr = GsonUtil.toJson(algPathStrategyInputData)
|
|
|
+ valueMap.put("inData", inDataStr)
|
|
|
+ HttpEntity<AlgPathStrategyInputData> request = new HttpEntity<>(algPathStrategyInputData, headers)
|
|
|
+ // 调用算法
|
|
|
+ ResponseEntity<AlgPathStrategyResultData> result = restTemplate.exchange(serviceAddress + "/" + interfaceName, HttpMethod.POST, request, AlgPathStrategyResultData.class)
|
|
|
+ // 算法输出数据
|
|
|
+ AlgPathStrategyResultData algPathStrategyOutputData = result.getBody()
|
|
|
+ String outDataStr = GsonUtil.toJson(algPathStrategyOutputData);
|
|
|
+ valueMap.put("outData", outDataStr)
|
|
|
+ AlgReMessage algReMessage = algPathStrategyOutputData.getAlgReMessage()
|
|
|
+ // 记录算法日志
|
|
|
+ if (algReMessage != null) {
|
|
|
+ valueMap.put("exeLog", algReMessage.getExeLog())
|
|
|
+ valueMap.put("errorLog", algReMessage.getErrorLog())
|
|
|
+ }
|
|
|
+ } else if ("Cwp".equals(GroovyUtil.getProperty(algVersionObj, "algDefId"))) {
|
|
|
+ // 根据ID,查询算法输入数据
|
|
|
+ CwpDataService cwpDataService = beanService.getBeanById(CwpDataService.class.getSimpleName());
|
|
|
+ AlgCwpInputData algCwpInputData = cwpDataService.queryAll(id)
|
|
|
+ String inDataStr = GsonUtil.toJson(algCwpInputData)
|
|
|
+ valueMap.put("inData", inDataStr)
|
|
|
+ HttpEntity<AlgCwpInputData> request = new HttpEntity<>(algCwpInputData, headers)
|
|
|
+ // 调用算法
|
|
|
+ ResponseEntity<ResponseData> result = restTemplate.exchange(serviceAddress + "/" + interfaceName, HttpMethod.POST, request, ResponseData.class)
|
|
|
+ ResponseData responseData = result.getBody()
|
|
|
+ // 算法输出数据
|
|
|
+ String outDataStr = GsonUtil.toJson(responseData.getResult());
|
|
|
+ AlgCwpResultData algCwpResultData = GsonUtil.fromJsonObject(outDataStr, AlgCwpResultData.class)
|
|
|
+ // 将算法结果保存到缓存中
|
|
|
+ saveCwpResult(algCwpResultData)
|
|
|
+ valueMap.put("outData", outDataStr)
|
|
|
+ com.ecnu.alg.pojo.port.results.AlgReMessage algReMessage = algCwpResultData.getAlgReMessage()
|
|
|
+ // 记录算法日志
|
|
|
+ if (algReMessage != null) {
|
|
|
+ valueMap.put("exeLog", algReMessage.getExecuteLog())
|
|
|
+ valueMap.put("errorLog", algReMessage.getErrorLog())
|
|
|
+ }
|
|
|
+ } else if ("AutoStow".equals(GroovyUtil.getProperty(algVersionObj, "algDefId"))) {
|
|
|
+
|
|
|
}
|
|
|
valueMap.put("exeState", "OK")
|
|
|
+ logger.info("算法[{}:{}]执行结束。", GroovyUtil.getProperty(algVersionObj, "algDefId"), GroovyUtil.getProperty(algVersionObj, "version"))
|
|
|
} catch (Exception e) {
|
|
|
- logger.info("路线规划算法执行异常:", e)
|
|
|
+ logger.info("算法[{}:{}]执行异常:", GroovyUtil.getProperty(algVersionObj, "algDefId"), GroovyUtil.getProperty(algVersionObj, "version"), 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>())
|
|
|
-
|
|
|
- return algPathStrategyOutputData
|
|
|
+ void saveCwpResult(AlgCwpResultData algCwpResultData) {
|
|
|
+ for (AlgReCwp algReCwp : algCwpResultData.getAlgReCwpList()) {
|
|
|
+ List<AlgReCwpBlock> algReCwpBlockList = algReCwp.getAlgReCwpBlockList()
|
|
|
+ for (AlgReCwpBlock algReCwpBlock : algReCwpBlockList) {
|
|
|
+ algReCwpBlock.setTmlCvGkey(algReCwp.getVesselVisitId())
|
|
|
+ }
|
|
|
+ redisUtil.set(AlgReCwpBlockKey + algReCwp.getVesselVisitId(), algReCwpBlockList)
|
|
|
+ // 默认取第一个结果
|
|
|
+ break
|
|
|
+ }
|
|
|
}
|
|
|
}
|