|
@ -1,10 +1,12 @@ |
|
|
package org.dromara.sample.manage.service.impl; |
|
|
package org.dromara.sample.manage.service.impl; |
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.map.MapUtil; |
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
import com.fasterxml.jackson.core.JsonProcessingException; |
|
|
import com.fasterxml.jackson.core.JsonProcessingException; |
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
import io.seata.common.util.StringUtils; |
|
|
import io.seata.common.util.StringUtils; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
import org.apache.dubbo.config.annotation.DubboReference; |
|
|
import org.apache.http.client.methods.CloseableHttpResponse; |
|
|
import org.apache.http.client.methods.CloseableHttpResponse; |
|
|
import org.apache.http.client.methods.HttpGet; |
|
|
import org.apache.http.client.methods.HttpGet; |
|
|
import org.apache.http.client.methods.HttpPost; |
|
|
import org.apache.http.client.methods.HttpPost; |
|
@ -15,11 +17,13 @@ import org.apache.http.impl.client.HttpClients; |
|
|
import org.apache.http.util.EntityUtils; |
|
|
import org.apache.http.util.EntityUtils; |
|
|
import org.dromara.sample.common.util.Md5Utils; |
|
|
import org.dromara.sample.common.util.Md5Utils; |
|
|
import org.dromara.sample.manage.mapper.ISampleEngineRecordMapper; |
|
|
import org.dromara.sample.manage.mapper.ISampleEngineRecordMapper; |
|
|
import org.dromara.sample.manage.model.dto.EngineResponse; |
|
|
import org.dromara.sample.manage.mapper.ITaskJobMapper; |
|
|
import org.dromara.sample.manage.model.dto.ExtraType; |
|
|
import org.dromara.sample.manage.model.dto.*; |
|
|
import org.dromara.sample.manage.model.dto.ExtraTypeResponse; |
|
|
|
|
|
import org.dromara.sample.manage.model.entity.EngineRecordEntity; |
|
|
import org.dromara.sample.manage.model.entity.EngineRecordEntity; |
|
|
|
|
|
import org.dromara.sample.manage.model.entity.TaskJobEntity; |
|
|
import org.dromara.sample.manage.service.ISampleEngineRecordService; |
|
|
import org.dromara.sample.manage.service.ISampleEngineRecordService; |
|
|
|
|
|
import org.dromara.system.api.RemoteConfigService; |
|
|
|
|
|
import org.json.JSONArray; |
|
|
import org.json.JSONObject; |
|
|
import org.json.JSONObject; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
import org.springframework.transaction.annotation.Transactional; |
|
@ -27,10 +31,7 @@ import org.springframework.transaction.annotation.Transactional; |
|
|
import javax.annotation.Resource; |
|
|
import javax.annotation.Resource; |
|
|
import java.net.URI; |
|
|
import java.net.URI; |
|
|
import java.net.URISyntaxException; |
|
|
import java.net.URISyntaxException; |
|
|
import java.util.ArrayList; |
|
|
import java.util.*; |
|
|
import java.util.HashMap; |
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
import static org.apache.commons.compress.utils.ArchiveUtils.sanitize; |
|
|
import static org.apache.commons.compress.utils.ArchiveUtils.sanitize; |
|
|
|
|
|
|
|
@ -41,17 +42,22 @@ public class SampleEngineRecordServiceImpl implements ISampleEngineRecordService |
|
|
@Resource |
|
|
@Resource |
|
|
ISampleEngineRecordMapper mapper; |
|
|
ISampleEngineRecordMapper mapper; |
|
|
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
|
|
ITaskJobMapper taskJobMapper; |
|
|
|
|
|
|
|
|
|
|
|
@DubboReference |
|
|
|
|
|
private RemoteConfigService remoteConfigService; |
|
|
@Override |
|
|
@Override |
|
|
public Map<String, Object> startEngine(Map<String, Object> objectMap){ |
|
|
public Map<String, Object> startEngine(Map<String, Object> objectMap){ |
|
|
EngineRecordEntity record = new EngineRecordEntity(); |
|
|
EngineRecordEntity record = new EngineRecordEntity(); |
|
|
|
|
|
String ip = remoteConfigService.selectStreamIp(); |
|
|
//获取token
|
|
|
//获取token
|
|
|
//设置请求Header和Body(如JSON)
|
|
|
//设置请求Header和Body(如JSON)
|
|
|
String md5 = Md5Utils.hash("xuzhou_ai"); |
|
|
String md5 = Md5Utils.hash("xuzhou_ai"); |
|
|
JSONObject jsonObj = new JSONObject(); |
|
|
JSONObject jsonObj = new JSONObject(); |
|
|
jsonObj.put("username", sanitize("xuzhou_ai")); // sanitize函数见下文
|
|
|
jsonObj.put("username", sanitize("xuzhou_ai")); // sanitize函数见下文
|
|
|
jsonObj.put("password", sanitize(md5)); |
|
|
jsonObj.put("password", sanitize(md5)); |
|
|
String responseBody = PostRequest(jsonObj, "http://60.204.247.65:8100/Third/Login"); |
|
|
String responseBody = PostRequest(jsonObj, "http://60.204.247.65:8100/Third/Login",""); |
|
|
JSONObject jsonObject = new JSONObject(responseBody); |
|
|
JSONObject jsonObject = new JSONObject(responseBody); |
|
|
// 逐层获取 data -> token
|
|
|
// 逐层获取 data -> token
|
|
|
JSONObject data = jsonObject.getJSONObject("data"); |
|
|
JSONObject data = jsonObject.getJSONObject("data"); |
|
@ -60,96 +66,134 @@ public class SampleEngineRecordServiceImpl implements ISampleEngineRecordService |
|
|
String algorithmResponseBody = null; |
|
|
String algorithmResponseBody = null; |
|
|
//查询用户开通的场景
|
|
|
//查询用户开通的场景
|
|
|
if (StringUtils.isNotEmpty(token)){ |
|
|
if (StringUtils.isNotEmpty(token)){ |
|
|
try { |
|
|
// try {
|
|
|
URI uri = new URIBuilder("http://60.204.247.65:8100/V2/AI/WX/User/Scene") |
|
|
// URI uri = new URIBuilder("http://60.204.247.65:8100/V2/AI/WX/User/Scene")
|
|
|
.build(); |
|
|
// .build();
|
|
|
sceneResponseBody = GetRequest(uri, token); |
|
|
// sceneResponseBody = GetRequest(uri, token);
|
|
|
} catch (URISyntaxException e) { |
|
|
// } catch (URISyntaxException e) {
|
|
|
throw new RuntimeException(e); |
|
|
// throw new RuntimeException(e);
|
|
|
} |
|
|
// }
|
|
|
JSONObject jsonObject1 = new JSONObject(sceneResponseBody); |
|
|
// EngineTypeResponse engineTypeResponse = null;
|
|
|
// 逐层获取 data -> token
|
|
|
// try {
|
|
|
JSONObject data1 = jsonObject.getJSONObject("data"); |
|
|
// ObjectMapper mapper = new ObjectMapper();
|
|
|
String scene = data1.getString("scene"); |
|
|
// engineTypeResponse = mapper.readValue(sceneResponseBody, EngineTypeResponse.class);
|
|
|
|
|
|
// } catch (JsonProcessingException e) {
|
|
|
//查询场景算法
|
|
|
// throw new RuntimeException(e);
|
|
|
List<String> extra_type_list = new ArrayList<>(); |
|
|
// }
|
|
|
try { |
|
|
//// JSONObject jsonObject1 = new JSONObject(sceneResponseBody);
|
|
|
URI uri = new URIBuilder("http://60.204.247.65:8100/V2/AI/WX/User/ExtraType/Get") |
|
|
// // 逐层获取 data -> token
|
|
|
.addParameter("scene",scene) |
|
|
//// JSONObject data1 = jsonObject1.getJSONObject("data");
|
|
|
.addParameter("if_open","1") |
|
|
// String scene = engineTypeResponse.getData().getScene();
|
|
|
.build(); |
|
|
//
|
|
|
algorithmResponseBody = GetRequest(uri, token); |
|
|
// //查询场景算法
|
|
|
ObjectMapper mapper = new ObjectMapper(); |
|
|
// List<String> extra_type_list = new ArrayList<>();
|
|
|
ExtraTypeResponse response = mapper.readValue(algorithmResponseBody,ExtraTypeResponse.class); |
|
|
// try {
|
|
|
List<ExtraType> list = response.getData(); |
|
|
// URI uri = new URIBuilder("http://60.204.247.65:8100/V2/AI/WX/User/ExtraType/Get")
|
|
|
for (ExtraType extraType : list) { |
|
|
// .addParameter("scene",scene)
|
|
|
extra_type_list.add(extraType.getExtra_type()); |
|
|
// .addParameter("if_open","1")
|
|
|
break; |
|
|
// .build();
|
|
|
} |
|
|
// algorithmResponseBody = GetRequest(uri, token);
|
|
|
} catch (URISyntaxException | JsonProcessingException e) { |
|
|
// ObjectMapper mapper = new ObjectMapper();
|
|
|
throw new RuntimeException(e); |
|
|
// ExtraTypeResponse response = mapper.readValue(algorithmResponseBody,ExtraTypeResponse.class);
|
|
|
} |
|
|
// List<ExtraType> list = response.getData();
|
|
|
|
|
|
// for (ExtraType extraType : list) {
|
|
|
|
|
|
// extra_type_list.add(extraType.getExtra_type());
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// } catch (URISyntaxException | JsonProcessingException e) {
|
|
|
|
|
|
// throw new RuntimeException(e);
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
JSONObject jsonObj2 = new JSONObject(); |
|
|
JSONObject jsonObj2 = new JSONObject(); |
|
|
jsonObj2.put("scene", scene); // sanitize函数见下文
|
|
|
String type = objectMap.get("type").toString(); |
|
|
jsonObj2.put("callback_url", objectMap.get("callback_url")); |
|
|
if (StringUtils.isNotEmpty(type)){ |
|
|
jsonObj2.put("input_url", objectMap.get("input_url")); |
|
|
List<String> list = Arrays.asList(type.split(",")); |
|
|
jsonObj2.put("push_url", objectMap.get("push_url")); |
|
|
jsonObj2.put("scene", "可见光"); // sanitize函数见下文
|
|
|
jsonObj2.put("play_url", objectMap.get("play_url")); |
|
|
jsonObj2.put("callback_url", "http://114.235.183.162:9888/ces/iot/deviceLog/some"); |
|
|
String responseBody2 = PostRequest(jsonObj2, "http://60.204.247.65:8100/Third/Engine/Start"); |
|
|
jsonObj2.put("extra_type_list",new JSONArray(list)); |
|
|
|
|
|
jsonObj2.put("input_url","http://"+ip+":19008/live/livesteam/"+objectMap.get("rtmpUrl")+".flv"); |
|
|
|
|
|
jsonObj2.put("push_url", "rtmp://"+ip+"/live/livesteam/"+objectMap.get("rtmpUrl")+"ai1"); |
|
|
|
|
|
// jsonObj2.put("play_url", objectMap.get("play_url"));
|
|
|
|
|
|
String responseBody2 = PostRequest(jsonObj2, "http://60.204.247.65:8100/Third/Engine/Start",token); |
|
|
ObjectMapper mapper1 = new ObjectMapper(); |
|
|
ObjectMapper mapper1 = new ObjectMapper(); |
|
|
EngineResponse response = null; |
|
|
EngineResponse response = null; |
|
|
try { |
|
|
try { |
|
|
response = mapper1.readValue(responseBody2, EngineResponse.class); |
|
|
response = mapper1.readValue(responseBody2, EngineResponse.class); |
|
|
|
|
|
if (response.getCode() != 200){ |
|
|
|
|
|
throw new RuntimeException("第三方接口调用失败!"); |
|
|
|
|
|
} |
|
|
} catch (JsonProcessingException e) { |
|
|
} catch (JsonProcessingException e) { |
|
|
throw new RuntimeException(e); |
|
|
throw new RuntimeException(e); |
|
|
} |
|
|
} |
|
|
objectMap.put("task_id",response.getData().getTask_id()); |
|
|
objectMap.put("task_id",response.getData().getTask_id()); |
|
|
record.setPushUrl(objectMap.get("push_url").toString()); |
|
|
record.setPushUrl(objectMap.get("rtmpUrl").toString()); |
|
|
record.setDeviceSn(objectMap.get("device_sn").toString()); |
|
|
record.setTaskId(MapUtil.getStr(objectMap,"task_id","")); |
|
|
|
|
|
record.setDeviceSn(MapUtil.getStr(objectMap,"deviceSn","")); |
|
|
record.setTaskId(response.getData().getTask_id()); |
|
|
record.setTaskId(response.getData().getTask_id()); |
|
|
record.setExtraType(String.join(",", extra_type_list)); |
|
|
record.setExtraType(objectMap.get("type").toString()); |
|
|
|
|
|
System.out.print(objectMap); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
Map<String, Object> map = new HashMap(); |
|
|
Map<String, Object> map = new HashMap(); |
|
|
map.put("callback_url", objectMap.get("callback_url")); |
|
|
|
|
|
map.put("input_url", objectMap.get("input_url")); |
|
|
map.put("push_url", "http://"+ip+":1985/rtc/v1/whep/?app=live/livesteam&stream="+objectMap.get("rtmpUrl")+"ai1"); |
|
|
map.put("push_url", objectMap.get("push_url")); |
|
|
|
|
|
map.put("play_url", objectMap.get("play_url")); |
|
|
|
|
|
map.put("task_id", objectMap.get("task_id")); |
|
|
map.put("task_id", objectMap.get("task_id")); |
|
|
//添加启动引擎记录
|
|
|
//添加启动引擎记录
|
|
|
mapper.insert(record); |
|
|
mapper.insert(record); |
|
|
|
|
|
|
|
|
|
|
|
//添加任务id和jobid关联关系
|
|
|
|
|
|
TaskJobEntity taskJobEntity = new TaskJobEntity(); |
|
|
|
|
|
taskJobEntity.setJobId(objectMap.get("jobId").toString()); |
|
|
|
|
|
taskJobEntity.setTaskId(objectMap.get("task_id").toString()); |
|
|
|
|
|
taskJobMapper.insert(taskJobEntity); |
|
|
return map; |
|
|
return map; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void closeEngine(Map<String, Object> objectMap) { |
|
|
public void closeEngine(Map<String, Object> objectMap) { |
|
|
JSONObject jsonObj2 = new JSONObject(); |
|
|
JSONObject jsonObj2 = new JSONObject(); |
|
|
EngineRecordEntity record = mapper.selectOne(new LambdaQueryWrapper<EngineRecordEntity>().eq(EngineRecordEntity::getDeviceSn, objectMap.get("device_sn"))); |
|
|
EngineRecordEntity record = mapper.selectOne(new LambdaQueryWrapper<EngineRecordEntity>().eq(EngineRecordEntity::getDeviceSn, objectMap.get("deviceSn"))); |
|
|
if (record == null){ |
|
|
if (record == null){ |
|
|
throw new RuntimeException("关闭引擎失败!"); |
|
|
// throw new RuntimeException("关闭引擎失败!");
|
|
|
} |
|
|
}else { |
|
|
|
|
|
//获取token
|
|
|
|
|
|
//设置请求Header和Body(如JSON)
|
|
|
|
|
|
String md5 = Md5Utils.hash("xuzhou_ai"); |
|
|
|
|
|
JSONObject jsonObj = new JSONObject(); |
|
|
|
|
|
jsonObj.put("username", sanitize("xuzhou_ai")); // sanitize函数见下文
|
|
|
|
|
|
jsonObj.put("password", sanitize(md5)); |
|
|
|
|
|
String responseBody = PostRequest(jsonObj, "http://60.204.247.65:8100/Third/Login",""); |
|
|
|
|
|
JSONObject jsonObject = new JSONObject(responseBody); |
|
|
|
|
|
// 逐层获取 data -> token
|
|
|
|
|
|
JSONObject data = jsonObject.getJSONObject("data"); |
|
|
|
|
|
String token = data.getString("token"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
jsonObj2.put("task_id", record.getTaskId()); // sanitize函数见下文
|
|
|
jsonObj2.put("task_id", record.getTaskId()); // sanitize函数见下文
|
|
|
String responseBody2 = PostRequest(jsonObj2, "http://60.204.247.65:8100/Third/Engine/Stop"); |
|
|
String responseBody2 = PostRequest(jsonObj2, "http://60.204.247.65:8100/Third/Engine/Stop",token); |
|
|
JSONObject json = new JSONObject(responseBody2); |
|
|
JSONObject json = new JSONObject(responseBody2); |
|
|
// 方式2:安全获取(先判空再转换)
|
|
|
// 方式2:安全获取(先判空再转换)
|
|
|
Object dataObj = json.get("data"); |
|
|
Object dataObj = json.get("data"); |
|
|
if(dataObj != null && dataObj instanceof Integer) { |
|
|
if(dataObj != null && dataObj instanceof Integer) { |
|
|
int data = (Integer) dataObj; |
|
|
int data1 = (Integer) dataObj; |
|
|
if (data ==1){ |
|
|
if (data1 ==1){ |
|
|
mapper.delete(new LambdaQueryWrapper<EngineRecordEntity>().eq(EngineRecordEntity::getDeviceSn, objectMap.get("device_sn"))); |
|
|
mapper.delete(new LambdaQueryWrapper<EngineRecordEntity>().eq(EngineRecordEntity::getDeviceSn, objectMap.get("deviceSn"))); |
|
|
}else { |
|
|
}else { |
|
|
throw new RuntimeException("关闭引擎失败!"); |
|
|
throw new RuntimeException("关闭引擎失败!"); |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
throw new RuntimeException("关闭引擎失败!"); |
|
|
throw new RuntimeException("关闭引擎失败!"); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String PostRequest( JSONObject jsonObj, String url |
|
|
public String PostRequest( JSONObject jsonObj, String url, String token |
|
|
) { |
|
|
) { |
|
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { |
|
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { |
|
|
// 1. 创建POST请求对象
|
|
|
// 1. 创建POST请求对象
|
|
@ -158,6 +202,7 @@ public class SampleEngineRecordServiceImpl implements ISampleEngineRecordService |
|
|
requestEntity.setContentType("application/json"); |
|
|
requestEntity.setContentType("application/json"); |
|
|
post.setEntity(requestEntity); |
|
|
post.setEntity(requestEntity); |
|
|
post.setHeader("Accept", "application/json"); |
|
|
post.setHeader("Accept", "application/json"); |
|
|
|
|
|
post.setHeader("token", token); |
|
|
// 3. 执行请求并获取响应
|
|
|
// 3. 执行请求并获取响应
|
|
|
try (CloseableHttpResponse response = httpClient.execute(post)) { |
|
|
try (CloseableHttpResponse response = httpClient.execute(post)) { |
|
|
String responseBody = EntityUtils.toString(response.getEntity()); |
|
|
String responseBody = EntityUtils.toString(response.getEntity()); |
|
@ -182,7 +227,9 @@ public class SampleEngineRecordServiceImpl implements ISampleEngineRecordService |
|
|
// .addParameter("param2", "value2")
|
|
|
// .addParameter("param2", "value2")
|
|
|
// .build();
|
|
|
// .build();
|
|
|
HttpGet httpGet = new HttpGet(uri); |
|
|
HttpGet httpGet = new HttpGet(uri); |
|
|
|
|
|
if (StringUtils.isNotEmpty(token)){ |
|
|
httpGet.setHeader("token",token); |
|
|
httpGet.setHeader("token",token); |
|
|
|
|
|
} |
|
|
try (CloseableHttpResponse response = httpClient.execute(httpGet)) { |
|
|
try (CloseableHttpResponse response = httpClient.execute(httpGet)) { |
|
|
// 处理响应
|
|
|
// 处理响应
|
|
|
String responseBody = EntityUtils.toString(response.getEntity()); |
|
|
String responseBody = EntityUtils.toString(response.getEntity()); |
|
|