Преглед изворни кода

异步抓取电流功率后同步设备的状态

lingpeng.li пре 6 месеци
родитељ
комит
7811b17867

+ 50 - 4
jeecg-module-gather/src/main/java/org/jeecg/modules/watch/MysqlWatch.java

@@ -1,6 +1,8 @@
 package org.jeecg.modules.watch;
 
+import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import lombok.extern.slf4j.Slf4j;
 import org.jeecg.modules.device.entity.DeviceInformation;
 import org.jeecg.modules.device.mapper.DeviceInformationMapper;
@@ -10,6 +12,8 @@ import org.jeecg.modules.devicePoint.mapper.DevicePointMapper;
 import org.jeecg.modules.gatherData.entity.FpgGatherData;
 import org.jeecg.modules.gatherData.entity.FpgGatherDataMongo;
 import org.jeecg.modules.gatherData.mapper.FpgGatherDataMapper;
+import org.jeecg.modules.peaksAndValleysTimeConfig.entity.SystemVariable;
+import org.jeecg.modules.peaksAndValleysTimeConfig.service.ISystemVariableService;
 import org.jeecg.modules.utils.ConnectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.mongodb.core.MongoTemplate;
@@ -46,11 +50,14 @@ public class MysqlWatch {
     @Autowired
     FpgGatherDataMapper fpgGatherDataMapper;
 
+    @Autowired
+    ISystemVariableService systemVariableService;
+
     @Autowired
     MongoTemplate mongoTemplate;
 
 
-    @Scheduled(cron = "0 0/1 * * * ?")
+    @Scheduled(fixedDelay = 1000)
     public void getTsData() {
         Connection connection = null;
         Statement statement = null;
@@ -83,11 +90,11 @@ public class MysqlWatch {
                 Date latestCreateTime1 = null;
                 if (latestData != null) {
                     latestCreateTime1 = latestData.getCreateTime();
-                    System.out.println("最新的 createTime: " + latestCreateTime1);
+                    log.info("最新的 createTime: " + latestCreateTime1);
                 } else {
                     // 如果没有找到记录,给一个默认时间
                     latestCreateTime1 = new Date(0); // 默认时间设置为 1970-01-01 00:00:00
-                    System.out.println("没有找到任何记录,使用默认时间: " + latestCreateTime1);
+                    log.info("没有找到任何记录,使用默认时间: " + latestCreateTime1);
                 }
 
 
@@ -97,7 +104,7 @@ public class MysqlWatch {
                     // 假设 latestCreateTime 是从 FpgGatherData 中获取的最新 createTime
                     Date latestCreateTime = latestCreateTime1;
 
-                   // 格式化 latestCreateTime 为 "yyyy-MM-dd HH:mm:ss" 格式的字符串
+                    // 格式化 latestCreateTime 为 "yyyy-MM-dd HH:mm:ss" 格式的字符串
                     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                     String formattedTime = dateFormat.format(latestCreateTime);
                     String sql = "SELECT * FROM ts_value WHERE tagname='" + point.getPointCode() +
@@ -161,6 +168,36 @@ public class MysqlWatch {
                                 .eq(FpgGatherData::getDeviceInformationId, data.getDeviceInformationId())
                                 .eq(FpgGatherData::getCreateTime, data.getCreateTime())
                 );
+                // 根据采集到的数据 创建时间,跟峰平谷配置的时间,判断属于哪个时段(尖、峰、平、谷)
+                QueryWrapper<SystemVariable> queryWrapper = new QueryWrapper<>();
+                queryWrapper.eq("variable_address", "sys_run_current_limit");
+                queryWrapper.eq("status", 0);
+                SystemVariable systemVariable = systemVariableService.getOne(queryWrapper);
+                if (systemVariable == null) {
+                    log.info("{}{}", "fpg_close系统变量未配置正常状态的运行电流限制!", JSON.toJSON(data));
+                    return;
+                }
+
+                DeviceInformation deviceInformation = deviceInformationMapper.selectOne(
+                        new LambdaQueryWrapper<DeviceInformation>()
+                                .eq(DeviceInformation::getId, data.getDeviceInformationId())
+                );
+
+                // 比较大小
+                int result = compareStringWithBigDecimal(systemVariable.getDefaultValue(), data.getRunCurrent());
+
+                if (result > 0) {
+                    deviceInformation.setStatus("1");
+
+                } else if (result < 0) {
+
+                    deviceInformation.setStatus("0");
+
+                } else {
+
+                }
+
+                deviceInformationMapper.updateById(deviceInformation);
 
                 // 如果不存在则插入
                 if (existingData == null) {
@@ -195,4 +232,13 @@ public class MysqlWatch {
         }
     }
 
+
+    public static int compareStringWithBigDecimal(String str, BigDecimal bd) {
+        // 将 String 转换为 BigDecimal
+        BigDecimal strAsBigDecimal = new BigDecimal(str);
+
+        // 使用 BigDecimal 的 compareTo 方法比较
+        return strAsBigDecimal.compareTo(bd);
+    }
+
 }