feat(timer): 添加MQTT心跳检测定时器并优化数据处理

- 新增MqttHeartCheckTimer定时器,每小时轮询在线设备并进行心跳检测
- 在CsEquipmentDeliveryService中添加getUseOnlineDevice方法获取启用且在线的MQTT设备
This commit is contained in:
xy
2026-05-08 16:30:41 +08:00
parent 0f532033b0
commit 2cad107c29
6 changed files with 266 additions and 115 deletions

View File

@@ -152,7 +152,7 @@ public class StatServiceImpl implements IStatService {
}
if (CollectionUtil.isNotEmpty(recordList)){
//influx数据批量入库
influxDbUtils.batchInsert(influxDbUtils.getDbName(), "", InfluxDB.ConsistencyLevel.ALL, TimeUnit.MILLISECONDS, recordList);
influxDbUtils.batchInsert(influxDbUtils.getDbName(), "", InfluxDB.ConsistencyLevel.ALL, TimeUnit.SECONDS, recordList);
//记录监测点最新数据时间
CsLineLatestData csLineLatestData = new CsLineLatestData();
csLineLatestData.setLineId(lineId);
@@ -192,23 +192,40 @@ public class StatServiceImpl implements IStatService {
*/
public List<String> assembleData(String lineId,List<CsDataArray> dataArrayList,AppAutoDataMessage.DataArray item,Integer clDid,String statMethod,Integer process,String devType,String accessMethod) {
List<String> records = new ArrayList<String>();
//解码
List<Float> floats = PubUtils.byteArrayToFloatList(Base64.getDecoder().decode(item.getData()));
if (CollectionUtil.isEmpty(floats)){
throw new BusinessException(StatResponseEnum.AUTO_DATA_NULL);
}
//校验模板和解码数据数量能否对应上
if (!Objects.equals(dataArrayList.size(),floats.size())){
throw new BusinessException(StatResponseEnum.ARRAY_DATA_NOT_MATCH);
}
Map<String,String> map = new Gson().fromJson(String.valueOf(redisUtil.getObjectByKey(AppRedisKey.ELE_EPD_PQD)), Map.class);
boolean flag = Objects.equals(DicDataEnum.DEV_CLD.getCode(), devType) && Objects.equals(accessMethod, "CLD");
//fixme 捂脸设备上送的是北京时间,时序数据库录入时 需要utc时间减去8小时
long originalTimeSec = flag ? item.getDataTimeSec() : item.getDataTimeSec() - 8 * 3600;
if (originalTimeSec < 0) {
System.out.println("originalTimeSec==:" + originalTimeSec);
}
for (int i = 0; i < dataArrayList.size(); i++) {
String tableName = map.get(dataArrayList.get(i).getName());
long adjustedTimeSec;
//短时闪变 || 电压波动 10分钟
if (Objects.equals(tableName,"data_flicker") || Objects.equals(tableName,"data_fluc")) {
adjustedTimeSec = (originalTimeSec / 600) * 600;
}
//长时闪变 2小时
else if (Objects.equals(tableName,"data_plt")) {
adjustedTimeSec = (originalTimeSec / 7200) * 7200;
}
else {
adjustedTimeSec = originalTimeSec;
}
Map<String, String> tags = new HashMap<>();
tags.put(InfluxDBTableConstant.LINE_ID,lineId);
tags.put(InfluxDBTableConstant.PHASIC_TYPE,dataArrayList.get(i).getPhase());
//todo 不清楚之前为啥要修改相别,这边按字典配置相别无法查询到数据,先改回来
//tags.put(InfluxDBTableConstant.PHASIC_TYPE,Objects.isNull(PHASE_MAPPING.get(dataArrayList.get(i).getPhase()))?dataArrayList.get(i).getPhase():PHASE_MAPPING.get(dataArrayList.get(i).getPhase()));
tags.put(InfluxDBTableConstant.VALUE_TYPE,statMethod);
tags.put(InfluxDBTableConstant.CL_DID,clDid.toString());
tags.put(InfluxDBTableConstant.PROCESS,process.toString());
@@ -217,9 +234,8 @@ public class StatServiceImpl implements IStatService {
//这边特殊处理如果数据为3.14159则将数据置为null
fields.put(dataArrayList.get(i).getName(),Objects.equals(floats.get(i),3.14159f) ? null:floats.get(i));
fields.put(InfluxDBTableConstant.IS_ABNORMAL,item.getDataTag());
//fixme 设备上送的是北京时间,时序数据库录入时 需要utc时间减去8小时
boolean flag = Objects.equals(DicDataEnum.DEV_CLD.getCode(), devType) && Objects.equals(accessMethod, "CLD");
Point point = influxDbUtils.pointBuilder(tableName, flag?item.getDataTimeSec():item.getDataTimeSec()-8*3600, TimeUnit.SECONDS, tags, fields);
Point point = influxDbUtils.pointBuilder(tableName, adjustedTimeSec, TimeUnit.SECONDS, tags, fields);
BatchPoints batchPoints = BatchPoints.database(influxDbUtils.getDbName()).retentionPolicy("").consistency(InfluxDB.ConsistencyLevel.ALL).build();
batchPoints.point(point);
records.add(batchPoints.lineProtocol());
@@ -227,6 +243,43 @@ public class StatServiceImpl implements IStatService {
return records;
}
// public List<String> assembleData(String lineId,List<CsDataArray> dataArrayList,AppAutoDataMessage.DataArray item,Integer clDid,String statMethod,Integer process,String devType,String accessMethod) {
// List<String> records = new ArrayList<String>();
// //解码
// List<Float> floats = PubUtils.byteArrayToFloatList(Base64.getDecoder().decode(item.getData()));
// if (CollectionUtil.isEmpty(floats)){
// throw new BusinessException(StatResponseEnum.AUTO_DATA_NULL);
// }
// //校验模板和解码数据数量能否对应上
// if (!Objects.equals(dataArrayList.size(),floats.size())){
// throw new BusinessException(StatResponseEnum.ARRAY_DATA_NOT_MATCH);
// }
// Map<String,String> map = new Gson().fromJson(String.valueOf(redisUtil.getObjectByKey(AppRedisKey.ELE_EPD_PQD)), Map.class);
// for (int i = 0; i < dataArrayList.size(); i++) {
// String tableName = map.get(dataArrayList.get(i).getName());
// Map<String, String> tags = new HashMap<>();
// tags.put(InfluxDBTableConstant.LINE_ID,lineId);
// tags.put(InfluxDBTableConstant.PHASIC_TYPE,dataArrayList.get(i).getPhase());
// //todo 不清楚之前为啥要修改相别,这边按字典配置相别无法查询到数据,先改回来
// //tags.put(InfluxDBTableConstant.PHASIC_TYPE,Objects.isNull(PHASE_MAPPING.get(dataArrayList.get(i).getPhase()))?dataArrayList.get(i).getPhase():PHASE_MAPPING.get(dataArrayList.get(i).getPhase()));
// tags.put(InfluxDBTableConstant.VALUE_TYPE,statMethod);
// tags.put(InfluxDBTableConstant.CL_DID,clDid.toString());
// tags.put(InfluxDBTableConstant.PROCESS,process.toString());
// tags.put(InfluxDBTableConstant.QUALITY_FLAG,"0");
// Map<String,Object> fields = new HashMap<>();
// //这边特殊处理如果数据为3.14159则将数据置为null
// fields.put(dataArrayList.get(i).getName(),Objects.equals(floats.get(i),3.14159f) ? null:floats.get(i));
// fields.put(InfluxDBTableConstant.IS_ABNORMAL,item.getDataTag());
// //fixme 设备上送的是北京时间,时序数据库录入时 需要utc时间减去8小时
// boolean flag = Objects.equals(DicDataEnum.DEV_CLD.getCode(), devType) && Objects.equals(accessMethod, "CLD");
// Point point = influxDbUtils.pointBuilder(tableName, flag?item.getDataTimeSec():item.getDataTimeSec()-8*3600, TimeUnit.SECONDS, tags, fields);
// BatchPoints batchPoints = BatchPoints.database(influxDbUtils.getDbName()).retentionPolicy("").consistency(InfluxDB.ConsistencyLevel.ALL).build();
// batchPoints.point(point);
// records.add(batchPoints.lineProtocol());
// }
// return records;
// }
public List<CsDataArray> objectToList(Object object) {
List<CsDataArray> urlList = new ArrayList<>();
if (object != null) {