From b298be7fd92ddde54bb0cd208e22b5ecd28383ec Mon Sep 17 00:00:00 2001 From: mh Date: Tue, 21 May 2024 16:09:04 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E7=BE=8E=E7=9A=84=E6=B3=B5=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E8=AE=BE=E7=BD=AE=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- user-service/pom.xml | 17 +++++++++++++++ .../java/com/mh/user/job/DealDataJob.java | 6 +++--- .../impl/HistoryDataPreServiceImpl.java | 21 ++++++++++++------- .../mh/user/strategy/HeatPumpStrategy.java | 1 + .../test/java/com/mh/user/DealDataTest.java | 17 +++++++++++++++ 5 files changed, 52 insertions(+), 10 deletions(-) diff --git a/user-service/pom.xml b/user-service/pom.xml index 9b53258..1903c1f 100644 --- a/user-service/pom.xml +++ b/user-service/pom.xml @@ -35,6 +35,23 @@ org.springframework.boot spring-boot-starter-quartz + + + + + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + org.mybatis.spring.boot diff --git a/user-service/src/main/java/com/mh/user/job/DealDataJob.java b/user-service/src/main/java/com/mh/user/job/DealDataJob.java index bfb8632..0997ddd 100644 --- a/user-service/src/main/java/com/mh/user/job/DealDataJob.java +++ b/user-service/src/main/java/com/mh/user/job/DealDataJob.java @@ -50,7 +50,7 @@ public class DealDataJob { /** * 定时处理汇总数据:每15分钟处理一次,十分钟(0 0/10 * * * ?) */ -// @Scheduled(cron = "0 0/15 * * * ?") + @Scheduled(cron = "0 0/15 * * * ?") public void ProEnergy() { try { SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:00:00"); @@ -73,7 +73,7 @@ public class DealDataJob { /** * 采集 */ -// @Scheduled(cron = "35 0/2 * * * ?") + @Scheduled(cron = "35 0/2 * * * ?") // @Scheduled(cron = "0/10 * * * * ?") 0 0/5 * * * ? // @Scheduled(cron = "0 0/5 * * * ?") //5分钟 public void collect() { @@ -148,7 +148,7 @@ public class DealDataJob { /** * 定时处理数据:每十五分钟处理一次 */ -// @Scheduled(cron = "0 0/15 * * * ?") + @Scheduled(cron = "0 0/15 * * * ?") public void dealData() { try { StopWatch stopWatch = new StopWatch(); diff --git a/user-service/src/main/java/com/mh/user/service/impl/HistoryDataPreServiceImpl.java b/user-service/src/main/java/com/mh/user/service/impl/HistoryDataPreServiceImpl.java index c2327fe..4072c97 100644 --- a/user-service/src/main/java/com/mh/user/service/impl/HistoryDataPreServiceImpl.java +++ b/user-service/src/main/java/com/mh/user/service/impl/HistoryDataPreServiceImpl.java @@ -17,6 +17,7 @@ import com.mh.user.dto.EnergyPreEchartDataDTO; import com.mh.user.dto.EnergyPreTopDataDTO; import com.mh.user.entity.HistoryDataPre; import com.mh.user.entity.SysParamEntity; +import com.mh.user.job.GetWeatherInfoJob; import com.mh.user.mapper.HistoryDataPreMapper; import com.mh.user.service.HistoryDataPreService; import com.mh.user.service.SysParamService; @@ -27,6 +28,7 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.math.BigDecimal; +import java.math.RoundingMode; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.ArrayList; @@ -54,6 +56,10 @@ public class HistoryDataPreServiceImpl implements HistoryDataPreService { @Resource private SysParamService sysParamService; + @Resource + private GetWeatherInfoJob getWeatherInfoJob; + + public static String[] convert(HistoryDataPre dataPre) { // 假设HistoryDataPre有字段如field1, field2, field3等,根据需要进行转换 return new String[] { @@ -98,15 +104,16 @@ public class HistoryDataPreServiceImpl implements HistoryDataPreService { @Override public void startPredictData(String buildingId, String curDate) throws Exception { // 判断是否存在天气温度数据以及现在的用水量用电量等 - int isPre = historyDataPreMapper.selectIsPre(buildingId, curDate); - if (isPre > 0) { - return; - } +// int isPre = historyDataPreMapper.selectIsPre(buildingId, curDate); +// if (isPre > 0) { +// return; +// } // 获取当前天气数据 SysParamEntity sysParam = sysParamService.selectSysParam(); Object weather = caffeineCache.getIfPresent(sysParam.getProArea()); if (weather == null) { - return; + getWeatherInfoJob.getWeatherInfo(); + weather = caffeineCache.getIfPresent(sysParam.getProArea()); } String weatherStr = (String) weather; JSONObject jsonObject = JSON.parseObject(weatherStr); @@ -154,7 +161,7 @@ public class HistoryDataPreServiceImpl implements HistoryDataPreService { for (int i = 0; i < result.getMatrixRowCount(); i++) { String[] record = new String[result.getMatrixColCount()]; for (int j = 0; j < result.getMatrixColCount(); j++) { - record[j] = result.getValOfIdx(i, j)+""; + record[j] = String.valueOf(result.getValOfIdx(i, j)); } // 拼接预测值 preHistoryData.setWaterValuePre(evaluateAndReturnBigDecimal(record[0])); @@ -178,7 +185,7 @@ public class HistoryDataPreServiceImpl implements HistoryDataPreService { public static BigDecimal evaluateAndReturnBigDecimal(String recordValue) { BigDecimal value = new BigDecimal(recordValue); if (value.compareTo(BigDecimal.ZERO) >= 0) { - return value; + return value.setScale(2, RoundingMode.HALF_UP); } else { return BigDecimal.ZERO; } diff --git a/user-service/src/main/java/com/mh/user/strategy/HeatPumpStrategy.java b/user-service/src/main/java/com/mh/user/strategy/HeatPumpStrategy.java index 5167737..5998aeb 100644 --- a/user-service/src/main/java/com/mh/user/strategy/HeatPumpStrategy.java +++ b/user-service/src/main/java/com/mh/user/strategy/HeatPumpStrategy.java @@ -243,6 +243,7 @@ public class HeatPumpStrategy implements DeviceStrategy { String closetTime = checkStr.substring(16, 18) + checkStr.substring(20, 22); sValue = startTime + closetTime; } + rtData = sValue; } else if (checkStr.substring(2, 4).equalsIgnoreCase("10")) { log.info("时间: " + dateStr + ",热泵ID: " + addr + ",操作成功! "); return Constant.SUCCESS; diff --git a/user-service/src/test/java/com/mh/user/DealDataTest.java b/user-service/src/test/java/com/mh/user/DealDataTest.java index cfef275..dd25c43 100644 --- a/user-service/src/test/java/com/mh/user/DealDataTest.java +++ b/user-service/src/test/java/com/mh/user/DealDataTest.java @@ -3,6 +3,7 @@ package com.mh.user; import com.mh.user.entity.EnergyEntity; import com.mh.user.mapper.EnergyMapper; import com.mh.user.mapper.DealDataMapper; +import com.mh.user.service.HistoryDataPreService; import com.mh.user.service.NowDataService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -41,4 +42,20 @@ public class DealDataTest extends UserServiceApplicationTests { List list = energyMapper.queryEnergyMonth("2-3", "2023-03", "2023-03", 1, 10); System.out.println(list); } + + @Autowired + private HistoryDataPreService historyDataPreService; + + @Test + public void testPre() { + try { + // 训练数据 + historyDataPreService.startTrainData("21"); + // 预测数据 + historyDataPreService.startPredictData("21", "2024-05-15"); + } catch (Exception e) { + e.printStackTrace(); + System.out.println(e.getMessage()); + } + } }