Search in sources :

Example 1 with RuleProcessingMetaData

use of org.thingsboard.server.extensions.api.rules.RuleProcessingMetaData in project thingsboard by thingsboard.

the class AlarmProcessor method process.

@Override
public RuleProcessingMetaData process(RuleContext ctx, ToDeviceActorMsg wrapper) throws RuleException {
    RuleProcessingMetaData md = new RuleProcessingMetaData();
    FromDeviceMsg msg = wrapper.getPayload();
    Bindings bindings = buildBindings(ctx, msg);
    boolean isActiveAlarm;
    boolean isClearedAlarm;
    VelocityContext context = VelocityUtils.createContext(ctx.getDeviceMetaData(), msg);
    for (Object key : context.getKeys()) {
        md.put(key.toString(), context.get(key.toString()));
    }
    try {
        isActiveAlarm = newAlarmEvaluator.execute(bindings);
        isClearedAlarm = clearAlarmEvaluator.execute(bindings);
    } catch (ScriptException e) {
        log.debug("[{}] Failed to evaluate alarm expressions!", ctx.getRuleId(), e);
        throw new RuleException("Failed to evaluate alarm expressions!", e);
    }
    if (!isActiveAlarm && !isClearedAlarm) {
        log.debug("[{}] Incoming message do not trigger alarm", ctx.getRuleId());
        return md;
    }
    Alarm existing;
    if (isActiveAlarm) {
        existing = processActiveAlarm(ctx, msg, md);
    } else {
        existing = processInactiveAlarm(ctx, md, context);
    }
    if (existing != null) {
        md.put("alarmId", existing.getId().getId());
        md.put("alarmType", existing.getType());
        md.put("alarmSeverity", existing.getSeverity());
        try {
            if (!StringUtils.isEmpty(existing.getDetails())) {
                md.put("alarmDetails", mapper.writeValueAsString(existing.getDetails()));
            } else {
                md.put("alarmDetails", "{}");
            }
        } catch (JsonProcessingException e) {
            throw new RuleException("Failed to serialize alarm details", e);
        }
    }
    return md;
}
Also used : FromDeviceMsg(org.thingsboard.server.common.msg.session.FromDeviceMsg) ScriptException(javax.script.ScriptException) VelocityContext(org.apache.velocity.VelocityContext) RuleProcessingMetaData(org.thingsboard.server.extensions.api.rules.RuleProcessingMetaData) Alarm(org.thingsboard.server.common.data.alarm.Alarm) RuleException(org.thingsboard.server.extensions.api.rules.RuleException) Bindings(javax.script.Bindings) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Bindings (javax.script.Bindings)1 ScriptException (javax.script.ScriptException)1 VelocityContext (org.apache.velocity.VelocityContext)1 Alarm (org.thingsboard.server.common.data.alarm.Alarm)1 FromDeviceMsg (org.thingsboard.server.common.msg.session.FromDeviceMsg)1 RuleException (org.thingsboard.server.extensions.api.rules.RuleException)1 RuleProcessingMetaData (org.thingsboard.server.extensions.api.rules.RuleProcessingMetaData)1