Browse Source

Bug fixes for Device Profile node

pull/3688/head
Andrii Shvaika 6 years ago
parent
commit
78ac876103
  1. 8
      application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java
  2. 2
      common/dao-api/src/main/java/org/thingsboard/server/dao/rule/RuleNodeStateService.java
  3. 3
      common/data/src/main/java/org/thingsboard/server/common/data/device/profile/DeviceProfileAlarm.java
  4. 14
      dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleNodeStateService.java
  5. 2
      dao/src/main/java/org/thingsboard/server/dao/rule/RuleNodeStateDao.java
  6. 6
      dao/src/main/java/org/thingsboard/server/dao/sql/rule/JpaRuleNodeStateDao.java
  7. 1
      dao/src/main/java/org/thingsboard/server/dao/sql/rule/RuleNodeStateRepository.java
  8. 2
      rule-engine/rule-engine-api/src/main/java/org/thingsboard/rule/engine/api/TbContext.java
  9. 15
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/TbDeviceProfileNode.java
  10. 3
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/profile/TbDeviceProfileNodeTest.java

8
application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java

@ -480,6 +480,14 @@ class DefaultTbContext implements TbContext {
mainCtx.getRuleNodeStateService().removeByRuleNodeId(getTenantId(), getSelfId()); mainCtx.getRuleNodeStateService().removeByRuleNodeId(getTenantId(), getSelfId());
} }
@Override
public void removeRuleNodeStateForEntity(EntityId entityId) {
if (log.isDebugEnabled()) {
log.debug("[{}][{}][{}] Remove Rule Node State for entity.", getTenantId(), getSelfId(), entityId);
}
mainCtx.getRuleNodeStateService().removeByRuleNodeIdAndEntityId(getTenantId(), getSelfId(), entityId);
}
@Override @Override
public void addDeviceProfileListeners(Consumer<DeviceProfile> profileListener, BiConsumer<DeviceId, DeviceProfile> deviceListener) { public void addDeviceProfileListeners(Consumer<DeviceProfile> profileListener, BiConsumer<DeviceId, DeviceProfile> deviceListener) {
mainCtx.getDeviceProfileCache().addListener(getTenantId(), getSelfId(), profileListener, deviceListener); mainCtx.getDeviceProfileCache().addListener(getTenantId(), getSelfId(), profileListener, deviceListener);

2
common/dao-api/src/main/java/org/thingsboard/server/dao/rule/RuleNodeStateService.java

@ -31,4 +31,6 @@ public interface RuleNodeStateService {
RuleNodeState save(TenantId tenantId, RuleNodeState ruleNodeState); RuleNodeState save(TenantId tenantId, RuleNodeState ruleNodeState);
void removeByRuleNodeId(TenantId tenantId, RuleNodeId selfId); void removeByRuleNodeId(TenantId tenantId, RuleNodeId selfId);
void removeByRuleNodeIdAndEntityId(TenantId tenantId, RuleNodeId selfId, EntityId entityId);
} }

3
common/data/src/main/java/org/thingsboard/server/common/data/device/profile/DeviceProfileAlarm.java

@ -18,6 +18,7 @@ package org.thingsboard.server.common.data.device.profile;
import lombok.Data; import lombok.Data;
import org.thingsboard.server.common.data.alarm.AlarmSeverity; import org.thingsboard.server.common.data.alarm.AlarmSeverity;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -27,7 +28,7 @@ public class DeviceProfileAlarm {
private String id; private String id;
private String alarmType; private String alarmType;
private Map<AlarmSeverity, AlarmRule> createRules; private LinkedHashMap<AlarmSeverity, AlarmRule> createRules;
private AlarmRule clearRule; private AlarmRule clearRule;
// Hidden in advanced settings // Hidden in advanced settings

14
dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleNodeStateService.java

@ -79,6 +79,20 @@ public class BaseRuleNodeStateService extends AbstractEntityService implements R
ruleNodeStateDao.removeByRuleNodeId(ruleNodeId.getId()); ruleNodeStateDao.removeByRuleNodeId(ruleNodeId.getId());
} }
@Override
public void removeByRuleNodeIdAndEntityId(TenantId tenantId, RuleNodeId ruleNodeId, EntityId entityId) {
if (tenantId == null) {
throw new DataValidationException("Tenant id should be specified!.");
}
if (ruleNodeId == null) {
throw new DataValidationException("Rule node id should be specified!.");
}
if (entityId == null) {
throw new DataValidationException("Entity id should be specified!.");
}
ruleNodeStateDao.removeByRuleNodeIdAndEntityId(ruleNodeId.getId(), entityId.getId());
}
public RuleNodeState saveOrUpdate(TenantId tenantId, RuleNodeState ruleNodeState, boolean update) { public RuleNodeState saveOrUpdate(TenantId tenantId, RuleNodeState ruleNodeState, boolean update) {
try { try {
if (update) { if (update) {

2
dao/src/main/java/org/thingsboard/server/dao/rule/RuleNodeStateDao.java

@ -35,4 +35,6 @@ public interface RuleNodeStateDao extends Dao<RuleNodeState> {
RuleNodeState findByRuleNodeIdAndEntityId(UUID ruleNodeId, UUID entityId); RuleNodeState findByRuleNodeIdAndEntityId(UUID ruleNodeId, UUID entityId);
void removeByRuleNodeId(UUID ruleNodeId); void removeByRuleNodeId(UUID ruleNodeId);
void removeByRuleNodeIdAndEntityId(UUID ruleNodeId, UUID entityId);
} }

6
dao/src/main/java/org/thingsboard/server/dao/sql/rule/JpaRuleNodeStateDao.java

@ -63,4 +63,10 @@ public class JpaRuleNodeStateDao extends JpaAbstractDao<RuleNodeStateEntity, Rul
public void removeByRuleNodeId(UUID ruleNodeId) { public void removeByRuleNodeId(UUID ruleNodeId) {
ruleNodeStateRepository.removeByRuleNodeId(ruleNodeId); ruleNodeStateRepository.removeByRuleNodeId(ruleNodeId);
} }
@Transactional
@Override
public void removeByRuleNodeIdAndEntityId(UUID ruleNodeId, UUID entityId) {
ruleNodeStateRepository.removeByRuleNodeIdAndEntityId(ruleNodeId, entityId);
}
} }

1
dao/src/main/java/org/thingsboard/server/dao/sql/rule/RuleNodeStateRepository.java

@ -36,4 +36,5 @@ public interface RuleNodeStateRepository extends PagingAndSortingRepository<Rule
void removeByRuleNodeId(@Param("ruleNodeId") UUID ruleNodeId); void removeByRuleNodeId(@Param("ruleNodeId") UUID ruleNodeId);
void removeByRuleNodeIdAndEntityId(@Param("ruleNodeId") UUID ruleNodeId, @Param("entityId") UUID entityId);
} }

2
rule-engine/rule-engine-api/src/main/java/org/thingsboard/rule/engine/api/TbContext.java

@ -223,6 +223,8 @@ public interface TbContext {
RuleNodeState findRuleNodeStateForEntity(EntityId entityId); RuleNodeState findRuleNodeStateForEntity(EntityId entityId);
void removeRuleNodeStateForEntity(EntityId entityId);
RuleNodeState saveRuleNodeState(RuleNodeState state); RuleNodeState saveRuleNodeState(RuleNodeState state);
void clearRuleNodeStates(); void clearRuleNodeStates();

15
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/TbDeviceProfileNode.java

@ -117,7 +117,7 @@ public class TbDeviceProfileNode implements TbNode {
if (data.has("profileId")) { if (data.has("profileId")) {
invalidateDeviceProfileCache(deviceId, new DeviceProfileId(UUID.fromString(data.get("deviceProfileId").asText()))); invalidateDeviceProfileCache(deviceId, new DeviceProfileId(UUID.fromString(data.get("deviceProfileId").asText())));
} else { } else {
deviceStates.remove(deviceId); removeDeviceState(deviceId);
} }
} else { } else {
@ -126,7 +126,7 @@ public class TbDeviceProfileNode implements TbNode {
if (msg.getType().equals(DataConstants.ENTITY_UPDATED)) { if (msg.getType().equals(DataConstants.ENTITY_UPDATED)) {
invalidateDeviceProfileCache(deviceId, msg.getData()); invalidateDeviceProfileCache(deviceId, msg.getData());
} else if (msg.getType().equals(DataConstants.ENTITY_DELETED)) { } else if (msg.getType().equals(DataConstants.ENTITY_DELETED)) {
deviceStates.remove(deviceId); removeDeviceState(deviceId);
} else { } else {
DeviceState deviceState = getOrCreateDeviceState(ctx, deviceId, null); DeviceState deviceState = getOrCreateDeviceState(ctx, deviceId, null);
if (deviceState != null) { if (deviceState != null) {
@ -209,7 +209,7 @@ public class TbDeviceProfileNode implements TbNode {
DeviceProfileId currentProfileId = deviceState.getProfileId(); DeviceProfileId currentProfileId = deviceState.getProfileId();
Device device = JacksonUtil.fromString(deviceJson, Device.class); Device device = JacksonUtil.fromString(deviceJson, Device.class);
if (!currentProfileId.equals(device.getDeviceProfileId())) { if (!currentProfileId.equals(device.getDeviceProfileId())) {
deviceStates.remove(deviceId); removeDeviceState(deviceId);
} }
} }
} }
@ -218,8 +218,15 @@ public class TbDeviceProfileNode implements TbNode {
DeviceState deviceState = deviceStates.get(deviceId); DeviceState deviceState = deviceStates.get(deviceId);
if (deviceState != null) { if (deviceState != null) {
if (!deviceState.getProfileId().equals(deviceProfileId)) { if (!deviceState.getProfileId().equals(deviceProfileId)) {
deviceStates.remove(deviceId); removeDeviceState(deviceId);
} }
} }
} }
private void removeDeviceState(DeviceId deviceId) {
DeviceState state = deviceStates.remove(deviceId);
if (config.isPersistAlarmRulesState() && (state != null || !config.isFetchAlarmRulesStateOnStart())) {
ctx.removeRuleNodeStateForEntity(deviceId);
}
}
} }

3
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/profile/TbDeviceProfileNodeTest.java

@ -56,6 +56,7 @@ import org.thingsboard.server.dao.alarm.AlarmService;
import org.thingsboard.server.dao.timeseries.TimeseriesService; import org.thingsboard.server.dao.timeseries.TimeseriesService;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.UUID; import java.util.UUID;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@ -139,7 +140,7 @@ public class TbDeviceProfileNodeTest {
DeviceProfileAlarm dpa = new DeviceProfileAlarm(); DeviceProfileAlarm dpa = new DeviceProfileAlarm();
dpa.setId("highTemperatureAlarmID"); dpa.setId("highTemperatureAlarmID");
dpa.setAlarmType("highTemperatureAlarm"); dpa.setAlarmType("highTemperatureAlarm");
dpa.setCreateRules(Collections.singletonMap(AlarmSeverity.CRITICAL, alarmRule)); dpa.setCreateRules(new LinkedHashMap<>(Collections.singletonMap(AlarmSeverity.CRITICAL, alarmRule)));
KeyFilter lowTempFilter = new KeyFilter(); KeyFilter lowTempFilter = new KeyFilter();
lowTempFilter.setKey(new EntityKey(EntityKeyType.TIME_SERIES, "temperature")); lowTempFilter.setKey(new EntityKey(EntityKeyType.TIME_SERIES, "temperature"));

Loading…
Cancel
Save