This commit is contained in:
谷成伟 2024-12-20 14:00:32 +08:00
commit ff933c877c
7 changed files with 51 additions and 5 deletions

View File

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

View File

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

View File

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

View File

@ -1,8 +1,11 @@
package com.das.modules.data.service;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
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.RTValue;
import com.das.modules.equipment.domain.vo.IotModelFieldVo;
@ -12,6 +15,7 @@ import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import jakarta.annotation.PreDestroy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
@ -45,6 +49,8 @@ public class TDEngineService {
@Value("${tdengine.batch-size:10000}")
private int batchSize;
@Autowired
private CacheService cacheService;
public void init() {
if (hikariDataSource == null) {
HikariConfig config = new HikariConfig();
@ -256,7 +262,7 @@ public class TDEngineService {
});
//初始化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 {
pstmt.executeUpdate(eventInfo);
} catch (SQLException ex) {
@ -414,6 +420,8 @@ public class TDEngineService {
sb.append(dv.getConfirmAccount());
sb.append(",");
sb.append(dv.getConfirmTime());
sb.append(",");
sb.append(dv.getFirstTriggeredCode());
sb.append(")");
}
try {
@ -833,7 +841,7 @@ public class TDEngineService {
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<>();
StringBuffer sb = new StringBuffer(2048);
Integer total = 0;
@ -842,6 +850,9 @@ public class TDEngineService {
if (eventLevel != null) {
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)) {
sb.append(" and t.device_code in (");
for (int i = 0; i < deviceCodeList.size(); i++) {
@ -864,7 +875,7 @@ public class TDEngineService {
}
if (pageSize != null) {
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());
@ -884,7 +895,11 @@ public class TDEngineService {
deviceEventInfo.setConfirmTime(rs.getLong("confirm_time"));
deviceEventInfo.setDeviceCode(rs.getString("device_code"));
deviceEventInfo.setDeviceId(rs.getString("device_id"));
DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheById(rs.getLong("device_id"));
deviceEventInfo.setDeviceName(rs.getString("device_name"));
deviceEventInfo.setFirstTriggeredCode(ObjectUtil.isEmpty(rs.getString("first_triggered_code")) ? null : Integer.valueOf(rs.getString("first_triggered_code")));
deviceEventInfo.setMadeinFactory(deviceInfoCache.getMadeinFactory());
deviceEventInfo.setModel(deviceInfoCache.getModel());
result.add(deviceEventInfo);
}
} catch (Exception e) {
@ -894,13 +909,16 @@ public class TDEngineService {
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);
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));
if (eventLevel != null) {
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)) {
sb.append(" and t.device_code in (");
for (int i = 0; i < deviceCodeList.size(); i++) {

View File

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

View File

@ -34,7 +34,7 @@ public class EventServiceImpl implements EventService {
if (param.getPageNum() != null) {
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;
}

View File

@ -358,6 +358,7 @@ public class NodeMessageServiceImpl extends TextWebSocketHandler implements Node
log.info("消息data转化deviceVo,{}", list);
for (DeviceEventVo item : list) {
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.setEventTime(item.getEventTime());
deviceEventInfo.setEventId(IdWorker.getId());
@ -365,6 +366,7 @@ public class NodeMessageServiceImpl extends TextWebSocketHandler implements Node
deviceEventInfo.setDeviceId(item.getDeviceId());
deviceEventInfo.setDeviceName(deviceInfoCache.getDeviceName());
deviceEventInfo.setDeviceCode(deviceInfoCache.getDeviceCode());
deviceEventInfo.setFirstTriggeredCode(firstTriggeredCode);
String eventType = getEventType(item.getEventType());
String model = dataService.deviceModelMap.get(item.getDeviceId());
if (StringUtils.isEmpty(model)) {