Переглянути джерело

1.修复mq消费队列报表问题
2.修复泵站状态报表数据问题

lihui007 3 роки тому
батько
коміт
2846ad7f25

+ 56 - 23
sms_water/src/main/java/com/huaxu/rabbitmq/ReceiveClearData.java

@@ -21,9 +21,7 @@ import javax.annotation.Resource;
 import java.math.BigDecimal;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.List;
+import java.util.*;
 
 
 /**
@@ -52,6 +50,8 @@ public class ReceiveClearData {
     @Autowired
     private MonitorDataService monitorDataService;
 
+    private static final String ORIGINAL_DATA_STRING = "originalData";
+
     /**
      * 先注入,然后再通过SPEL取值
      * @return
@@ -76,41 +76,46 @@ public class ReceiveClearData {
 
     }
 
+    /***
+     * 		manufacturer 厂家,mode型号,设备编号unitIdentifier,type设备类型
+     *
+     * @author lihui
+     * @date 10:07 2021/5/28
+     * @param receivedData :
+     * @return void
+     **/
     public void receivedDataHandle(byte[] receivedData){
+        JSONObject originalData = new JSONObject();
         JSONObject jsonObject = JSONObject.parseObject(new String(receivedData));
         String eventTime      = jsonObject.getString("eventTime");
         String deviceCode     = jsonObject.getString("unitIdentifier");
-        log.info("rabbitMq接收消息处理,code:{},上报时间:{}",deviceCode, eventTime);
-        // 对象有时间、有设备编码、有数据则解析,任何一个位空直接退出
-        if (!jsonObject.containsKey("eventTime") || !jsonObject.containsKey("unitIdentifier") ||
-                !jsonObject.containsKey("parsedData")) {
+        Date receiveDateTime  = parseDate(eventTime);
+        log.info("rabbitMq接收消息处理,code:{},上报时间:{}", deviceCode, eventTime);
+        if (!checkParameter(jsonObject, deviceCode, receiveDateTime)){
             return;
         }
-        Date receiveDateTime = DatesUtil.parseDate(eventTime, "yyyy-MM-dd HH:mm:ss");
-        if (receiveDateTime == null) {
-            log.error("报警信息查询时间转换错误,原数据:eventTime:{}", eventTime);
-            return;
-        }
-        JSONObject receiveData = JSONObject.parseObject(jsonObject .getString("parsedData"));
-        if (StringUtils.isBlank(deviceCode)) {
-            log.error("deviceCode为空退出。");
-            return;
-        }
-
-        MonitorDataEntity monitorDataEntity = monitorDataService.getDeviceMonitorInfoByDeviceCode(deviceCode);
         // 查询不到设备或者设备属性为空
+        MonitorDataEntity monitorDataEntity = monitorDataService.getDeviceMonitorInfoByDeviceCode(deviceCode);
         if (monitorDataEntity == null || CollectionUtils.isEmpty(monitorDataEntity.getDataValues())){
+            log.error("rabbitMq接收消息处理,code:{},查询不到设备或者设备属性为空,退出",deviceCode);
             return;
         }
+        // 英文版、中文版
+        JSONObject receiveData  = JSONObject.parseObject(jsonObject .getString("parsedData"));
+        if (jsonObject.containsKey(ORIGINAL_DATA_STRING)){
+            originalData = JSONObject.parseObject(jsonObject .getString(ORIGINAL_DATA_STRING));
+        }
         List<MonitorDataValueEntity> monitorDataValueEntities = monitorDataEntity.getDataValues();
         Integer number = 0;
         for (MonitorDataValueEntity monitorDataValueEntity : monitorDataValueEntities) {
-            if (!receiveData.containsKey(monitorDataValueEntity.getIdentifier())) {
-                continue;
-            }
             BigDecimal bigDecimal = null;
             try {
-                bigDecimal = new BigDecimal(receiveData.getDouble(monitorDataValueEntity.getIdentifier()));
+                Double doubleValue = receiveData.getDouble(monitorDataValueEntity.getIdentifier());
+                doubleValue = doubleValue == null ? originalData.getDouble(monitorDataValueEntity.getIdentifier()) : doubleValue;
+                if (doubleValue == null) {
+                    continue;
+                }
+                bigDecimal = new BigDecimal(doubleValue);
             } catch (NumberFormatException e) {
                 log.error("double 转换 error ->", e);
                 continue;
@@ -127,6 +132,7 @@ public class ReceiveClearData {
         }
         // 没有匹配到属性,视为垃圾数据忽略
         if (number == 0) {
+            log.error("rabbitMq接收消息处理,code:{},上报时间:{},没有匹配到属性,视为垃圾数据忽略,退出",deviceCode, eventTime);
             return;
         }
         // 保存到缓存和mongodb数据库
@@ -140,6 +146,32 @@ public class ReceiveClearData {
     }
 
 
+    private Date parseDate(String eventTime){
+        return StringUtils.isEmpty(eventTime) ? null : DatesUtil.parseDate(eventTime, "yyyy-MM-dd HH:mm:ss");
+    }
+
+    /***
+     * 对象有时间、有设备编码、有数据则解析,任何一个位空直接退出
+     * @author lihui
+     * @date 10:53 2021/5/28
+     * @param jsonObject :
+     * @param deviceCode :
+     * @return boolean
+     **/
+    private boolean checkParameter(JSONObject jsonObject, String deviceCode, Date receiveDateTime){
+        if (!jsonObject.containsKey("eventTime") || !jsonObject.containsKey("unitIdentifier") ||
+                !jsonObject.containsKey("parsedData")) {
+            log.error("rabbitMq接收消息处理,code:{},上报时间:{},退出",deviceCode);
+            return false;
+        }
+        if (receiveDateTime == null) {
+            log.error("rabbitMq接收消息处理,code:{},报警信息查询时间转换错误,退出", deviceCode);
+            return false;
+        }
+        return true;
+    }
+
+
     private void saveToCacheAndMongodb(MonitorDataEntity monitorDataEntity, Date receiveDateTime){
         monitorDataEntity.setCollectDate(receiveDateTime);
         Calendar cal = Calendar.getInstance();
@@ -150,4 +182,5 @@ public class ReceiveClearData {
         monitorDataEntity.setHour(cal.get(Calendar.HOUR_OF_DAY));
         monitorDataService.save(monitorDataEntity);
     }
+
 }

+ 31 - 8
sms_water/src/main/java/com/huaxu/rabbitmq/ReportWaterPumpStateHandler.java

@@ -116,18 +116,40 @@ public class ReportWaterPumpStateHandler {
                                   int stateValue, Date receiveDateTime){
         ReportWaterPumpStateEntity stateEntity = reportWaterPumpStateMapper.findReportWaterPumpState(md5Query);
         if (stateEntity == null) {
-            insertList.add(getReportWaterPumpStateEntity(monitorDataEntity, valueEntity, receiveDateTime, stateValue));
+            insertList.add(getReportWaterPumpStateEntity(monitorDataEntity, valueEntity, receiveDateTime, null, stateValue));
             return;
         }
-        Date beginTime  = null;
+        int stateEntityValue =  stateEntity.getState().intValue();
+        Date beginTime = stateEntity.getStateBeginTime();
+        Date endTime    = null;
         // 计算出时间差 等于0表示同一天的数据,大于0表示跨天
         int day  = timeDifference(stateEntity.getStateBeginTime(), receiveDateTime);
-        boolean theSameSate = stateEntity.getState().intValue() == stateValue;
-        // 跨天需要新增一条数据,如果是跨一天的数据起始时间为 receiveDateTime + 00:00:00, 跨多天就按当前上报的时间
+        boolean theSameSate = stateEntityValue == stateValue;
+        /**
+         * 跨天需要新增数据
+         * 跨天数据计算规则:
+         * 1.开始时间取最后一条的起始时间,循环一次加一天
+         * 2.如果不是最后一天,末尾时间就取(上面1.的)起始时间追加23:59:59分,
+         *   如果是最后一天的数据,需要判断当前状态和最新状态是否一直,如果不一致需要添加2条数据
+         */
         if (day != 0) {
-            beginTime  = day == 1 ? parseDate(receiveDateTime, "00:00:00") : receiveDateTime;
             reportWaterPumpStateMapper.updateReportWaterPumpState(stateEntity.getId(), parseDate(stateEntity.getStateBeginTime(), "23:59:59"), new Date());
-            insertList.add(getReportWaterPumpStateEntity(monitorDataEntity, valueEntity, beginTime, stateValue));
+            for (int i = 1; i <= day; i++) {
+                beginTime = parseDate(DatesUtil.addDayOfDate(beginTime, 1), "00:00:00");
+                endTime   = parseDate(beginTime, "23:59:59");
+                // 先增加不是最后一天的数据
+                if (i != day ) {
+                    insertList.add(getReportWaterPumpStateEntity(monitorDataEntity, valueEntity, beginTime, endTime, stateEntityValue));
+                    continue;
+                }
+                // 最后一天的数据,需要到当前状态和最新状态是否一直,如果不一致需要添加2条数据
+                if (theSameSate) {
+                    insertList.add(getReportWaterPumpStateEntity(monitorDataEntity, valueEntity, beginTime, null, stateEntityValue));
+                    continue;
+                }
+                insertList.add(getReportWaterPumpStateEntity(monitorDataEntity, valueEntity, beginTime, receiveDateTime, stateEntityValue));
+                insertList.add(getReportWaterPumpStateEntity(monitorDataEntity, valueEntity, receiveDateTime, null, stateValue));
+            }
             return;
         }
         // 同一个状态的数据
@@ -135,7 +157,7 @@ public class ReportWaterPumpStateHandler {
             return;
         }
         reportWaterPumpStateMapper.updateReportWaterPumpState(stateEntity.getId(), receiveDateTime, new Date());
-        insertList.add(getReportWaterPumpStateEntity(monitorDataEntity, valueEntity, receiveDateTime, stateValue));
+        insertList.add(getReportWaterPumpStateEntity(monitorDataEntity, valueEntity, receiveDateTime, null, stateValue));
     }
 
 
@@ -152,7 +174,7 @@ public class ReportWaterPumpStateHandler {
     }
 
     private ReportWaterPumpStateEntity getReportWaterPumpStateEntity(MonitorDataEntity monitorDataEntity, MonitorDataValueEntity monitorDataValueEntity,
-                                                                     Date receiveDateTime, Integer state){
+                                                                     Date receiveDateTime, Date endTime, Integer state){
         Date nowTime = new Date();
         ReportWaterPumpStateEntity entity = new ReportWaterPumpStateEntity();
         entity.setState(state);
@@ -166,6 +188,7 @@ public class ReportWaterPumpStateHandler {
         entity.setAttributeId(monitorDataValueEntity.getAttributeId());
         entity.setAttributeName(monitorDataValueEntity.getAttributeName());
         entity.setStateBeginTime(receiveDateTime);
+        entity.setStateEndTime(endTime);
         entity.setDateCreate(nowTime);
         entity.setDateUpdate(nowTime);
         entity.setMd5Query(entity.getMd5());