|
|
@@ -1,17 +1,32 @@
|
|
|
package com.zd.alg.forward.serivce;
|
|
|
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.zd.alg.forward.config.AlgorithmYml;
|
|
|
import com.zd.alg.forward.domain.AnalysisData;
|
|
|
+import com.zd.alg.forward.domain.AnalysisReturnData;
|
|
|
import com.zd.alg.forward.domain.DataPostAnalysisRespDto;
|
|
|
import com.zd.alg.forward.domain.ImgPostResponse;
|
|
|
import com.zd.alg.forward.properties.FireProperties;
|
|
|
import com.zd.alg.forward.utils.HttpUtils;
|
|
|
import com.zd.alg.forward.utils.VideoUtils;
|
|
|
import com.zd.common.core.exception.ServiceException;
|
|
|
+import com.zd.common.core.redis.RedisService;
|
|
|
+import com.zd.laboratory.api.entity.SensorFunctionStatus;
|
|
|
+import com.zd.laboratory.api.feign.RemoteLaboratoryService;
|
|
|
+import com.zd.laboratory.api.vo.SubFunction;
|
|
|
+import com.zd.model.domain.ResultData;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.bytedeco.javacv.FFmpegFrameGrabber;
|
|
|
import org.bytedeco.javacv.Frame;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpEntity;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
@@ -22,14 +37,15 @@ import javax.imageio.ImageIO;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Optional;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneOffset;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
import static com.zd.alg.face.utils.FileUtil.multipartFileToFile;
|
|
|
import static com.zd.alg.forward.utils.HttpUtils.getHttpEntityMap;
|
|
|
import static com.zd.alg.forward.utils.VideoUtils.frameToBufferedImage;
|
|
|
+import static com.zd.model.constant.BaseConstants.MAP_INIT_SIZE;
|
|
|
|
|
|
/**
|
|
|
* 火焰图片抓拍处理
|
|
|
@@ -45,6 +61,12 @@ public class FireImageService {
|
|
|
@Resource
|
|
|
private AlgorithmYml algorithmYml;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ RedisService redisService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RemoteLaboratoryService remoteLaboratoryService;
|
|
|
+
|
|
|
@Resource
|
|
|
private RestTemplate restTemplate;
|
|
|
@Resource
|
|
|
@@ -60,9 +82,10 @@ public class FireImageService {
|
|
|
*/
|
|
|
private static final String IMAGE_FORMAT = "jpg";
|
|
|
|
|
|
-// @Scheduled(cron = "0/5 * * * * ?")
|
|
|
+ @Scheduled(cron = "0/5 * * * * ?")
|
|
|
public void camera(){
|
|
|
try {
|
|
|
+ log.info("开始执行定时火焰检测!");
|
|
|
String streamUrl = fireProperties.getStreamUrl();
|
|
|
if (streamUrl != null && !"".equals(streamUrl)) {
|
|
|
catchImage(streamUrl);
|
|
|
@@ -75,11 +98,16 @@ public class FireImageService {
|
|
|
}
|
|
|
|
|
|
public void catchImage(String streamUrl) throws IOException {
|
|
|
+ log.info("获取视频流开始=================");
|
|
|
try (FFmpegFrameGrabber grabber = VideoUtils.createGrabber(streamUrl)) {
|
|
|
grabber.start();
|
|
|
- String fileName = "test";
|
|
|
+ String fileName = "huoyan-"+ DateUtil.format(new DateTime(), "yyyyMMddHHmmssSSS");;
|
|
|
//文件储存对象
|
|
|
- File file=new File(fileName+"." + IMAGE_FORMAT);
|
|
|
+ File file=new File("/img/"+fileName+"." + IMAGE_FORMAT);
|
|
|
+ if (!file.exists()) {
|
|
|
+ file.mkdirs();
|
|
|
+ }
|
|
|
+ log.info("获取第一帧");
|
|
|
//获取第一帧
|
|
|
Frame frame = grabber.grabFrame();
|
|
|
if (frame != null) {
|
|
|
@@ -110,29 +138,28 @@ public class FireImageService {
|
|
|
if (fireProperties.getAlgoId() == null) {
|
|
|
throw new ServiceException("未配置火焰算法ID");
|
|
|
}
|
|
|
- MultiValueMap<String, Object> params = HttpUtils.getMultiValueMap(fireProperties, null);
|
|
|
- HttpEntity<MultiValueMap<String, Object>> files = getHttpEntityMap(file, params);
|
|
|
- ImgPostResponse<DataPostAnalysisRespDto> send = HttpUtils.send(restTemplate, files, algorithmYml);
|
|
|
- if (send == null || send.getStatus_code() != SUCCESS_CODE) {
|
|
|
- assert send != null;
|
|
|
- //log.error(send.getMessage());
|
|
|
+ HashMap<String, Object> paramMap = new HashMap<>();
|
|
|
+ paramMap.put("image", FileUtil.file(file.getAbsoluteFile()));
|
|
|
+ paramMap.put("aid",fireProperties.getAlgoId());
|
|
|
+ paramMap.put("cid",fireProperties.getCid());
|
|
|
+ paramMap.put("sync",1);
|
|
|
+ paramMap.put("timestamp", LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli() / 1000);
|
|
|
+ log.info("火焰算法请求参数={}", JSON.toJSONString(paramMap));
|
|
|
+ String resultStr= HttpUtil.post(algorithmYml.getImgPostUrl(), paramMap);
|
|
|
+ log.info("火焰算法响应参数status_code={}", JSON.parseObject(resultStr).get("status_code"));
|
|
|
+ log.info("火焰算法响应参数message={}", JSON.parseObject(resultStr).get("message"));
|
|
|
+ if (StringUtils.isBlank(resultStr) || SUCCESS_CODE != JSON.parseObject(resultStr).getIntValue("status_code")) {
|
|
|
+ log.info("火焰算法请求失败!响应参数={}",resultStr);
|
|
|
return;
|
|
|
}
|
|
|
- DataPostAnalysisRespDto data = send.getData();
|
|
|
- List<AnalysisData> analysisDatas = data.getAnalysisDatas();
|
|
|
- AnalysisData analysisData = analysisDatas.get(0);
|
|
|
- int code = analysisData.getCode();
|
|
|
- if (code==-1){
|
|
|
- log.error("==============请求失败:{}=================",analysisData.getMsg());
|
|
|
- }else {
|
|
|
- log.info("===============向算法服务发送数据完成====================");
|
|
|
- Map<String, Object> result = (Map<String, Object>) Optional.ofNullable(analysisData.getResult()).orElse(Collections.emptyMap());
|
|
|
- Map<String, Object> algorithmData = (Map<String, Object>) Optional.ofNullable(result.get("algorithm_data")).orElse(Collections.emptyMap());
|
|
|
- boolean alert = "false".equals(algorithmData.getOrDefault("is_alert", "").toString());
|
|
|
- if (!alert) {
|
|
|
- log.info("===============返回告警信息====================");
|
|
|
- sendSginAccessLogService.saveAlarm(fireProperties);
|
|
|
- }
|
|
|
+ AnalysisReturnData data = JSON.parseObject(resultStr).getObject("data", AnalysisReturnData.class);
|
|
|
+ JSONObject resultJSON = JSON.parseObject(JSON.toJSONString(data.getResult()));
|
|
|
+ JSONObject algorithm_data = resultJSON.getJSONObject("algorithm_data");
|
|
|
+ Boolean is_alert = algorithm_data.getBoolean("is_alert");
|
|
|
+ log.info("火焰算法识别结果is_alert={}",is_alert);
|
|
|
+ if (is_alert) {
|
|
|
+ log.info("======火焰预案报警!!!======");
|
|
|
+ sendSginAccessLogService.saveAlarm(fireProperties);
|
|
|
}
|
|
|
}
|
|
|
}
|