Search in sources :

Example 1 with RuleNodeState

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();
    }
}
Also used : DeviceId(org.thingsboard.server.common.data.id.DeviceId) PageLink(org.thingsboard.server.common.data.page.PageLink) RuleNodeState(org.thingsboard.server.common.data.rule.RuleNodeState)

Example 2 with RuleNodeState

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;
}
Also used : RuleNodeStateId(org.thingsboard.server.common.data.id.RuleNodeStateId) RuleNodeState(org.thingsboard.server.common.data.rule.RuleNodeState) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

Example 3 with RuleNodeState

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;
        }
    }
}
Also used : DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) RuleNodeState(org.thingsboard.server.common.data.rule.RuleNodeState) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException)

Aggregations

RuleNodeState (org.thingsboard.server.common.data.rule.RuleNodeState)3 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)1 DeviceId (org.thingsboard.server.common.data.id.DeviceId)1 RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)1 RuleNodeStateId (org.thingsboard.server.common.data.id.RuleNodeStateId)1 PageLink (org.thingsboard.server.common.data.page.PageLink)1 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)1