Search in sources :

Example 6 with RuleException

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

the class MqttMsgHandler method process.

@Override
public void process(PluginContext ctx, TenantId tenantId, RuleId ruleId, RuleToPluginMsg<?> msg) throws RuleException {
    if (!(msg instanceof MqttActionMsg)) {
        throw new RuleException("Unsupported message type " + msg.getClass().getName() + "!");
    }
    MqttActionPayload payload = ((MqttActionMsg) msg).getPayload();
    MqttMessage mqttMsg = new MqttMessage(payload.getMsgBody().getBytes(StandardCharsets.UTF_8));
    try {
        mqttClient.publish(payload.getTopic(), mqttMsg, null, new IMqttActionListener() {

            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                log.debug("Message [{}] was successfully delivered to topic [{}]!", msg.toString(), payload.getTopic());
                if (payload.isSync()) {
                    ctx.reply(new ResponsePluginToRuleMsg(msg.getUid(), tenantId, ruleId, BasicStatusCodeResponse.onSuccess(payload.getMsgType(), payload.getRequestId())));
                }
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable e) {
                log.warn("Failed to deliver message [{}] to topic [{}]!", msg.toString(), payload.getTopic());
                if (payload.isSync()) {
                    ctx.reply(new ResponsePluginToRuleMsg(msg.getUid(), tenantId, ruleId, BasicStatusCodeResponse.onError(payload.getMsgType(), payload.getRequestId(), new Exception(e))));
                }
            }
        });
    } catch (MqttException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : MqttActionPayload(org.thingsboard.server.extensions.mqtt.action.MqttActionPayload) MqttActionMsg(org.thingsboard.server.extensions.mqtt.action.MqttActionMsg) 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 7 with RuleException

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

the class SnsMessageHandler method process.

@Override
public void process(PluginContext ctx, TenantId tenantId, RuleId ruleId, RuleToPluginMsg<?> msg) throws RuleException {
    if (msg instanceof SnsTopicActionMsg) {
        SnsTopicActionPayload payload = ((SnsTopicActionMsg) msg).getPayload();
        PublishRequest publishRequest = new PublishRequest().withTopicArn(payload.getTopicArn()).withMessage(payload.getMsgBody());
        sns.publish(publishRequest);
        if (payload.isSync()) {
            ctx.reply(new ResponsePluginToRuleMsg(msg.getUid(), tenantId, ruleId, BasicStatusCodeResponse.onSuccess(payload.getMsgType(), payload.getRequestId())));
        }
        return;
    }
    throw new RuleException("Unsupported message type " + msg.getClass().getName() + "!");
}
Also used : SnsTopicActionMsg(org.thingsboard.server.extensions.sns.action.SnsTopicActionMsg) ResponsePluginToRuleMsg(org.thingsboard.server.extensions.api.plugins.msg.ResponsePluginToRuleMsg) PublishRequest(com.amazonaws.services.sns.model.PublishRequest) RuleException(org.thingsboard.server.extensions.api.rules.RuleException) SnsTopicActionPayload(org.thingsboard.server.extensions.sns.action.SnsTopicActionPayload)

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