Browse Source

优化全量设备状态更新定时任务调用超时,将全量设备状态更新接口设置为异步执行,同时封装ApiResult<>

james 1 week ago
parent
commit
d3fcec54f1

+ 62 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/config/DeviceOperate.java

@@ -0,0 +1,62 @@
+package com.usky.iot.service.config;
+
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.usky.common.core.bean.ApiResult;
+import com.usky.demo.RemoteTsdbProxyService;
+import com.usky.iot.domain.DmpDeviceStatus;
+import com.usky.iot.mapper.DmpDeviceStatusMapper;
+import com.usky.iot.service.DmpDeviceStatusService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.Async;
+
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeFormatter;
+import java.util.List;
+import java.util.Map;
+
+@Slf4j
+@Configuration
+public class DeviceOperate {
+    @Autowired
+    private RemoteTsdbProxyService remoteTsdbProxyService;
+
+    @Autowired
+    DmpDeviceStatusService dmpDeviceStatusService;
+
+    @Async
+    public void updateDeviceStatus(){
+        log.info("定时同步设备状态数据 start");
+        ApiResult<List<Map<String,Object>>> list = remoteTsdbProxyService.getAllDeviceRealTime();
+        List<Map<String,Object>> dataList = list.getData();
+        if(CollectionUtils.isNotEmpty(dataList)){
+            for(int i=0;i<dataList.size();i++){
+                String deviceuuid = dataList.get(i).get("deviceuuid").toString();
+                LocalDateTime lTime = Instant.ofEpochMilli(Long.valueOf(dataList.get(i).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");
+    }
+}

+ 8 - 43
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/impl/DmpDeviceInfoServiceImpl.java

@@ -40,6 +40,7 @@ import com.usky.iot.service.BaseFacilityDeviceService;
 import com.usky.iot.service.DmpDeviceInfoService;
 import com.usky.iot.service.DmpDeviceStatusService;
 import com.usky.iot.service.DmpProductAttributeService;
+import com.usky.iot.service.config.DeviceOperate;
 import com.usky.iot.service.enums.TopicInfo;
 import com.usky.iot.service.vo.*;
 import com.usky.transfer.RemoteTransferService;
@@ -97,6 +98,9 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
 	@Autowired
     private DmpProductAttributeMapper dmpProductAttributeMapper;
 
+    @Autowired
+    private DeviceOperate deviceOperate;
+
     private static final String ALARM_HTTP_URL = "/service-alarm/baseAlarm/alarmInfo";
 
 
@@ -422,50 +426,11 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
         return null != one && !Objects.equals(one.getId(), id);
     }
 
-    @Async
+    @Override
     public void deviceStatus(){
-        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<Map<String,Object>> list = remoteTsdbProxyService.getAllDeviceRealTime();
-        if(CollectionUtils.isNotEmpty(list)){
-            for(int i=0;i<list.size();i++){
-                String deviceuuid = list.get(i).get("deviceuuid").toString();
-                LocalDateTime lTime = Instant.ofEpochMilli(Long.valueOf(list.get(i).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");
+        log.info("设备状态 start");
+        deviceOperate.updateDeviceStatus();
+        log.info("设备状态 end");
     }
 
     @Override