10 changed files with 352 additions and 16 deletions
@ -0,0 +1,77 @@ |
|||
package org.dromara.sample.manage.controller; |
|||
|
|||
import com.alibaba.nacos.common.utils.CollectionUtils; |
|||
import io.swagger.v3.oas.annotations.Operation; |
|||
import io.swagger.v3.oas.annotations.tags.Tag; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.dromara.common.sdk.common.HttpResultResponse; |
|||
import org.dromara.sample.manage.model.dto.CallBackDTO; |
|||
import org.dromara.sample.manage.model.dto.EventsDTO; |
|||
import org.dromara.sample.manage.model.entity.WarningRecordEntity; |
|||
import org.dromara.sample.manage.service.ISampleEngineRecordService; |
|||
import org.dromara.sample.manage.service.ISampleWarningRecordService; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.web.bind.annotation.PostMapping; |
|||
import org.springframework.web.bind.annotation.RequestBody; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* @author sean.zhou |
|||
* @version 0.1 |
|||
* @date 2021/11/15 |
|||
*/ |
|||
@RestController |
|||
@Slf4j |
|||
@RequestMapping("${url.manage.prefix}${url.manage.version}/saEngineRecord") |
|||
@Tag(name = "无人机设备模块") |
|||
public class SampleEngineRecordController { |
|||
|
|||
@Autowired |
|||
private ISampleEngineRecordService sampleEngineRecordService; |
|||
|
|||
@Autowired |
|||
private ISampleWarningRecordService sampleWarningRecordService; |
|||
|
|||
/** |
|||
* 启动星罗引擎。 |
|||
* Get the topology list of all online devices in one workspace. |
|||
* @return |
|||
*/ |
|||
@PostMapping("/startEngine") |
|||
@Operation(summary = "启动星罗引擎。", description = "启动星罗引擎") |
|||
public HttpResultResponse<Map<String,Object>> startEngine(@RequestBody Map<String,Object> objectMap) { |
|||
return HttpResultResponse.success(sampleEngineRecordService.startEngine(objectMap)); |
|||
} |
|||
|
|||
@PostMapping("/closeEngine") |
|||
@Operation(summary = "关闭星罗引擎。", description = "关闭星罗引擎") |
|||
public HttpResultResponse closeEngine(@RequestBody Map<String,Object> objectMap) { |
|||
sampleEngineRecordService.closeEngine(objectMap); |
|||
return HttpResultResponse.success(); |
|||
} |
|||
|
|||
@PostMapping("/some") |
|||
@Operation(summary = "存储推流的预警(对外接口)。", description = "存储推流的预警(对外接口)。") |
|||
public void some(@RequestBody CallBackDTO callBackVo){ |
|||
WarningRecordEntity warningRecord = new WarningRecordEntity(); |
|||
List<WarningRecordEntity> list = new ArrayList<>(); |
|||
if (CollectionUtils.isNotEmpty(callBackVo.getEvents())){ |
|||
for (EventsDTO eventsVo :callBackVo.getEvents()){ |
|||
warningRecord.setTaskId(callBackVo.getTaskId()); |
|||
warningRecord.setAlgoId(eventsVo.getAlgoId()); |
|||
warningRecord.setEventId(eventsVo.getEventId()); |
|||
warningRecord.setExtraType(eventsVo.getExtraType()); |
|||
warningRecord.setPicUrl(eventsVo.getPicUrl()); |
|||
warningRecord.setTimestamp(eventsVo.getTimestamp()); |
|||
list.add(warningRecord); |
|||
} |
|||
sampleWarningRecordService.saveBatch(list); |
|||
System.out.println(callBackVo); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,9 @@ |
|||
package org.dromara.sample.manage.mapper; |
|||
|
|||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
|||
import org.dromara.sample.manage.model.entity.EngineRecordEntity; |
|||
import org.mapstruct.Mapper; |
|||
|
|||
|
|||
public interface ISampleEngineRecordMapper extends BaseMapper<EngineRecordEntity> { |
|||
} |
@ -0,0 +1,18 @@ |
|||
package org.dromara.sample.manage.mapper; |
|||
|
|||
|
|||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
|||
import org.apache.ibatis.annotations.Mapper; |
|||
import org.dromara.sample.manage.model.entity.WarningRecordEntity; |
|||
|
|||
/** |
|||
* 设备对象-》用于存储设备Mapper接口 |
|||
* |
|||
* @author wuyuan |
|||
* @date 2022-10-25 |
|||
*/ |
|||
@Mapper |
|||
public interface SampleWarningRecordMapper extends BaseMapper<WarningRecordEntity> { |
|||
|
|||
} |
|||
|
@ -0,0 +1,9 @@ |
|||
package org.dromara.sample.manage.service; |
|||
|
|||
import java.util.Map; |
|||
|
|||
public interface ISampleEngineRecordService { |
|||
Map<String,Object> startEngine(Map<String,Object> objectMap); |
|||
|
|||
void closeEngine(Map<String,Object> objectMap); |
|||
} |
@ -0,0 +1,8 @@ |
|||
package org.dromara.sample.manage.service; |
|||
|
|||
import com.baomidou.mybatisplus.extension.service.IService; |
|||
import org.dromara.sample.manage.model.entity.WarningRecordEntity; |
|||
|
|||
|
|||
public interface ISampleWarningRecordService extends IService<WarningRecordEntity> { |
|||
} |
@ -0,0 +1,203 @@ |
|||
package org.dromara.sample.manage.service.impl; |
|||
|
|||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|||
import com.fasterxml.jackson.core.JsonProcessingException; |
|||
import com.fasterxml.jackson.databind.ObjectMapper; |
|||
import io.seata.common.util.StringUtils; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.apache.http.client.methods.CloseableHttpResponse; |
|||
import org.apache.http.client.methods.HttpGet; |
|||
import org.apache.http.client.methods.HttpPost; |
|||
import org.apache.http.client.utils.URIBuilder; |
|||
import org.apache.http.entity.StringEntity; |
|||
import org.apache.http.impl.client.CloseableHttpClient; |
|||
import org.apache.http.impl.client.HttpClients; |
|||
import org.apache.http.util.EntityUtils; |
|||
import org.dromara.sample.common.util.Md5Utils; |
|||
import org.dromara.sample.manage.mapper.ISampleEngineRecordMapper; |
|||
import org.dromara.sample.manage.model.dto.EngineResponse; |
|||
import org.dromara.sample.manage.model.dto.ExtraType; |
|||
import org.dromara.sample.manage.model.dto.ExtraTypeResponse; |
|||
import org.dromara.sample.manage.model.entity.EngineRecordEntity; |
|||
import org.dromara.sample.manage.service.ISampleEngineRecordService; |
|||
import org.json.JSONObject; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.transaction.annotation.Transactional; |
|||
|
|||
import javax.annotation.Resource; |
|||
import java.net.URI; |
|||
import java.net.URISyntaxException; |
|||
import java.util.ArrayList; |
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
import static org.apache.commons.compress.utils.ArchiveUtils.sanitize; |
|||
|
|||
@Service |
|||
@Slf4j |
|||
@Transactional |
|||
public class SampleEngineRecordServiceImpl implements ISampleEngineRecordService { |
|||
@Resource |
|||
ISampleEngineRecordMapper mapper; |
|||
|
|||
|
|||
@Override |
|||
public Map<String, Object> startEngine(Map<String, Object> objectMap){ |
|||
EngineRecordEntity record = new EngineRecordEntity(); |
|||
//获取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"); |
|||
String sceneResponseBody = null; |
|||
String algorithmResponseBody = null; |
|||
//查询用户开通的场景
|
|||
if (StringUtils.isNotEmpty(token)){ |
|||
try { |
|||
URI uri = new URIBuilder("http://60.204.247.65:8100/V2/AI/WX/User/Scene") |
|||
.build(); |
|||
sceneResponseBody = GetRequest(uri, token); |
|||
} catch (URISyntaxException e) { |
|||
throw new RuntimeException(e); |
|||
} |
|||
JSONObject jsonObject1 = new JSONObject(sceneResponseBody); |
|||
// 逐层获取 data -> token
|
|||
JSONObject data1 = jsonObject.getJSONObject("data"); |
|||
String scene = data1.getString("scene"); |
|||
|
|||
//查询场景算法
|
|||
List<String> extra_type_list = new ArrayList<>(); |
|||
try { |
|||
URI uri = new URIBuilder("http://60.204.247.65:8100/V2/AI/WX/User/ExtraType/Get") |
|||
.addParameter("scene",scene) |
|||
.addParameter("if_open","1") |
|||
.build(); |
|||
algorithmResponseBody = GetRequest(uri, token); |
|||
ObjectMapper mapper = new ObjectMapper(); |
|||
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(); |
|||
jsonObj2.put("scene", scene); // sanitize函数见下文
|
|||
jsonObj2.put("callback_url", objectMap.get("callback_url")); |
|||
jsonObj2.put("input_url", objectMap.get("input_url")); |
|||
jsonObj2.put("push_url", objectMap.get("push_url")); |
|||
jsonObj2.put("play_url", objectMap.get("play_url")); |
|||
String responseBody2 = PostRequest(jsonObj2, "http://60.204.247.65:8100/Third/Engine/Start"); |
|||
ObjectMapper mapper1 = new ObjectMapper(); |
|||
EngineResponse response = null; |
|||
try { |
|||
response = mapper1.readValue(responseBody2, EngineResponse.class); |
|||
} catch (JsonProcessingException e) { |
|||
throw new RuntimeException(e); |
|||
} |
|||
objectMap.put("task_id",response.getData().getTask_id()); |
|||
record.setPushUrl(objectMap.get("push_url").toString()); |
|||
record.setDeviceSn(objectMap.get("device_sn").toString()); |
|||
record.setTaskId(response.getData().getTask_id()); |
|||
record.setExtraType(String.join(",", extra_type_list)); |
|||
} |
|||
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", objectMap.get("push_url")); |
|||
map.put("play_url", objectMap.get("play_url")); |
|||
map.put("task_id", objectMap.get("task_id")); |
|||
//添加启动引擎记录
|
|||
mapper.insert(record); |
|||
return map; |
|||
} |
|||
|
|||
@Override |
|||
public void closeEngine(Map<String, Object> objectMap) { |
|||
JSONObject jsonObj2 = new JSONObject(); |
|||
EngineRecordEntity record = mapper.selectOne(new LambdaQueryWrapper<EngineRecordEntity>().eq(EngineRecordEntity::getDeviceSn, objectMap.get("device_sn"))); |
|||
if (record == null){ |
|||
throw new RuntimeException("关闭引擎失败!"); |
|||
} |
|||
jsonObj2.put("task_id", record.getTaskId()); // sanitize函数见下文
|
|||
String responseBody2 = PostRequest(jsonObj2, "http://60.204.247.65:8100/Third/Engine/Stop"); |
|||
JSONObject json = new JSONObject(responseBody2); |
|||
// 方式2:安全获取(先判空再转换)
|
|||
Object dataObj = json.get("data"); |
|||
if(dataObj != null && dataObj instanceof Integer) { |
|||
int data = (Integer) dataObj; |
|||
if (data ==1){ |
|||
mapper.delete(new LambdaQueryWrapper<EngineRecordEntity>().eq(EngineRecordEntity::getDeviceSn, objectMap.get("device_sn"))); |
|||
}else { |
|||
throw new RuntimeException("关闭引擎失败!"); |
|||
} |
|||
} else { |
|||
throw new RuntimeException("关闭引擎失败!"); |
|||
} |
|||
|
|||
|
|||
} |
|||
|
|||
|
|||
public String PostRequest( JSONObject jsonObj, String url |
|||
) { |
|||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { |
|||
// 1. 创建POST请求对象
|
|||
HttpPost post = new HttpPost(url); |
|||
StringEntity requestEntity = new StringEntity(jsonObj.toString(), "UTF-8"); |
|||
requestEntity.setContentType("application/json"); |
|||
post.setEntity(requestEntity); |
|||
post.setHeader("Accept", "application/json"); |
|||
// 3. 执行请求并获取响应
|
|||
try (CloseableHttpResponse response = httpClient.execute(post)) { |
|||
String responseBody = EntityUtils.toString(response.getEntity()); |
|||
if (response.getStatusLine().getStatusCode() == 200) { |
|||
return responseBody; |
|||
} else { |
|||
throw new RuntimeException("第三方接口调用失败!"); |
|||
} |
|||
} |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return null; |
|||
|
|||
} |
|||
|
|||
public String GetRequest(URI uri, String token |
|||
) { |
|||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { |
|||
// URI uri = new URIBuilder("https://api.example.com/data")
|
|||
// .addParameter("param1", "value1")
|
|||
// .addParameter("param2", "value2")
|
|||
// .build();
|
|||
HttpGet httpGet = new HttpGet(uri); |
|||
httpGet.setHeader("token",token); |
|||
try (CloseableHttpResponse response = httpClient.execute(httpGet)) { |
|||
// 处理响应
|
|||
String responseBody = EntityUtils.toString(response.getEntity()); |
|||
if (response.getStatusLine().getStatusCode() == 200){ |
|||
return responseBody; |
|||
}else { |
|||
throw new RuntimeException("第三方接口调用失败!"); |
|||
} |
|||
} |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return null; |
|||
|
|||
} |
|||
|
|||
|
|||
} |
@ -0,0 +1,15 @@ |
|||
package org.dromara.sample.manage.service.impl; |
|||
|
|||
import com.baomidou.mybatisplus.extension.service.IService; |
|||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.dromara.sample.manage.mapper.SampleWarningRecordMapper; |
|||
import org.dromara.sample.manage.mapper.WarningRecordMapper; |
|||
import org.dromara.sample.manage.model.entity.WarningRecordEntity; |
|||
import org.dromara.sample.manage.service.ISampleWarningRecordService; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
@Service |
|||
@Slf4j |
|||
public class SampleWarningRecordServiceImpl extends ServiceImpl<SampleWarningRecordMapper, WarningRecordEntity> implements ISampleWarningRecordService { |
|||
} |
Loading…
Reference in new issue