|
|
@@ -0,0 +1,95 @@
|
|
|
+package com.template.scheduled;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.template.model.Devices;
|
|
|
+import com.template.model.Records;
|
|
|
+import com.template.services.DevicesService;
|
|
|
+import com.template.services.RecordsService;
|
|
|
+import org.apache.commons.lang3.time.DateUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author ceshi
|
|
|
+ * @since 2023-04-20
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Controller
|
|
|
+public class ScheduledService {
|
|
|
+
|
|
|
+ private static Logger logger = LoggerFactory.getLogger( ScheduledService.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RecordsService recordsService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ DevicesService devicesService;
|
|
|
+
|
|
|
+ //定时保存停车场实时数据 每小时一次
|
|
|
+ @Scheduled(cron="0 0/10 * * * ?")
|
|
|
+ public void getParkingInfoScheduled() {
|
|
|
+ QueryWrapper<Records> queryWrapper=new QueryWrapper<>();
|
|
|
+ queryWrapper.last("order by update_at desc");
|
|
|
+ //查询最新记录获取事件,更新10分钟后数据
|
|
|
+ Records oldRecords=recordsService.getOne(queryWrapper);
|
|
|
+ Date endTime = DateUtils.addMinutes(oldRecords.getUpdateAt(), 10);
|
|
|
+ if(endTime.after(new Date())){
|
|
|
+ System.exit(0);
|
|
|
+ }
|
|
|
+ List<Devices> devicesList=devicesService.list(new QueryWrapper<>());
|
|
|
+ logger.info("devicesList++++++++++++++"+devicesList.size());
|
|
|
+ List<Records> recordsList=new ArrayList<>();
|
|
|
+ String[] arr= {"Temperature","Humidity","Light","CO2"};
|
|
|
+ for(Devices devic:devicesList){
|
|
|
+ for(int i=0;i< arr.length;i++){
|
|
|
+ Records records=new Records();
|
|
|
+ records.setDeviceId(devic.getId());
|
|
|
+ switch (arr[i]){
|
|
|
+ case "Temperature":
|
|
|
+ records.setType("Temperature");
|
|
|
+ records.setValue(this.getRandValue(18,28));
|
|
|
+ records.setCreateAt(endTime);
|
|
|
+ records.setUpdateAt(endTime);
|
|
|
+ break;
|
|
|
+ case "Humidity":
|
|
|
+ records.setType("Humidity");
|
|
|
+ records.setValue(this.getRandValue(30,80));
|
|
|
+ records.setCreateAt(endTime);
|
|
|
+ records.setUpdateAt(endTime);
|
|
|
+ break;
|
|
|
+ case "CO2":
|
|
|
+ records.setType("CO2");
|
|
|
+ records.setValue(this.getRandValue(400,1000));
|
|
|
+ records.setCreateAt(endTime);
|
|
|
+ records.setUpdateAt(endTime);
|
|
|
+ break;
|
|
|
+ case "Light":
|
|
|
+ records.setType("Light");
|
|
|
+ records.setValue(this.getRandValue(300,3500));
|
|
|
+ records.setCreateAt(endTime);
|
|
|
+ records.setUpdateAt(endTime);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ recordsList.add(records);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ boolean a=recordsService.saveOrUpdateBatch(recordsList);
|
|
|
+ logger.info("批量导入结果:"+a);
|
|
|
+ }
|
|
|
+
|
|
|
+ public double getRandValue(int min, int max) {
|
|
|
+ Random rand = new Random();
|
|
|
+ int count = rand.nextInt(max - min + 1) + min;
|
|
|
+ return count;
|
|
|
+ }
|
|
|
+}
|