csw 5 months ago
parent
commit
6a103bdbae

+ 13 - 0
src/main/java/com/ecnu/platform/service/AlgExecutionLogService.groovy

@@ -1,6 +1,9 @@
 package com.ecnu.platform.service
 
+import com.ecnu.alg.pojo.path.data.AlgPathStrategyInputData
 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.SelectOption
 import com.three.data_api.dm.annos.BizApiMethod
@@ -50,6 +53,16 @@ class AlgExecutionLogService extends BaseEntityServiceImpl {
         return findOneObject(EN_AlgExecutionLog, gkey)
     }
 
+    @BizApiMethod(matchedApis = ThreeConstant.BIZ_API_NAME, desc = "结果展示")
+    @Doc4MethodRes(key = "result", desc = "算法执行日志", refClassName = EN_AlgExecutionLog)
+    Object showResult(@BizRequestPm(key = "gkey", nullable = false, desc = "算法执行日志主键") String gkey) throws Exception {
+        Map<String, Object> resultMap = new HashMap<>()
+        // 根据日志主键查询日志记录,返回输入数据、算法结果
+        Object object = findOneObject(EN_AlgExecutionLog, gkey)
+
+        return object
+    }
+
     @Override
     List<SelectOption> getSelectOptions(Map<String, Object> map) throws Exception {
         return super.getSelectOptions(map)

+ 124 - 0
src/main/java/com/ecnu/platform/service/AlgExecutionService.groovy

@@ -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
+    }
+}

+ 55 - 4
src/main/java/com/ecnu/platform/service/PathStrategyDataService.groovy

@@ -1,5 +1,6 @@
 package com.ecnu.platform.service
 
+import com.ecnu.alg.pojo.path.data.AlgPathStrategyInputData
 import com.ecnu.alg.pojo.path.inputs.AlgBaseMap
 import com.ecnu.alg.pojo.path.inputs.AlgBaseMapArea
 import com.ecnu.alg.pojo.path.inputs.AlgObstacle
@@ -7,6 +8,7 @@ import com.ecnu.alg.pojo.path.inputs.AlgParkSpace
 import com.ecnu.alg.pojo.path.inputs.AlgVehicle
 import com.ecnu.alg.pojo.path.inputs.AlgVehicleTask
 import com.three.common.exception.ParamException
+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.annos.Doc4MethodRes
@@ -42,9 +44,31 @@ class PathStrategyDataService {
 
     @BizApiMethod(matchedApis = ThreeConstant.BIZ_API_NAME, desc = "初始化底图信息")
     @Doc4MethodRes(key = "result", desc = "底图信息", refClasses = AlgBaseMap.class)
-    AlgBaseMap initBaseMap(@BizRequestPm(key = "body", nullable = false, desc = "底图信息") AlgBaseMap algBaseMap) throws Exception {
-        redisUtil.set(AlgBaseMapKey + algBaseMap.getId(), algBaseMap)
-        return algBaseMap
+    AlgPathStrategyInputData initData(@BizRequestPm(key = "body", nullable = false, desc = "底图信息") AlgPathStrategyInputData algPathStrategyInputData) throws Exception {
+        AlgBaseMap algBaseMap = algPathStrategyInputData.getAlgBaseMap();
+        if (algBaseMap != null && StringUtil.isNotBlank(algBaseMap.getId())) {
+            String mapId = algBaseMap.getId();
+            redisUtil.set(AlgBaseMapKey + algBaseMap.getId(), algBaseMap)
+            // 车辆信息
+            for (AlgVehicle algVehicle : algPathStrategyInputData.getAlgVehicleList()) {
+                redisUtil.set(AlgVehicleKey + mapId + conn + algVehicle.getVehicleNo(), algVehicle)
+            }
+            // 车辆任务信息
+            for (AlgVehicleTask algVehicleTask : algPathStrategyInputData.getAlgVehicleTaskList()) {
+                redisUtil.set(AlgVehicleTaskKey + mapId + conn + algVehicleTask.getVehicleNo(), algVehicleTask)
+            }
+            // 障碍物信息
+            for (AlgObstacle algObstacle : algPathStrategyInputData.getAlgObstacleList()) {
+                redisUtil.set(AlgObstacleKey + mapId + conn + algObstacle.getId(), algObstacle)
+            }
+            // 停车位信息
+            for (AlgParkSpace algParkSpace : algPathStrategyInputData.getAlgParkSpaceList()) {
+                redisUtil.set(AlgParkSpaceKey + mapId + conn + algParkSpace.getId(), algParkSpace)
+            }
+        } else {
+            throw new ParamException("底图信息为空")
+        }
+        return algPathStrategyInputData
     }
 
     @BizApiMethod(matchedApis = ThreeConstant.BIZ_API_NAME, desc = "查询所有底图信息")
@@ -172,7 +196,34 @@ class PathStrategyDataService {
     @Doc4MethodRes(key = "result", desc = "底图信息", refClasses = AlgVehicleTask.class)
     AlgVehicleTask queryVehicleTask(@BizRequestPm(key = "mapId", nullable = false, desc = "底图ID") String mapId,
                                     @BizRequestPm(key = "vehicleNo", nullable = false, desc = "车辆编号") String vehicleNo) throws Exception {
-        return redisUtil.getByPrefix(AlgVehicleTaskKey + mapId + conn + vehicleNo) as AlgVehicleTask
+        return redisUtil.get(AlgVehicleTaskKey + mapId + conn + vehicleNo) as AlgVehicleTask
+    }
+
+    @BizApiMethod(matchedApis = ThreeConstant.BIZ_API_NAME, desc = "清空数据")
+    void deleteAll(@BizRequestPm(key = "mapId", nullable = false, desc = "底图ID") String mapId) throws Exception {
+        redisUtil.getRedisTemplate().delete(redisUtil.getKeyByPrefix(AlgParkSpaceKey + mapId + conn + "*"))
+        redisUtil.getRedisTemplate().delete(redisUtil.getKeyByPrefix(AlgObstacleKey + mapId + conn + "*"))
+        redisUtil.getRedisTemplate().delete(redisUtil.getKeyByPrefix(AlgBaseMapAreaKey + mapId + conn + "*"))
+        redisUtil.getRedisTemplate().delete(redisUtil.getKeyByPrefix(AlgVehicleKey + mapId + conn + "*"))
+        redisUtil.getRedisTemplate().delete(redisUtil.getKeyByPrefix(AlgVehicleTaskKey + mapId + conn + "*"))
+    }
+
+    @BizApiMethod(matchedApis = ThreeConstant.BIZ_API_NAME, desc = "查询数据")
+    AlgPathStrategyInputData queryAll(@BizRequestPm(key = "mapId", nullable = false, desc = "底图ID") String mapId) throws Exception {
+        AlgPathStrategyInputData algPathStrategyInputData = new AlgPathStrategyInputData();
+        AlgBaseMap algBaseMap = redisUtil.get(AlgBaseMapKey + mapId) as AlgBaseMap;
+        List<AlgVehicle> algVehicleList = redisUtil.getByPrefix(AlgVehicleKey + mapId + conn + "*") as List<AlgVehicle>
+        List<AlgVehicleTask> algVehicleTaskList = redisUtil.getByPrefix(AlgVehicleTaskKey + mapId + conn + "*") as List<AlgVehicleTask>
+        List<AlgBaseMapArea> algBaseMapAreaList = redisUtil.getByPrefix(AlgBaseMapAreaKey + mapId + conn + "*") as List<AlgBaseMapArea>
+        List<AlgObstacle> algObstacleList = redisUtil.getByPrefix(AlgObstacleKey + mapId + conn + "*") as List<AlgObstacle>
+        List<AlgParkSpace> algParkSpaceList = redisUtil.getByPrefix(AlgParkSpaceKey + mapId + conn + "*") as List<AlgParkSpace>
+        algPathStrategyInputData.setAlgBaseMap(algBaseMap)
+        algPathStrategyInputData.setAlgVehicleList(algVehicleList)
+        algPathStrategyInputData.setAlgVehicleTaskList(algVehicleTaskList)
+        algPathStrategyInputData.setAlgBaseMapAreaList(algBaseMapAreaList)
+        algPathStrategyInputData.setAlgObstacleList(algObstacleList)
+        algPathStrategyInputData.setAlgParkSpaceList(algParkSpaceList)
+        return algPathStrategyInputData
     }
 
 }