|
@@ -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);
|
|
|
}
|
|
|
+
|
|
|
}
|