Procházet zdrojové kódy

预警合并代码

hanzhiwei před 2 roky
rodič
revize
7f7a082c7f

+ 37 - 0
zd-api/zd-algorithm-api/src/main/java/com/zd/algorithm/api/forward/bo/AlgorithmWarningBo.java

@@ -0,0 +1,37 @@
+package com.zd.algorithm.api.forward.bo;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import javax.validation.constraints.NotNull;
+import java.io.Serializable;
+
+/***
+ * <p>识别传入参数</p>
+ *
+ * @author linft
+ * @date 6/16/2023
+ * @version 3.0
+ */
+@Data
+public class AlgorithmWarningBo implements Serializable {
+
+    private static final long serialVersionUID = -6807148872001305307L;
+
+    @ApiModelProperty("id")
+    @NotNull(message = "id不能为空")
+    private Long id;
+
+    @ApiModelProperty("实验室id")
+    @NotNull(message = "实验室id不能为空")
+    private Long subId;
+
+    @ApiModelProperty("实验室名称")
+    @NotNull(message = "实验室名称不能为空")
+    private String subName;
+
+    @ApiModelProperty("图片文件url")
+    @NotNull(message = "文件不能为空")
+    private String fileUrl;
+
+}

+ 4 - 3
zd-modules/zd-algorithm/src/main/java/com/zd/alg/forward/controller/AlarmPhotoController.java

@@ -33,13 +33,14 @@ public class AlarmPhotoController {
     @GetMapping("/photograph")
     @GetMapping("/photograph")
     public R<SysFile> photograph(@RequestParam("streamUrl") String streamUrl) {
     public R<SysFile> photograph(@RequestParam("streamUrl") String streamUrl) {
         try {
         try {
-            SysFile sysFile = imageService.photograph(streamUrl);
+            R<SysFile> sysFile = imageService.photograph(streamUrl);
             if (sysFile!=null){
             if (sysFile!=null){
-                return R.ok(sysFile);
+                return sysFile;
             }
             }
             return R.fail("图片抓拍失败");
             return R.fail("图片抓拍失败");
         } catch (IOException|ServiceException e) {
         } catch (IOException|ServiceException e) {
-            throw new ServiceException(e.getMessage());
+            e.printStackTrace();
         }
         }
+        return R.fail();
     }
     }
 }
 }

+ 58 - 0
zd-modules/zd-algorithm/src/main/java/com/zd/alg/forward/controller/AlgorithmController.java

@@ -0,0 +1,58 @@
+package com.zd.alg.forward.controller;
+
+
+import com.zd.alg.forward.config.WarningProperties;
+import com.zd.alg.forward.serivce.AlgorithmService;
+import com.zd.algorithm.api.forward.bo.AlgorithmWarningBo;
+import com.zd.algorithm.api.forward.vo.AlgorithmWarningVo;
+import com.zd.common.core.redis.RedisService;
+import com.zd.model.domain.ResultData;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
+
+/***
+ * <p>算法识别接口</p>
+ *
+ * @author linft
+ * @date 6/16/2023
+ * @version 3.0
+ */
+@Api(tags = "算法识别")
+@RestController
+@RequestMapping("/algorithm")
+public class AlgorithmController {
+
+    @Autowired
+    private AlgorithmService algorithmService;
+
+    @Autowired
+    RedisService redisService;
+
+    @Resource
+    private WarningProperties warningProperties;
+
+    @ApiOperation(value = "图片识别")
+    @PostMapping("/warningCheck")
+    public ResultData warningCheck(@RequestBody @Validated AlgorithmWarningBo bo) {
+        //检查配置
+        if (warningProperties == null && warningProperties.getEnable() == null
+                || warningProperties.getEnable().equals(Boolean.FALSE)
+                || warningProperties.getCode() == null) {
+            return ResultData.fail("未配置识别参数或未启用");
+        }
+        AlgorithmWarningVo vo = algorithmService.checkByPicture(bo.getId(), bo.getSubId(), bo.getSubName(), bo.getFileUrl(),warningProperties.getCode(), warningProperties.getName());
+        if (vo != null) {
+            return ResultData.success(vo);
+        }
+        return ResultData.fail("算法识别异常,请稍后重试!");
+    }
+
+
+
+
+}

+ 55 - 0
zd-modules/zd-algorithm/src/main/java/com/zd/alg/forward/serivce/AlgorithmService.java

@@ -0,0 +1,55 @@
+package com.zd.alg.forward.serivce;
+
+import com.zd.alg.forward.domain.AlgorithmResponseResult;
+import com.zd.alg.forward.domain.AnalysisReturnData;
+import com.zd.alg.forward.domain.ImgPostResponse;
+import com.zd.algorithm.api.forward.vo.AlgorithmWarningVo;
+import com.zd.model.entity.Algorithm;
+
+/***
+ * <p>算法服务</p>
+ *
+ * @author linft
+ * @date 6/16/2023
+ * @version 3.0
+ */
+public interface AlgorithmService {
+
+
+    /**
+     * 图片识别
+     * @param id
+     * @param subId
+     * @param subName
+     * @param file
+     * @param code
+     * @param name
+     */
+    AlgorithmWarningVo checkByPicture(Long id, Long subId, String subName, String fileUrl, int code, String name);
+
+    /**
+     * 添加算法请求日志
+     * @param subId
+     * @param subName
+     * @param picUrl
+     * @param algorithmName
+     * @return 返回插入后的主键id
+     */
+    Long insertRequestRecordLog(Long subId, String subName, String picUrl, String algorithmName);
+
+    /**
+     * 更新日志
+     * @param logId
+     * @param responseResult
+     * @return
+     */
+    Algorithm updateRequestRecordLog(Long logId, AlgorithmResponseResult responseResult);
+
+    /**
+     * 获取响应结果
+     * @param send
+     * @return
+     */
+    AlgorithmResponseResult getResponseResult(ImgPostResponse<AnalysisReturnData> send);
+
+}

+ 5 - 6
zd-modules/zd-algorithm/src/main/java/com/zd/alg/forward/serivce/ImageService.java

@@ -38,28 +38,27 @@ public class ImageService {
      */
      */
     private static final String IMAGE_FORMAT = "jpg";
     private static final String IMAGE_FORMAT = "jpg";
 
 
-    public SysFile photograph(String streamUrl) throws IOException {
+    public R<SysFile> photograph(String streamUrl) throws IOException {
         try (FFmpegFrameGrabber grabber = VideoUtils.createGrabber(streamUrl)) {
         try (FFmpegFrameGrabber grabber = VideoUtils.createGrabber(streamUrl)) {
             grabber.start();
             grabber.start();
             String fileName = "test";
             String fileName = "test";
             //文件储存对象
             //文件储存对象
             File file = new File(fileName + "." + IMAGE_FORMAT);
             File file = new File(fileName + "." + IMAGE_FORMAT);
             //获取第一帧
             //获取第一帧
-            Frame frame = grabber.grabFrame();
+            Frame frame = grabber.grabImage();
             if (frame != null) {
             if (frame != null) {
                 //视频快照
                 //视频快照
-                frame = grabber.grabImage();
                 BufferedImage bufferedImage = VideoUtils.frameToBufferedImage(frame);
                 BufferedImage bufferedImage = VideoUtils.frameToBufferedImage(frame);
                 if (bufferedImage != null) {
                 if (bufferedImage != null) {
                     ImageIO.write(bufferedImage, IMAGE_FORMAT, file);
                     ImageIO.write(bufferedImage, IMAGE_FORMAT, file);
                 }
                 }
                 log.info("图片地址为[{}]", file.getAbsoluteFile());
                 log.info("图片地址为[{}]", file.getAbsoluteFile());
                 grabber.stop();
                 grabber.stop();
+                grabber.release();
                 MultipartFile multipartFile = getMultipartFile(file);
                 MultipartFile multipartFile = getMultipartFile(file);
                 R<SysFile> upload = remoteFileService.upload(multipartFile);
                 R<SysFile> upload = remoteFileService.upload(multipartFile);
                 if (upload.getCode() == HttpStatus.SUCCESS && upload.getData() != null) {
                 if (upload.getCode() == HttpStatus.SUCCESS && upload.getData() != null) {
-                    upload.getData().setMultipartFile(multipartFile);
-                    return upload.getData();
+                    return upload;
                 }else {
                 }else {
                     throw new ServerException(upload.getMsg());
                     throw new ServerException(upload.getMsg());
                 }
                 }
@@ -71,7 +70,7 @@ public class ImageService {
     private MultipartFile getMultipartFile(File file) {
     private MultipartFile getMultipartFile(File file) {
         try(FileInputStream fileInputStream = new FileInputStream(file)) {
         try(FileInputStream fileInputStream = new FileInputStream(file)) {
             return new MockMultipartFile(file.getName(),file.getName(),
             return new MockMultipartFile(file.getName(),file.getName(),
-                    ContentType.APPLICATION_OCTET_STREAM.toString(),fileInputStream);
+                    ContentType.MULTIPART_FORM_DATA.toString(),fileInputStream);
         } catch (Exception e) {
         } catch (Exception e) {
             throw new ServiceException(e.getMessage());
             throw new ServiceException(e.getMessage());
         }
         }

+ 198 - 0
zd-modules/zd-algorithm/src/main/java/com/zd/alg/forward/serivce/impl/AlgorithmServiceImpl.java

@@ -0,0 +1,198 @@
+package com.zd.alg.forward.serivce.impl;
+
+import com.alibaba.fastjson.JSONObject;
+import com.zd.alg.forward.config.AlgorithmYml;
+import com.zd.alg.forward.domain.AlgorithmResponseResult;
+import com.zd.alg.forward.domain.AnalysisReturnData;
+import com.zd.alg.forward.domain.ImgPostResponse;
+import com.zd.alg.forward.serivce.AlgorithmService;
+import com.zd.alg.forward.utils.Base64DecodedMultipartFile;
+import com.zd.alg.forward.utils.HttpUtils;
+import com.zd.algorithm.api.forward.vo.AlgorithmWarningVo;
+import com.zd.base.api.feign.RemoteFileService;
+import com.zd.laboratory.api.feign.RemoteLaboratoryService;
+import com.zd.model.constant.HttpStatus;
+import com.zd.model.domain.R;
+import com.zd.model.entity.Algorithm;
+import com.zd.model.entity.SysFile;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.HttpEntity;
+import org.springframework.stereotype.Service;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.client.RestTemplate;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.annotation.Resource;
+import java.io.File;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+import static com.zd.alg.forward.utils.HttpUtils.*;
+
+/***
+ * <p>类注释说明</p>
+ *
+ * @author linft
+ * @date 6/16/2023
+ * @version 3.0
+ */
+@Service
+public class AlgorithmServiceImpl implements AlgorithmService {
+
+    private Logger log = LoggerFactory.getLogger(AlgorithmServiceImpl.class);
+
+    @Resource(name = "restTemplateLocal")
+    private RestTemplate restTemplateLocal;
+
+    @Autowired
+    private AlgorithmYml algorithmYml;
+
+    @Autowired
+    private RemoteFileService remoteFileService;
+
+    @Autowired
+    private RemoteLaboratoryService laboratoryService;
+
+    @Value("${file.path}")
+    private String rootPath;
+
+    @Override
+    public AlgorithmWarningVo checkByPicture(Long id, Long subId, String subName, String fileUrl, int code, String name) {
+        //添加一条算法请求记录
+        Long logId = insertRequestRecordLog(subId, subName, fileUrl, name);
+        log.info("【调用算法服务】 添加算法调用日志,返回logId = "+ logId);
+
+       // File toFile = multipartFileToFile(file);
+        //========= 获取算法INFO ===========
+        AlgorithmYml.CheckValid checkValid = algorithmYml.getCheckValid(Integer.valueOf(code));
+        MultiValueMap<String, Object> params = getStringObjectMultiValueMap(checkValid, id.toString());
+        File toFile = new File(rootPath+fileUrl);
+        HttpEntity<MultiValueMap<String, Object>> files = getHttpEntityMap(toFile, params);
+
+        //请求算法服务
+        ImgPostResponse<AnalysisReturnData> send = HttpUtils.sendV5(restTemplateLocal, files, algorithmYml);
+        AlgorithmResponseResult responseResult = getResponseResult(send);
+        //添加成功,更新算法日志记录
+        if (logId != null) {
+            Algorithm algorithm = updateRequestRecordLog(logId, responseResult);
+            log.info("【调用算法服务】 更新算法调用日志记录");
+            if (responseResult.getCode() == 1000) {
+                AlgorithmWarningVo vo = new AlgorithmWarningVo();
+                vo.setId(id);
+                vo.setLogId(logId);
+                vo.setOriginalImage(fileUrl);
+                vo.setResultImage(algorithm.getAlgorithmResult());
+                vo.setIsAlert(responseResult.getIsPass());
+                return vo;
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public Long insertRequestRecordLog(Long subId, String subName, String url, String algorithmName) {
+        Algorithm algorithm = new Algorithm();
+        algorithm.setSubId(subId);
+        algorithm.setSubName(subName);
+        algorithm.setOriginalImg(url);
+        algorithm.setAlgorithmType("image");
+        algorithm.setAlgorithmName(algorithmName);
+        //logger.info("【算法服务】 添加日志记录实体:"+JSONObject.toJSONString(algorithm));
+        R alg = laboratoryService.saveAlgorithmData(algorithm);
+        if (alg.getCode() == HttpStatus.SUCCESS) {
+            return alg.getData() != null?Long.valueOf(alg.getData()+""):null;
+        }
+        return null;
+    }
+
+    @Override
+    public Algorithm updateRequestRecordLog(Long logId, AlgorithmResponseResult responseResult) {
+        try {
+            Algorithm algorithm = new Algorithm();
+            algorithm.setId(logId);
+            if (responseResult.getCode() == 1000) {
+                //识别后的图片
+                String picture = responseResult.getRetImage();
+                String header = "data:image/jpeg;base64," + picture;
+                MultipartFile multipartFile = Base64DecodedMultipartFile.base64ToMultipart(header);
+                R<SysFile> sysFileR = remoteFileService.upload(multipartFile);
+                String imageUrl = sysFileR.getData().getUrl();
+                algorithm.setAlgorithmResult(imageUrl);
+                //识别id
+                algorithm.setSignId(Long.parseLong(responseResult.getCid()));
+                //是否通过
+                algorithm.setIsAlarm(responseResult.getIsPass()?0:1);
+                algorithm.setStatus(1);
+
+            } else {
+                algorithm.setStatus(0);
+            }
+            //存响应实体数据
+            algorithm.setRespData(responseResult.getResponseBody());
+            algorithm.setRespCode(responseResult.getCode());
+            algorithm.setUpdateTime(new Date());
+            //logger.info("【算法服务】 更新日志实体信息:"+JSONObject.toJSONString(algorithm));
+            laboratoryService.update(algorithm);
+            return algorithm;
+        } catch (Exception e) {
+            log.error("【调用算法服务】调用算法服务后,更新日志记录发生异常", e);
+        }
+        return null;
+    }
+
+    /**
+     * 获取响应结果
+     * @param send
+     * @return
+     */
+    @Override
+    public AlgorithmResponseResult getResponseResult(ImgPostResponse<AnalysisReturnData> send) {
+        AlgorithmResponseResult responseResult = new AlgorithmResponseResult();
+        if (send != null) {
+            responseResult.setCode(send.getStatus_code());
+            responseResult.setMessage(send.getMessage());
+            if (send.getStatus_code() == 1000 && send.getData() != null) {
+                AnalysisReturnData data = send.getData();
+                responseResult.setAid(data.getAid());
+                responseResult.setCid(data.getCid());
+                responseResult.setRetImage(data.getRet_image());
+                responseResult.setSrcImage(data.getSrc_image());
+                //检查结果
+                Map<String, Object> result = (Map<String, Object>) data.getResult();
+                Map<String, Object> algorithmData = (Map<String, Object>) result.get("algorithm_data");
+                Map<String, Object> modelResult = (Map<String, Object>) result.get("model_data");
+                List<Object> objects = (List<Object>) modelResult.get("objects");
+                //是否验证通过
+                //算法厂家技术人员前期要添加objects判断精准性,后面对接人员要让去掉对objects的判断
+                if (algorithmData.getOrDefault("is_alert", "").toString().equals("false") && !objects.isEmpty()) {
+                    //不报警,代表验证通过
+                    responseResult.setIsPass(Boolean.TRUE);
+                } else {
+                    //报警,代表不通过
+                    responseResult.setIsPass(Boolean.FALSE);
+                }
+                responseResult.setObjects(objects);
+                //存响应实体数据,去掉图片
+                data.setRet_image("");
+                data.setSrc_image("");
+                ImgPostResponse<AnalysisReturnData> response = new ImgPostResponse<>();
+                response.setData(data);
+                response.setStatus_code(send.getStatus_code());
+                response.setMessage(send.getMessage());
+                responseResult.setResponseBody(JSONObject.toJSONString(response));
+            } else {
+                responseResult.setResponseBody(JSONObject.toJSONString(send));
+            }
+        } else {
+            responseResult.setCode(0L);
+            responseResult.setMessage("算法服务未响应");
+            responseResult.setResponseBody("算法服务未响应");
+        }
+        log.info("【调用算法服务】 算法服务请求结果:code = {}, message = {}, isPass = {}", responseResult.getCode(), responseResult.getMessage(), responseResult.getIsPass());
+        return responseResult;
+    }
+}

+ 6 - 3
zd-modules/zd-algorithm/src/main/java/com/zd/alg/forward/utils/VideoUtils.java

@@ -44,8 +44,11 @@ public class VideoUtils {
      */
      */
     public static BufferedImage frameToBufferedImage(Frame frame) {
     public static BufferedImage frameToBufferedImage(Frame frame) {
         //创建BufferedImage对象
         //创建BufferedImage对象
-        try (Java2DFrameConverter converter = new Java2DFrameConverter()) {
-            return converter.getBufferedImage(frame);
-        }
+//        try (Java2DFrameConverter converter = new Java2DFrameConverter()) {
+//            return converter.getBufferedImage(frame);
+//        }
+        Java2DFrameConverter converter = new Java2DFrameConverter();
+        BufferedImage bufferedImage = converter.getBufferedImage(frame);
+        return bufferedImage;
     }
     }
 }
 }

+ 6 - 0
zd-modules/zd-modules-laboratory/pom.xml

@@ -149,6 +149,12 @@
             <groupId>io.netty</groupId>
             <groupId>io.netty</groupId>
             <artifactId>netty-all</artifactId>
             <artifactId>netty-all</artifactId>
         </dependency>
         </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-mock</artifactId>
+            <version>2.0.8</version>
+            <scope>compile</scope>
+        </dependency>
     </dependencies>
     </dependencies>
 
 
     <build>
     <build>

+ 9 - 1
zd-modules/zd-modules-laboratory/src/main/java/com/zd/laboratory/controller/WarningNoticeLogController.java

@@ -75,6 +75,7 @@ public class WarningNoticeLogController extends AbstractController {
         if(StringUtils.isNotBlank(queryWarningLogParam.getWarningWay())){
         if(StringUtils.isNotBlank(queryWarningLogParam.getWarningWay())){
             queryWrapper.in(WarningNoticeLog::getWarningWay, queryWarningLogParam.getWarningWay());
             queryWrapper.in(WarningNoticeLog::getWarningWay, queryWarningLogParam.getWarningWay());
         }
         }
+        queryWrapper.eq(WarningNoticeLog::getIsDeleted,Boolean.FALSE);
         queryWrapper.orderByDesc(WarningNoticeLog::getId);
         queryWrapper.orderByDesc(WarningNoticeLog::getId);
         Page page = warningNoticeLogService.page(new Page(queryWarningLogParam.getPageNum(), queryWarningLogParam.getPageSize()), queryWrapper);
         Page page = warningNoticeLogService.page(new Page(queryWarningLogParam.getPageNum(), queryWarningLogParam.getPageSize()), queryWrapper);
         return ResultData.success(page);
         return ResultData.success(page);
@@ -84,7 +85,7 @@ public class WarningNoticeLogController extends AbstractController {
     @ApiOperation("小程序查询预警通知列表")
     @ApiOperation("小程序查询预警通知列表")
     public ResultData appList(QueryAppWarningLogParam queryAppWarningLogParam){
     public ResultData appList(QueryAppWarningLogParam queryAppWarningLogParam){
         Page<LinkedHashMap<LocalDate, List<WarningNoticeLogVO>>> VOPage = new Page<LinkedHashMap<LocalDate, List<WarningNoticeLogVO>>>();
         Page<LinkedHashMap<LocalDate, List<WarningNoticeLogVO>>> VOPage = new Page<LinkedHashMap<LocalDate, List<WarningNoticeLogVO>>>();
-        Page<WarningNoticeLog> page = warningNoticeLogService.page(new Page<WarningNoticeLog>(queryAppWarningLogParam.getPageNum(), queryAppWarningLogParam.getPageSize()), new LambdaQueryWrapper<WarningNoticeLog>().orderByDesc(WarningNoticeLog::getCreateTime));
+        Page<WarningNoticeLog> page = warningNoticeLogService.page(new Page<WarningNoticeLog>(queryAppWarningLogParam.getPageNum(), queryAppWarningLogParam.getPageSize()), new LambdaQueryWrapper<WarningNoticeLog>().eq(WarningNoticeLog::getIsDeleted,Boolean.FALSE).orderByDesc(WarningNoticeLog::getCreateTime));
         List<WarningNoticeLog> records = page.getRecords();
         List<WarningNoticeLog> records = page.getRecords();
         if (records.size() > 0) {
         if (records.size() > 0) {
             List<WarningNoticeLogVO> warningNoticeLogVOS = com.zd.common.core.utils.BeanUtils.copyList2List(records, WarningNoticeLogVO.class);
             List<WarningNoticeLogVO> warningNoticeLogVOS = com.zd.common.core.utils.BeanUtils.copyList2List(records, WarningNoticeLogVO.class);
@@ -114,4 +115,11 @@ public class WarningNoticeLogController extends AbstractController {
         List<WarningNoticeLogDto> warningNoticeLogDtos = com.zd.common.core.utils.BeanUtils.copyList2List(list, WarningNoticeLogDto.class);
         List<WarningNoticeLogDto> warningNoticeLogDtos = com.zd.common.core.utils.BeanUtils.copyList2List(list, WarningNoticeLogDto.class);
         return ResultData.success(warningNoticeLogDtos);
         return ResultData.success(warningNoticeLogDtos);
     }
     }
+
+    @GetMapping("/labCoatTask")
+    @ApiOperation("未穿戴实验服定时任务")
+    public ResultData labCoatTask() {
+        warningNoticeLogService.labCoatTask();
+        return ResultData.success();
+    }
 }
 }

+ 100 - 73
zd-modules/zd-modules-laboratory/src/main/java/com/zd/laboratory/event/RedisExpiredPhotographListener.java

@@ -34,6 +34,7 @@ import com.zd.model.entity.SysFile;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.redis.connection.Message;
 import org.springframework.data.redis.connection.Message;
 import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
 import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
 import org.springframework.data.redis.listener.RedisMessageListenerContainer;
 import org.springframework.data.redis.listener.RedisMessageListenerContainer;
@@ -96,6 +97,12 @@ public class RedisExpiredPhotographListener extends KeyExpirationEventMessageLis
     @Autowired
     @Autowired
     private RemoteMessageService remoteMessageService;
     private RemoteMessageService remoteMessageService;
 
 
+    private final String testFormat = "【实验室安全智能】{}发生{}{}";
+    private final String streamUrlFormat = "rtsp://admin:hk123456@{}:554/h264/ch1/main/av_stream";
+
+    @Value("${file.path}")
+    private String rootPath;
+
     /**
     /**
      * Creates new {@link MessageListener} for {@code __keyevent@*__:expired} messages.
      * Creates new {@link MessageListener} for {@code __keyevent@*__:expired} messages.
      *
      *
@@ -111,39 +118,56 @@ public class RedisExpiredPhotographListener extends KeyExpirationEventMessageLis
         if (key.indexOf(BaseConstants.PHOTOGRAPH_QUEUE) != -1) {
         if (key.indexOf(BaseConstants.PHOTOGRAPH_QUEUE) != -1) {
             log.info("预警异常,缓存信息:" + key);
             log.info("预警异常,缓存信息:" + key);
             String[] keyStr = key.split("~");
             String[] keyStr = key.split("~");
-            WarningNoticeLog warningNoticeLog = JSON.parseObject(JSON.toJSONString(JSON.parseObject(keyStr[1]).getJSONObject("photographQueue")), WarningNoticeLog.class);
+            WarningNoticeLog warningNoticeLog = JSON.parseObject(JSON.toJSONString(JSON.parseObject(keyStr[1]).getJSONObject(BaseConstants.PHOTOGRAPH_QUEUE)), WarningNoticeLog.class);
             //调用拍摄照片接口
             //调用拍摄照片接口
             LabHardware labHardware = labHardwareService.findCameraByType(warningNoticeLog.getSubId(), 4, "1");
             LabHardware labHardware = labHardwareService.findCameraByType(warningNoticeLog.getSubId(), 4, "1");
             if (labHardware != null && StringUtils.isNotBlank(labHardware.getIpAddress())) {
             if (labHardware != null && StringUtils.isNotBlank(labHardware.getIpAddress())) {
                 //拼装StreamUrl
                 //拼装StreamUrl
-                String streamUrl = "rtsp://admin:hk123456@" + labHardware.getIpAddress() + ":554/h264/ch1/main/av_stream";
-                R<SysFile> fileR = remoteForwardService.photograph(streamUrl);
+                String streamUrl = StrUtil.format(streamUrlFormat,labHardware.getIpAddress());
+                R<SysFile>  fileR = remoteForwardService.photograph(streamUrl);
                 log.info("文件上传状态:{},接口返回消息:{},文件上传路径:{}", fileR.getCode(), fileR.getMsg(), fileR.getData());
                 log.info("文件上传状态:{},接口返回消息:{},文件上传路径:{}", fileR.getCode(), fileR.getMsg(), fileR.getData());
                 //调用照片到算法服务,返回结果
                 //调用照片到算法服务,返回结果
                 AlgorithmWarningBo algorithmWarningBo = new AlgorithmWarningBo();
                 AlgorithmWarningBo algorithmWarningBo = new AlgorithmWarningBo();
-                algorithmWarningBo.setId(warningNoticeLog.getId());
                 algorithmWarningBo.setSubId(warningNoticeLog.getSubId());
                 algorithmWarningBo.setSubId(warningNoticeLog.getSubId());
                 algorithmWarningBo.setSubName(warningNoticeLog.getSubName());
                 algorithmWarningBo.setSubName(warningNoticeLog.getSubName());
-                algorithmWarningBo.setFile(fileR.getData().getMultipartFile());
+                algorithmWarningBo.setId(warningNoticeLog.getId());
+                algorithmWarningBo.setFileUrl(fileR.getData().getUrl());
+                //algorithmWarningBo.setFileUrl("d:\\test.jpg");
                 ResultData resultData = remoteForwardService.warningCheck(algorithmWarningBo);
                 ResultData resultData = remoteForwardService.warningCheck(algorithmWarningBo);
                 if (HttpStatus.SUCCESS == resultData.getCode()) {
                 if (HttpStatus.SUCCESS == resultData.getCode()) {
-                    AlgorithmWarningVo algorithmWarningVo = JSON.parseObject(JSONObject.toJSONString(resultData.getData()), AlgorithmWarningVo.class);
-                    WarningDetail warningDetail = new WarningDetail();
-                    warningDetail.setLogId(warningNoticeLog.getId());
-                    warningDetail.setSubId(warningNoticeLog.getSubId());
-                    //预警类型(1预案抓拍 2穿戴抓拍)
-//                    warningDetail.setType(2);
-                    warningDetail.setPhotoUrl(fileR.getData().getUrl());
+                //算法调用 默认返回成功!
+                //if (HttpStatus.SUCCESS != 200) {
+                    log.error("调用算法服务失败!{}",resultData);
+                    return;
+                }
+                AlgorithmWarningVo algorithmWarningVo = JSON.parseObject(JSONObject.toJSONString(resultData.getData()), AlgorithmWarningVo.class);
+
+                //==========================test code 算法调用 默认返回异常假数据=========================
+                //AlgorithmWarningVo algorithmWarningVo = new AlgorithmWarningVo();
+                //algorithmWarningVo.setOriginalImage("/statics/2023/06/27/8897e540-481d-4731-a199-29bb1a9ad30f.jpg");
+                //algorithmWarningVo.setIsAlert(Boolean.TRUE);
+                //algorithmWarningVo.setId(warningNoticeLog.getId());
+                //==========================test code 算法调用 默认返回异常假数据=========================
+
+
+                WarningDetail warningDetail = new WarningDetail();
+                warningDetail.setLogId(warningNoticeLog.getId());
+                warningDetail.setSubId(warningNoticeLog.getSubId());
+                //预警类型(1预案抓拍 2穿戴抓拍)
+                //warningDetail.setType(2);
+                warningDetail.setPhotoUrl(fileR.getData().getUrl());
+                if(algorithmWarningVo.getIsAlert()){
                     warningDetail.setCallbackPhotoUrl(algorithmWarningVo.getResultImage());
                     warningDetail.setCallbackPhotoUrl(algorithmWarningVo.getResultImage());
-                    warningDetail.setResult(algorithmWarningVo.getIsAlert());
-                    warningDetail.setCreateTime(LocalDateTime.now());
-                    warningDetailService.save(warningDetail);
                 }
                 }
+                warningDetail.setResult(algorithmWarningVo.getIsAlert());
+                warningDetail.setCreateTime(LocalDateTime.now());
+                warningDetailService.save(warningDetail);
 
 
+                StringBuffer userIds = new StringBuffer();
+                StringBuffer phones = new StringBuffer();
                 if (!redisService.hasKey(key)) {
                 if (!redisService.hasKey(key)) {
                     //如果没有这个key了 则统计异常率
                     //如果没有这个key了 则统计异常率
                     //查询配置
                     //查询配置
-                    long count1 = warningNoticeLogService.count(new LambdaQueryWrapper<WarningNoticeLog>().eq(WarningNoticeLog::getSubId, warningNoticeLog.getSubId()));
                     WarningConfig warningConfig = warningConfigService.getOne(new LambdaQueryWrapper<WarningConfig>().eq(WarningConfig::getWarningType, 1));
                     WarningConfig warningConfig = warningConfigService.getOne(new LambdaQueryWrapper<WarningConfig>().eq(WarningConfig::getWarningType, 1));
                     List<WarningDetail> list = warningDetailService.list(new LambdaQueryWrapper<WarningDetail>().eq(WarningDetail::getLogId, warningNoticeLog.getId()));
                     List<WarningDetail> list = warningDetailService.list(new LambdaQueryWrapper<WarningDetail>().eq(WarningDetail::getLogId, warningNoticeLog.getId()));
                     if (list.size() > 0) {
                     if (list.size() > 0) {
@@ -152,14 +176,13 @@ public class RedisExpiredPhotographListener extends KeyExpirationEventMessageLis
                         if (div > warningConfig.getAnomalyRate()) {
                         if (div > warningConfig.getAnomalyRate()) {
 
 
                             WarningNoticeLogDto warningNoticeLogDto = new WarningNoticeLogDto();
                             WarningNoticeLogDto warningNoticeLogDto = new WarningNoticeLogDto();
+                            warningNoticeLogDto.setId(warningNoticeLog.getId());
                             warningNoticeLogDto.setWarningType(1);
                             warningNoticeLogDto.setWarningType(1);
                             warningNoticeLogDto.setWarningContent("未穿戴实验服");
                             warningNoticeLogDto.setWarningContent("未穿戴实验服");
                             warningNoticeLogDto.setWarningTime(LocalDateTime.now());
                             warningNoticeLogDto.setWarningTime(LocalDateTime.now());
                             warningNoticeLogDto.setWarningWay(warningConfig.getIllegalRemoval());
                             warningNoticeLogDto.setWarningWay(warningConfig.getIllegalRemoval());
                             //查询到实验室负责人id 安全责任人id
                             //查询到实验室负责人id 安全责任人id
                             List<CheckSubjectDto> subjectInfoList = labSubjectService.findSubjectInfoList(String.valueOf(warningNoticeLog.getSubId()));
                             List<CheckSubjectDto> subjectInfoList = labSubjectService.findSubjectInfoList(String.valueOf(warningNoticeLog.getSubId()));
-                            StringBuffer userIds = new StringBuffer();
-                            StringBuffer phones = new StringBuffer();
                             if (subjectInfoList.size() > 0) {
                             if (subjectInfoList.size() > 0) {
                                 CheckSubjectDto i = subjectInfoList.get(0);
                                 CheckSubjectDto i = subjectInfoList.get(0);
                                 warningNoticeLogDto.setSubId(i.getSubId());
                                 warningNoticeLogDto.setSubId(i.getSubId());
@@ -168,69 +191,73 @@ public class RedisExpiredPhotographListener extends KeyExpirationEventMessageLis
                                 warningNoticeLogDto.setFloorId(i.getFloorId());
                                 warningNoticeLogDto.setFloorId(i.getFloorId());
                                 warningNoticeLogDto.setFloorName(i.getFloorName());
                                 warningNoticeLogDto.setFloorName(i.getFloorName());
                                 warningNoticeLogDto.setRoomNum(i.getRoomNumber());
                                 warningNoticeLogDto.setRoomNum(i.getRoomNumber());
+                                warningNoticeLogDto.setIsDeleted(Boolean.FALSE);
                                 userIds.append(i.getSafeUserId()).append(",").append(i.getAdminId());
                                 userIds.append(i.getSafeUserId()).append(",").append(i.getAdminId());
                                 phones.append(i.getAdminPhone()).append(",").append(i.getSafeUserPhone());
                                 phones.append(i.getAdminPhone()).append(",").append(i.getSafeUserPhone());
                             }
                             }
-                            ResultData result = remoteLaboratoryService.addWarningNoticeLog(warningNoticeLogDto);
+                            ResultData result = remoteLaboratoryService.updateWarningNoticeLog(warningNoticeLogDto);
                             if (HttpStatus.SUCCESS == result.getCode()) {
                             if (HttpStatus.SUCCESS == result.getCode()) {
                                 log.info("未穿戴实验服保存日志成功!");
                                 log.info("未穿戴实验服保存日志成功!");
                             }
                             }
-                            ResultData urlScheme = remoteMessageService.getUrlScheme(Long.valueOf(String.valueOf(result.getData())));
-                            if(urlScheme.getCode() != HttpStatus.SUCCESS){
-                                log.error("获取urlScheme失败!");
-                            }
-                            String text = "【实验室安全系统】"+warningNoticeLogDto.getSubName()+"-监测到实验人员违规未穿戴实验服,请尽快确认。点击查看:https://lab.zjznai.com/labAppTest/earlyWarning/index.html?id="+result.getData()+"&urlScheme="+urlScheme.getData();
-                            //系统 短信 语音通知
-                            if (warningConfig.getSystemNotice() != null && count1 > warningConfig.getSystemNotice()) {
-                                //系统通知
-                                LabMessageContent labMessageContent = new LabMessageContent();
-                                labMessageContent.setSendMode(2);
-                                labMessageContent.setSendRange(3);
-                                labMessageContent.setMessClass(1);
-                                labMessageContent.setMessType(13);
-                                labMessageContent.setSubIds(String.valueOf(warningNoticeLog.getSubId()));
-                                labMessageContent.setUserIds(userIds.toString());
-                                labMessageContent.setContent(text);
-                                labMessageContentService.sendMessage(labMessageContent);
-                            }
-                            if (warningConfig.getMessageNotice() != null && count1 > warningConfig.getMessageNotice()) {
-                                //短信通知
-                                String[] strings = Stream.of(phones.toString().split(",")).filter(a -> StrUtil.isNotBlank(a)).collect(Collectors.joining(",")).split(",");
-
-                                if (strings != null) {
-                                    AlarmEntrty alarmEntrty = new AlarmEntrty(Routes.NoticePush, strings, SendTypes.SMS.toString(), text);
-                                    remoteAlarmService.send(alarmEntrty);
-                                    log.info("未穿戴实验服发送短信打电话消息推送完成!");
-                                }
-                            }
-                            if (warningConfig.getVoiceNotcie() != null && count1 > warningConfig.getVoiceNotcie()) {
-                                //短信通知
-                                log.info("打开喇叭-远程调用查询喇叭列表,实验室id={}", warningNoticeLog.getSubId());
-                                Integer count = labSparseHardwareService.selectSpeakerCount();
-                                R deviceList = remoteSpeakService.getDeviceList(1, count + 10, -99L, warningNoticeLog.getSubId());
-                                log.info("打开喇叭-远程调用喇叭列表返回内容: deviceList={}", JSON.toJSONString(deviceList));
-                                if (deviceList.getCode() == 200) {
-                                    List<PlayVo> playVoList = new ArrayList<>();
-                                    List<Map<String, Object>> mapList = (List<Map<String, Object>>) deviceList.getData();
-                                    for (Map<String, Object> map : mapList) {
-                                        if (com.zd.common.core.utils.StringUtils.isNotNull(map.get("deviceSn")) && com.zd.common.core.utils.StringUtils.isNotNull(map.get("port"))) {
-                                            PlayVo playVo = new PlayVo();
-                                            playVo.setSn(map.get("deviceSn") + "");
-                                            playVo.setDeviceIp(map.get("deviceIp") + "");
-                                            playVo.setPort(Integer.parseInt(map.get("port") + ""));
-                                            ParamVo paramVo = new ParamVo();
-                                            paramVo.setVol(Integer.parseInt(map.get("deviceVol").toString()));
-                                            playVo.setParams(paramVo);
-                                            playVoList.add(playVo);
-                                        } else {
-                                            log.info("打开喇叭-喇叭deviceSn/port为空!");
-                                        }
-                                    }
-                                    log.info("打开喇叭-远程调用喇叭播放音乐,playVoList={}", JSON.toJSONString(playVoList));
-                                    R r = remoteSpeakService.textParseUrlIps("sss", playVoList);
-                                    log.info("打开喇叭-远程调用喇叭播放音乐返回信息:{}", JSON.toJSONString(r));
+                        }
+                    }
+
+                    //是否通知
+                    long count1 = warningNoticeLogService.count(new LambdaQueryWrapper<WarningNoticeLog>().eq(WarningNoticeLog::getSubId, warningNoticeLog.getSubId()));
+                    ResultData urlScheme = remoteMessageService.getUrlScheme(warningNoticeLog.getId());
+                    if(urlScheme.getCode() != HttpStatus.SUCCESS){
+                        log.error("获取urlScheme失败!");
+                    }
+                    String text = "【实验室安全系统】"+warningNoticeLog.getSubName()+"-监测到实验人员违规未穿戴实验服,请尽快确认。点击查看:https://lab.zjznai.com/labAppTest/earlyWarning/index.html?id="+warningNoticeLog.getId()+"&urlScheme="+urlScheme.getData();
+                    //系统 短信 语音通知
+                    if (warningConfig.getSystemNotice() != null && count1 > warningConfig.getSystemNotice()) {
+                        //系统通知
+                        LabMessageContent labMessageContent = new LabMessageContent();
+                        labMessageContent.setSendMode(2);
+                        labMessageContent.setSendRange(3);
+                        labMessageContent.setMessClass(1);
+                        labMessageContent.setMessType(13);
+                        labMessageContent.setSubIds(String.valueOf(warningNoticeLog.getSubId()));
+                        labMessageContent.setUserIds(userIds.toString());
+                        labMessageContent.setContent(text);
+                        labMessageContentService.sendMessage(labMessageContent);
+                    }
+                    if (warningConfig.getMessageNotice() != null && count1 > warningConfig.getMessageNotice()) {
+                        //短信通知
+                        String[] strings = Stream.of(phones.toString().split(",")).filter(a -> StrUtil.isNotBlank(a)).collect(Collectors.joining(",")).split(",");
+
+                        if (strings != null) {
+                            AlarmEntrty alarmEntrty = new AlarmEntrty(Routes.NoticePush, strings, SendTypes.SMS.toString(), text);
+                            remoteAlarmService.send(alarmEntrty);
+                            log.info("未穿戴实验服发送短信打电话消息推送完成!");
+                        }
+                    }
+                    if (warningConfig.getVoiceNotcie() != null && count1 > warningConfig.getVoiceNotcie()) {
+                        //短信通知
+                        log.info("打开喇叭-远程调用查询喇叭列表,实验室id={}", warningNoticeLog.getSubId());
+                        Integer count = labSparseHardwareService.selectSpeakerCount();
+                        R deviceList = remoteSpeakService.getDeviceList(1, count + 10, -99L, warningNoticeLog.getSubId());
+                        log.info("打开喇叭-远程调用喇叭列表返回内容: deviceList={}", JSON.toJSONString(deviceList));
+                        if (deviceList.getCode() == 200) {
+                            List<PlayVo> playVoList = new ArrayList<>();
+                            List<Map<String, Object>> mapList = (List<Map<String, Object>>) deviceList.getData();
+                            for (Map<String, Object> map : mapList) {
+                                if (com.zd.common.core.utils.StringUtils.isNotNull(map.get("deviceSn")) && com.zd.common.core.utils.StringUtils.isNotNull(map.get("port"))) {
+                                    PlayVo playVo = new PlayVo();
+                                    playVo.setSn(map.get("deviceSn") + "");
+                                    playVo.setDeviceIp(map.get("deviceIp") + "");
+                                    playVo.setPort(Integer.parseInt(map.get("port") + ""));
+                                    ParamVo paramVo = new ParamVo();
+                                    paramVo.setVol(Integer.parseInt(map.get("deviceVol").toString()));
+                                    playVo.setParams(paramVo);
+                                    playVoList.add(playVo);
+                                } else {
+                                    log.info("打开喇叭-喇叭deviceSn/port为空!");
                                 }
                                 }
                             }
                             }
+                            log.info("打开喇叭-远程调用喇叭播放音乐,playVoList={}", JSON.toJSONString(playVoList));
+                            R r = remoteSpeakService.textParseUrlIps("sss", playVoList);
+                            log.info("打开喇叭-远程调用喇叭播放音乐返回信息:{}", JSON.toJSONString(r));
                         }
                         }
                     }
                     }
                 }
                 }

+ 6 - 0
zd-modules/zd-modules-laboratory/src/main/java/com/zd/laboratory/service/WarningNoticeLogService.java

@@ -28,4 +28,10 @@ public interface WarningNoticeLogService extends IService<WarningNoticeLog> {
      **/
      **/
     WarningNoticeLogVO getLogById(Long id);
     WarningNoticeLogVO getLogById(Long id);
 
 
+    /**
+     * 定时扫描实验服
+     * @Param []
+     * @Return void
+     **/
+    void labCoatTask();
 }
 }

+ 6 - 7
zd-modules/zd-modules-laboratory/src/main/java/com/zd/laboratory/service/impl/WarningNoticeLogServiceImpl.java

@@ -22,7 +22,6 @@ import com.zd.laboratory.service.WarningNoticeLogService;
 import com.zd.model.constant.BaseConstants;
 import com.zd.model.constant.BaseConstants;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
 import java.time.LocalDateTime;
 import java.time.LocalDateTime;
@@ -70,7 +69,6 @@ public class WarningNoticeLogServiceImpl extends ServiceImpl<WarningNoticeLogMap
         WarningNoticeLogVO warningNoticeLogVO = new WarningNoticeLogVO();
         WarningNoticeLogVO warningNoticeLogVO = new WarningNoticeLogVO();
         WarningNoticeLog warningNoticeLog = baseMapper.selectById(id);
         WarningNoticeLog warningNoticeLog = baseMapper.selectById(id);
         BeanUtils.copyProperties(warningNoticeLog, warningNoticeLogVO);
         BeanUtils.copyProperties(warningNoticeLog, warningNoticeLogVO);
-        //todo 查询短信、电话等信息
         List<WarningDetail> list = warningDetailService.list(new LambdaQueryWrapper<WarningDetail>().eq(WarningDetail::getLogId, warningNoticeLogVO.getId()).orderByDesc(WarningDetail::getId));
         List<WarningDetail> list = warningDetailService.list(new LambdaQueryWrapper<WarningDetail>().eq(WarningDetail::getLogId, warningNoticeLogVO.getId()).orderByDesc(WarningDetail::getId));
         warningNoticeLogVO.setWarningDetailList(list);
         warningNoticeLogVO.setWarningDetailList(list);
         List<AlarmLog> alarmLogs = alarmLogService.selectAlarmLogByKeyId(warningNoticeLog.getKeyId());
         List<AlarmLog> alarmLogs = alarmLogService.selectAlarmLogByKeyId(warningNoticeLog.getKeyId());
@@ -88,24 +86,25 @@ public class WarningNoticeLogServiceImpl extends ServiceImpl<WarningNoticeLogMap
 
 
     /**
     /**
      * 实验室有人时定时拍摄识别实验服
      * 实验室有人时定时拍摄识别实验服
+     * 每小时执行一次
      */
      */
-    @Scheduled(cron = "0 0/2 * * * ? ")
+//    @Scheduled(cron = "0 0/2 * * * ? ")
     public void labCoatTask() {
     public void labCoatTask() {
         //查询实验室是否有人
         //查询实验室是否有人
         List<LabOnlineSubVO> labSubjects = hardwareStateMapper.onlineByBigView(new LabSubjectVO());
         List<LabOnlineSubVO> labSubjects = hardwareStateMapper.onlineByBigView(new LabSubjectVO());
         //查询配置
         //查询配置
-        WarningConfig warningConfig = warningConfigService.getOne(new LambdaQueryWrapper<WarningConfig>().eq(WarningConfig::getWarningType, 2));
+        WarningConfig warningConfig = warningConfigService.getOne(new LambdaQueryWrapper<WarningConfig>().eq(WarningConfig::getWarningType, 1));
         Optional.ofNullable(labSubjects).orElseGet(Collections::emptyList).stream().forEach(LabOnlineSubVO -> {
         Optional.ofNullable(labSubjects).orElseGet(Collections::emptyList).stream().forEach(LabOnlineSubVO -> {
             //异常识别频率
             //异常识别频率
-            Long frequency = 60L;
+            Long frequency = 10L;
             //异常再识别数
             //异常再识别数
             Integer anomalyCount = warningConfig.getAnomalyCount();
             Integer anomalyCount = warningConfig.getAnomalyCount();
             WarningNoticeLog warningNoticeLog = new WarningNoticeLog();
             WarningNoticeLog warningNoticeLog = new WarningNoticeLog();
             warningNoticeLog.setWarningType(1);
             warningNoticeLog.setWarningType(1);
             warningNoticeLog.setSubId(LabOnlineSubVO.getId());
             warningNoticeLog.setSubId(LabOnlineSubVO.getId());
             warningNoticeLog.setSubName(LabOnlineSubVO.getName());
             warningNoticeLog.setSubName(LabOnlineSubVO.getName());
-//            warningNoticeLog.setWarningContent("未穿戴实验服");
-//            baseMapper.insert(warningNoticeLog);
+            warningNoticeLog.setIsDeleted(Boolean.TRUE);
+            baseMapper.insert(warningNoticeLog);
             for (int i = 0; i < anomalyCount; i++) {
             for (int i = 0; i < anomalyCount; i++) {
                 JSONObject jsonObject = new JSONObject();
                 JSONObject jsonObject = new JSONObject();
                 jsonObject.put(BaseConstants.PHOTOGRAPH_QUEUE,warningNoticeLog);
                 jsonObject.put(BaseConstants.PHOTOGRAPH_QUEUE,warningNoticeLog);