Browse Source

Merge remote-tracking branch 'origin/V1.0.11' into V1.0.11

jingyuanchao 11 tháng trước cách đây
mục cha
commit
c8eb4213df

+ 11 - 0
soc-api/soc-api-system/src/main/java/com/xunmei/system/api/RemoteHostService.java

@@ -12,6 +12,9 @@ import com.xunmei.system.api.factory.RemoteHostFallbackFactory;
 import org.springframework.cloud.openfeign.FeignClient;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.List;
+import java.util.Set;
+
 @FeignClient(contextId = "remoteHostService", value = ServiceNameConstants.HOST, fallbackFactory = RemoteHostFallbackFactory.class)
 public interface RemoteHostService {
     @PostMapping("/getRecordInfo")
@@ -62,6 +65,14 @@ public interface RemoteHostService {
     @PostMapping("/syncRule/{id}")
     AjaxResult syncAlarmRuleToHost(@PathVariable("id") Long id);
 
+    /**
+     * 向主机同步视频诊断
+     * @param iotCodes
+     * @return
+     */
+    @PostMapping("/syncDiagnose")
+    AjaxResult syncDiagnoseToHost(@RequestParam(value = "iotCodes") Set<String> iotCodes);
+
     @GetMapping("/syncDevice/{id}")
     AjaxResult syncDeviceToHost(@PathVariable("id") Long id);
 }

+ 8 - 0
soc-api/soc-api-system/src/main/java/com/xunmei/system/api/factory/RemoteHostFallbackFactory.java

@@ -13,6 +13,9 @@ import org.slf4j.LoggerFactory;
 import org.springframework.cloud.openfeign.FallbackFactory;
 import org.springframework.stereotype.Component;
 
+import java.util.List;
+import java.util.Set;
+
 @Component
 public class RemoteHostFallbackFactory implements FallbackFactory<RemoteHostService> {
     private static final Logger log = LoggerFactory.getLogger(RemoteHostFallbackFactory.class);
@@ -76,6 +79,11 @@ public class RemoteHostFallbackFactory implements FallbackFactory<RemoteHostServ
             }
 
             @Override
+            public AjaxResult syncDiagnoseToHost(Set<String> iotCodes) {
+                return null;
+            }
+
+            @Override
             public AjaxResult syncDeviceToHost(Long id) {
                 return null;
             }

+ 12 - 0
soc-modules/soc-modules-host/src/main/java/com/xunmei/host/iot/mapper/IotDeviceDiagnoseMissionChMapper.java

@@ -2,6 +2,12 @@ package com.xunmei.host.iot.mapper;
 
 import com.xunmei.common.core.domain.iot.domain.IotDeviceDiagnoseMissionCh;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.xunmei.common.core.domain.iot.domain.IotDeviceDiagnoseThresholdCh;
+import com.xunmei.host.iot.dto.IotDeviceDiagnoseMissionReq;
+import com.xunmei.host.iot.dto.IotDeviceDiagnoseThresholdReq;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
 
 /**
  * <p>
@@ -12,5 +18,11 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  * @since 2024-11-18
  */
 public interface IotDeviceDiagnoseMissionChMapper extends BaseMapper<IotDeviceDiagnoseMissionCh> {
+    List<IotDeviceDiagnoseMissionReq> selectMissions();
+    List<IotDeviceDiagnoseMissionCh> selectMissionChns(@Param("missionId")String missionId,@Param("iotCodes")List<String> iotCodes);
+    List<IotDeviceDiagnoseThresholdReq> selectThresholds();
+    List<IotDeviceDiagnoseThresholdCh> selectThresholdChns(@Param("thresholdCode")String thresholdCode,@Param("iotCodes")List<String> iotCodes);
 
+    //修改阈值绑定的设备数量
+    Integer updateDiagnoseNumber(@Param("thresholdCode")String thresholdCode);
 }

+ 3 - 0
soc-modules/soc-modules-host/src/main/java/com/xunmei/host/iot/service/IotCameraMissionRuleService.java

@@ -1,4 +1,7 @@
 package com.xunmei.host.iot.service;
 
+import java.util.List;
+
 public interface IotCameraMissionRuleService {
+    void syncDiagnoseToHost(List<String> iotCodes);
 }

+ 55 - 0
soc-modules/soc-modules-host/src/main/java/com/xunmei/host/iot/service/impl/IotCameraMissionRuleServiceImpl.java

@@ -3,6 +3,7 @@ package com.xunmei.host.iot.service.impl;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.google.common.collect.Lists;
 import com.xunmei.common.core.domain.iot.domain.*;
 import com.xunmei.common.core.utils.IDHelper;
 import com.xunmei.common.core.utils.StringUtils;
@@ -13,16 +14,21 @@ import com.xunmei.host.iot.mapper.IotDeviceDiagnoseMissionMapper;
 import com.xunmei.host.iot.mapper.IotDeviceDiagnoseThresholdChMapper;
 import com.xunmei.host.iot.mapper.IotDeviceDiagnoseThresholdMapper;
 import com.xunmei.host.iot.service.IotCameraMissionRuleService;
+import com.xunmei.host.north.service.IotWebsocketMsgService;
 import com.xunmei.host.websocket.constant.WebSocketConstants;
 import com.xunmei.host.websocket.dto.WebsocketExecuteReq;
 import com.xunmei.host.websocket.dto.WebsocketResult;
 import com.xunmei.host.websocket.enums.ProductEnums;
+import com.xunmei.host.websocket.enums.TopicTypeEnums;
 import com.xunmei.host.websocket.service.RouterService;
+import com.xunmei.host.websocket.service.WebsocketService;
 import com.xunmei.system.api.util.LogUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
+
+import javax.annotation.Resource;
 import java.util.List;
 import java.util.StringJoiner;
 
@@ -37,6 +43,11 @@ public class IotCameraMissionRuleServiceImpl implements IotCameraMissionRuleServ
     private IotDeviceDiagnoseThresholdMapper thresholdMapper;
     @Autowired
     private IotDeviceDiagnoseThresholdChMapper thresholdChMapper;
+    //下发消息使用
+    @Resource
+    private WebsocketService websocketService;
+    @Resource
+    private IotWebsocketMsgService websocketMsgService;
 
     @Override
     public ProductEnums product() {
@@ -146,6 +157,8 @@ public class IotCameraMissionRuleServiceImpl implements IotCameraMissionRuleServ
                         }
                     }
                 }
+                //更新绑定通道数量
+                missionChMapper.updateDiagnoseNumber(threshold.getThresholdCode());
             }
 
 
@@ -155,4 +168,46 @@ public class IotCameraMissionRuleServiceImpl implements IotCameraMissionRuleServ
         }
         return WebsocketResult.getSuccessPayload();
     }
+
+
+    @Override
+    public void syncDiagnoseToHost(List<String> iotCodes){
+        List<IotDeviceDiagnoseMissionReq> missions = missionChMapper.selectMissions();
+        List<IotDeviceDiagnoseMissionCh> missionChns;
+        List<String> subIotCodes;
+
+        for(String iotCode:iotCodes) {
+            subIotCodes = Lists.newArrayList();
+            subIotCodes.add(iotCode);
+            for (IotDeviceDiagnoseMissionReq mission : missions) {
+                missionChns = missionChMapper.selectMissionChns(mission.getMissionId(), subIotCodes);
+                if (missionChns != null && !missionChns.isEmpty()) {
+                    mission.setChns(missionChns);
+                } else {
+                    mission.setChns(Lists.newArrayList());
+                }
+            }
+            List<IotDeviceDiagnoseThresholdReq> thresholds = missionChMapper.selectThresholds();
+            List<IotDeviceDiagnoseThresholdCh> thresholdChns;
+            for (IotDeviceDiagnoseThresholdReq threshold : thresholds) {
+                thresholdChns = missionChMapper.selectThresholdChns(threshold.getThresholdCode(), subIotCodes);
+                if (thresholdChns != null && !thresholdChns.isEmpty()) {
+                    threshold.setChns(thresholdChns);
+                } else {
+                    threshold.setChns(Lists.newArrayList());
+                }
+            }
+
+            JSONObject param = new JSONObject();
+            param.put("missions",missions);
+            param.put("thresholds",thresholds);
+
+            final String topic = TopicTypeEnums.formatUrl(TopicTypeEnums.PRODUCT_SERVICE_INVOKE.getUrl(), ProductEnums.DETECTION_HOST.getProductName().get(0), ProductEnums.DETECTION_HOST.getProductName().get(1));
+            final WebsocketResult result = WebsocketResult.invokeHostServer(topic, new com.alibaba.fastjson.JSONObject(), WebSocketConstants.CAMERA_DIAGNOSE_TO_HOST, param);
+
+
+            final IotWebsocketMsg saveMsg = websocketMsgService.proactiveSendAndSaveMsg(iotCode, result);
+            websocketService.sendMsgByTokens(saveMsg, iotCode);
+        }
+    }
 }

+ 10 - 0
soc-modules/soc-modules-host/src/main/java/com/xunmei/host/north/controller/HostController.java

@@ -6,6 +6,7 @@ import com.xunmei.common.core.domain.work.dto.WorkDayDto;
 import com.xunmei.common.core.web.domain.AjaxResult;
 import com.xunmei.host.alarm.service.IotAlarmRuleService;
 import com.xunmei.host.iot.service.IIotDeviceInfoService;
+import com.xunmei.host.iot.service.IotCameraMissionRuleService;
 import com.xunmei.host.protection.service.IIotAlarmHostService;
 import com.xunmei.host.server.service.IotServerInfoService;
 import com.xunmei.host.websocket.service.WebsocketService;
@@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
 import java.io.IOException;
+import java.util.List;
 
 
 /**
@@ -38,6 +40,8 @@ public class HostController {
 
     @Resource
     IotAlarmRuleService alarmRuleService;
+    @Resource
+    IotCameraMissionRuleService iotCameraMissionRuleService;
 
     @ApiOperation("主动获取所有的主机基本信息")
     @GetMapping({"/getDevices"})
@@ -111,6 +115,12 @@ public class HostController {
         return AjaxResult.success();
     }
 
+    @PostMapping("/syncDiagnose")
+    AjaxResult syncDiagnoseToHost(@RequestParam(value = "iotCodes") List<String> iotCodes) {
+        iotCameraMissionRuleService.syncDiagnoseToHost(iotCodes);
+        return AjaxResult.success();
+    }
+
     @GetMapping("/syncDevice/{id}")
     AjaxResult syncDeviceToHost(@PathVariable("id") Long id) {
         deviceInfoService.syncDeviceToHost(id);

+ 2 - 0
soc-modules/soc-modules-host/src/main/java/com/xunmei/host/websocket/constant/WebSocketConstants.java

@@ -196,6 +196,8 @@ public interface WebSocketConstants {
     String ALARM_RULE = "alarmRule";
     //同步告警规则到主机
     String ALARM_RULE_TO_HOST = "alarmRuleToHost";
+    //同步视频诊断到主机
+    String CAMERA_DIAGNOSE_TO_HOST = "cameraDiagnoseToHost";
 
     /**
      * 增量workRule

+ 30 - 0
soc-modules/soc-modules-host/src/main/resources/mapper/IotDeviceDiagnoseMissionChMapper.xml

@@ -1,5 +1,35 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.xunmei.host.iot.mapper.IotDeviceDiagnoseMissionChMapper">
+    <select id="selectMissions" resultType="com.xunmei.host.iot.dto.IotDeviceDiagnoseMissionReq">
+        select * from iot_device_diagnose_mission
+    </select>
 
+    <select id="selectMissionChns" resultType="com.xunmei.common.core.domain.iot.domain.IotDeviceDiagnoseMissionCh">
+        select * from iot_device_diagnose_mission_ch where mission_id = #{missionId}
+        and  iot_token in
+        <foreach collection="iotCodes" index="index" item="item" open="(" separator="," close=")">
+            #{item}
+        </foreach>
+    </select>
+
+    <select id="selectThresholds" resultType="com.xunmei.host.iot.dto.IotDeviceDiagnoseThresholdReq">
+        select * from iot_device_diagnose_threshold
+    </select>
+
+    <select id="selectThresholdChns" resultType="com.xunmei.common.core.domain.iot.domain.IotDeviceDiagnoseThresholdCh">
+        select * from iot_device_diagnose_threshold_ch where threshold_code = #{thresholdCode}
+        and iot_token in
+        <foreach collection="iotCodes" index="index" item="item" open="(" separator="," close=")">
+            #{item}
+        </foreach>
+    </select>
+
+    <select id="updateDiagnoseNumber" resultType="java.lang.Integer">
+        update iot_device_diagnose_threshold set diagnose_number =
+        (
+           select count(1) from iot_device_diagnose_threshold_ch where threshold_code = #{thresholdCode}
+        )
+        where threshold_code =  #{thresholdCode}
+    </select>
 </mapper>

+ 3 - 0
soc-modules/soc-modules-iot/src/main/java/com/xunmei/iot/service/DiagnoseThresholdService.java

@@ -8,6 +8,7 @@ import com.xunmei.iot.vo.sensor.DiagnoseThresholdAddVo;
 import com.xunmei.iot.vo.sensor.DiagnoseThresholdPageVo;
 import javax.servlet.http.HttpServletResponse;
 import java.util.List;
+import java.util.Set;
 
 public interface DiagnoseThresholdService {
 
@@ -27,5 +28,7 @@ public interface DiagnoseThresholdService {
 
     void exportCamera(DiagnoseThresholdPageDto request, HttpServletResponse response);
 
+    void syncDiagnose(Set<String> iotCodes);
+
     List<AlarmTypeSelectedVO> thresholdTypeList();
 }

+ 16 - 3
soc-modules/soc-modules-iot/src/main/java/com/xunmei/iot/service/impl/DiagnoseMissionServiceImpl.java

@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
 import com.xunmei.common.core.constant.CacheConstants;
 import com.xunmei.common.core.constant.Constants;
 import com.xunmei.common.core.constant.SecurityConstants;
@@ -25,6 +26,7 @@ import com.xunmei.iot.dto.cameraDiagnose.DiagnoseMissionPageDto;
 import com.xunmei.iot.mapper.DiagnoseMissionChMapper;
 import com.xunmei.iot.mapper.DiagnoseThresholdChMapper;
 import com.xunmei.iot.service.DiagnoseMissionService;
+import com.xunmei.iot.service.DiagnoseThresholdService;
 import com.xunmei.iot.vo.alarmData.AlarmTypeSelectedVO;
 import com.xunmei.iot.vo.sensor.DiagnoseMissionBindVo;
 import com.xunmei.iot.vo.sensor.DiagnoseMissionExportVo;
@@ -43,6 +45,7 @@ import javax.servlet.http.HttpServletResponse;
 import java.net.URLEncoder;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Set;
 import java.util.UUID;
 import java.util.stream.Collectors;
 
@@ -52,7 +55,8 @@ public class DiagnoseMissionServiceImpl implements DiagnoseMissionService {
 
     @Resource
     private RemoteOrgService orgService;
-
+    @Resource
+    private DiagnoseThresholdService diagnoseThresholdService;
     @Autowired
     private DiagnoseMissionChMapper diagnoseMissionChMapper;
     @Autowired
@@ -132,9 +136,13 @@ public class DiagnoseMissionServiceImpl implements DiagnoseMissionService {
         //获取要绑定的通道
         List<IotDeviceDiagnoseMissionCh> batchInserts = Lists.newArrayList();
         List<IotDeviceDiagnoseMissionCh> batchUpdates = Lists.newArrayList();
+        Set<String> syncIotTokens = Sets.newHashSet();
 
         List<DiagnoseMissionBindVo> list = diagnoseMissionChMapper.selectCamerasBind(request);
         for (DiagnoseMissionBindVo info :list) {
+            //添加需要同步到主机的token
+            syncIotTokens.add(info.getIotToken());
+
             String beanId = info.getMissionId();
             IotDeviceDiagnoseMissionCh chn = new IotDeviceDiagnoseMissionCh();
             chn.setIotToken(info.getIotToken());
@@ -175,6 +183,8 @@ public class DiagnoseMissionServiceImpl implements DiagnoseMissionService {
 
         //更新默认阈值绑定数量
         diagnoseThresholdChMapper.updateDiagnoseNumber(system.getThresholdCode());
+        //下发同步主机
+        diagnoseThresholdService.syncDiagnose(syncIotTokens);
     }
 
     @Override
@@ -194,15 +204,18 @@ public class DiagnoseMissionServiceImpl implements DiagnoseMissionService {
 
 
         List<DiagnoseMissionBindVo> list = diagnoseMissionChMapper.selectCamerasBind(request);
-
+        Set<String> syncIotTokens = Sets.newHashSet();
         //需要解除绑定的集合
         List<Long> unbindIds = Lists.newArrayList();
         for (DiagnoseMissionBindVo item : list) {
+            //添加需要同步到主机的token
+            syncIotTokens.add(item.getIotToken());
             if (StringUtils.isNotEmpty(item.getMissionId())) unbindIds.add(item.getId());
         }
         //删除绑定的诊断任务
         if (unbindIds != null && !unbindIds.isEmpty()) diagnoseMissionChMapper.batchDeleteMissionCh(unbindIds);
-
+        //下发同步主机
+        diagnoseThresholdService.syncDiagnose(syncIotTokens);
     }
 
     @Override

+ 31 - 2
soc-modules/soc-modules-iot/src/main/java/com/xunmei/iot/service/impl/DiagnoseThresholdServiceImpl.java

@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
 import com.xunmei.common.core.constant.CacheConstants;
 import com.xunmei.common.core.constant.SecurityConstants;
 import com.xunmei.common.core.domain.iot.domain.IotDeviceDiagnoseThreshold;
@@ -26,6 +27,7 @@ import com.xunmei.iot.vo.sensor.DiagnoseThresholdAddVo;
 import com.xunmei.iot.vo.sensor.DiagnoseThresholdBindVo;
 import com.xunmei.iot.vo.sensor.DiagnoseThresholdExportVo;
 import com.xunmei.iot.vo.sensor.DiagnoseThresholdPageVo;
+import com.xunmei.system.api.RemoteHostService;
 import com.xunmei.system.api.RemoteOrgService;
 import com.xunmei.system.api.domain.SysOrg;
 import com.xunmei.system.api.vo.SysOrgVO;
@@ -33,19 +35,23 @@ import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.transaction.support.TransactionSynchronizationAdapter;
+import org.springframework.transaction.support.TransactionSynchronizationManager;
 import org.springframework.util.DigestUtils;
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
 import java.net.URLEncoder;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 @Service
 public class DiagnoseThresholdServiceImpl  implements DiagnoseThresholdService {
     @Resource
     private RemoteOrgService orgService;
-
+    @Autowired
+    private RemoteHostService hostService;
     @Autowired
     private DiagnoseThresholdChMapper diagnoseThresholdChMapper;
 
@@ -118,12 +124,15 @@ public class DiagnoseThresholdServiceImpl  implements DiagnoseThresholdService {
         List<DiagnoseThresholdBindVo> list = diagnoseThresholdChMapper.selectCamerasBind(req);
         //获取要绑定的通道
         List<IotDeviceDiagnoseThresholdCh> batchInserts = Lists.newArrayList();
+        Set<String> syncIotTokens = Sets.newHashSet();
 
         List<String> bindThresholdCodes = req.getBindThresholdCodes();
         if (bindThresholdCodes != null && !bindThresholdCodes.isEmpty()) {
             for (String bindThresholdCode:bindThresholdCodes) {
                 for (DiagnoseThresholdBindVo info : list) {
                     String beanThresholdCodes = info.getThresholdCodes();
+                    //添加需要同步到主机的token
+                    syncIotTokens.add(info.getIotToken());
                     //避免重复绑定
                     if (StringUtils.isNotEmpty(beanThresholdCodes)
                             &&beanThresholdCodes.contains(bindThresholdCode)) {continue;}
@@ -147,9 +156,12 @@ public class DiagnoseThresholdServiceImpl  implements DiagnoseThresholdService {
                 diagnoseThresholdChMapper.updateDiagnoseNumber(bindThresholdCode);
             }
         }
+        //下发同步主机
+        this.syncDiagnose(syncIotTokens);
     }
 
     @Override
+    @Transactional
     public void camerasUnbind(DiagnoseThresholdPageDto req){
         final SysOrg sysOrg = orgService.selectOrgById(req.getOrgId(), SecurityConstants.INNER);
         if (ObjectUtil.equal(Boolean.TRUE, req.getCheckSub())) {
@@ -164,7 +176,7 @@ public class DiagnoseThresholdServiceImpl  implements DiagnoseThresholdService {
         }
 
         List<DiagnoseThresholdBindVo> list = diagnoseThresholdChMapper.selectCamerasBind(req);
-
+        Set<String> syncIotTokens = Sets.newHashSet();
         //需要解除绑定的id集合
         List<Long> unbindIds = Lists.newArrayList();
 
@@ -173,6 +185,8 @@ public class DiagnoseThresholdServiceImpl  implements DiagnoseThresholdService {
             for (String unbindThresholdCode:unbindThresholdCodes) {
                 for (DiagnoseThresholdBindVo info : list) {
                     String beanThresholdCodes = info.getThresholdCodes();
+                    //添加需要同步到主机的token
+                    syncIotTokens.add(info.getIotToken());
                     //通道是否有绑定阈值
                     if(StringUtils.isNotEmpty(beanThresholdCodes)) {
                         String[] beanThresholdCodeArr = beanThresholdCodes.split(",");
@@ -196,6 +210,8 @@ public class DiagnoseThresholdServiceImpl  implements DiagnoseThresholdService {
                 diagnoseThresholdChMapper.updateDiagnoseNumber(bindThresholdCode);
             }
         }
+        //下发同步主机
+        this.syncDiagnose(syncIotTokens);
     }
 
     @Override
@@ -397,6 +413,19 @@ public class DiagnoseThresholdServiceImpl  implements DiagnoseThresholdService {
         return list;
     }
 
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void syncDiagnose(Set<String> iotCodes) {
+        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
+            @Override
+            public void afterCommit() {
+
+                hostService.syncDiagnoseToHost(iotCodes);
+
+            }
+        });
+    }
+
     private String getThresholdGuid(IotDeviceDiagnoseThreshold threshold) {
         StringBuffer sb = new StringBuffer();
         String color = "@value>=150";