Browse Source

优化定时任务批量更新设备状态方法,将离线告警推送拆分出来单独创建一个设备离线告警定时任务;

james 1 month ago
parent
commit
eea0b93cec

+ 62 - 100
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/impl/DmpDeviceInfoServiceImpl.java

@@ -212,91 +212,50 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
 
     @Override
     public void deviceStatus(){
-//        log.info("定时同步设备状态和设备告警数据 start");
-//        ApiResult<List<StatusVO>> resultList = dataQueryClient.status();
-//        List<StatusVO> list = resultList.getData();
-//        if(CollectionUtils.isNotEmpty(list)){
-//            List<String> devList = new ArrayList<>();
-//            for(int i=0;i<list.size();i++){
-//                devList.add(list.get(i).getDeviceId());
-//            }
-//
-//            List<DmpDeviceInfo> devInfoList = new ArrayList<>();
-//            List<BaseAlarm> baseUnAlarmList = new ArrayList<>();
-////            List<BaseAlarm> baseAlarmList = new ArrayList<>();
-//            if(devList.size() > 0) {
-//                devInfoList = baseMapper.getDeviceInfoList(devList);
-//                baseUnAlarmList = baseMapper.getUnDeviceAlarmList(devList);
-////                baseAlarmList = baseMapper.getDeviceAlarmList(devList);
-//            }
-//
-//            if(CollectionUtils.isNotEmpty(devInfoList)){
-//                for(int i=0;i<list.size();i++){
-//                    String date =  list.get(i).getTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
-//                    String currentDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
-//                    LambdaUpdateWrapper<DmpDeviceStatus> updateWrapper = Wrappers.lambdaUpdate();
-//                    if(date.equals(currentDate)){
-//                        updateWrapper.set(DmpDeviceStatus::getDeviceStatus,1)  //设备在线
-//                                .set(DmpDeviceStatus::getLastOnlineTime,list.get(i).getTime())
-//                                .eq(DmpDeviceStatus::getDeviceId,list.get(i).getDeviceId())
-//                                .eq(DmpDeviceStatus::getProductCode,list.get(i).getProductCode());
-//                        //设备离线告警恢复在线自动默认处理
-//                        if(CollectionUtils.isNotEmpty(baseUnAlarmList)){
-//                            for(int j=0;j<baseUnAlarmList.size();j++){
-//                                if(list.get(i).getDeviceId().equals(baseUnAlarmList.get(j).getDeviceId()) && list.get(i).getProductCode().equals(baseUnAlarmList.get(j).getProductCode())){
-//                                    LambdaUpdateWrapper<BaseAlarm> updateWrapper1 = Wrappers.lambdaUpdate();
-//                                    updateWrapper1.set(BaseAlarm::getHandleStatus,1)
-//                                            .set(BaseAlarm::getHandleBy,"YT_admin")
-//                                            .set(BaseAlarm::getHandleTime,LocalDateTime.now())
-//                                            .eq(BaseAlarm::getDeviceId,list.get(i).getDeviceId())
-//                                            .eq(BaseAlarm::getHandleStatus,0)
-//                                            .eq(BaseAlarm::getProductCode,list.get(i).getProductCode());
-//                                    this.update(updateWrapper1);
-//                                    break;
-//                                }
-//                            }
-//                        }
-//
-//                    }else{
-//                        updateWrapper.set(DmpDeviceStatus::getDeviceStatus,2)  //设备离线
-//                                .set(DmpDeviceStatus::getLastOfflineTime,list.get(i).getTime())
-//                                .eq(DmpDeviceStatus::getDeviceId,list.get(i).getDeviceId())
-//                                .eq(DmpDeviceStatus::getProductCode,list.get(i).getProductCode());
-//
-//                        for(int j=0;j<devInfoList.size();j++){
-//                            if(list.get(i).getDeviceId().equals(devInfoList.get(j).getDeviceId()) && list.get(i).getProductCode().equals(devInfoList.get(j).getProductCode())){
-//                                int count = 0;
-//                                for(int k=0;k<baseUnAlarmList.size();k++){
-//                                    if(list.get(i).getDeviceId().equals(baseUnAlarmList.get(k).getDeviceId()) && list.get(i).getProductCode().equals(baseUnAlarmList.get(k).getProductCode())){
-//                                        count++;
-//                                    }
-//                                }
-//                                if(count == 0){
-//                                    BaseAlarm baseAlarm = new BaseAlarm();
-//                                    baseAlarm.setDeviceId(list.get(i).getDeviceId());
-//                                    baseAlarm.setAlarmTime(LocalDateTime.parse(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH"))+":00:00",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
-//                                    baseAlarm.setAlarmType("001");
-//                                    baseAlarm.setAlarmObject(devInfoList.get(j).getDeviceName());
-//                                    baseAlarm.setAlarmData("");
-//                                    baseAlarm.setAlarmAttribute("离线告警");
-//                                    baseAlarm.setAlarmContent("设备离线告警");
-//                                    baseAlarm.setAlarmGrade(1);
-//
-//                                    baseAlarm.setAlarmAddress(devInfoList.get(j).getInstallAddress());
-//                                    baseAlarm.setProductCode(devInfoList.get(j).getProductCode());
-//                                    this.add(baseAlarm);
-//                                }
-//                                break;
-//                            }
-//                        }
-//
-//                    }
-//                    dmpDeviceStatusService.update(updateWrapper);
-//                }
-//            }
-//
-//        }
-//        log.info("定时同步设备状态和设备告警数据 end");
+        log.info("定时同步设备状态数据 start");
+        LastInnerQueryVO queryVO = new LastInnerQueryVO();
+        List<String> deviceuuidList = new ArrayList<>();
+        LambdaQueryWrapper<DmpDeviceInfo> queryWrapper = Wrappers.lambdaQuery();
+        queryWrapper.select(DmpDeviceInfo::getDeviceUuid)
+                .eq(DmpDeviceInfo::getDeleteFlag,0)
+                .ne(DmpDeviceInfo::getServiceStatus,3)
+                .orderByDesc(DmpDeviceInfo::getId);
+        List<DmpDeviceInfo> devList = this.list(queryWrapper);
+        if(CollectionUtils.isNotEmpty(devList)){
+            int count = devList.size();
+            for (int i = 0; i < devList.size(); i++) {
+                deviceuuidList.add(devList.get(i).getDeviceUuid());
+            }
+        }
+        queryVO.setDeviceuuid(deviceuuidList);
+
+        List<LastInnerResultVO> list = remoteTsdbProxyService.last(queryVO);
+        if(CollectionUtils.isNotEmpty(list)){
+            for(int i=0;i<list.size();i++){
+                if(Objects.nonNull(list.get(i).getMetrics())){
+                    String deviceuuid = list.get(i).getDeviceuuid();
+                    LocalDateTime lTime = Instant.ofEpochMilli(Long.valueOf(list.get(i).getMetrics().get("time").toString())).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
+                    String date =  lTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
+                    String currentDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
+                    LambdaUpdateWrapper<DmpDeviceStatus> updateWrapper = Wrappers.lambdaUpdate();
+                    if(date.equals(currentDate)){
+                        updateWrapper.set(DmpDeviceStatus::getDeviceStatus,1)  //设备在线
+                                .set(DmpDeviceStatus::getLastOnlineTime,lTime)
+                                .eq(DmpDeviceStatus::getDeviceUuid,deviceuuid);
+
+                    }else{
+                        updateWrapper.set(DmpDeviceStatus::getDeviceStatus,2)  //设备离线
+                                .set(DmpDeviceStatus::getLastOnlineTime,lTime)
+                                .eq(DmpDeviceStatus::getDeviceUuid,deviceuuid);
+
+                    }
+                    dmpDeviceStatusService.update(updateWrapper);
+                }
+
+            }
+
+        }
+        log.info("定时同步设备状态数据 end");
     }
 
     @Override
@@ -304,23 +263,26 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
         List<LastInnerResultVO> list = remoteTsdbProxyService.last(queryVO);
         if(CollectionUtils.isNotEmpty(list)){
             for(int i=0;i<list.size();i++){
-                String deviceuuid = list.get(i).getDeviceuuid();
-                LocalDateTime lTime = Instant.ofEpochMilli(Long.valueOf(list.get(i).getMetrics().get("time").toString())).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
-                String date =  lTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
-                String currentDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
-                LambdaUpdateWrapper<DmpDeviceStatus> updateWrapper = Wrappers.lambdaUpdate();
-                if(date.equals(currentDate)){
-                    updateWrapper.set(DmpDeviceStatus::getDeviceStatus,1)  //设备在线
-                            .set(DmpDeviceStatus::getLastOnlineTime,lTime)
-                            .eq(DmpDeviceStatus::getDeviceUuid,deviceuuid);
-
-                }else{
-                    updateWrapper.set(DmpDeviceStatus::getDeviceStatus,2)  //设备离线
-                            .set(DmpDeviceStatus::getLastOnlineTime,lTime)
-                            .eq(DmpDeviceStatus::getDeviceUuid,deviceuuid);
+                if(Objects.nonNull(list.get(i).getMetrics())){
+                    String deviceuuid = list.get(i).getDeviceuuid();
+                    LocalDateTime lTime = Instant.ofEpochMilli(Long.valueOf(list.get(i).getMetrics().get("time").toString())).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
+                    String date =  lTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
+                    String currentDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
+                    LambdaUpdateWrapper<DmpDeviceStatus> updateWrapper = Wrappers.lambdaUpdate();
+                    if(date.equals(currentDate)){
+                        updateWrapper.set(DmpDeviceStatus::getDeviceStatus,1)  //设备在线
+                                .set(DmpDeviceStatus::getLastOnlineTime,lTime)
+                                .eq(DmpDeviceStatus::getDeviceUuid,deviceuuid);
+
+                    }else{
+                        updateWrapper.set(DmpDeviceStatus::getDeviceStatus,2)  //设备离线
+                                .set(DmpDeviceStatus::getLastOnlineTime,lTime)
+                                .eq(DmpDeviceStatus::getDeviceUuid,deviceuuid);
 
+                    }
+                    dmpDeviceStatusService.update(updateWrapper);
                 }
-                dmpDeviceStatusService.update(updateWrapper);
+
             }
 
         }