From f99abcd94998485854ca18f149fecc33c24bf5de Mon Sep 17 00:00:00 2001
From: lizijie <lzjiiie@163.com>
Date: Fri, 20 Nov 2020 14:10:25 +0800
Subject: [PATCH] 校准值插入redis
---
src/main/java/com/moral/task/HistoryTableInsertTask.java | 143 +++++++++++++++++++++++++----------------------
1 files changed, 77 insertions(+), 66 deletions(-)
diff --git a/src/main/java/com/moral/task/HistoryTableInsertTask.java b/src/main/java/com/moral/task/HistoryTableInsertTask.java
index c77ba4c..075b348 100644
--- a/src/main/java/com/moral/task/HistoryTableInsertTask.java
+++ b/src/main/java/com/moral/task/HistoryTableInsertTask.java
@@ -1,22 +1,12 @@
package com.moral.task;
import java.math.BigDecimal;
+import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
import javax.annotation.Resource;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.stereotype.Component;
-import org.springframework.util.CollectionUtils;
-import org.springframework.util.ObjectUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
@@ -29,10 +19,17 @@
import com.moral.service.HistoryService;
import com.moral.service.OrganizationService;
import com.moral.service.SensorService;
+import com.moral.util.DateUtil;
import com.moral.util.WeatherUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.ObjectUtils;
@Component
public class HistoryTableInsertTask {
@@ -65,31 +62,31 @@
@XxlJob("historyMinutely")
public ReturnT insertHistoryMinutelyTable(String params) {
- LocalDateTime time = LocalDateTime.now();
- int year = time.getYear();
- int month = time.getMonthValue();
- int day = time.getDayOfMonth();
- int hour = time.getHour();
- int minute = time.getMinute();
- if (day == 1) {
- if (hour == 0 && minute == 0) {
- if (month == 1) {
- month = 12;
- year = year - 1;
- } else {
- month = month - 1;
- }
- }
- }
- String monthStr = month < 10 ? ("0" + month) : month + "";
- String yearAndMonth = year + monthStr;
- LocalDateTime endTime = time.truncatedTo(ChronoUnit.MINUTES);
- LocalDateTime startTime = endTime.minusMinutes(1);
- List<String> sensorKeys = sensorService.getSensorKeys();
+ //������������������
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
+ Date endTimeDate = new Date();
+ Date startTimeDate = DateUtil.rollMinute(endTimeDate, -1);
+ StringBuilder endTime = new StringBuilder(sdf.format(endTimeDate));
+ StringBuilder startTime = new StringBuilder(sdf.format(startTimeDate));
+ String queryYearAndMonthDay = DateUtil.getYear(startTimeDate) + DateUtil.getMonth(startTimeDate) + DateUtil.getDay(startTimeDate);
+
+ //������������
+ /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ Date endTimeDate = sdf.parse("2020-11-01 00:00:20");
+ Date startTimeDate = DateUtil.rollMinute(endTimeDate, -1);
+ StringBuilder endTime = new StringBuilder(sdf.format(endTimeDate));
+ StringBuilder startTime = new StringBuilder(sdf.format(startTimeDate));
+ endTime.replace(17, 19, "00");
+ startTime.replace(17, 19, "00");
+ String queryYearAndMonthDay = DateUtil.getYear(startTimeDate) + DateUtil.getMonth(startTimeDate) + DateUtil.getDay(startTimeDate);*/
+
Map<String, Object> devices = new HashMap<>();
+ List<String> sensorKeys = sensorService.getSensorKeys();
devices.put("sensorKeys", sensorKeys);
- devices.put("start", startTime);
- devices.put("end", endTime);
+ devices.put("start", startTime.toString());
+ devices.put("end", endTime.toString());
+ devices.put("yearAndMonthDay", queryYearAndMonthDay);
+
try {
List<Map<String, Object>> minutelyData = deviceService.getSensorData(devices);
XxlJobLogger.log("historyMinutelyData:" + minutelyData.size());
@@ -99,7 +96,7 @@
Map<String, Object> minutelyDataMap = new LinkedHashMap<>();
JSONObject jo = new JSONObject(true);
minutelyDataMap.put("mac", deviceData.get("mac"));
- minutelyDataMap.put("time", startTime);
+ minutelyDataMap.put("time", endTime.toString());
JSONArray jsonArray = new JSONArray();
for (String key : deviceData.keySet()) {
if (!key.equals("mac") && !key.startsWith("M")) {
@@ -116,7 +113,8 @@
}
}
if (!CollectionUtils.isEmpty(minutelyDataList)) {
- historyMinutelyService.insertHistoryMinutely(minutelyDataList, yearAndMonth);
+ String insertYearAndMonth = DateUtil.getYear(endTimeDate) + DateUtil.getMonth(endTimeDate);
+ historyMinutelyService.insertHistoryMinutely(minutelyDataList, insertYearAndMonth);
ReturnT returnT = new ReturnT(200, "���������������������");
return returnT;
}
@@ -124,6 +122,8 @@
XxlJobLogger.log("historyMinutelyException:" + e.getMessage());
logger.error(e.getMessage());
e.printStackTrace();
+ ReturnT returnT = new ReturnT(500, e.getMessage());
+ return returnT;
}
ReturnT returnT = new ReturnT(500, "���������������������");
return returnT;
@@ -131,33 +131,33 @@
@XxlJob("historyHourly")
public ReturnT insertHistoryHourlyTable(String params) {
- LocalDateTime time = LocalDateTime.now();
- int year = time.getYear();
- int month = time.getMonthValue();
- int day = time.getDayOfMonth();
- int hour = time.getHour();
- if (day == 1) {
- if (hour == 0) {
- if (month == 1) {
- month = 12;
- year = year - 1;
- } else {
- month = month - 1;
- }
- }
- }
- String monthStr = month < 10 ? ("0" + month) : month + "";
- String yearAndMonth = year + monthStr;
- LocalDateTime endTime = time.truncatedTo(ChronoUnit.HOURS);
- LocalDateTime startTime = endTime.minusHours(1);
+ //������������������
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
+ Date endTimeDate = new Date();
+ Date startTimeDate = DateUtil.rollHour(endTimeDate, -1);
+ StringBuilder endTime = new StringBuilder(sdf.format(endTimeDate));
+ StringBuilder startTime = new StringBuilder(sdf.format(startTimeDate));
+ String yearAndMonth = DateUtil.getYear(startTimeDate) + DateUtil.getMonth(startTimeDate);
+
+ //������������������������������������������������
+ /* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ Date endTimeDate = sdf.parse("2020-11-01 00:00:22");
+ Date startTimeDate = DateUtil.rollHour(endTimeDate, -1);
+ StringBuilder endTime = new StringBuilder(sdf.format(endTimeDate));
+ StringBuilder startTime = new StringBuilder(sdf.format(startTimeDate));
+ endTime.replace(14, 19, "00:00");
+ startTime.replace(14, 19, "00:00");
+ String yearAndMonth = DateUtil.getYear(startTimeDate) + DateUtil.getMonth(startTimeDate);*/
+
List<String> sensorKeys = sensorService.getSensorKeys();
List<String> macs = deviceService.getMacs();
Map<String, Object> devices = new HashMap<>();
devices.put("sensorKeys", sensorKeys);
- devices.put("start", startTime);
- devices.put("end", endTime);
+ devices.put("start", startTime.toString());
+ devices.put("end", endTime.toString());
devices.put("macs", macs);
devices.put("yearAndMonth", yearAndMonth);
+
try {
List<Map<String, Object>> hourlyData = historyMinutelyService.getMinutelySensorData(devices);
XxlJobLogger.log("historyHourlyData:" + hourlyData.size());
@@ -167,7 +167,7 @@
Map<String, Object> hourlyDataMap = new LinkedHashMap<>();
JSONObject jo = new JSONObject(true);
hourlyDataMap.put("mac", deviceData.get("mac"));
- hourlyDataMap.put("time", startTime);
+ hourlyDataMap.put("time", endTime.toString());
JSONArray jsonArray = new JSONArray();
for (String key : deviceData.keySet()) {
if (!key.equals("mac") && !key.startsWith("M")) {
@@ -332,10 +332,17 @@
public ReturnT insertHistorySpecialTable(String params) {
Map macMap = JSON.parseObject(params);
List<String> macList = (List<String>) macMap.get("mac");
- LocalDateTime value = LocalDateTime.now();
+ SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ Calendar calendar=Calendar.getInstance();
+ Date date=calendar.getTime();
+ String s=format.format(date);
+ calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
+ Date date1=calendar.getTime();
+ String s1=format.format(date1);
Map<String, Object> devices = new HashMap<>();
devices.put("macList", macList);
- devices.put("time", value);
+ devices.put("startTime", s1);
+ devices.put("endTime", s);
try {
int count = historyService.insertHistorySpecialTable(devices);
XxlJobLogger.log("insertHistorySpecialTable:" + count);
@@ -356,12 +363,12 @@
public ReturnT insertHistoryO3Table(String params) {
Map macMap = JSON.parseObject(params);
List<String> macList = (List<String>) macMap.get("mac");
- String temperatureRatio = macMap.get("temperatureRatio").toString();
String illuminationRatio = macMap.get("illuminationRatio").toString();
String pressureRatio1 = macMap.get("pressureRatio1").toString();
String pressureRatio2 = macMap.get("pressureRatio2").toString();
- String humidityRatio = macMap.get("humidityRatio").toString();
String windSpeedRatio = macMap.get("windSpeedRatio").toString();
+ BigDecimal temperatureRatio;
+ BigDecimal humidityRatio;
LocalDateTime time = LocalDateTime.now();
LocalDateTime value = time.truncatedTo(ChronoUnit.HOURS);
LocalDateTime illuminationTime = value.minusHours(1);
@@ -421,10 +428,14 @@
} else {
illumination = new BigDecimal("0");
}
- BigDecimal bignum1 = temperature.multiply(new BigDecimal(temperatureRatio));
- BigDecimal bignum2 = illumination.divide(new BigDecimal(illuminationRatio));
+ temperatureRatio = new BigDecimal("8").add(new BigDecimal("32").divide(temperature.subtract(new BigDecimal("12")), 4, BigDecimal.ROUND_HALF_UP));
+ BigDecimal bignum1 = temperature.multiply(temperatureRatio);
+ BigDecimal bignum2 = illumination.divide(new BigDecimal(illuminationRatio), 4, BigDecimal.ROUND_HALF_UP);
BigDecimal bignum3 = new BigDecimal(pressureRatio1).divide(pressure.subtract(new BigDecimal(pressureRatio2)), 4, BigDecimal.ROUND_HALF_UP);
- BigDecimal bignum4 = humidity.multiply(new BigDecimal(humidityRatio));
+ BigDecimal index = humidity.subtract(new BigDecimal("100")).divide(new BigDecimal("50"), 4, BigDecimal.ROUND_HALF_UP);
+ double indexDouble = Math.pow(0.8, index.doubleValue());
+ humidityRatio = new BigDecimal("1").add(BigDecimal.valueOf(indexDouble));
+ BigDecimal bignum4 = humidity.multiply(humidityRatio);
BigDecimal bignum5 = windSpeed.multiply(new BigDecimal(windSpeedRatio));
BigDecimal result = bignum1.add(bignum2).add(bignum3).subtract(bignum4).subtract(bignum5).setScale(4, BigDecimal.ROUND_HALF_UP);
jo.put("e15", result);
--
Gitblit v1.8.0