Search in sources :

Example 6 with ResponsePluginToRuleMsg

use of org.thingsboard.server.extensions.api.plugins.msg.ResponsePluginToRuleMsg 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 ResponsePluginToRuleMsg

use of org.thingsboard.server.extensions.api.plugins.msg.ResponsePluginToRuleMsg 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)

Example 8 with ResponsePluginToRuleMsg

use of org.thingsboard.server.extensions.api.plugins.msg.ResponsePluginToRuleMsg in project thingsboard by thingsboard.

the class SqsMessageHandler method sendMessageToStandardQueue.

private void sendMessageToStandardQueue(PluginContext ctx, TenantId tenantId, RuleId ruleId, RuleToPluginMsg<?> msg) {
    SqsStandardQueueActionPayload payload = ((SqsStandardQueueActionMsg) msg).getPayload();
    SendMessageRequest sendMsgRequest = new SendMessageRequest().withDelaySeconds(payload.getDelaySeconds()).withQueueUrl(payload.getQueue()).withMessageBody(payload.getMsgBody());
    sqs.sendMessage(sendMsgRequest);
    if (payload.isSync()) {
        ctx.reply(new ResponsePluginToRuleMsg(msg.getUid(), tenantId, ruleId, BasicStatusCodeResponse.onSuccess(payload.getMsgType(), payload.getRequestId())));
    }
}
Also used : SqsStandardQueueActionMsg(org.thingsboard.server.extensions.sqs.action.standard.SqsStandardQueueActionMsg) SqsStandardQueueActionPayload(org.thingsboard.server.extensions.sqs.action.standard.SqsStandardQueueActionPayload) ResponsePluginToRuleMsg(org.thingsboard.server.extensions.api.plugins.msg.ResponsePluginToRuleMsg) SendMessageRequest(com.amazonaws.services.sqs.model.SendMessageRequest)

Aggregations

ResponsePluginToRuleMsg (org.thingsboard.server.extensions.api.plugins.msg.ResponsePluginToRuleMsg)8 RuleException (org.thingsboard.server.extensions.api.rules.RuleException)4 SendMessageRequest (com.amazonaws.services.sqs.model.SendMessageRequest)2 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)2 PluginContext (org.thingsboard.server.extensions.api.plugins.PluginContext)2 PublishRequest (com.amazonaws.services.sns.model.PublishRequest)1 AMQP (com.rabbitmq.client.AMQP)1 IOException (java.io.IOException)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Slf4j (lombok.extern.slf4j.Slf4j)1 DataConstants (org.thingsboard.server.common.data.DataConstants)1 DeviceId (org.thingsboard.server.common.data.id.DeviceId)1 RuleId (org.thingsboard.server.common.data.id.RuleId)1 TenantId (org.thingsboard.server.common.data.id.TenantId)1 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)1 KvEntry (org.thingsboard.server.common.data.kv.KvEntry)1 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)1 org.thingsboard.server.common.msg.core (org.thingsboard.server.common.msg.core)1 BasicAttributeKVMsg (org.thingsboard.server.common.msg.kv.BasicAttributeKVMsg)1