|
@@ -1,9 +1,12 @@
|
|
|
package com.bz.zoneiot.water.web.service.handle;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.bz.zoneiot.core.mongo.utils.StringUtil;
|
|
|
import com.bz.zoneiot.core.utils.ByteArrayUtils;
|
|
|
import com.bz.zoneiot.core.utils.DateUtil;
|
|
|
import com.bz.zoneiot.core.utils.util.RedisUtils;
|
|
|
+import com.bz.zoneiot.water.api.dto.DeviceDataDto;
|
|
|
+import com.bz.zoneiot.water.api.dto.TimeValueDto;
|
|
|
import com.bz.zoneiot.water.core.dao.DeviceMapper;
|
|
|
import com.bz.zoneiot.water.core.dao.MonitorDataMapper;
|
|
|
import com.bz.zoneiot.water.core.entity.MonitorDataEntity;
|
|
@@ -17,9 +20,7 @@ import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.util.Calendar;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
@@ -79,6 +80,16 @@ public class ReceiveDeviceDataService {
|
|
|
return monitorDataMapper.getDeviceMonitorInfoByDeviceCode(deviceCode);
|
|
|
}
|
|
|
|
|
|
+ public MonitorDataEntity getDeviceMonitorInfoByIotDeviceId(Long iotDeviceId){
|
|
|
+ // 先取缓存里的数据
|
|
|
+ byte[] bytes = redisUtils.get(("sms_water_" + iotDeviceId).getBytes());
|
|
|
+ if(bytes != null && bytes.length > 0 ){
|
|
|
+ return (MonitorDataEntity)ByteArrayUtils.bytesToObject(bytes).get();
|
|
|
+ }
|
|
|
+ return monitorDataMapper.getDeviceMonitorInfoByIotDeviceId(iotDeviceId);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
public MonitorDataEntity save(MonitorDataEntity monitorDataEntity) {
|
|
|
// 缓存数据
|
|
|
redisUtils.setExpire(("sms_water_"+monitorDataEntity.getDeviceCode()).getBytes(), ByteArrayUtils.objectToBytes(monitorDataEntity).get());
|
|
@@ -88,38 +99,57 @@ public class ReceiveDeviceDataService {
|
|
|
return mongoTemplate.save(monitorDataEntity);
|
|
|
}
|
|
|
|
|
|
- /***
|
|
|
+ /**
|
|
|
* manufacturer 厂家,mode型号,设备编号unitIdentifier,type设备类型
|
|
|
* @author Andy
|
|
|
- * @date 17:26 2021/10/19
|
|
|
- * @param jsonObject:
|
|
|
+ * @date 16:40 2021/11/5
|
|
|
+ * @param deviceDataDto:
|
|
|
* @return void
|
|
|
**/
|
|
|
- public void receivedDataHandle(JSONObject jsonObject){
|
|
|
- String eventTime = jsonObject.getString("eventTime");
|
|
|
- String deviceCode = jsonObject.getString("deviceNo");
|
|
|
- Date receiveDateTime = parseDate(eventTime);
|
|
|
- log.info("rabbitMq接收消息处理,code:{},上报时间:{}", deviceCode, eventTime);
|
|
|
- if (!checkParameter(jsonObject, deviceCode, receiveDateTime)){
|
|
|
+ public void receivedDataHandle(DeviceDataDto deviceDataDto){
|
|
|
+ String deviceCode = deviceDataDto.getDeviceNo();
|
|
|
+ Date receiveDateTime = deviceDataDto.getEventTime();
|
|
|
+ log.info("rabbitMq接收消息处理,code:{},上报时间:{}", deviceCode, DateUtil.format(receiveDateTime, "yyyy-MM-dd HH:mm:ss"));
|
|
|
+
|
|
|
+ if (receiveDateTime == null || StringUtil.isEmpty(deviceCode) || CollectionUtils.isEmpty(deviceDataDto.getDecodeData())){
|
|
|
return;
|
|
|
}
|
|
|
// 查询不到设备或者设备属性为空
|
|
|
- MonitorDataEntity monitorDataEntity = getDeviceMonitorInfoByDeviceCode(deviceCode);
|
|
|
+ MonitorDataEntity monitorDataEntity = this.getDeviceMonitorInfoByIotDeviceId(deviceDataDto.getDeviceId());
|
|
|
if (monitorDataEntity == null || CollectionUtils.isEmpty(monitorDataEntity.getDataValues())){
|
|
|
log.error("rabbitMq接收消息处理,code:{},查询不到设备或者设备属性为空,退出",deviceCode);
|
|
|
return;
|
|
|
}
|
|
|
- JSONObject receiveData = JSONObject.parseObject(jsonObject.getString("decodeData"));
|
|
|
- List<MonitorDataValueEntity> monitorDataValueEntities = monitorDataEntity.getDataValues();
|
|
|
+ List<TimeValueDto> timeValueDtoList = deviceDataDto.getDecodeData();
|
|
|
+ timeValueDtoList.sort(Comparator.comparing(TimeValueDto::getTime));
|
|
|
+ Date lastTime = null;
|
|
|
+ for (TimeValueDto timeValueDto : timeValueDtoList) {
|
|
|
+ this.saveData(timeValueDto, monitorDataEntity);
|
|
|
+ lastTime = timeValueDto.getTime();
|
|
|
+ }
|
|
|
+ // 修改设备上报时间
|
|
|
+ deviceMapper.updateLastTime(monitorDataEntity.getDeviceId(), lastTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存数据
|
|
|
+ * @author Andy
|
|
|
+ * @date 16:36 2021/11/5
|
|
|
+ * @param timeValueDto:
|
|
|
+ * @param monitorDataEntity:
|
|
|
+ * @return void
|
|
|
+ **/
|
|
|
+ private void saveData(TimeValueDto timeValueDto, MonitorDataEntity monitorDataEntity){
|
|
|
Integer number = 0;
|
|
|
- for (MonitorDataValueEntity monitorDataValueEntity : monitorDataValueEntities) {
|
|
|
+ Map<String, Object> receiveData = timeValueDto.getValue();
|
|
|
+ for (MonitorDataValueEntity monitorDataValueEntity : monitorDataEntity.getDataValues()) {
|
|
|
BigDecimal bigDecimal = null;
|
|
|
try {
|
|
|
- Double doubleValue = receiveData.getDouble(monitorDataValueEntity.getIdentifier());
|
|
|
- if (doubleValue == null) {
|
|
|
+ Object value = receiveData.get(monitorDataValueEntity.getIdentifier());
|
|
|
+ if (value == null || !StringUtil.isNumber(value.toString())){
|
|
|
continue;
|
|
|
}
|
|
|
- bigDecimal = new BigDecimal(doubleValue);
|
|
|
+ bigDecimal = new BigDecimal(value.toString());
|
|
|
} catch (Exception e) {
|
|
|
log.error("double 转换 error ->", e);
|
|
|
continue;
|
|
@@ -136,52 +166,25 @@ public class ReceiveDeviceDataService {
|
|
|
}
|
|
|
// 没有匹配到属性,视为垃圾数据忽略
|
|
|
if (number == 0) {
|
|
|
- log.error("rabbitMq接收消息处理,code:{},上报时间:{},没有匹配到属性,视为垃圾数据忽略,退出",deviceCode, eventTime);
|
|
|
+ log.error("rabbitMq接收消息处理,code:{},上报时间:{},没有匹配到属性,视为垃圾数据忽略,退出",
|
|
|
+ monitorDataEntity.getId(), DateUtil.format(timeValueDto.getTime(), "yyyy-MM-dd HH:mm:ss"));
|
|
|
return;
|
|
|
}
|
|
|
// 保存到缓存和mongodb数据库
|
|
|
- saveToCacheAndMongodb(monitorDataEntity, receiveDateTime);
|
|
|
- // 修改设备上报时间
|
|
|
- deviceMapper.updateLastTime(monitorDataEntity.getDeviceId(),receiveDateTime);
|
|
|
+ saveToCacheAndMongodb(monitorDataEntity, timeValueDto.getTime());
|
|
|
// 调度预案处理
|
|
|
- dispatchReportHandler.handler(monitorDataEntity, receiveData, receiveDateTime);
|
|
|
+ // dispatchReportHandler.handler(monitorDataEntity, receiveData, timeValueDto.getTime());
|
|
|
// 水泵运行状态报表业务处理
|
|
|
- reportWaterPumpStateHandler.handler(monitorDataEntity, receiveData, receiveDateTime);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private Date parseDate(String eventTime){
|
|
|
- return StringUtils.isEmpty(eventTime) ? null : DateUtil.parseDate(eventTime, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ reportWaterPumpStateHandler.handler(monitorDataEntity, timeValueDto, timeValueDto.getTime());
|
|
|
}
|
|
|
|
|
|
|
|
|
- /***
|
|
|
- * 对象有时间、有设备编码、有数据则解析,任何一个位空直接退出
|
|
|
- * @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(EVENT_TIME_KEY) || !jsonObject.containsKey(UNIT_IDENTIFIER_KEY) ||
|
|
|
- !jsonObject.containsKey(PARSED_DATA_KEY)) {
|
|
|
- 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();
|
|
|
cal.setTime(receiveDateTime);
|
|
|
monitorDataEntity.setYear(cal.get(Calendar.YEAR));
|
|
|
- monitorDataEntity.setMonth(cal.get(Calendar.MONTH)+1);
|
|
|
+ monitorDataEntity.setMonth(cal.get(Calendar.MONTH) + 1);
|
|
|
monitorDataEntity.setDay(cal.get(Calendar.DAY_OF_MONTH));
|
|
|
monitorDataEntity.setHour(cal.get(Calendar.HOUR_OF_DAY));
|
|
|
save(monitorDataEntity);
|