@Autowired | @Autowired | ||||
private IDspService dspService; | private IDspService dspService; | ||||
@Value("${tuoheng.dsp-callback-url:}") | |||||
private String dspCallbackUrl; | |||||
public JsonResult updateFlightStatus(Inspection inspection, MissionStatusRequest missionStatusRequest) { | public JsonResult updateFlightStatus(Inspection inspection, MissionStatusRequest missionStatusRequest) { | ||||
Inspection inspectionUpdate = new Inspection(); | Inspection inspectionUpdate = new Inspection(); | ||||
inspectionUpdate.setId(inspection.getId()); | inspectionUpdate.setId(inspection.getId()); | ||||
JsonResult result = startAI(missionStatusRequest); | |||||
JsonResult result = dspService.startAI(missionStatusRequest); | |||||
if (0 != result.getCode()) { | if (0 != result.getCode()) { | ||||
//如果调用DSP失败就直接失败! | //如果调用DSP失败就直接失败! | ||||
log.info("修改任务状态业务接口:调用DSP数据失败:{}", result.getMsg()); | log.info("修改任务状态业务接口:调用DSP数据失败:{}", result.getMsg()); | ||||
return JsonResult.success(); | return JsonResult.success(); | ||||
} | } | ||||
/** | |||||
* 调用DSP接口 | |||||
* | |||||
* @param missionStatusRequest | |||||
* @return | |||||
*/ | |||||
private JsonResult startAI(MissionStatusRequest missionStatusRequest) { | |||||
log.info("进入调用DSP接口方法"); | |||||
JSONObject request = new JSONObject(); | |||||
JSONArray configList = new JSONArray(); | |||||
JSONObject instConfigMode = new JSONObject(); | |||||
instConfigMode.put("ename", "service_mode"); | |||||
instConfigMode.put("evalue", "1"); | |||||
configList.add(instConfigMode); | |||||
JSONObject instConfigUrl = new JSONObject(); | |||||
instConfigUrl.put("ename", "callback_url"); | |||||
instConfigUrl.put("evalue", dspCallbackUrl); | |||||
configList.add(instConfigUrl); | |||||
request.put("serviceInstConfigList", configList); | |||||
JSONArray reqList = new JSONArray(); | |||||
JSONObject instReqUrlPush = new JSONObject(); | |||||
instReqUrlPush.put("ename", "push_url"); | |||||
instReqUrlPush.put("evalue", missionStatusRequest.getPushUrl()); | |||||
reqList.add(instReqUrlPush); | |||||
JSONObject instReqUrlPull = new JSONObject(); | |||||
instReqUrlPull.put("ename", "pull_url"); | |||||
instReqUrlPull.put("evalue", missionStatusRequest.getPullUrl()); | |||||
reqList.add(instReqUrlPull); | |||||
request.put("serviceInstReqList", reqList); | |||||
JSONObject responseJson = dspService.serviceInstApplication(request); | |||||
if (responseJson.getIntValue("code") == 0) { | |||||
String requestId = responseJson.getJSONObject("data").getString("requestId"); | |||||
log.info("dsp实时调用响应requestId:{}", requestId); | |||||
} else { | |||||
log.error("dsp实时调用失败:{}", responseJson); | |||||
return JsonResult.error(EditInspectionStatusCodeEnum.TASK_ANALYSIS_FAILED.getCode(), EditInspectionStatusCodeEnum.TASK_ANALYSIS_FAILED.getMsg()); | |||||
} | |||||
return JsonResult.success(responseJson); | |||||
} | |||||
/** | /** | ||||
* 处理通道数据 | * 处理通道数据 | ||||
* | * |
return; | return; | ||||
} | } | ||||
//硬件停止后,停止AI分析 | |||||
stopAI(businessList.get(0).getMsgId(), inspection.getId()); | |||||
// 硬件停止后,停止AI分析 | |||||
dspService.stopAI(businessList.get(0).getMsgId(), inspection.getId()); | |||||
//获取天气信息 | //获取天气信息 | ||||
//查询业务对应的租户信息 | //查询业务对应的租户信息 | ||||
inspectionMapper.updateById(inspectionUpdate); | inspectionMapper.updateById(inspectionUpdate); | ||||
} | } | ||||
private JsonResult stopAI(String msgId, String inspectionId) { | |||||
//任务调用完成之后,调用发送通道,请求DSP关闭请求 | |||||
//调用DSP接口 | |||||
JSONObject jsonObject = new JSONObject(); | |||||
jsonObject.put("requestId", msgId); | |||||
JSONObject responseJson = new JSONObject(); | |||||
Inspection inspectionUpdate = new Inspection(); | |||||
inspectionUpdate.setId(inspectionId); | |||||
try { | |||||
responseJson = dspService.serviceStopApplication(jsonObject); | |||||
if (responseJson.getIntValue("code") == 0) { | |||||
if (responseJson.containsKey("data") && ObjectUtil.isNotEmpty(responseJson.get("data"))) { | |||||
String requestId = responseJson.getJSONObject("data").getString("requestId"); | |||||
log.info("dsp实时调用响应requestId:{}", requestId); | |||||
} else { | |||||
log.info("dsp实时调用响应:{}", responseJson); | |||||
} | |||||
return JsonResult.success(); | |||||
} else { | |||||
log.error("dsp实时调用失败:{}", responseJson); | |||||
inspectionUpdate.setAnalyseStatus(AiAnalyseStatusEnum.FAILED.getCode()); | |||||
} | |||||
inspectionMapper.updateById(inspectionUpdate); | |||||
} catch (Exception e) { | |||||
log.error("dsp实时调用失败:{}", responseJson); | |||||
inspectionUpdate.setAnalyseStatus(AiAnalyseStatusEnum.FAILED.getCode()); | |||||
inspectionMapper.updateById(inspectionUpdate); | |||||
} | |||||
return JsonResult.success(); | |||||
} | |||||
public JsonResult getWeather(String airportUrl, Integer airportId) { | public JsonResult getWeather(String airportUrl, Integer airportId) { | ||||
String url = airportUrl + SystemConstant.API_AIRPORT_GET_WEATHER; | String url = airportUrl + SystemConstant.API_AIRPORT_GET_WEATHER; | ||||
String param = "airportId=" + airportId; | String param = "airportId=" + airportId; |
package com.tuoheng.admin.service.third.dsp; | package com.tuoheng.admin.service.third.dsp; | ||||
import com.alibaba.fastjson.JSONObject; | import com.alibaba.fastjson.JSONObject; | ||||
import com.tuoheng.admin.request.inspection.MissionStatusRequest; | |||||
import com.tuoheng.common.core.utils.HttpUtils; | import com.tuoheng.common.core.utils.HttpUtils; | ||||
import com.tuoheng.common.core.utils.JsonResult; | |||||
import lombok.extern.slf4j.Slf4j; | import lombok.extern.slf4j.Slf4j; | ||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import org.springframework.beans.factory.annotation.Value; | import org.springframework.beans.factory.annotation.Value; | ||||
import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||
@Service | @Service | ||||
public class DspServiceImpl implements IDspService { | public class DspServiceImpl implements IDspService { | ||||
@Autowired | |||||
private StartAIService startAIService; | |||||
@Autowired | |||||
private StopAIService stopAIService; | |||||
/** | /** | ||||
* DSP服务API接口域名 | * DSP服务API接口域名 | ||||
*/ | */ | ||||
String url = String.format(Locale.ENGLISH, "%s/api/web/serviceInst/%s/application", dspDomainUrl, dspServiceInstId); | String url = String.format(Locale.ENGLISH, "%s/api/web/serviceInst/%s/application", dspDomainUrl, dspServiceInstId); | ||||
log.info("调用DSP服务, url:{}", url); | log.info("调用DSP服务, url:{}", url); | ||||
log.info("调用DSP服务, jsonObject:{}", jsonObject); | log.info("调用DSP服务, jsonObject:{}", jsonObject); | ||||
String result = doPost(url, jsonObject, null); | |||||
String result = SendPost.doPost(url, jsonObject, null); | |||||
log.info("调用DSP服务, result:{}", result); | log.info("调用DSP服务, result:{}", result); | ||||
return JSONObject.parseObject(result); | return JSONObject.parseObject(result); | ||||
} | } | ||||
return JSONObject.parseObject(result); | return JSONObject.parseObject(result); | ||||
} | } | ||||
/** | |||||
* 处理post请求 | |||||
* @param urlStr 请求url | |||||
* @param data 请求体数据 | |||||
* @param properties 请求头参数 | |||||
* @return | |||||
*/ | |||||
public static String doPost(String urlStr, JSONObject data, Map<String, String> properties){ | |||||
try { | |||||
log.info("请求url={}", urlStr); | |||||
log.info("请求体数据data={}", data.toJSONString()); | |||||
log.info("请求头参数properties={}", properties); | |||||
URL url = new URL(urlStr); | |||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |||||
connection.setDoInput(true); // 设置可输入 | |||||
connection.setDoOutput(true); // 设置该连接是可以输出的 | |||||
//设置连接超时时间和读取超时时间 | |||||
connection.setConnectTimeout(15000); | |||||
connection.setReadTimeout(60000); | |||||
//设置请求方式 | |||||
connection.setRequestMethod("POST"); | |||||
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); | |||||
if(null != properties && !properties.isEmpty()){ | |||||
for (String key:properties.keySet()){ | |||||
connection.setRequestProperty(key, properties.get(key)); | |||||
} | |||||
} | |||||
PrintWriter pw = new PrintWriter(new BufferedOutputStream(connection.getOutputStream())); | |||||
pw.write(data.toJSONString()); | |||||
pw.flush(); | |||||
pw.close(); | |||||
@Override | |||||
public JsonResult startAI(MissionStatusRequest missionStatusRequest) { | |||||
return startAIService.startAI(missionStatusRequest) | |||||
} | |||||
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); | |||||
String line = null; | |||||
StringBuilder result = new StringBuilder(); | |||||
while ((line = br.readLine()) != null) { // 读取数据 | |||||
result.append(line + "\n"); | |||||
} | |||||
connection.disconnect(); | |||||
return result.toString(); | |||||
} catch (Exception e) { | |||||
log.error("post请求失败"); | |||||
return "post请求失败!"; | |||||
} | |||||
@Override | |||||
public JsonResult stopAI(String msgId, String inspectionId) { | |||||
return stopAIService.stopAI(msgId, inspectionId); | |||||
} | } | ||||
} | } |
package com.tuoheng.admin.service.third.dsp; | package com.tuoheng.admin.service.third.dsp; | ||||
import com.alibaba.fastjson.JSONObject; | import com.alibaba.fastjson.JSONObject; | ||||
import com.tuoheng.admin.request.inspection.MissionStatusRequest; | |||||
import com.tuoheng.common.core.utils.JsonResult; | |||||
public interface IDspService { | public interface IDspService { | ||||
JSONObject serviceInstApplication(JSONObject jsonObject); | JSONObject serviceInstApplication(JSONObject jsonObject); | ||||
JSONObject serviceStopApplication(JSONObject jsonObject); | JSONObject serviceStopApplication(JSONObject jsonObject); | ||||
/** | |||||
* | |||||
* 调用DSP进行AI分析 | |||||
* | |||||
* @param missionStatusRequest | |||||
* @return | |||||
*/ | |||||
JsonResult startAI(MissionStatusRequest missionStatusRequest); | |||||
/** | |||||
* 硬件停止后,停止AI分析 | |||||
* @param msgId | |||||
* @param inspectionId | |||||
* @return | |||||
*/ | |||||
JsonResult stopAI(String msgId, String inspectionId); | |||||
} | } |
package com.tuoheng.admin.service.third.dsp; | |||||
import com.alibaba.fastjson.JSONObject; | |||||
import lombok.extern.slf4j.Slf4j; | |||||
import java.io.BufferedOutputStream; | |||||
import java.io.BufferedReader; | |||||
import java.io.InputStreamReader; | |||||
import java.io.PrintWriter; | |||||
import java.net.HttpURLConnection; | |||||
import java.net.URL; | |||||
import java.util.Map; | |||||
@Slf4j | |||||
public class SendPost { | |||||
/** | |||||
* 处理post请求 | |||||
* @param urlStr 请求url | |||||
* @param data 请求体数据 | |||||
* @param properties 请求头参数 | |||||
* @return | |||||
*/ | |||||
public static String doPost(String urlStr, JSONObject data, Map<String, String> properties){ | |||||
try { | |||||
log.info("请求url={}", urlStr); | |||||
log.info("请求体数据data={}", data.toJSONString()); | |||||
log.info("请求头参数properties={}", properties); | |||||
URL url = new URL(urlStr); | |||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |||||
connection.setDoInput(true); // 设置可输入 | |||||
connection.setDoOutput(true); // 设置该连接是可以输出的 | |||||
//设置连接超时时间和读取超时时间 | |||||
connection.setConnectTimeout(15000); | |||||
connection.setReadTimeout(60000); | |||||
//设置请求方式 | |||||
connection.setRequestMethod("POST"); | |||||
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); | |||||
if(null != properties && !properties.isEmpty()){ | |||||
for (String key:properties.keySet()){ | |||||
connection.setRequestProperty(key, properties.get(key)); | |||||
} | |||||
} | |||||
PrintWriter pw = new PrintWriter(new BufferedOutputStream(connection.getOutputStream())); | |||||
pw.write(data.toJSONString()); | |||||
pw.flush(); | |||||
pw.close(); | |||||
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); | |||||
String line = null; | |||||
StringBuilder result = new StringBuilder(); | |||||
while ((line = br.readLine()) != null) { // 读取数据 | |||||
result.append(line + "\n"); | |||||
} | |||||
connection.disconnect(); | |||||
return result.toString(); | |||||
} catch (Exception e) { | |||||
log.error("post请求失败"); | |||||
return "post请求失败!"; | |||||
} | |||||
} | |||||
} |
package com.tuoheng.admin.service.third.dsp; | |||||
import com.alibaba.fastjson.JSONArray; | |||||
import com.alibaba.fastjson.JSONObject; | |||||
import com.tuoheng.admin.enums.code.inspection.EditInspectionStatusCodeEnum; | |||||
import com.tuoheng.admin.request.inspection.MissionStatusRequest; | |||||
import com.tuoheng.common.core.utils.JsonResult; | |||||
import lombok.extern.slf4j.Slf4j; | |||||
import org.springframework.beans.factory.annotation.Value; | |||||
import org.springframework.stereotype.Service; | |||||
import java.util.Locale; | |||||
@Slf4j | |||||
@Service | |||||
public class StartAIService { | |||||
/** | |||||
* DSP服务API接口域名 | |||||
*/ | |||||
@Value("${tuoheng.dsp-domain-url:}") | |||||
private String dspDomainUrl; | |||||
@Value("${tuoheng.dsp-service-inst-id:}") | |||||
private String dspServiceInstId; | |||||
@Value("${tuoheng.dsp-callback-url:}") | |||||
private String dspCallbackUrl; | |||||
/** | |||||
* 调用DSP接口 | |||||
* | |||||
* @param missionStatusRequest | |||||
* @return | |||||
*/ | |||||
public JsonResult startAI(MissionStatusRequest missionStatusRequest) { | |||||
log.info("进入调用DSP接口方法"); | |||||
JSONObject request = new JSONObject(); | |||||
JSONArray configList = new JSONArray(); | |||||
JSONObject instConfigMode = new JSONObject(); | |||||
instConfigMode.put("ename", "service_mode"); | |||||
instConfigMode.put("evalue", "1"); | |||||
configList.add(instConfigMode); | |||||
JSONObject instConfigUrl = new JSONObject(); | |||||
instConfigUrl.put("ename", "callback_url"); | |||||
instConfigUrl.put("evalue", dspCallbackUrl); | |||||
configList.add(instConfigUrl); | |||||
request.put("serviceInstConfigList", configList); | |||||
JSONArray reqList = new JSONArray(); | |||||
JSONObject instReqUrlPush = new JSONObject(); | |||||
instReqUrlPush.put("ename", "push_url"); | |||||
instReqUrlPush.put("evalue", missionStatusRequest.getPushUrl()); | |||||
reqList.add(instReqUrlPush); | |||||
JSONObject instReqUrlPull = new JSONObject(); | |||||
instReqUrlPull.put("ename", "pull_url"); | |||||
instReqUrlPull.put("evalue", missionStatusRequest.getPullUrl()); | |||||
reqList.add(instReqUrlPull); | |||||
request.put("serviceInstReqList", reqList); | |||||
JSONObject responseJson = this.serviceInstApplication(request); | |||||
if (responseJson.getIntValue("code") == 0) { | |||||
String requestId = responseJson.getJSONObject("data").getString("requestId"); | |||||
log.info("dsp实时调用响应requestId:{}", requestId); | |||||
} else { | |||||
log.error("dsp实时调用失败:{}", responseJson); | |||||
return JsonResult.error(EditInspectionStatusCodeEnum.TASK_ANALYSIS_FAILED.getCode(), EditInspectionStatusCodeEnum.TASK_ANALYSIS_FAILED.getMsg()); | |||||
} | |||||
return JsonResult.success(responseJson); | |||||
} | |||||
private JSONObject serviceInstApplication(JSONObject jsonObject) { | |||||
String url = String.format(Locale.ENGLISH, "%s/api/web/serviceInst/%s/application", dspDomainUrl, dspServiceInstId); | |||||
log.info("调用DSP服务, url:{}", url); | |||||
log.info("调用DSP服务, jsonObject:{}", jsonObject); | |||||
String result = SendPost.doPost(url, jsonObject, null); | |||||
log.info("调用DSP服务, result:{}", result); | |||||
return JSONObject.parseObject(result); | |||||
} | |||||
} |
package com.tuoheng.admin.service.third.dsp; | |||||
import cn.hutool.core.util.ObjectUtil; | |||||
import com.alibaba.fastjson.JSONObject; | |||||
import com.tuoheng.admin.entity.Inspection; | |||||
import com.tuoheng.admin.enums.AiAnalyseStatusEnum; | |||||
import com.tuoheng.admin.mapper.InspectionMapper; | |||||
import com.tuoheng.common.core.utils.HttpUtils; | |||||
import com.tuoheng.common.core.utils.JsonResult; | |||||
import lombok.extern.slf4j.Slf4j; | |||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import org.springframework.beans.factory.annotation.Value; | |||||
import org.springframework.stereotype.Service; | |||||
@Slf4j | |||||
@Service | |||||
public class StopAIService { | |||||
@Autowired | |||||
private InspectionMapper inspectionMapper; | |||||
/** | |||||
* DSP服务API接口域名 | |||||
*/ | |||||
@Value("${tuoheng.dsp-domain-url:}") | |||||
private String dspDomainUrl; | |||||
@Value("${tuoheng.dsp-service-inst-id:}") | |||||
private String dspServiceInstId; | |||||
public JsonResult stopAI(String msgId, String inspectionId) { | |||||
//任务调用完成之后,调用发送通道,请求DSP关闭请求 | |||||
//调用DSP接口 | |||||
JSONObject jsonObject = new JSONObject(); | |||||
jsonObject.put("requestId", msgId); | |||||
JSONObject responseJson = new JSONObject(); | |||||
Inspection inspectionUpdate = new Inspection(); | |||||
inspectionUpdate.setId(inspectionId); | |||||
try { | |||||
responseJson = this.serviceStopApplication(jsonObject); | |||||
if (responseJson.getIntValue("code") == 0) { | |||||
if (responseJson.containsKey("data") && ObjectUtil.isNotEmpty(responseJson.get("data"))) { | |||||
String requestId = responseJson.getJSONObject("data").getString("requestId"); | |||||
log.info("dsp实时调用响应requestId:{}", requestId); | |||||
} else { | |||||
log.info("dsp实时调用响应:{}", responseJson); | |||||
} | |||||
return JsonResult.success(); | |||||
} else { | |||||
log.error("dsp实时调用失败:{}", responseJson); | |||||
inspectionUpdate.setAnalyseStatus(AiAnalyseStatusEnum.FAILED.getCode()); | |||||
} | |||||
inspectionMapper.updateById(inspectionUpdate); | |||||
} catch (Exception e) { | |||||
log.error("dsp实时调用失败:{}", responseJson); | |||||
inspectionUpdate.setAnalyseStatus(AiAnalyseStatusEnum.FAILED.getCode()); | |||||
inspectionMapper.updateById(inspectionUpdate); | |||||
} | |||||
return JsonResult.success(); | |||||
} | |||||
private JSONObject serviceStopApplication(JSONObject jsonObject) { | |||||
String url = dspDomainUrl + "api/web/serviceInst/" + jsonObject.getString("requestId") + "/stop"; | |||||
String result = HttpUtils.doSend(url, jsonObject, null, "PUT"); | |||||
return JSONObject.parseObject(result); | |||||
} | |||||
} |