Commit d8d9e368 authored by yf's avatar yf

修改iot推送问题

parent 3864c971
......@@ -69,6 +69,27 @@
</exclusions>
</dependency>
<!--http 基础包-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.9</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>com.dahuatech.icc</groupId>
<artifactId>java-sdk-core</artifactId>
......@@ -114,6 +135,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.1.RELEASE</version>
<!--<configuration>
<mainClass>com.example.mykafkaproject.MyKafkaProjectApplication</mainClass>
<skip>true</skip>
......
......@@ -13,6 +13,7 @@ public class IotWarnPushDto {
private String grade_type_code;
private String alarm_time;
private String picture;
private String video;
private String belong;
private String create_time;
......@@ -87,4 +88,12 @@ public class IotWarnPushDto {
public void setGrade_type_code(String grade_type_code) {
this.grade_type_code = grade_type_code;
}
public String getVideo() {
return video;
}
public void setVideo(String video) {
this.video = video;
}
}
......@@ -52,6 +52,8 @@ public class WarnPushBase{
/** 摄像头Id */
private String channelId;
private String alarmMsg;
public Long getId() {
return id;
......@@ -164,4 +166,12 @@ public class WarnPushBase{
public void setChannelId(String channelId) {
this.channelId = channelId;
}
public String getAlarmMsg() {
return alarmMsg;
}
public void setAlarmMsg(String alarmMsg) {
this.alarmMsg = alarmMsg;
}
}
......@@ -10,9 +10,10 @@ import com.dahuatech.icc.oauth.http.DefaultClient;
import com.dahuatech.icc.oauth.http.IClient;
import com.dahuatech.icc.oauth.model.v202010.GeneralRequest;
import com.dahuatech.icc.oauth.model.v202010.GeneralResponse;
import com.example.mykafkaproject.bean.IotWarnPushDto;
import com.example.mykafkaproject.bean.WarnPushBase;
import com.example.mykafkaproject.mapper.WarnPushBaseMapper;
import com.example.mykafkaproject.utils.HttpUtils;
import com.example.mykafkaproject.utils.JsonUtil;
import com.example.mykafkaproject.utils.PinYinUtil;
import com.example.mykafkaproject.utils.RtspToMP4;
import org.slf4j.Logger;
......@@ -22,6 +23,8 @@ import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Service("WarnPushBaseServer")
public class WarnPushBaseServer {
......@@ -36,14 +39,15 @@ public class WarnPushBaseServer {
public void addMessage(String message) {
LOGGER.info("预警信息:"+ message);
LOGGER.info("预警信息:" + message);
WarnPushBase warnPushBase = JSON.parseObject(message, WarnPushBase.class);
//获取视频地址
//获取前后五秒时间戳
Date alarmTime = warnPushBase.getAlarmTime();
String video = getVideo(warnPushBase.getChannelId(), alarmTime.getTime() / 1000 - 5, alarmTime.getTime() / 1000 + 5);
warnPushBase.setAlarmMsg(warnPushBase.getGradeType());
warnPushBase.setGradeType("摄像头预警");
warnPushBase.setVideo(video);
warnPushBaseMapper.add(warnPushBase);
......@@ -73,7 +77,7 @@ public class WarnPushBaseServer {
rtspToMP4.stopRecord(process[0]);
return "http://172.16.20.211:8080/profile/MP4/"+fileName;
return "http://172.16.20.211:8080/profile/MP4/" + fileName;
} catch (ClientException e) {
e.printStackTrace();
} catch (InterruptedException e) {
......@@ -105,30 +109,35 @@ public class WarnPushBaseServer {
String url = obj.getJSONObject("data").getStr("url");
String token = obj.getJSONObject("data").getStr("token");
String[] parts = url.split("\\|");
return parts[0] + "?token=" + token+"&trackID=0";
return parts[0] + "?token=" + token + "&trackID=0";
}
public void iotPush(WarnPushBase warnPushBase){
public void iotPush(WarnPushBase warnPushBase) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String url = "http://172.16.20.170:8089/api/v1/aqgl/warn";
IotWarnPushDto iotWarnPushDto = new IotWarnPushDto();
iotWarnPushDto.setOrder_name("智能安全系纺");
iotWarnPushDto.setAlarm_device_name(warnPushBase.getAlarmDeviceName());
iotWarnPushDto.setGrade_name(warnPushBase.getGradeName());
iotWarnPushDto.setGrade_type("摄像头预警");
iotWarnPushDto.setGrade_type_code(PinYinUtil.getPinYinHeadChar(iotWarnPushDto.getGrade_type()));
iotWarnPushDto.setAlarm_time(simpleDateFormat.format(warnPushBase.getAlarmTime()));
iotWarnPushDto.setPicture(warnPushBase.getPicture());
iotWarnPushDto.setBelong(warnPushBase.getBelong());
iotWarnPushDto.setCreate_time(simpleDateFormat.format(new Date()));
String json = JSON.toJSONString(iotWarnPushDto);
String result = HttpRequest.post(url)
.body(json)
.execute().body();
System.out.println("IOT返回结果:"+result);
String url = "http://172.16.20.170:8089/api/v1";
String result_info = null;
try {
//同步IOT
Map<String, String> bodys = new HashMap<>();
bodys.put("order_name", "AI预警平台");
bodys.put("alarm_device_name", warnPushBase.getAlarmDeviceName());
bodys.put("grade_name", warnPushBase.getGradeName());
bodys.put("grade_type", "摄像头预警");
bodys.put("grade_type_code", PinYinUtil.getPinYinHeadChar(warnPushBase.getGradeType()));
bodys.put("alarm_time", simpleDateFormat.format(warnPushBase.getAlarmTime()));
bodys.put("alarm_msg", warnPushBase.getAlarmMsg());
bodys.put("picture", warnPushBase.getPicture());
bodys.put("mine", warnPushBase.getBelong());
bodys.put("create_time", simpleDateFormat.format(new Date()));
result_info = HttpUtils.doPostJson(url, null, JsonUtil.getObjectToString(bodys));
} catch (Exception e) {
System.out.println("同步IOT出错!!!");
}
System.out.println("IOT返回结果:" + result_info);
}
}
package com.example.mykafkaproject.utils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
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 java.io.IOException;
import java.util.Iterator;
import java.util.Map;
public class HttpUtils {
/**
* Post请求传参
* @param url
* @param headerMap
* @param json
* @return
*/
public static String doPostJson(String url, Map<String, String> headerMap, String json) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
if (headerMap != null) {
Iterator headerIterator = headerMap.entrySet().iterator(); //循环增加header
while(headerIterator.hasNext()){
Map.Entry<String,String> elem = (Map.Entry<String, String>) headerIterator.next();
httpPost.addHeader(elem.getKey(),elem.getValue());
}
}
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "UTF8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
}
package com.example.mykafkaproject.utils;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.JSON;
public class JsonUtil {
/**
* 功能描述:把java对象转换成JSON数据
* @param object java对象
* @return JSON数据
*/
public static String getObjectToString(Object object) {
return JSON.toJSONString(object, SerializerFeature.WriteMapNullValue);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment