Procházet zdrojové kódy

soc-modules-host模块,视频诊断上报优化

humingshi-7@163.com před 11 měsíci
rodič
revize
6a908979e0

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

@@ -4,9 +4,9 @@ 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.google.common.collect.Sets;
 import com.xunmei.common.core.domain.iot.domain.*;
 import com.xunmei.common.core.utils.IDHelper;
-import com.xunmei.common.core.utils.StringUtils;
 import com.xunmei.host.iot.dto.IotDeviceDiagnoseMissionReq;
 import com.xunmei.host.iot.dto.IotDeviceDiagnoseThresholdReq;
 import com.xunmei.host.iot.mapper.IotDeviceDiagnoseMissionChMapper;
@@ -27,9 +27,11 @@ 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 org.springframework.util.DigestUtils;
 import javax.annotation.Resource;
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
 import java.util.StringJoiner;
 
 @Service
@@ -72,6 +74,7 @@ public class IotCameraMissionRuleServiceImpl implements IotCameraMissionRuleServ
 
     private JSONObject receiveAndUpdateMissionCamera(WebsocketExecuteReq req) {
         try {
+            Set<String> iotCodes = Sets.newHashSet();
             String limitOne = "limit 1";
 
             final Object data = req.getData();
@@ -82,24 +85,41 @@ public class IotCameraMissionRuleServiceImpl implements IotCameraMissionRuleServ
             final List<IotDeviceDiagnoseMissionReq> missions = JSON.parseArray(jsb.getString("missions"),IotDeviceDiagnoseMissionReq.class);
             final List<IotDeviceDiagnoseThresholdReq> thresholds = JSON.parseArray(jsb.getString("thresholds"),IotDeviceDiagnoseThresholdReq.class);
 
+            for(IotDeviceDiagnoseMissionReq missionReq: missions){
+                String missionId = missionReq.getMissionId();
+                if(!missionId.equals("5ed19826-dd47-421e-a632-b00ba9271c45")){//非-系统默认任务
+
+                    QueryWrapper<IotDeviceDiagnoseMission> wrapper = new QueryWrapper<>();
+                    wrapper.eq("mission_id", missionId);
+                    wrapper.last(limitOne);
+                    IotDeviceDiagnoseMission bean = missionMapper.selectOne(wrapper);
+
+                    if(bean != null){//匹配到missionId
+                        missionMapper.updateById(missionReq);
+                    }else{//未匹配到missionId
+                        wrapper = new QueryWrapper<>();
+                        wrapper.and(on -> on.eq("begin_time", missionReq.getBeginTime()).eq("end_time", missionReq.getEndTime()).eq("cycle",missionReq.getCycle())
+                                .or().eq("name",missionReq.getName()));
+                        wrapper.last(limitOne);
+
+                        IotDeviceDiagnoseMission similarBean = missionMapper.selectOne(wrapper);
+                        //名称或者规则一样,治理该主机的诊断任务(不执行插入更新操作,但添加下发该主机IotToken同步)
+                        if(similarBean != null){
+                            missionId = similarBean.getMissionId();
+                            //todo:
+                            LogUtils.STATUS_INFO_STATUS_SENSOR.info("治理的主机missionId={}",missionReq.getMissionId());
+                            iotCodes.add(iotToken);
+                        }else{//未匹配到missionId,且名称或规则不一样
+                            missionId = IDHelper.id().toString();
+                            missionReq.setMissionId(missionId);
+                            missionMapper.insert(missionReq);
+                        }
+                    }
 
-            for(IotDeviceDiagnoseMissionReq mission: missions){
-                //入库任务
-                QueryWrapper<IotDeviceDiagnoseMission> wrapper = new QueryWrapper<>();
-                wrapper.eq("mission_id", mission.getMissionId());
-                if(StringUtils.isNotEmpty(mission.getIotToken())) {
-                    wrapper.eq("iot_token", mission.getIotToken());
-                }
-                wrapper.last(limitOne);
-                IotDeviceDiagnoseMission bean = missionMapper.selectOne(wrapper);
-                if (bean == null) {
-                    missionMapper.insert(mission);
-                }else if(bean.getMissionId().equals("5ed19826-dd47-421e-a632-b00ba9271c45")){//系统默认任务
-                }else{
-                    missionMapper.updateById(mission);
                 }
 
-                List<IotDeviceDiagnoseMissionCh> chns = mission.getChns();
+
+                List<IotDeviceDiagnoseMissionCh> chns = missionReq.getChns();
                 if(chns != null && !chns.isEmpty()){
                     for(IotDeviceDiagnoseMissionCh missionCh:chns){
                         QueryWrapper<IotDeviceDiagnoseMissionCh> wrapperCh = new QueryWrapper<>();
@@ -109,6 +129,9 @@ public class IotCameraMissionRuleServiceImpl implements IotCameraMissionRuleServ
                         wrapperCh.eq("channel", missionCh.getChannel());
                         wrapperCh.last(limitOne);
                         IotDeviceDiagnoseMissionCh beanCh = missionChMapper.selectOne(wrapperCh);
+
+                        missionCh.setMissionId(missionId);
+
                         if(beanCh == null){
                             missionCh.setIotToken(iotToken);
                             missionChMapper.insert(missionCh);
@@ -119,27 +142,37 @@ public class IotCameraMissionRuleServiceImpl implements IotCameraMissionRuleServ
                 }
             }
 
-            for(IotDeviceDiagnoseThresholdReq threshold: thresholds){
-                //入库状态app附表
-                QueryWrapper<IotDeviceDiagnoseThreshold> wrapper = new QueryWrapper<>();
-                wrapper.eq("threshold_code", threshold.getThresholdCode());
-                if(StringUtils.isNotEmpty(threshold.getIotToken())) {
-                    wrapper.eq("iot_token", threshold.getIotToken());
-                }
-                wrapper.last(limitOne);
-                IotDeviceDiagnoseThreshold bean = thresholdMapper.selectOne(wrapper);
-                if (bean == null) {
-                    //新增时,使用平台生成的id,然后下发到主机统一治理
-                    threshold.setThresholdId(IDHelper.id());
-                    threshold.setDiagnoseNumber(0);
-                    thresholdMapper.insert(threshold);
-                }else if(bean.getThresholdId()==1){//系统默认阈值
-                }else{
-                    threshold.setThresholdId(bean.getThresholdId());
-                    thresholdMapper.updateById(threshold);
+            for(IotDeviceDiagnoseThresholdReq thresholdReq: thresholds){
+                Long thresholdId = thresholdReq.getThresholdId();
+                if(thresholdId != 1){//非-系统默认阈值
+                    QueryWrapper<IotDeviceDiagnoseThreshold> wrapper = new QueryWrapper<>();
+                    wrapper.eq("threshold_code", thresholdReq.getThresholdCode());
+//                    if(StringUtils.isNotEmpty(thresholdReq.getIotToken())) {
+//                        wrapper.eq("iot_token", thresholdReq.getIotToken());
+//                    }
+                    wrapper.last(limitOne);
+                    IotDeviceDiagnoseThreshold bean = thresholdMapper.selectOne(wrapper);
+                    if (bean == null) {
+                        //新增时,使用平台生成的id,然后下发到主机统一治理
+                        thresholdId = IDHelper.id();
+                        thresholdReq.setThresholdId(thresholdId);
+                        thresholdReq.setDiagnoseNumber(0);
+                        thresholdMapper.insert(thresholdReq);
+                    }else{
+                        thresholdId = bean.getThresholdId();
+                        thresholdReq.setThresholdId(thresholdId);
+                        if(!thresholdReq.getThresholdName().equals(bean.getThresholdName())){//规则一样,但名称不一样,治理该主机的诊断阈值(添加下发该主机IotToken同步)
+                            thresholdReq.setThresholdName(bean.getThresholdName());
+                            //todo:
+                            LogUtils.STATUS_INFO_STATUS_SENSOR.info("治理的主机thresholdCode={}",thresholdReq.getThresholdCode());
+                            iotCodes.add(iotToken);
+                        }
+                        thresholdMapper.updateById(thresholdReq);
+                    }
                 }
 
-                List<IotDeviceDiagnoseThresholdCh> chns = threshold.getChns();
+
+                List<IotDeviceDiagnoseThresholdCh> chns = thresholdReq.getChns();
                 if(chns != null && !chns.isEmpty()){
                     for(IotDeviceDiagnoseThresholdCh thresholdCh:chns){
                         QueryWrapper<IotDeviceDiagnoseThresholdCh> wrapperCh = new QueryWrapper<>();
@@ -158,10 +191,12 @@ public class IotCameraMissionRuleServiceImpl implements IotCameraMissionRuleServ
                     }
                 }
                 //更新绑定通道数量
-                missionChMapper.updateDiagnoseNumber(threshold.getThresholdCode());
+                missionChMapper.updateDiagnoseNumber(thresholdReq.getThresholdCode());
             }
 
-
+            if(iotCodes != null && !iotCodes.isEmpty()){
+                this.syncDiagnoseToHost(new ArrayList<>(iotCodes));
+            }
         } catch (Exception e) {
             LogUtils.STATUS_INFO_STATUS_SENSOR.info("诊断绑定摄像机: 处理出现异常{}", e);
             throw new RuntimeException(e);

+ 28 - 10
soc-modules/soc-modules-iot/src/main/java/com/xunmei/iot/service/impl/DiagnoseMissionServiceImpl.java

@@ -247,17 +247,26 @@ public class DiagnoseMissionServiceImpl implements DiagnoseMissionService {
 
     @Override
     public void addMission(IotDeviceDiagnoseMission request){
-        QueryWrapper<IotDeviceDiagnoseMission> queryWrapper = new QueryWrapper<>();
-        queryWrapper.eq("name", request.getName());
+        QueryWrapper<IotDeviceDiagnoseMission> queryWrapper = new QueryWrapper();
+        queryWrapper.eq("begin_time", request.getBeginTime()).eq("end_time", request.getEndTime())
+        .eq("cycle",request.getCycle());
         List<IotDeviceDiagnoseMission> list = diagnoseMissionChMapper.selectList(queryWrapper);
-        if (list != null && !list.isEmpty()) {
-            String errorMsg = "任务名已存在";
+        if (!list.isEmpty()) {
+            String errorMsg = "已存在相同参数的任务配置'" + list.get(0).getName() + "'!";
             throw  new ServiceException(errorMsg);
         }
+
+        queryWrapper = new QueryWrapper<>();
+        queryWrapper.eq("name", request.getName());
+        long nameCount = diagnoseMissionChMapper.selectCount(queryWrapper);
+        if (nameCount > 0) {
+            String errorMsg = "已存在相同名称的任务配置";
+            throw  new ServiceException(errorMsg);
+        }
+
         IotDeviceDiagnoseMission mission = new IotDeviceDiagnoseMission();
 
-        UUID uuid = UUID.randomUUID();
-        mission.setMissionId(uuid.toString());
+        mission.setMissionId(IDHelper.id().toString());
         mission.setCreateTime(DateUtils.getTime());
 
         mission.setName(request.getName());
@@ -281,14 +290,23 @@ public class DiagnoseMissionServiceImpl implements DiagnoseMissionService {
             throw  new ServiceException(errorMsg);
         }
 
-        QueryWrapper<IotDeviceDiagnoseMission> queryWrapper = new QueryWrapper<>();
-        queryWrapper.eq("name", request.getName()).ne("mission_id", request.getMissionId());
+        QueryWrapper<IotDeviceDiagnoseMission> queryWrapper = new QueryWrapper();
+        queryWrapper.eq("begin_time", request.getBeginTime()).eq("end_time", request.getEndTime())
+                .eq("cycle",request.getCycle()).ne("mission_id", request.getMissionId());
         List<IotDeviceDiagnoseMission> list = diagnoseMissionChMapper.selectList(queryWrapper);
-        if (list != null && !list.isEmpty()) {
-            String errorMsg = "任务名已存在";
+        if (!list.isEmpty()) {
+            String errorMsg = "已存在相同参数的任务配置'" + list.get(0).getName() + "'!";
             throw  new ServiceException(errorMsg);
         }
 
+
+        queryWrapper = new QueryWrapper<>();
+        queryWrapper.eq("name", request.getName()).ne("mission_id", request.getMissionId());
+        long nameCount = diagnoseMissionChMapper.selectCount(queryWrapper);
+        if (nameCount > 0) {
+            String errorMsg = "已存在相同名称的任务配置";
+            throw  new ServiceException(errorMsg);
+        }
         IotDeviceDiagnoseMission mission = new IotDeviceDiagnoseMission();
 
         mission.setMissionId(request.getMissionId());