use of org.thingsboard.server.common.data.rule.RuleNodeState in project thingsboard by thingsboard.
the class TbDeviceProfileNode method init.
@Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
this.config = TbNodeUtils.convert(configuration, TbDeviceProfileNodeConfiguration.class);
this.cache = ctx.getDeviceProfileCache();
this.ctx = ctx;
scheduleAlarmHarvesting(ctx, null);
ctx.addDeviceProfileListeners(this::onProfileUpdate, this::onDeviceUpdate);
if (config.isFetchAlarmRulesStateOnStart()) {
log.info("[{}] Fetching alarm rule state", ctx.getSelfId());
int fetchCount = 0;
PageLink pageLink = new PageLink(1024);
while (true) {
PageData<RuleNodeState> states = ctx.findRuleNodeStates(pageLink);
if (!states.getData().isEmpty()) {
for (RuleNodeState rns : states.getData()) {
fetchCount++;
if (rns.getEntityId().getEntityType().equals(EntityType.DEVICE) && ctx.isLocalEntity(rns.getEntityId())) {
getOrCreateDeviceState(ctx, new DeviceId(rns.getEntityId().getId()), rns);
}
}
}
if (!states.hasNext()) {
break;
} else {
pageLink = pageLink.nextPageLink();
}
}
log.info("[{}] Fetched alarm rule state for {} entities", ctx.getSelfId(), fetchCount);
}
if (!config.isPersistAlarmRulesState() && ctx.isLocalEntity(ctx.getSelfId())) {
log.debug("[{}] Going to cleanup rule node states", ctx.getSelfId());
ctx.clearRuleNodeStates();
}
}
use of org.thingsboard.server.common.data.rule.RuleNodeState in project thingsboard by thingsboard.
the class RuleNodeStateEntity method toData.
@Override
public RuleNodeState toData() {
RuleNodeState ruleNode = new RuleNodeState(new RuleNodeStateId(this.getUuid()));
ruleNode.setCreatedTime(createdTime);
ruleNode.setRuleNodeId(new RuleNodeId(ruleNodeId));
ruleNode.setEntityId(EntityIdFactory.getByTypeAndUuid(entityType, entityId));
ruleNode.setStateData(stateData);
return ruleNode;
}
use of org.thingsboard.server.common.data.rule.RuleNodeState in project thingsboard by thingsboard.
the class BaseRuleNodeStateService method saveOrUpdate.
public RuleNodeState saveOrUpdate(TenantId tenantId, RuleNodeState ruleNodeState, boolean update) {
try {
if (update) {
RuleNodeState old = ruleNodeStateDao.findByRuleNodeIdAndEntityId(ruleNodeState.getRuleNodeId().getId(), ruleNodeState.getEntityId().getId());
if (old != null && !old.getId().equals(ruleNodeState.getId())) {
ruleNodeState.setId(old.getId());
ruleNodeState.setCreatedTime(old.getCreatedTime());
}
}
return ruleNodeStateDao.save(tenantId, ruleNodeState);
} catch (Exception t) {
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("rule_node_state_unq_key")) {
if (!update) {
return saveOrUpdate(tenantId, ruleNodeState, true);
} else {
throw new DataValidationException("Rule node state for such rule node id and entity id already exists!");
}
} else {
throw t;
}
}
}
Aggregations