Search in sources :

Example 1 with RuleException

use of org.thingsboard.server.extensions.api.rules.RuleException 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)

Example 2 with RuleException

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

the class AlarmProcessor method buildAlarm.

private Alarm buildAlarm(RuleContext ctx, FromDeviceMsg msg) throws RuleException {
    VelocityContext context = VelocityUtils.createContext(ctx.getDeviceMetaData(), msg);
    String alarmType = VelocityUtils.merge(alarmTypeTemplate, context);
    String alarmDetails = VelocityUtils.merge(alarmDetailsTemplate, context);
    Alarm alarm = new Alarm();
    alarm.setOriginator(ctx.getDeviceMetaData().getDeviceId());
    alarm.setType(alarmType);
    alarm.setStatus(status);
    alarm.setSeverity(severity);
    alarm.setPropagate(configuration.isAlarmPropagateFlag());
    try {
        alarm.setDetails(mapper.readTree(alarmDetails));
    } catch (IOException e) {
        log.debug("[{}] Failed to parse alarm details {} as json string after evaluation.", ctx.getRuleId(), e);
        throw new RuleException("Failed to parse alarm details as json string after evaluation!", e);
    }
    return alarm;
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) Alarm(org.thingsboard.server.common.data.alarm.Alarm) IOException(java.io.IOException) RuleException(org.thingsboard.server.extensions.api.rules.RuleException)

Example 3 with RuleException

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

the class RabbitMqMsgHandler method process.

@Override
public void process(PluginContext ctx, TenantId tenantId, RuleId ruleId, RuleToPluginMsg<?> msg) throws RuleException {
    if (!(msg instanceof RabbitMqActionMsg)) {
        throw new RuleException("Unsupported message type " + msg.getClass().getName() + "!");
    }
    RabbitMqActionPayload payload = ((RabbitMqActionMsg) msg).getPayload();
    AMQP.BasicProperties properties = convert(payload.getMessageProperties());
    try {
        channel.basicPublish(payload.getExchange() != null ? payload.getExchange() : "", payload.getQueueName(), properties, payload.getPayload().getBytes(UTF8));
        if (payload.isSync()) {
            ctx.reply(new ResponsePluginToRuleMsg(msg.getUid(), tenantId, ruleId, BasicStatusCodeResponse.onSuccess(payload.getMsgType(), payload.getRequestId())));
        }
    } catch (IOException e) {
        throw new RuleException(e.getMessage(), e);
    }
}
Also used : RabbitMqActionMsg(org.thingsboard.server.extensions.rabbitmq.action.RabbitMqActionMsg) AMQP(com.rabbitmq.client.AMQP) ResponsePluginToRuleMsg(org.thingsboard.server.extensions.api.plugins.msg.ResponsePluginToRuleMsg) IOException(java.io.IOException) RuleException(org.thingsboard.server.extensions.api.rules.RuleException) RabbitMqActionPayload(org.thingsboard.server.extensions.rabbitmq.action.RabbitMqActionPayload)

Example 4 with RuleException

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

the class KafkaMsgHandler method process.

@Override
public void process(PluginContext ctx, TenantId tenantId, RuleId ruleId, RuleToPluginMsg<?> msg) throws RuleException {
    if (!(msg instanceof KafkaActionMsg)) {
        throw new RuleException("Unsupported message type " + msg.getClass().getName() + "!");
    }
    KafkaActionPayload payload = ((KafkaActionMsg) msg).getPayload();
    log.debug("Processing kafka payload: {}", payload);
    try {
        producer.send(new ProducerRecord<>(payload.getTopic(), payload.getMsgBody()), (metadata, e) -> {
            if (payload.isSync()) {
                if (metadata != null) {
                    ctx.reply(new ResponsePluginToRuleMsg(msg.getUid(), tenantId, ruleId, BasicStatusCodeResponse.onSuccess(payload.getMsgType(), payload.getRequestId())));
                } else {
                    ctx.reply(new ResponsePluginToRuleMsg(msg.getUid(), tenantId, ruleId, BasicStatusCodeResponse.onError(payload.getMsgType(), payload.getRequestId(), e)));
                }
            }
        });
    } catch (Exception e) {
        throw new RuleException(e.getMessage(), e);
    }
}
Also used : KafkaActionMsg(org.thingsboard.server.extensions.kafka.action.KafkaActionMsg) KafkaActionPayload(org.thingsboard.server.extensions.kafka.action.KafkaActionPayload) ResponsePluginToRuleMsg(org.thingsboard.server.extensions.api.plugins.msg.ResponsePluginToRuleMsg) RuleException(org.thingsboard.server.extensions.api.rules.RuleException) RuleException(org.thingsboard.server.extensions.api.rules.RuleException)

Example 5 with RuleException

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

the class AlarmProcessor method processActiveAlarm.

private Alarm processActiveAlarm(RuleContext ctx, FromDeviceMsg msg, RuleProcessingMetaData md) throws RuleException {
    Alarm alarm = buildAlarm(ctx, msg);
    if (configuration.isNewAlarmFlag()) {
        Optional<Alarm> oldAlarmOpt = ctx.findLatestAlarm(alarm.getOriginator(), alarm.getType());
        if (oldAlarmOpt.isPresent() && !oldAlarmOpt.get().getStatus().isCleared()) {
            try {
                ctx.clearAlarm(oldAlarmOpt.get().getId(), oldAlarmOpt.get().getEndTs()).get();
            } catch (Exception e) {
                throw new RuleException("Failed to clear old alarm", e);
            }
        }
    }
    Alarm existing = ctx.createOrUpdateAlarm(alarm);
    if (existing.getStartTs() == alarm.getStartTs()) {
        log.debug("[{}][{}] New Active Alarm detected", ctx.getRuleId(), existing.getId());
        md.put(IS_NEW_ALARM, Boolean.TRUE);
        md.put(IS_NEW_OR_CLEARED_ALARM, Boolean.TRUE);
    } else {
        log.debug("[{}][{}] Existing Active Alarm detected", ctx.getRuleId(), existing.getId());
        md.put(IS_EXISTING_ALARM, Boolean.TRUE);
    }
    return existing;
}
Also used : Alarm(org.thingsboard.server.common.data.alarm.Alarm) RuleException(org.thingsboard.server.extensions.api.rules.RuleException) ScriptException(javax.script.ScriptException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) RuleException(org.thingsboard.server.extensions.api.rules.RuleException) IOException(java.io.IOException)

Aggregations

RuleException (org.thingsboard.server.extensions.api.rules.RuleException)7 ResponsePluginToRuleMsg (org.thingsboard.server.extensions.api.plugins.msg.ResponsePluginToRuleMsg)4 IOException (java.io.IOException)3 Alarm (org.thingsboard.server.common.data.alarm.Alarm)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ScriptException (javax.script.ScriptException)2 VelocityContext (org.apache.velocity.VelocityContext)2 PublishRequest (com.amazonaws.services.sns.model.PublishRequest)1 AMQP (com.rabbitmq.client.AMQP)1 Bindings (javax.script.Bindings)1 FromDeviceMsg (org.thingsboard.server.common.msg.session.FromDeviceMsg)1 RuleProcessingMetaData (org.thingsboard.server.extensions.api.rules.RuleProcessingMetaData)1 KafkaActionMsg (org.thingsboard.server.extensions.kafka.action.KafkaActionMsg)1 KafkaActionPayload (org.thingsboard.server.extensions.kafka.action.KafkaActionPayload)1 MqttActionMsg (org.thingsboard.server.extensions.mqtt.action.MqttActionMsg)1 MqttActionPayload (org.thingsboard.server.extensions.mqtt.action.MqttActionPayload)1 RabbitMqActionMsg (org.thingsboard.server.extensions.rabbitmq.action.RabbitMqActionMsg)1 RabbitMqActionPayload (org.thingsboard.server.extensions.rabbitmq.action.RabbitMqActionPayload)1 SnsTopicActionMsg (org.thingsboard.server.extensions.sns.action.SnsTopicActionMsg)1 SnsTopicActionPayload (org.thingsboard.server.extensions.sns.action.SnsTopicActionPayload)1