物模型,物模型属性缓存刷新,deviceEventCommand修改

This commit is contained in:
huguanghan 2025-01-08 16:15:02 +08:00
parent 1baa3f77c2
commit 1b6ef3cc28
5 changed files with 82 additions and 12 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;
@Data @Data
@ -10,6 +11,8 @@ public class IotFieldInfoCache {
private Integer porder; private Integer porder;
private Integer highspeed; private Integer highspeed;
private String datatype; private String datatype;
private Integer level;
private String stateDesc;
public boolean isHighSpeed() { public boolean isHighSpeed() {
return highspeed.equals(1) && attributeType.equals(138); return highspeed.equals(1) && attributeType.equals(138);

View File

@ -1,7 +1,16 @@
package com.das.modules.cache.service; package com.das.modules.cache.service;
import com.das.modules.cache.domain.IotFieldInfoCache;
import com.das.modules.equipment.entity.SysIotModel;
import com.das.modules.equipment.entity.SysIotModelField;
public interface IotModelCache { public interface IotModelCache {
public boolean isHighSpeed(Long modelId, String attr); public boolean isHighSpeed(Long modelId, String attr);
public boolean isLowSpeed(Long modelId, String attr); public boolean isLowSpeed(Long modelId, String attr);
public boolean isCalculate(Long modelId, String attr); public boolean isCalculate(Long modelId, String attr);
void refreshIotFieldCache(SysIotModelField sysIotModelField);
void removeIotFieldCache(Long modelId, String attributeCode);
void refreshIotModelInfoIdMap(SysIotModel sysIotModel);
void removeIotModelInfoIdMap(Long modelId);
IotFieldInfoCache getIotFiledCache(Long modelId,String attr);
} }

View File

@ -3,6 +3,8 @@ package com.das.modules.cache.service.impl;
import com.das.modules.cache.domain.IotFieldInfoCache; import com.das.modules.cache.domain.IotFieldInfoCache;
import com.das.modules.cache.domain.IotModelInfoCache; import com.das.modules.cache.domain.IotModelInfoCache;
import com.das.modules.cache.service.IotModelCache; import com.das.modules.cache.service.IotModelCache;
import com.das.modules.equipment.entity.SysIotModel;
import com.das.modules.equipment.entity.SysIotModelField;
import com.das.modules.equipment.mapper.SysIotModelMapper; import com.das.modules.equipment.mapper.SysIotModelMapper;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy; import jakarta.annotation.PreDestroy;
@ -38,6 +40,8 @@ public class IotModelCacheImpl implements IotModelCache {
fieldInfoCache.setAttributeType(item.getAttributeType()); fieldInfoCache.setAttributeType(item.getAttributeType());
fieldInfoCache.setHighspeed(item.getHighSpeed()); fieldInfoCache.setHighspeed(item.getHighSpeed());
fieldInfoCache.setDatatype(item.getDataType()); fieldInfoCache.setDatatype(item.getDataType());
fieldInfoCache.setLevel(item.getLevel());
fieldInfoCache.setStateDesc(item.getStateDesc());
iotFieldsMap.put(String.format("%d_%s", k, item.getAttributeCode()), fieldInfoCache); iotFieldsMap.put(String.format("%d_%s", k, item.getAttributeCode()), fieldInfoCache);
}); });
}); });
@ -75,4 +79,42 @@ public class IotModelCacheImpl implements IotModelCache {
} }
return fieldInfoCache.isCalculate(); return fieldInfoCache.isCalculate();
} }
@Override
public void refreshIotFieldCache(SysIotModelField sysIotModelField) {
IotFieldInfoCache fieldInfoCache = new IotFieldInfoCache();
fieldInfoCache.setAttributeCode(sysIotModelField.getAttributeCode());
fieldInfoCache.setPorder(sysIotModelField.getPorder());
fieldInfoCache.setAttributeName(sysIotModelField.getAttributeName());
fieldInfoCache.setAttributeType(sysIotModelField.getAttributeType());
fieldInfoCache.setHighspeed(sysIotModelField.getHighSpeed());
fieldInfoCache.setDatatype(sysIotModelField.getDataType());
fieldInfoCache.setLevel(sysIotModelField.getLevel());
fieldInfoCache.setStateDesc(sysIotModelField.getStateDesc());
iotFieldsMap.put(String.format("%d_%s", sysIotModelField.getIotModelId(), sysIotModelField.getAttributeCode()), fieldInfoCache);
}
@Override
public void removeIotFieldCache(Long modelId, String attributeCode){
iotFieldsMap.remove(String.format("%d_%s", modelId, attributeCode));
}
@Override
public void refreshIotModelInfoIdMap(SysIotModel sysIotModel){
IotModelInfoCache info = new IotModelInfoCache();
info.setIotModelId(sysIotModel.getId());
info.setIodModelCode(sysIotModel.getIotModelCode());
iotModelInfoIdMap.put(sysIotModel.getId(), info);
}
@Override
public void removeIotModelInfoIdMap(Long modelId) {
iotModelInfoIdMap.remove(modelId);
}
@Override
public IotFieldInfoCache getIotFiledCache(Long modelId, String attr) {
return iotFieldsMap.get(String.format("%d_%s", modelId, attr));
}
} }

View File

@ -10,6 +10,7 @@ import com.das.common.config.SessionUtil;
import com.das.common.exceptions.ServiceException; import com.das.common.exceptions.ServiceException;
import com.das.common.utils.*; import com.das.common.utils.*;
import com.das.modules.auth.domain.vo.SysUserVo; import com.das.modules.auth.domain.vo.SysUserVo;
import com.das.modules.cache.service.CacheService;
import com.das.modules.data.service.impl.DataServiceImpl; import com.das.modules.data.service.impl.DataServiceImpl;
import com.das.modules.equipment.domain.dto.SysIotModelDto; import com.das.modules.equipment.domain.dto.SysIotModelDto;
import com.das.modules.equipment.domain.dto.SysIotModelFieldDto; import com.das.modules.equipment.domain.dto.SysIotModelFieldDto;
@ -65,6 +66,9 @@ public class SysIotModelServiceImpl implements SysIotModelService {
@Autowired @Autowired
SysRecordLogService sysRecordLogService; SysRecordLogService sysRecordLogService;
@Autowired
CacheService cacheService;
public SysIotModelVo creatSysIotModel(SysIotModelDto sysIotModelDto) { public SysIotModelVo creatSysIotModel(SysIotModelDto sysIotModelDto) {
SysIotModel sysIotModel = new SysIotModel(); SysIotModel sysIotModel = new SysIotModel();
BeanCopyUtils.copy(sysIotModelDto, sysIotModel); BeanCopyUtils.copy(sysIotModelDto, sysIotModel);
@ -81,7 +85,9 @@ public class SysIotModelServiceImpl implements SysIotModelService {
sysIotModel.setRevision(1); sysIotModel.setRevision(1);
sysIotModelMapper.insert(sysIotModel); sysIotModelMapper.insert(sysIotModel);
//刷新缓存
addModelCache(sysIotModel); addModelCache(sysIotModel);
cacheService.getIotModelCache().refreshIotModelInfoIdMap(sysIotModel);
SysIotModelVo sysIotModelVo = new SysIotModelVo(); SysIotModelVo sysIotModelVo = new SysIotModelVo();
BeanCopyUtils.copy(sysIotModel, sysIotModelVo); BeanCopyUtils.copy(sysIotModel, sysIotModelVo);
sysIotModelVo.setIotModelCode(sysIotModelDto.getIotModelCode()); sysIotModelVo.setIotModelCode(sysIotModelDto.getIotModelCode());
@ -119,6 +125,8 @@ public class SysIotModelServiceImpl implements SysIotModelService {
throw new RuntimeException("该物模型下面有类型,不能删除"); throw new RuntimeException("该物模型下面有类型,不能删除");
} }
sysIotModelMapper.deleteById(sysIotModelDto.getId()); sysIotModelMapper.deleteById(sysIotModelDto.getId());
//刷新缓存
cacheService.getIotModelCache().removeIotModelInfoIdMap(sysIotModelDto.getId());
deleteModelCache(sysIotModelDto.getId()); deleteModelCache(sysIotModelDto.getId());
} }
@ -184,6 +192,7 @@ public class SysIotModelServiceImpl implements SysIotModelService {
sysIotModelFieldMapper.insert(sysIotModelField); sysIotModelFieldMapper.insert(sysIotModelField);
//新增物模型属性缓存 //新增物模型属性缓存
cacheService.getIotModelCache().refreshIotFieldCache(sysIotModelField);
addModelFieldCache(sysIotModelField); addModelFieldCache(sysIotModelField);
SysIotModelFieldVo sysIotModelFieldVo = new SysIotModelFieldVo(); SysIotModelFieldVo sysIotModelFieldVo = new SysIotModelFieldVo();
BeanCopyUtils.copy(sysIotModelField, sysIotModelFieldVo); BeanCopyUtils.copy(sysIotModelField, sysIotModelFieldVo);
@ -216,6 +225,7 @@ public class SysIotModelServiceImpl implements SysIotModelService {
if (!oldSysIotField.getAttributeCode().equals(sysIotModelField.getAttributeCode()) || !oldSysIotField.getDataType().equals(sysIotModelField.getDataType()) || Objects.equals(oldSysIotField.getHighSpeed(), sysIotModelField.getHighSpeed())) { if (!oldSysIotField.getAttributeCode().equals(sysIotModelField.getAttributeCode()) || !oldSysIotField.getDataType().equals(sysIotModelField.getDataType()) || Objects.equals(oldSysIotField.getHighSpeed(), sysIotModelField.getHighSpeed())) {
//更新td表结构 //更新td表结构
updateTDStableOrColumn(sysIotModelField, oldSysIotField); updateTDStableOrColumn(sysIotModelField, oldSysIotField);
cacheService.getIotModelCache().refreshIotFieldCache(sysIotModelField);
updateModelFieldCache(sysIotModelField, oldSysIotField); updateModelFieldCache(sysIotModelField, oldSysIotField);
} }
sysIotModelFieldMapper.updateById(sysIotModelField); sysIotModelFieldMapper.updateById(sysIotModelField);
@ -232,6 +242,7 @@ public class SysIotModelServiceImpl implements SysIotModelService {
sysIotModelFieldMapper.deleteById(sysIotModelFieldDto.getId()); sysIotModelFieldMapper.deleteById(sysIotModelFieldDto.getId());
//删除物模型属性缓存 //删除物模型属性缓存
cacheService.getIotModelCache().removeIotFieldCache(sysIotModelField.getIotModelId(), sysIotModelField.getAttributeCode());
deleteModelFieldCache(sysIotModelField); deleteModelFieldCache(sysIotModelField);
} }
@ -488,6 +499,10 @@ public class SysIotModelServiceImpl implements SysIotModelService {
} }
//新增pg数据库 //新增pg数据库
sysIotModelFieldMapper.insertBatch(sysIotModelFieldList); sysIotModelFieldMapper.insertBatch(sysIotModelFieldList);
//刷新缓存
for (SysIotModelField item : sysIotModelFieldList){
cacheService.getIotModelCache().refreshIotFieldCache(item);
}
} }
if (CollectionUtils.isNotEmpty(updateSysIotModelFieldList)) { if (CollectionUtils.isNotEmpty(updateSysIotModelFieldList)) {
for (SysIotModelField item : updateSysIotModelFieldList) { for (SysIotModelField item : updateSysIotModelFieldList) {
@ -496,6 +511,7 @@ public class SysIotModelServiceImpl implements SysIotModelService {
if (!oldSysIotField.getAttributeCode().equals(item.getAttributeCode()) && oldSysIotField.getDataType().equals(item.getDataType()) && Objects.equals(oldSysIotField.getHighSpeed(), item.getHighSpeed())) { if (!oldSysIotField.getAttributeCode().equals(item.getAttributeCode()) && oldSysIotField.getDataType().equals(item.getDataType()) && Objects.equals(oldSysIotField.getHighSpeed(), item.getHighSpeed())) {
//更新td表结构 //更新td表结构
updateTDStableOrColumn(item, oldSysIotField); updateTDStableOrColumn(item, oldSysIotField);
cacheService.getIotModelCache().refreshIotFieldCache(item);
updateModelFieldCache(item, oldSysIotField); updateModelFieldCache(item, oldSysIotField);
} }
} }
@ -509,6 +525,7 @@ public class SysIotModelServiceImpl implements SysIotModelService {
for (SysIotModelField item : delSysIotModelFieldList) { for (SysIotModelField item : delSysIotModelFieldList) {
deleteTDStableOrColumn(item); deleteTDStableOrColumn(item);
sysIotModelFieldMapper.deleteById(item); sysIotModelFieldMapper.deleteById(item);
cacheService.getIotModelCache().removeIotFieldCache(item.getIotModelId(), item.getAttributeCode());
deleteModelFieldCache(item); deleteModelFieldCache(item);
} }
} }

View File

@ -1,17 +1,17 @@
package com.das.modules.node.command; package com.das.modules.node.command;
import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.das.common.exceptions.ServiceException;
import com.das.common.utils.AdminRedisTemplate; import com.das.common.utils.AdminRedisTemplate;
import com.das.common.utils.StringUtils; import com.das.common.utils.StringUtils;
import com.das.modules.cache.domain.DeviceInfoCache; import com.das.modules.cache.domain.DeviceInfoCache;
import com.das.modules.cache.domain.IotFieldInfoCache;
import com.das.modules.cache.service.CacheService; 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.service.TDEngineService; import com.das.modules.data.service.TDEngineService;
import com.das.modules.data.service.impl.DataServiceImpl;
import com.das.modules.node.constant.NodeConstant; import com.das.modules.node.constant.NodeConstant;
import com.das.modules.node.domain.bo.TerminalMessage; import com.das.modules.node.domain.bo.TerminalMessage;
import com.das.modules.node.domain.vo.DeviceEventVo; import com.das.modules.node.domain.vo.DeviceEventVo;
import com.das.modules.node.service.NodeMessageService;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@ -29,9 +29,6 @@ public class DeviceEventCommand implements BaseCommand{
@Autowired @Autowired
AdminRedisTemplate adminRedisTemplate; AdminRedisTemplate adminRedisTemplate;
@Autowired
private DataServiceImpl dataService;
@Autowired @Autowired
TDEngineService tdEngineService; TDEngineService tdEngineService;
@ -72,18 +69,20 @@ public class DeviceEventCommand implements BaseCommand{
deviceEventInfo.setDeviceCode(deviceInfoCache.getDeviceCode()); deviceEventInfo.setDeviceCode(deviceInfoCache.getDeviceCode());
deviceEventInfo.setFirstTriggeredCode(firstTriggeredCode); deviceEventInfo.setFirstTriggeredCode(firstTriggeredCode);
String eventType = getEventType(item.getEventType()); String eventType = getEventType(item.getEventType());
String model = dataService.deviceModelMap.get(item.getDeviceId()); Long iotModelId = deviceInfoCache.getIotModelId();
if (StringUtils.isEmpty(model)) { if (iotModelId == null) {
log.debug("未查询到物模型code设备id{}", item.getDeviceId()); log.debug("未查询到物模型code设备id{}", item.getDeviceId());
} }
String fieldName = dataService.fieldCodeNameMap.get(model).get(item.getAttrCode()); IotFieldInfoCache iotFiledCache = cacheService.getIotModelCache().getIotFiledCache(iotModelId, item.getAttrCode());
if (StringUtils.isEmpty(fieldName)) { if (iotFiledCache == null) {
log.debug("未查询到物模型属性code设备id{}", item.getDeviceId()); log.debug("未查询到物模型属性设备id{},属性AttrCode{}", item.getDeviceId(),item.getAttrCode());
throw new ServiceException("未查询到物模型属性设备id"+item.getDeviceId()+",属性AttrCode"+item.getAttrCode());
} }
String fieldName = iotFiledCache.getAttributeName();
deviceEventInfo.setEventType(item.getEventType()); deviceEventInfo.setEventType(item.getEventType());
deviceEventInfo.setConfirmed(0); deviceEventInfo.setConfirmed(0);
if (!StringUtils.isEmpty(eventType) && eventType.equals("遥信变位")) { if (!StringUtils.isEmpty(eventType) && eventType.equals("遥信变位")) {
String stateDesc = dataService.stateDescMap.get(model).get(item.getAttrCode()); String stateDesc = iotFiledCache.getStateDesc();
if (item.getAttrValue().equals(0)) { if (item.getAttrValue().equals(0)) {
deviceEventInfo.setEventText(item.getAttrCode() + fieldName + " 复归"); deviceEventInfo.setEventText(item.getAttrCode() + fieldName + " 复归");
if (StringUtils.isNotEmpty(stateDesc)) { if (StringUtils.isNotEmpty(stateDesc)) {
@ -97,7 +96,7 @@ public class DeviceEventCommand implements BaseCommand{
List<String> descList = Arrays.stream(stateDesc.split("\\|")).toList(); List<String> descList = Arrays.stream(stateDesc.split("\\|")).toList();
deviceEventInfo.setEventText(item.getAttrCode() + fieldName + descList.get(1)); deviceEventInfo.setEventText(item.getAttrCode() + fieldName + descList.get(1));
} }
Integer level = dataService.eventLevelMap.get(model).get(item.getAttrCode()); Integer level = iotFiledCache.getLevel();
log.debug("level:{}", level); log.debug("level:{}", level);
log.debug("fieldname{}", fieldName); log.debug("fieldname{}", fieldName);
deviceEventInfo.setEventLevel(level == null ? 0 : level); deviceEventInfo.setEventLevel(level == null ? 0 : level);