From eccde23ea401394aed432865bfbbbcb89539ba20 Mon Sep 17 00:00:00 2001
From: lizijie <lzjiiie@163.com>
Date: Wed, 18 Aug 2021 15:33:45 +0800
Subject: [PATCH] 特殊设备历史表数据查询、删除,校准接口判断特殊设备表

---
 screen-manage/src/main/java/com/moral/api/util/AdjustDataUtils.java |   68 ++++++++++++++--------------------
 1 files changed, 28 insertions(+), 40 deletions(-)

diff --git a/screen-manage/src/main/java/com/moral/api/util/AdjustDataUtils.java b/screen-manage/src/main/java/com/moral/api/util/AdjustDataUtils.java
index 36fdf60..86f21fc 100644
--- a/screen-manage/src/main/java/com/moral/api/util/AdjustDataUtils.java
+++ b/screen-manage/src/main/java/com/moral/api/util/AdjustDataUtils.java
@@ -1,10 +1,9 @@
 package com.moral.api.util;
 
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
 import org.springframework.util.ObjectUtils;
+import org.springframework.util.StringUtils;
 
 import java.util.Date;
 import java.util.HashMap;
@@ -15,76 +14,66 @@
 import com.googlecode.aviator.AviatorEvaluator;
 import com.googlecode.aviator.Expression;
 import com.moral.api.entity.DeviceAdjustValue;
-import com.moral.api.service.DeviceService;
-import com.moral.constant.RedisConstants;
+import com.moral.constant.Constants;
 import com.moral.util.DateUtils;
 
 @Slf4j
 @Component
 public class AdjustDataUtils {
-
-    @Autowired
-    private DeviceService deviceService;
-
-    @Autowired
-    private RedisTemplate redisTemplate;
-
-    public Map<String, Object> adjust(Map<String, Object> deviceData) {
+    /**
+     * @param deviceData    ������������
+     * @param adjustFormula ������������
+     * @param aqiMap        ���������������������������������aqi������
+     * @return Map<String, Object> ���������������
+     */
+    public Map<String, Object> adjust(Map<String, Object> deviceData, Map<String, Object> adjustFormula, Map<String, Object> aqiMap) {
         try {
-            Object dataTime = deviceData.get("DataTime");
-            String mac = deviceData.get("mac").toString();
-            //���������������������������
-            long time = Math.round(new Double((String) dataTime) / 1000) * 1000L;
-            long finalTime = DateUtils.dataToTimeStampTime(new Date(time), DateUtils.HH_mm_ss_EN).getTime();
-            //������������
-            Map<String, Object> deviceInfo = deviceService.getDeviceByMac(mac);
-            Map<String, Object> monitorPoint = (Map<String, Object>) deviceInfo.get("monitorPoint");
-            Object areaCode = monitorPoint.get("areaCode");
-            Object cityCode = monitorPoint.get("cityCode");
+            Date time = DateUtils.getDate((String) deviceData.remove("DataTime"), DateUtils.yyyyMMddHHmmss_EN);
+            long finalTime = DateUtils.dataToTimeStampTime(time, DateUtils.HH_mm_ss_EN).getTime();
             for (String key : deviceData.keySet()) {
-                if (!key.equals("mac") && !key.equals("time") && !key.equals("DataTime") && !key.equals("ver") && !key.contains("Flag")) {
+                if (!key.contains("Flag")) {
                     //���������
                     Object measuredValue = deviceData.get(key);
-                    List<DeviceAdjustValue> adjustValues = (List<DeviceAdjustValue>) redisTemplate.opsForHash().get(RedisConstants.ADJUST + "_" + mac, key);
-                    if (ObjectUtils.isEmpty(adjustValues)) {
+                    //������������������������
+                    List<DeviceAdjustValue> sensorFormulas = (List<DeviceAdjustValue>) adjustFormula.get(key);
+                    if (ObjectUtils.isEmpty(sensorFormulas)) {
                         deviceData.put(key, measuredValue);
                         continue;
                     }
 
                     //���������������������������������
-                    DeviceAdjustValue deviceAdjustValue = adjustValues.stream()
+                    DeviceAdjustValue deviceAdjustValue = sensorFormulas.stream()
                             .filter(o -> o.getStartTime().getTime() <= finalTime && o.getEndTime().getTime() > finalTime)
                             .findFirst().get();
-                    String adjustValue = deviceAdjustValue.getValue();
-                    if (ObjectUtils.isEmpty(adjustValue)) {
+                    String formula = deviceAdjustValue.getValue();
+                    if (StringUtils.isEmpty(formula)) {
                         deviceData.put(key, measuredValue);
                         continue;
                     }
 
-                    Expression expression = AviatorEvaluator.compile(adjustValue);
+                    Expression expression = AviatorEvaluator.compile(formula);
                     Map<String, Object> env = new HashMap<>();
-                    if (adjustValue.contains("aqi")) {
-                        Object aqiValue = redisTemplate.opsForHash().get("aqi_" + areaCode, key);
-                        if (ObjectUtils.isEmpty(aqiValue)) {
-                            aqiValue = redisTemplate.opsForHash().get("aqi_" + cityCode, key);
+                    if (formula.contains("aqi")) {
+                        Object aqiValue = null;
+                        if (aqiMap != null) {
+                            aqiValue = aqiMap.get(key);
                         }
                         env.put("aqi", ObjectUtils.isEmpty(aqiValue) ? 0F : Float.parseFloat((String) aqiValue));
                     }
-                    if (adjustValue.contains("vocs")) {
-                        Object vocsValue = ObjectUtils.isEmpty(deviceData.get("a99054")) ? 0F : deviceData.get("a99054");
+                    if (formula.contains("vocs")) {
+                        Object vocsValue = ObjectUtils.isEmpty(deviceData.get(Constants.SENSOR_CODE_VOCS)) ? 0F : deviceData.get(Constants.SENSOR_CODE_VOCS);
                         env.put("vocs", vocsValue);
                     }
-                    if (adjustValue.contains("cel")) {
+                    if (formula.contains("cel")) {
                         env.put("cel", Float.parseFloat((String) measuredValue));
                     }
                     //������
                     measuredValue = expression.execute(env);
                     //������������
-                    if (Float.parseFloat(measuredValue.toString()) < 0 && !"a01001".equals(measuredValue)) {
+                    if (!Constants.SENSOR_CODE_TEMP.equals(measuredValue) && Float.parseFloat(measuredValue.toString()) < 0) {
                         measuredValue = 0F;
                     }
-                    deviceData.put(key, Double.parseDouble(String.format("%.3f", measuredValue)));
-
+                    deviceData.put(key, Double.parseDouble(String.format("%.4f", measuredValue)));
                 }
             }
         } catch (Exception e) {
@@ -93,5 +82,4 @@
         }
         return deviceData;
     }
-
 }

--
Gitblit v1.8.0