告警记录增加字段FirstTriggeredCode

This commit is contained in:
huguanghan 2024-12-20 12:40:12 +08:00
parent 9b4c1db23e
commit b5859fef80
7 changed files with 48 additions and 5 deletions

View File

@ -1,5 +1,6 @@
package com.das.modules.cache.domain; package com.das.modules.cache.domain;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data; import lombok.Data;
/** /**
@ -32,4 +33,14 @@ public class DeviceInfoCache {
* 物模型ID * 物模型ID
*/ */
private Long iotModelId; private Long iotModelId;
/**
* 制造商
*/
private String madeinFactory;
/**
* 型号规格
*/
private String model;
} }

View File

@ -75,6 +75,8 @@ public class EquipmentCacheImpl implements EquipmentCache {
if (equipment != null) { if (equipment != null) {
DeviceInfoCache deviceInfoCache = new DeviceInfoCache(); DeviceInfoCache deviceInfoCache = new DeviceInfoCache();
deviceInfoCache.setDeviceId(equipment.getId()); deviceInfoCache.setDeviceId(equipment.getId());
deviceInfoCache.setMadeinFactory(equipment.getMadeinFactory());
deviceInfoCache.setModel(equipment.getModel());
deviceInfoCache.setDeviceCode(equipment.getCode()); deviceInfoCache.setDeviceCode(equipment.getCode());
deviceInfoCache.setDeviceName(equipment.getName()); deviceInfoCache.setDeviceName(equipment.getName());
deviceInfoCache.setObjectType(equipment.getObjectType()); deviceInfoCache.setObjectType(equipment.getObjectType());

View File

@ -30,4 +30,10 @@ public class DeviceEventInfo {
private String deviceCode; private String deviceCode;
private String deviceName; private String deviceName;
private String model;
private String madeinFactory;
private Integer firstTriggeredCode;
} }

View File

@ -3,6 +3,8 @@ package com.das.modules.data.service;
import cn.hutool.core.collection.ListUtil; import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.das.common.utils.PageDataInfo; import com.das.common.utils.PageDataInfo;
import com.das.modules.cache.domain.DeviceInfoCache;
import com.das.modules.cache.service.CacheService;
import com.das.modules.data.domain.DeviceEventInfo; import com.das.modules.data.domain.DeviceEventInfo;
import com.das.modules.data.domain.RTValue; import com.das.modules.data.domain.RTValue;
import com.das.modules.equipment.domain.vo.IotModelFieldVo; import com.das.modules.equipment.domain.vo.IotModelFieldVo;
@ -12,6 +14,7 @@ import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import jakarta.annotation.PreDestroy; import jakarta.annotation.PreDestroy;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableAsync;
@ -45,6 +48,8 @@ public class TDEngineService {
@Value("${tdengine.batch-size:10000}") @Value("${tdengine.batch-size:10000}")
private int batchSize; private int batchSize;
@Autowired
private CacheService cacheService;
public void init() { public void init() {
if (hikariDataSource == null) { if (hikariDataSource == null) {
HikariConfig config = new HikariConfig(); HikariConfig config = new HikariConfig();
@ -256,7 +261,7 @@ public class TDEngineService {
}); });
//初始化event_info //初始化event_info
String eventInfo = "create stable IF NOT EXISTS event_info (event_time timestamp,event_id bigint primary key,attributecode varchar(64),event_type tinyint,event_level tinyint,event_text varchar(256),confirmed tinyint,confirm_account varchar(32),confirm_time timestamp) tags (device_id bigint,device_code varchar(64),device_name varchar(192))"; String eventInfo = "create stable IF NOT EXISTS event_info (event_time timestamp,event_id bigint primary key,attributecode varchar(64),event_type tinyint,event_level tinyint,event_text varchar(256),confirmed tinyint,confirm_account varchar(32),confirm_time timestamp,first_triggered_code INTEGER) tags (device_id bigint,device_code varchar(64),device_name varchar(192))";
try { try {
pstmt.executeUpdate(eventInfo); pstmt.executeUpdate(eventInfo);
} catch (SQLException ex) { } catch (SQLException ex) {
@ -414,6 +419,8 @@ public class TDEngineService {
sb.append(dv.getConfirmAccount()); sb.append(dv.getConfirmAccount());
sb.append(","); sb.append(",");
sb.append(dv.getConfirmTime()); sb.append(dv.getConfirmTime());
sb.append(",");
sb.append(dv.getFirstTriggeredCode());
sb.append(")"); sb.append(")");
} }
try { try {
@ -833,7 +840,7 @@ public class TDEngineService {
return result; return result;
} }
public PageDataInfo<DeviceEventInfo> queryEvent(Integer eventLevel, Long startTime, Long endTime, List<String> deviceCodeList, Integer pageSize, Integer offset, Integer limit) { public PageDataInfo<DeviceEventInfo> queryEvent(Integer eventLevel, Long startTime, Long endTime, List<String> deviceCodeList, Integer pageSize, Integer offset, Integer limit,Integer firstTriggeredCode) {
List<DeviceEventInfo> result = new ArrayList<>(); List<DeviceEventInfo> result = new ArrayList<>();
StringBuffer sb = new StringBuffer(2048); StringBuffer sb = new StringBuffer(2048);
Integer total = 0; Integer total = 0;
@ -842,6 +849,9 @@ public class TDEngineService {
if (eventLevel != null) { if (eventLevel != null) {
sb.append(String.format(" and t.event_level = %d", eventLevel)); sb.append(String.format(" and t.event_level = %d", eventLevel));
} }
if (firstTriggeredCode != null){
sb.append(String.format(" and t.first_triggered_code = %d", firstTriggeredCode));
}
if (!CollectionUtils.isEmpty(deviceCodeList)) { if (!CollectionUtils.isEmpty(deviceCodeList)) {
sb.append(" and t.device_code in ("); sb.append(" and t.device_code in (");
for (int i = 0; i < deviceCodeList.size(); i++) { for (int i = 0; i < deviceCodeList.size(); i++) {
@ -864,7 +874,7 @@ public class TDEngineService {
} }
if (pageSize != null) { if (pageSize != null) {
sb.append(" desc limit ").append(offset).append(",").append(pageSize); sb.append(" desc limit ").append(offset).append(",").append(pageSize);
total = getEventCount(eventLevel, startTime, endTime, deviceCodeList); total = getEventCount(eventLevel, startTime, endTime, deviceCodeList,firstTriggeredCode);
} }
log.debug(sb.toString()); log.debug(sb.toString());
@ -884,7 +894,11 @@ public class TDEngineService {
deviceEventInfo.setConfirmTime(rs.getLong("confirm_time")); deviceEventInfo.setConfirmTime(rs.getLong("confirm_time"));
deviceEventInfo.setDeviceCode(rs.getString("device_code")); deviceEventInfo.setDeviceCode(rs.getString("device_code"));
deviceEventInfo.setDeviceId(rs.getString("device_id")); deviceEventInfo.setDeviceId(rs.getString("device_id"));
DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheById(rs.getLong("device_id"));
deviceEventInfo.setDeviceName(rs.getString("device_name")); deviceEventInfo.setDeviceName(rs.getString("device_name"));
deviceEventInfo.setFirstTriggeredCode(rs.getInt("first_triggered_code"));
deviceEventInfo.setMadeinFactory(deviceInfoCache.getMadeinFactory());
deviceEventInfo.setModel(deviceInfoCache.getModel());
result.add(deviceEventInfo); result.add(deviceEventInfo);
} }
} catch (Exception e) { } catch (Exception e) {
@ -894,13 +908,16 @@ public class TDEngineService {
return PageDataInfo.build(result, total); return PageDataInfo.build(result, total);
} }
private Integer getEventCount(Integer eventLevel, Long startTime, Long endTime, List<String> deviceCodeList) { private Integer getEventCount(Integer eventLevel, Long startTime, Long endTime, List<String> deviceCodeList,Integer firstTriggeredCode) {
StringBuffer sb = new StringBuffer(2048); StringBuffer sb = new StringBuffer(2048);
sb.append("select count(t.*) as total from event_info t where "); sb.append("select count(t.*) as total from event_info t where ");
sb.append(String.format(" t.event_time >= %d and t.event_time < %d", startTime, endTime)); sb.append(String.format(" t.event_time >= %d and t.event_time < %d", startTime, endTime));
if (eventLevel != null) { if (eventLevel != null) {
sb.append(String.format(" and t.event_level = %d", eventLevel)); sb.append(String.format(" and t.event_level = %d", eventLevel));
} }
if (firstTriggeredCode != null){
sb.append(String.format(" and t.first_triggered_code = %d", firstTriggeredCode));
}
if (!CollectionUtils.isEmpty(deviceCodeList)) { if (!CollectionUtils.isEmpty(deviceCodeList)) {
sb.append(" and t.device_code in ("); sb.append(" and t.device_code in (");
for (int i = 0; i < deviceCodeList.size(); i++) { for (int i = 0; i < deviceCodeList.size(); i++) {

View File

@ -45,4 +45,9 @@ public class EventQueryParam
* pageSize * pageSize
*/ */
private Integer pageSize; private Integer pageSize;
/**
* 首次故障码
*/
private Integer firstTriggeredCode;
} }

View File

@ -34,7 +34,7 @@ public class EventServiceImpl implements EventService {
if (param.getPageNum() != null) { if (param.getPageNum() != null) {
offset = (param.getPageNum() - 1) * param.getPageSize(); offset = (param.getPageNum() - 1) * param.getPageSize();
} }
PageDataInfo<DeviceEventInfo> deviceEventInfos = tdEngineService.queryEvent(param.getEventLevel(), param.getStartTime(), param.getEndTime(), param.getDeviceCode(), param.getPageSize(), offset, param.getLimit()); PageDataInfo<DeviceEventInfo> deviceEventInfos = tdEngineService.queryEvent(param.getEventLevel(), param.getStartTime(), param.getEndTime(), param.getDeviceCode(), param.getPageSize(), offset, param.getLimit(),param.getFirstTriggeredCode());
return deviceEventInfos; return deviceEventInfos;
} }

View File

@ -358,6 +358,7 @@ public class NodeMessageServiceImpl extends TextWebSocketHandler implements Node
log.info("消息data转化deviceVo,{}", list); log.info("消息data转化deviceVo,{}", list);
for (DeviceEventVo item : list) { for (DeviceEventVo item : list) {
DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheById(Long.valueOf(item.getDeviceId())); DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheById(Long.valueOf(item.getDeviceId()));
Integer firstTriggeredCode = adminRedisTemplate.get(String.format("RT:%s:%s", item.getDeviceId(), "FirstTriggeredCode".toLowerCase()));
DeviceEventInfo deviceEventInfo = new DeviceEventInfo(); DeviceEventInfo deviceEventInfo = new DeviceEventInfo();
deviceEventInfo.setEventTime(item.getEventTime()); deviceEventInfo.setEventTime(item.getEventTime());
deviceEventInfo.setEventId(IdWorker.getId()); deviceEventInfo.setEventId(IdWorker.getId());
@ -365,6 +366,7 @@ public class NodeMessageServiceImpl extends TextWebSocketHandler implements Node
deviceEventInfo.setDeviceId(item.getDeviceId()); deviceEventInfo.setDeviceId(item.getDeviceId());
deviceEventInfo.setDeviceName(deviceInfoCache.getDeviceName()); deviceEventInfo.setDeviceName(deviceInfoCache.getDeviceName());
deviceEventInfo.setDeviceCode(deviceInfoCache.getDeviceCode()); deviceEventInfo.setDeviceCode(deviceInfoCache.getDeviceCode());
deviceEventInfo.setFirstTriggeredCode(firstTriggeredCode);
String eventType = getEventType(item.getEventType()); String eventType = getEventType(item.getEventType());
String model = dataService.deviceModelMap.get(item.getDeviceId()); String model = dataService.deviceModelMap.get(item.getDeviceId());
if (StringUtils.isEmpty(model)) { if (StringUtils.isEmpty(model)) {