|
|
|
@ -20,6 +20,7 @@ import com.techsor.datacenter.sender.compiler.MvelExecutor; |
|
|
|
import com.techsor.datacenter.sender.components.GuavaRedisCache; |
|
|
|
import com.techsor.datacenter.sender.config.DataSourceContextHolder; |
|
|
|
import com.techsor.datacenter.sender.constants.Constants; |
|
|
|
import com.techsor.datacenter.sender.constants.DeviceAttrCode; |
|
|
|
import com.techsor.datacenter.sender.dao.*; |
|
|
|
import com.techsor.datacenter.sender.disruptor.AccumulateService; |
|
|
|
import com.techsor.datacenter.sender.disruptor.AlertService; |
|
|
|
@ -88,11 +89,11 @@ import static org.apache.commons.text.StringEscapeUtils.unescapeJson; |
|
|
|
public class DataProcessServiceImpl implements IDataProcessService { |
|
|
|
|
|
|
|
private static Logger log= LoggerFactory.getLogger(DataProcessServiceImpl.class); |
|
|
|
|
|
|
|
|
|
|
|
// @Autowired
|
|
|
|
// @Qualifier("sysMqttClient")
|
|
|
|
// private MqttClient sysMqttClient;
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${category.alarm.deviceTypeIds}") |
|
|
|
private List<Integer> alarmTypeIds; |
|
|
|
|
|
|
|
@ -105,6 +106,9 @@ public class DataProcessServiceImpl implements IDataProcessService { |
|
|
|
@Value("${category.status.deviceTypeIds}") |
|
|
|
private List<Integer> statusTypeIds; |
|
|
|
|
|
|
|
@Value("${category.temperature-humidity.deviceTypeIds}") |
|
|
|
private List<Integer> temperatureHumidityTypeIds; |
|
|
|
|
|
|
|
// 所有设备类型ID集合
|
|
|
|
public static final List<Integer> ALL_DEVICE_TYPE_IDS = new ArrayList<>(); |
|
|
|
|
|
|
|
@ -115,15 +119,15 @@ public class DataProcessServiceImpl implements IDataProcessService { |
|
|
|
ALL_DEVICE_TYPE_IDS.addAll(accumulateTypeIds); |
|
|
|
ALL_DEVICE_TYPE_IDS.addAll(statusTypeIds); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static final String REDIS_DASHBOARD_DEVICE_STATUS_KEY = "dashboard_device_status"; |
|
|
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
RedisTemplate<String, Object> alramRedisTemplate; |
|
|
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
AlarmDataPushLambda alarmDataPushLambda; |
|
|
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
private IDeviceTypeConfigService deviceTypeConfigService; |
|
|
|
|
|
|
|
@ -136,13 +140,13 @@ public class DataProcessServiceImpl implements IDataProcessService { |
|
|
|
private DeviceAlertTemplateDao deviceAlertTemplateDao; |
|
|
|
@Resource |
|
|
|
DeviceDao deviceDao; |
|
|
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
private DashboardAlertDao dashboardAlertDao; |
|
|
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
private DashboardStatisticsDao dashboardStatisticsDao; |
|
|
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
private BaStatusDao baStatusDao; |
|
|
|
@Resource |
|
|
|
@ -168,7 +172,7 @@ public class DataProcessServiceImpl implements IDataProcessService { |
|
|
|
|
|
|
|
@Resource |
|
|
|
DispatchService dispatchService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
private CommonOpt commonOpt; |
|
|
|
|
|
|
|
@ -178,9 +182,9 @@ public class DataProcessServiceImpl implements IDataProcessService { |
|
|
|
private AccumulateService accumulateService; |
|
|
|
@Autowired |
|
|
|
private AlertService alertService; |
|
|
|
|
|
|
|
|
|
|
|
private static final ObjectMapper objectMapper = new ObjectMapper(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//告警alert topic
|
|
|
|
@Value("${amazon.aws.alert.topic}") |
|
|
|
@ -251,7 +255,7 @@ public class DataProcessServiceImpl implements IDataProcessService { |
|
|
|
processJson = targetJson; |
|
|
|
Map<String,Object> innerMap=JSON.parseObject(targetJson, Map.class); |
|
|
|
if (StringUtils.isNotEmpty(MapUtils.getString(innerMap,"BUILDINRAWDATA",""))){ |
|
|
|
processJson=MapUtils.getString(innerMap,"BUILDINRAWDATA"); |
|
|
|
processJson=MapUtils.getString(innerMap,"BUILDINRAWDATA"); |
|
|
|
} |
|
|
|
//Save To DynamoDB
|
|
|
|
//The normal data stored as one item. But [KINGIOSERVER] is special.
|
|
|
|
@ -351,7 +355,7 @@ public class DataProcessServiceImpl implements IDataProcessService { |
|
|
|
public void processZAIoTData(ResultDeviceIdEntity entity){ |
|
|
|
String deviceId = entity.getDeviceId(); |
|
|
|
String content = entity.getResult(); |
|
|
|
|
|
|
|
|
|
|
|
String topCompanyId= commonOpt.getTopCompanyId(deviceId); |
|
|
|
if (topCompanyId==null || topCompanyId.equals("0")){ |
|
|
|
return; |
|
|
|
@ -597,10 +601,10 @@ public class DataProcessServiceImpl implements IDataProcessService { |
|
|
|
setTransferStatus(baseTransDataEntity, deviceForwardRelations, currentDeviceAlertInfoList); |
|
|
|
//等待处理
|
|
|
|
Set<String> waitingTargetKeySets=new HashSet<>(); |
|
|
|
|
|
|
|
|
|
|
|
//设备信息
|
|
|
|
DeviceInfoVO deviceInfoVO = alarmDataPushLambda.queryDeviceInfoByDeviceId(deviceId); |
|
|
|
try { |
|
|
|
DeviceInfoVO deviceInfoVO = alarmDataPushLambda.queryDeviceInfoByDeviceId(deviceId); |
|
|
|
try { |
|
|
|
log.debug("triggerAlertConditions:{} {} {}", content, JSON.toJSON(baseTransDataEntity), JSON.toJSON(currentDeviceAlertInfoBindTemplateLists)); |
|
|
|
triggerAlertConditions(companyId, content, currentDeviceAlertInfoBindTemplateLists, baseTransDataEntity,waitingTargetKeySets,currentDevice.getDeviceSN(), deviceInfoVO); |
|
|
|
} catch (Exception e) { |
|
|
|
@ -617,19 +621,19 @@ public class DataProcessServiceImpl implements IDataProcessService { |
|
|
|
} |
|
|
|
} |
|
|
|
baseTransDataEntity.setHashId(UUID.randomUUID()); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
handleDashboardAlert(baseTransDataEntity); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("dashboard alert error", e); |
|
|
|
} |
|
|
|
|
|
|
|
handleDashboardAlert(baseTransDataEntity); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("dashboard alert error", e); |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
minuteLevelStorage(baseTransDataEntity); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("minuteLevelStorage error", e); |
|
|
|
} |
|
|
|
|
|
|
|
minuteLevelStorage(baseTransDataEntity); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("minuteLevelStorage error", e); |
|
|
|
} |
|
|
|
|
|
|
|
// try {
|
|
|
|
// if ("alert".equals(baseTransDataEntity.getStatus())) {
|
|
|
|
// sysAlarmNotify(baseTransDataEntity, deviceInfoVO);
|
|
|
|
@ -650,14 +654,14 @@ public class DataProcessServiceImpl implements IDataProcessService { |
|
|
|
// this.dispatchService.dispatchMessage(baseTransDataEntity.getDeviceId(), JSON.toJSONString(baseTransDataEntity),triggerExpression,0,waitingTargetKeySets);
|
|
|
|
// }
|
|
|
|
log.debug("save data to aws mqtt success!! deviceId:{},contents:{}", deviceId, JSON.toJSON(baseTransDataEntity)); |
|
|
|
|
|
|
|
|
|
|
|
//全部转发iotcore
|
|
|
|
alarmDataPushLambda.iotcoreOpt(companyId, deviceId, JSON.parseObject(JSON.toJSONString(baseTransDataEntity)), deviceInfoVO, 2); |
|
|
|
|
|
|
|
|
|
|
|
//更新
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// private void sysAlarmNotify(DynamodbEntity baseTransDataEntity, DeviceInfoVO deviceInfoVO) throws Exception {
|
|
|
|
// String temp = "建物「{0}」の監視ポイント「{1}」でアラームが発生しました";
|
|
|
|
// String content = MessageFormat.format(temp, deviceInfoVO.getBuildingName(), deviceInfoVO.getMonitoringPointName());
|
|
|
|
@ -666,108 +670,120 @@ public class DataProcessServiceImpl implements IDataProcessService { |
|
|
|
// String topic = MessageFormat.format("sys/alarmNotify/{0}/{1}", baseTransDataEntity.getCompanyId(), baseTransDataEntity.getDbBuildingId());
|
|
|
|
// sysMqttClient.publish(topic, message);
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public void minuteLevelStorage(DynamodbEntity baseTransDataEntity) throws Exception { |
|
|
|
String uploadValue = extractFirstValue(baseTransDataEntity.getRawData()); |
|
|
|
if (StringUtils.isBlank(uploadValue)) { |
|
|
|
List<String> uploadValueList = extractAllValues(baseTransDataEntity.getRawData()); |
|
|
|
if (CollectionUtil.isEmpty(uploadValueList)) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (accumulateTypeIds.contains(baseTransDataEntity.getTypeId())) { |
|
|
|
storageAccumulate(uploadValue, baseTransDataEntity); |
|
|
|
} |
|
|
|
|
|
|
|
if (measureTypeIds.contains(baseTransDataEntity.getTypeId())) { |
|
|
|
storageMeasure(uploadValue, baseTransDataEntity); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void storageMeasure(String uploadValue, DynamodbEntity baseTransDataEntity) throws Exception { |
|
|
|
BigDecimal currentValue = new BigDecimal(uploadValue); |
|
|
|
BigDecimal minValue = currentValue; |
|
|
|
BigDecimal maxValue = currentValue; |
|
|
|
|
|
|
|
// 获取东京时间
|
|
|
|
ComplexTime complexTime = DateUtils.getComplexTime(baseTransDataEntity.getReceive_ts()); |
|
|
|
|
|
|
|
// 获取 Redis 数据
|
|
|
|
String currentDayKey = Constants.STATISTICS_MEASURE_LATEST_PREFIX + complexTime.getDateKey(); |
|
|
|
Object currentDayInfoObj = this.redisTemplate.opsForHash().get(currentDayKey, baseTransDataEntity.getDeviceId()); |
|
|
|
|
|
|
|
//比较值
|
|
|
|
if (null != currentDayInfoObj) { |
|
|
|
StatisticsMeasureInfo currentDayInfo = objectMapper.readValue(currentDayInfoObj.toString(), StatisticsMeasureInfo.class); |
|
|
|
BigDecimal oldMaxValue = new BigDecimal(currentDayInfo.getMaxValue().toString()); |
|
|
|
BigDecimal oldMinValue = new BigDecimal(currentDayInfo.getMinValue().toString()); |
|
|
|
if (ArithUtil.compareTo(oldMaxValue, currentValue) > 0) { |
|
|
|
maxValue = oldMaxValue; |
|
|
|
} |
|
|
|
if (ArithUtil.compareTo(currentValue, oldMinValue) > 0) { |
|
|
|
minValue = oldMinValue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//最新数据存入redis
|
|
|
|
StatisticsMeasureInfo currentInfo = new StatisticsMeasureInfo(); |
|
|
|
BeanUtils.copyProperties(complexTime, currentInfo); |
|
|
|
currentInfo.setValue(uploadValue); |
|
|
|
currentInfo.setUploadAt(baseTransDataEntity.getReceive_ts()); |
|
|
|
currentInfo.setMaxValue(maxValue); |
|
|
|
currentInfo.setMinValue(minValue); |
|
|
|
|
|
|
|
redisTemplate.opsForHash().put(currentDayKey, baseTransDataEntity.getDeviceId(), JSON.toJSONString(currentInfo)); |
|
|
|
// 设置过期时间 7 天
|
|
|
|
redisTemplate.expire(currentDayKey, 7, TimeUnit.DAYS); |
|
|
|
|
|
|
|
//历史表和实时表
|
|
|
|
if (temperatureHumidityTypeIds.contains(baseTransDataEntity.getTypeId())) { |
|
|
|
for (int i = 0; i < uploadValueList.size(); i++) { |
|
|
|
String uploadValue = uploadValueList.get(i); |
|
|
|
//温湿度的rawdata,一定是温度在前,湿度在后
|
|
|
|
if (0 == i) { |
|
|
|
storageMeasure(DeviceAttrCode.MEASURE_TEMPERATURE, uploadValue, baseTransDataEntity); |
|
|
|
} else if (1 == i) { |
|
|
|
storageMeasure(DeviceAttrCode.MEASURE_HUMIDITY, uploadValue, baseTransDataEntity); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
String uploadValue = uploadValueList.get(0);//这里只取第一个元素
|
|
|
|
if (accumulateTypeIds.contains(baseTransDataEntity.getTypeId())) { |
|
|
|
storageAccumulate(DeviceAttrCode.COMMON, uploadValue, baseTransDataEntity); |
|
|
|
} |
|
|
|
if (measureTypeIds.contains(baseTransDataEntity.getTypeId())) { |
|
|
|
storageMeasure(DeviceAttrCode.COMMON, uploadValue, baseTransDataEntity); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void storageMeasure(String attrCode, String uploadValue, DynamodbEntity baseTransDataEntity) throws Exception { |
|
|
|
BigDecimal currentValue = new BigDecimal(uploadValue); |
|
|
|
BigDecimal minValue = currentValue; |
|
|
|
BigDecimal maxValue = currentValue; |
|
|
|
|
|
|
|
// 获取东京时间
|
|
|
|
ComplexTime complexTime = DateUtils.getComplexTime(baseTransDataEntity.getReceive_ts()); |
|
|
|
|
|
|
|
// 获取 Redis 数据
|
|
|
|
String currentDayKey = Constants.STATISTICS_MEASURE_LATEST_PREFIX + complexTime.getDateKey(); |
|
|
|
if (!DeviceAttrCode.COMMON.equalsIgnoreCase(attrCode)) { |
|
|
|
|
|
|
|
} |
|
|
|
Object currentDayInfoObj = this.redisTemplate.opsForHash().get(currentDayKey, baseTransDataEntity.getDeviceId()); |
|
|
|
|
|
|
|
//比较值
|
|
|
|
if (null != currentDayInfoObj) { |
|
|
|
StatisticsMeasureInfo currentDayInfo = objectMapper.readValue(currentDayInfoObj.toString(), StatisticsMeasureInfo.class); |
|
|
|
BigDecimal oldMaxValue = new BigDecimal(currentDayInfo.getMaxValue().toString()); |
|
|
|
BigDecimal oldMinValue = new BigDecimal(currentDayInfo.getMinValue().toString()); |
|
|
|
if (ArithUtil.compareTo(oldMaxValue, currentValue) > 0) { |
|
|
|
maxValue = oldMaxValue; |
|
|
|
} |
|
|
|
if (ArithUtil.compareTo(currentValue, oldMinValue) > 0) { |
|
|
|
minValue = oldMinValue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//最新数据存入redis
|
|
|
|
StatisticsMeasureInfo currentInfo = new StatisticsMeasureInfo(); |
|
|
|
BeanUtils.copyProperties(complexTime, currentInfo); |
|
|
|
currentInfo.setValue(uploadValue); |
|
|
|
currentInfo.setUploadAt(baseTransDataEntity.getReceive_ts()); |
|
|
|
currentInfo.setMaxValue(maxValue); |
|
|
|
currentInfo.setMinValue(minValue); |
|
|
|
|
|
|
|
redisTemplate.opsForHash().put(currentDayKey, baseTransDataEntity.getDeviceId(), JSON.toJSONString(currentInfo)); |
|
|
|
// 设置过期时间 7 天
|
|
|
|
redisTemplate.expire(currentDayKey, 7, TimeUnit.DAYS); |
|
|
|
|
|
|
|
//历史表和实时表
|
|
|
|
// dashboardStatisticsDao.insertDeviceMeasureInfo(uploadValue, baseTransDataEntity.getDeviceId(), currentInfo);
|
|
|
|
// dashboardStatisticsDao.upsertDeviceRealtimeMeasure(uploadValue, baseTransDataEntity.getDeviceId(), minValue, maxValue, currentInfo);
|
|
|
|
measureService.write(uploadValue, baseTransDataEntity.getDeviceId(), currentInfo, minValue, maxValue); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private void storageAccumulate(String uploadValue, DynamodbEntity baseTransDataEntity) throws Exception { |
|
|
|
BigDecimal currentValue = new BigDecimal(uploadValue); |
|
|
|
Double incrementToday = null; |
|
|
|
Double incrementMinute = null; |
|
|
|
|
|
|
|
// 获取东京时间
|
|
|
|
ComplexTime complexTime = DateUtils.getComplexTime(baseTransDataEntity.getReceive_ts()); |
|
|
|
|
|
|
|
// 获取 Redis 数据
|
|
|
|
String currentDayKey = Constants.STATISTICS_ACCUMULATE_LATEST_PREFIX + complexTime.getDateKey(); |
|
|
|
String lastDayKey = Constants.STATISTICS_ACCUMULATE_LATEST_PREFIX + complexTime.getPreviousDateKey(); |
|
|
|
Object currentDayInfoObj = this.redisTemplate.opsForHash().get(currentDayKey, baseTransDataEntity.getDeviceId()); |
|
|
|
Object lastDayInfoObj = this.redisTemplate.opsForHash().get(lastDayKey, baseTransDataEntity.getDeviceId()); |
|
|
|
|
|
|
|
// 今日增量
|
|
|
|
// 如果昨天的没有,那直接取当前数据
|
|
|
|
if (null != lastDayInfoObj) { |
|
|
|
StatisticsAccumulateInfo lastDayInfo = objectMapper.readValue(lastDayInfoObj.toString(), StatisticsAccumulateInfo.class); |
|
|
|
BigDecimal lastDayValue = new BigDecimal(lastDayInfo.getValue().toString()); |
|
|
|
if (ArithUtil.compareTo(currentValue, lastDayValue) >= 0) { |
|
|
|
incrementToday = ArithUtil.sub(currentValue, lastDayValue); |
|
|
|
} |
|
|
|
} else { |
|
|
|
incrementToday = currentValue.doubleValue(); |
|
|
|
} |
|
|
|
|
|
|
|
// 1分钟增量
|
|
|
|
// 这个是如果这条数据的前一分钟没有数据的话,这条数据它就不计算增量,但是保留这个数据,然后下一条数据上来的时候就继续跟这条数据计算增量就行
|
|
|
|
long diff = 3600000L; |
|
|
|
measureService.write(attrCode, uploadValue, baseTransDataEntity.getDeviceId(), currentInfo, minValue, maxValue); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private void storageAccumulate(String attrCode, String uploadValue, DynamodbEntity baseTransDataEntity) throws Exception { |
|
|
|
BigDecimal currentValue = new BigDecimal(uploadValue); |
|
|
|
Double incrementToday = null; |
|
|
|
Double incrementMinute = null; |
|
|
|
|
|
|
|
// 获取东京时间
|
|
|
|
ComplexTime complexTime = DateUtils.getComplexTime(baseTransDataEntity.getReceive_ts()); |
|
|
|
|
|
|
|
// 获取 Redis 数据
|
|
|
|
String currentDayKey = Constants.STATISTICS_ACCUMULATE_LATEST_PREFIX + complexTime.getDateKey(); |
|
|
|
String lastDayKey = Constants.STATISTICS_ACCUMULATE_LATEST_PREFIX + complexTime.getPreviousDateKey(); |
|
|
|
Object currentDayInfoObj = this.redisTemplate.opsForHash().get(currentDayKey, baseTransDataEntity.getDeviceId()); |
|
|
|
Object lastDayInfoObj = this.redisTemplate.opsForHash().get(lastDayKey, baseTransDataEntity.getDeviceId()); |
|
|
|
|
|
|
|
// 今日增量
|
|
|
|
// 如果昨天的没有,那直接取当前数据
|
|
|
|
if (null != lastDayInfoObj) { |
|
|
|
StatisticsAccumulateInfo lastDayInfo = objectMapper.readValue(lastDayInfoObj.toString(), StatisticsAccumulateInfo.class); |
|
|
|
BigDecimal lastDayValue = new BigDecimal(lastDayInfo.getValue().toString()); |
|
|
|
incrementToday = ArithUtil.sub(currentValue, lastDayValue); |
|
|
|
} else { |
|
|
|
incrementToday = currentValue.doubleValue(); |
|
|
|
} |
|
|
|
|
|
|
|
// 1分钟增量
|
|
|
|
// 这个是如果这条数据的前一分钟没有数据的话,这条数据它就不计算增量,但是保留这个数据,然后下一条数据上来的时候就继续跟这条数据计算增量就行
|
|
|
|
long diff = 3600000L; |
|
|
|
BigDecimal lastMinuteValue = null; |
|
|
|
long nowTs = baseTransDataEntity.getReceive_ts(); |
|
|
|
if (currentDayInfoObj == null) { |
|
|
|
if (complexTime.getHourKey() == 0 && lastDayInfoObj != null) { |
|
|
|
StatisticsAccumulateInfo lastMinInfo = objectMapper.readValue(lastDayInfoObj.toString(), StatisticsAccumulateInfo.class); |
|
|
|
StatisticsAccumulateInfo lastMinInfo = objectMapper.readValue(lastDayInfoObj.toString(), StatisticsAccumulateInfo.class); |
|
|
|
if (nowTs - lastMinInfo.getUploadAt() < diff) { //需要1小时内的数据
|
|
|
|
lastMinuteValue = new BigDecimal(String.valueOf(lastMinInfo.getValue())); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
StatisticsAccumulateInfo currentDayInfo = objectMapper.readValue(currentDayInfoObj.toString(), StatisticsAccumulateInfo.class); |
|
|
|
StatisticsAccumulateInfo currentDayInfo = objectMapper.readValue(currentDayInfoObj.toString(), StatisticsAccumulateInfo.class); |
|
|
|
if (nowTs - currentDayInfo.getUploadAt() < diff) { |
|
|
|
lastMinuteValue = new BigDecimal(String.valueOf(currentDayInfo.getValue())); |
|
|
|
} |
|
|
|
@ -775,81 +791,108 @@ public class DataProcessServiceImpl implements IDataProcessService { |
|
|
|
if (lastMinuteValue != null && currentValue.compareTo(lastMinuteValue) >= 0) { |
|
|
|
incrementMinute = currentValue.subtract(lastMinuteValue).doubleValue(); |
|
|
|
} |
|
|
|
|
|
|
|
//最新数据存入redis
|
|
|
|
|
|
|
|
//最新数据存入redis
|
|
|
|
StatisticsAccumulateInfo currentInfo = new StatisticsAccumulateInfo(); |
|
|
|
BeanUtils.copyProperties(complexTime, currentInfo); |
|
|
|
currentInfo.setValue(uploadValue); |
|
|
|
currentInfo.setUploadAt(baseTransDataEntity.getReceive_ts()); |
|
|
|
|
|
|
|
redisTemplate.opsForHash().put(currentDayKey, baseTransDataEntity.getDeviceId(), JSON.toJSONString(currentInfo)); |
|
|
|
// 设置过期时间 7 天
|
|
|
|
redisTemplate.expire(currentDayKey, 7, TimeUnit.DAYS); |
|
|
|
|
|
|
|
//历史表和日期实时表
|
|
|
|
BeanUtils.copyProperties(complexTime, currentInfo); |
|
|
|
currentInfo.setValue(uploadValue); |
|
|
|
currentInfo.setUploadAt(baseTransDataEntity.getReceive_ts()); |
|
|
|
|
|
|
|
redisTemplate.opsForHash().put(currentDayKey, baseTransDataEntity.getDeviceId(), JSON.toJSONString(currentInfo)); |
|
|
|
// 设置过期时间 7 天
|
|
|
|
redisTemplate.expire(currentDayKey, 7, TimeUnit.DAYS); |
|
|
|
|
|
|
|
//历史表和日期实时表
|
|
|
|
// dashboardStatisticsDao.insertDeviceAccumulateInfo(uploadValue, baseTransDataEntity.getDeviceId(), incrementToday, incrementMinute, currentInfo);
|
|
|
|
// dashboardStatisticsDao.insertOrUpdateRealtimeAccumulateDay(uploadValue, baseTransDataEntity.getDeviceId(), incrementToday, currentInfo);
|
|
|
|
accumulateService.write(uploadValue, baseTransDataEntity.getDeviceId(), incrementToday, incrementMinute, currentInfo); |
|
|
|
} |
|
|
|
|
|
|
|
public String extractFirstValue(String rawData) { |
|
|
|
if (StringUtils.isBlank(rawData)){ |
|
|
|
return ""; |
|
|
|
} |
|
|
|
try { |
|
|
|
JsonNode node = objectMapper.readTree(rawData); |
|
|
|
Iterator<Map.Entry<String, JsonNode>> fields = node.fields(); |
|
|
|
if (fields.hasNext()) { |
|
|
|
return fields.next().getValue().asText(); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("Failed to parse rawData JSON: " + rawData, e); |
|
|
|
} |
|
|
|
return ""; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void handleDashboardAlert(DynamodbEntity baseTransDataEntity) { |
|
|
|
|
|
|
|
if (!ALL_DEVICE_TYPE_IDS.contains(baseTransDataEntity.getTypeId())) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
Object redisOldStatusObj = this.alramRedisTemplate.opsForHash().get(REDIS_DASHBOARD_DEVICE_STATUS_KEY, baseTransDataEntity.getDeviceId()); |
|
|
|
|
|
|
|
//更新实时信息
|
|
|
|
try { |
|
|
|
accumulateService.write(attrCode, uploadValue, baseTransDataEntity.getDeviceId(), incrementToday, incrementMinute, currentInfo); |
|
|
|
} |
|
|
|
|
|
|
|
// public String extractFirstValue(String rawData) {
|
|
|
|
// if (StringUtils.isBlank(rawData)){
|
|
|
|
// return "";
|
|
|
|
// }
|
|
|
|
// try {
|
|
|
|
// JsonNode node = objectMapper.readTree(rawData);
|
|
|
|
// Iterator<Map.Entry<String, JsonNode>> fields = node.fields();
|
|
|
|
// if (fields.hasNext()) {
|
|
|
|
// return fields.next().getValue().asText();
|
|
|
|
// }
|
|
|
|
// } catch (Exception e) {
|
|
|
|
// log.error("Failed to parse rawData JSON: " + rawData, e);
|
|
|
|
// }
|
|
|
|
// return "";
|
|
|
|
// }
|
|
|
|
|
|
|
|
public List<String> extractAllValues(String rawData) { |
|
|
|
List<String> result = new ArrayList<>(); |
|
|
|
|
|
|
|
if (StringUtils.isBlank(rawData)) { |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
JsonNode node = objectMapper.readTree(rawData); |
|
|
|
|
|
|
|
if (!node.isObject()) { |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
Iterator<Map.Entry<String, JsonNode>> fields = node.fields(); |
|
|
|
while (fields.hasNext()) { |
|
|
|
JsonNode valueNode = fields.next().getValue(); |
|
|
|
result.add(valueNode.isValueNode() ? valueNode.asText() : valueNode.toString()); |
|
|
|
} |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
log.error("Failed to parse rawData JSON: {}", rawData, e); |
|
|
|
} |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void handleDashboardAlert(DynamodbEntity baseTransDataEntity) { |
|
|
|
|
|
|
|
if (!ALL_DEVICE_TYPE_IDS.contains(baseTransDataEntity.getTypeId())) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
Object redisOldStatusObj = this.alramRedisTemplate.opsForHash().get(REDIS_DASHBOARD_DEVICE_STATUS_KEY, baseTransDataEntity.getDeviceId()); |
|
|
|
|
|
|
|
//更新实时信息
|
|
|
|
try { |
|
|
|
// dashboardAlertDao.upsertDeviceRawData(baseTransDataEntity);
|
|
|
|
alertService.write(baseTransDataEntity); |
|
|
|
this.alramRedisTemplate.opsForHash().put( |
|
|
|
REDIS_DASHBOARD_DEVICE_STATUS_KEY, |
|
|
|
baseTransDataEntity.getDeviceId(), |
|
|
|
baseTransDataEntity.getStatus()); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("upsertDeviceRawData error", e); |
|
|
|
} |
|
|
|
|
|
|
|
//告警历史处理
|
|
|
|
if (alarmTypeIds.contains(baseTransDataEntity.getTypeId())) { |
|
|
|
String status = baseTransDataEntity.getStatus(); |
|
|
|
if (null == redisOldStatusObj) { |
|
|
|
if ("alert".equals(status)) { |
|
|
|
dashboardAlertDao.insertAlertHistory(baseTransDataEntity); |
|
|
|
} |
|
|
|
} else { |
|
|
|
String redisOldStatus = (String) redisOldStatusObj; |
|
|
|
if ("alert".equals(status) && !"alert".equals(redisOldStatus)) { |
|
|
|
dashboardAlertDao.insertAlertHistory(baseTransDataEntity); |
|
|
|
} else if (!"alert".equals(status) && "alert".equals(redisOldStatus)) { |
|
|
|
if (0 == baseTransDataEntity.getRetainAlert()) { |
|
|
|
dashboardAlertDao.updateLatestAlertToAutoRecovered(baseTransDataEntity); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
this.alramRedisTemplate.opsForHash().put( |
|
|
|
REDIS_DASHBOARD_DEVICE_STATUS_KEY, |
|
|
|
baseTransDataEntity.getDeviceId(), |
|
|
|
baseTransDataEntity.getStatus()); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("upsertDeviceRawData error", e); |
|
|
|
} |
|
|
|
|
|
|
|
//告警历史处理
|
|
|
|
if (alarmTypeIds.contains(baseTransDataEntity.getTypeId())) { |
|
|
|
String status = baseTransDataEntity.getStatus(); |
|
|
|
if (null == redisOldStatusObj) { |
|
|
|
if ("alert".equals(status)) { |
|
|
|
dashboardAlertDao.insertAlertHistory(baseTransDataEntity); |
|
|
|
} |
|
|
|
} else { |
|
|
|
String redisOldStatus = (String) redisOldStatusObj; |
|
|
|
if ("alert".equals(status) && !"alert".equals(redisOldStatus)) { |
|
|
|
dashboardAlertDao.insertAlertHistory(baseTransDataEntity); |
|
|
|
} else if (!"alert".equals(status) && "alert".equals(redisOldStatus)) { |
|
|
|
if (0 == baseTransDataEntity.getRetainAlert()) { |
|
|
|
dashboardAlertDao.updateLatestAlertToAutoRecovered(baseTransDataEntity); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 根据设备信息和内容创建一个DynamodbEntity基础实体。 |
|
|
|
* <p> |
|
|
|
* 该方法利用给定的设备信息和内容创建并返回一个DynamodbEntity对象。 |
|
|
|
@ -1004,7 +1047,7 @@ public class DataProcessServiceImpl implements IDataProcessService { |
|
|
|
if (StringUtils.isEmpty(targetId)||StringUtils.equals("-1",targetId)){ |
|
|
|
|
|
|
|
}else{ |
|
|
|
paramsMap=JSON.parseObject(targetId, new TypeReference<Map<String,Object>>(){}); |
|
|
|
paramsMap=JSON.parseObject(targetId, new TypeReference<Map<String,Object>>(){}); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -1102,23 +1145,23 @@ public class DataProcessServiceImpl implements IDataProcessService { |
|
|
|
private void triggerAndSave(String companyId, String content, List<DeviceAlertInfo> currentDeviceAlertInfoList, |
|
|
|
DynamodbEntity baseTransDataEntity,Set<String> waitingProcessTargetSets, |
|
|
|
Set<String> existingTargetSets,String deviceSN, DeviceInfoVO deviceInfoVO) { |
|
|
|
|
|
|
|
JSONObject alertTemplateInfo = null; |
|
|
|
if (StringUtils.isNotEmpty(baseTransDataEntity.getRawData()) && StringUtils.isNotEmpty(baseTransDataEntity.getAlertTemplateIds())) { |
|
|
|
JSONObject postJsonObject = new JSONObject(); |
|
|
|
postJsonObject.put("deviceId", baseTransDataEntity.getDeviceId()); |
|
|
|
postJsonObject.put("status", baseTransDataEntity.getStatus()); |
|
|
|
postJsonObject.put("companyId", companyId); |
|
|
|
if (StringUtils.isNotEmpty(baseTransDataEntity.getAlertTemplateIds())){ |
|
|
|
postJsonObject.put("alarmTmplIds", baseTransDataEntity.getAlertTemplateIds().replace("[", "").replace("]", "")); |
|
|
|
} |
|
|
|
|
|
|
|
JSONObject alertTemplateInfo = null; |
|
|
|
if (StringUtils.isNotEmpty(baseTransDataEntity.getRawData()) && StringUtils.isNotEmpty(baseTransDataEntity.getAlertTemplateIds())) { |
|
|
|
JSONObject postJsonObject = new JSONObject(); |
|
|
|
postJsonObject.put("deviceId", baseTransDataEntity.getDeviceId()); |
|
|
|
postJsonObject.put("status", baseTransDataEntity.getStatus()); |
|
|
|
postJsonObject.put("companyId", companyId); |
|
|
|
if (StringUtils.isNotEmpty(baseTransDataEntity.getAlertTemplateIds())){ |
|
|
|
postJsonObject.put("alarmTmplIds", baseTransDataEntity.getAlertTemplateIds().replace("[", "").replace("]", "")); |
|
|
|
} |
|
|
|
log.debug("triggerAndSave postJsonObject:"+postJsonObject.toString()); |
|
|
|
|
|
|
|
//根据告警模板获取转发信息和告警方式
|
|
|
|
String resp = HttpUtil.doPost(businessQueryPushInfo, postJsonObject.toString() , null); |
|
|
|
log.info("queryByDeviceId result:{}", resp); |
|
|
|
if (StringUtils.isNotBlank(resp)) { |
|
|
|
alertTemplateInfo = JSONObject.parseObject(resp); |
|
|
|
//根据告警模板获取转发信息和告警方式
|
|
|
|
String resp = HttpUtil.doPost(businessQueryPushInfo, postJsonObject.toString() , null); |
|
|
|
log.info("queryByDeviceId result:{}", resp); |
|
|
|
if (StringUtils.isNotBlank(resp)) { |
|
|
|
alertTemplateInfo = JSONObject.parseObject(resp); |
|
|
|
//Process Value replace/用于替换模版中的{Value}字段,这里和下面一段都是做处理,分别对两块数据进行了处理
|
|
|
|
JSONArray parsedAlarmInfoListArray = alertTemplateInfo.getJSONObject("data").getJSONArray("parsedAlarmInfoList"); |
|
|
|
JSONObject jsonObject = JSON.parseObject(baseTransDataEntity.getRawData(), JSONObject.class); |
|
|
|
@ -1140,9 +1183,9 @@ public class DataProcessServiceImpl implements IDataProcessService { |
|
|
|
log.info("parsedAlarmInfoListArray 1:{}", parsedAlarmInfoListArray); |
|
|
|
|
|
|
|
|
|
|
|
//处理转发
|
|
|
|
alarmDataPushLambda.handleTargetUrl(alertTemplateInfo, JSON.parseObject(JSON.toJSONString(baseTransDataEntity))); |
|
|
|
} |
|
|
|
//处理转发
|
|
|
|
alarmDataPushLambda.handleTargetUrl(alertTemplateInfo, JSON.parseObject(JSON.toJSONString(baseTransDataEntity))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//处理告警内容中的{Value}变量,用实际值替换
|
|
|
|
@ -1301,7 +1344,7 @@ public class DataProcessServiceImpl implements IDataProcessService { |
|
|
|
Map<String, String> map = gson.fromJson(jsonMapStr, Map.class); |
|
|
|
return map; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getCompanyId(String deviceId){ |
|
|
|
String companyId = ""; |
|
|
|
|