Search in sources :

Example 1 with TbAlarmResult

use of org.thingsboard.rule.engine.action.TbAlarmResult in project thingsboard by thingsboard.

the class AlarmState method createOrClearAlarms.

public <T> boolean createOrClearAlarms(TbContext ctx, TbMsg msg, T data, SnapshotUpdate update, BiFunction<AlarmRuleState, T, AlarmEvalResult> evalFunction) {
    boolean stateUpdate = false;
    AlarmRuleState resultState = null;
    log.debug("[{}] processing update: {}", alarmDefinition.getId(), data);
    for (AlarmRuleState state : createRulesSortedBySeverityDesc) {
        if (!validateUpdate(update, state)) {
            log.debug("[{}][{}] Update is not valid for current rule state", alarmDefinition.getId(), state.getSeverity());
            continue;
        }
        AlarmEvalResult evalResult = evalFunction.apply(state, data);
        stateUpdate |= state.checkUpdate();
        if (AlarmEvalResult.TRUE.equals(evalResult)) {
            resultState = state;
            break;
        } else if (AlarmEvalResult.FALSE.equals(evalResult)) {
            stateUpdate = clearAlarmState(stateUpdate, state);
        }
    }
    if (resultState != null) {
        TbAlarmResult result = calculateAlarmResult(ctx, resultState);
        if (result != null) {
            pushMsg(ctx, msg, result, resultState);
        }
        stateUpdate = clearAlarmState(stateUpdate, clearState);
    } else if (currentAlarm != null && clearState != null) {
        if (!validateUpdate(update, clearState)) {
            log.debug("[{}] Update is not valid for current clear state", alarmDefinition.getId());
            return stateUpdate;
        }
        AlarmEvalResult evalResult = evalFunction.apply(clearState, data);
        if (AlarmEvalResult.TRUE.equals(evalResult)) {
            stateUpdate = clearAlarmState(stateUpdate, clearState);
            for (AlarmRuleState state : createRulesSortedBySeverityDesc) {
                stateUpdate = clearAlarmState(stateUpdate, state);
            }
            ListenableFuture<AlarmOperationResult> alarmClearOperationResult = ctx.getAlarmService().clearAlarmForResult(ctx.getTenantId(), currentAlarm.getId(), createDetails(clearState), System.currentTimeMillis());
            DonAsynchron.withCallback(alarmClearOperationResult, result -> {
                pushMsg(ctx, msg, new TbAlarmResult(false, false, true, result.getAlarm()), clearState);
            }, throwable -> {
                throw new RuntimeException(throwable);
            });
            currentAlarm = null;
        } else if (AlarmEvalResult.FALSE.equals(evalResult)) {
            stateUpdate = clearAlarmState(stateUpdate, clearState);
        }
    }
    return stateUpdate;
}
Also used : AlarmSeverity(org.thingsboard.server.common.data.alarm.AlarmSeverity) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TbMsg(org.thingsboard.server.common.msg.TbMsg) BiFunction(java.util.function.BiFunction) JacksonUtil(org.thingsboard.common.util.JacksonUtil) DonAsynchron(org.thingsboard.common.util.DonAsynchron) TbAlarmResult(org.thingsboard.rule.engine.action.TbAlarmResult) AlarmConditionKeyType(org.thingsboard.server.common.data.device.profile.AlarmConditionKeyType) TbContext(org.thingsboard.rule.engine.api.TbContext) StringUtils(org.apache.commons.lang3.StringUtils) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) ArrayList(java.util.ArrayList) DeviceProfileAlarm(org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm) EntityId(org.thingsboard.server.common.data.id.EntityId) JsonNode(com.fasterxml.jackson.databind.JsonNode) AlarmStatus(org.thingsboard.server.common.data.alarm.AlarmStatus) PersistedAlarmRuleState(org.thingsboard.rule.engine.profile.state.PersistedAlarmRuleState) DashboardId(org.thingsboard.server.common.data.id.DashboardId) DataConstants(org.thingsboard.server.common.data.DataConstants) PersistedAlarmState(org.thingsboard.rule.engine.profile.state.PersistedAlarmState) AlarmOperationResult(org.thingsboard.server.dao.alarm.AlarmOperationResult) Alarm(org.thingsboard.server.common.data.alarm.Alarm) ExecutionException(java.util.concurrent.ExecutionException) Slf4j(lombok.extern.slf4j.Slf4j) ServiceQueue(org.thingsboard.server.common.msg.queue.ServiceQueue) List(java.util.List) Data(lombok.Data) AlarmConditionSpecType(org.thingsboard.server.common.data.device.profile.AlarmConditionSpecType) Comparator(java.util.Comparator) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TbAlarmResult(org.thingsboard.rule.engine.action.TbAlarmResult) PersistedAlarmRuleState(org.thingsboard.rule.engine.profile.state.PersistedAlarmRuleState)

Example 2 with TbAlarmResult

use of org.thingsboard.rule.engine.action.TbAlarmResult in project thingsboard by thingsboard.

the class AlarmState method calculateAlarmResult.

private TbAlarmResult calculateAlarmResult(TbContext ctx, AlarmRuleState ruleState) {
    AlarmSeverity severity = ruleState.getSeverity();
    if (currentAlarm != null) {
        // TODO: In some extremely rare cases, we might miss the event of alarm clear (If one use in-mem queue and restarted the server) or (if one manipulated the rule chain).
        // Maybe we should fetch alarm every time?
        currentAlarm.setEndTs(System.currentTimeMillis());
        AlarmSeverity oldSeverity = currentAlarm.getSeverity();
        // Skip update if severity is decreased.
        if (severity.ordinal() <= oldSeverity.ordinal()) {
            currentAlarm.setDetails(createDetails(ruleState));
            if (!oldSeverity.equals(severity)) {
                currentAlarm.setSeverity(severity);
                currentAlarm = ctx.getAlarmService().createOrUpdateAlarm(currentAlarm);
                return new TbAlarmResult(false, false, true, false, currentAlarm);
            } else {
                currentAlarm = ctx.getAlarmService().createOrUpdateAlarm(currentAlarm);
                return new TbAlarmResult(false, true, false, false, currentAlarm);
            }
        } else {
            return null;
        }
    } else {
        currentAlarm = new Alarm();
        currentAlarm.setType(alarmDefinition.getAlarmType());
        currentAlarm.setStatus(AlarmStatus.ACTIVE_UNACK);
        currentAlarm.setSeverity(severity);
        long startTs = dataSnapshot.getTs();
        if (startTs == 0L) {
            startTs = System.currentTimeMillis();
        }
        currentAlarm.setStartTs(startTs);
        currentAlarm.setEndTs(currentAlarm.getStartTs());
        currentAlarm.setDetails(createDetails(ruleState));
        currentAlarm.setOriginator(originator);
        currentAlarm.setTenantId(ctx.getTenantId());
        currentAlarm.setPropagate(alarmDefinition.isPropagate());
        currentAlarm.setPropagateToOwner(alarmDefinition.isPropagateToOwner());
        currentAlarm.setPropagateToTenant(alarmDefinition.isPropagateToTenant());
        if (alarmDefinition.getPropagateRelationTypes() != null) {
            currentAlarm.setPropagateRelationTypes(alarmDefinition.getPropagateRelationTypes());
        }
        currentAlarm = ctx.getAlarmService().createOrUpdateAlarm(currentAlarm);
        boolean updated = currentAlarm.getStartTs() != currentAlarm.getEndTs();
        return new TbAlarmResult(!updated, updated, false, false, currentAlarm);
    }
}
Also used : AlarmSeverity(org.thingsboard.server.common.data.alarm.AlarmSeverity) DeviceProfileAlarm(org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm) Alarm(org.thingsboard.server.common.data.alarm.Alarm) TbAlarmResult(org.thingsboard.rule.engine.action.TbAlarmResult)

Aggregations

TbAlarmResult (org.thingsboard.rule.engine.action.TbAlarmResult)2 Alarm (org.thingsboard.server.common.data.alarm.Alarm)2 AlarmSeverity (org.thingsboard.server.common.data.alarm.AlarmSeverity)2 DeviceProfileAlarm (org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 BiFunction (java.util.function.BiFunction)1 Data (lombok.Data)1 Slf4j (lombok.extern.slf4j.Slf4j)1 StringUtils (org.apache.commons.lang3.StringUtils)1 DonAsynchron (org.thingsboard.common.util.DonAsynchron)1 JacksonUtil (org.thingsboard.common.util.JacksonUtil)1 TbContext (org.thingsboard.rule.engine.api.TbContext)1 PersistedAlarmRuleState (org.thingsboard.rule.engine.profile.state.PersistedAlarmRuleState)1 PersistedAlarmState (org.thingsboard.rule.engine.profile.state.PersistedAlarmState)1