Search in sources :

Example 1 with ResponsePluginToRuleMsg

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

the class TelemetryRuleMsgHandler method handleGetAttributesRequest.

@Override
public void handleGetAttributesRequest(PluginContext ctx, TenantId tenantId, RuleId ruleId, GetAttributesRequestRuleToPluginMsg msg) {
    GetAttributesRequest request = msg.getPayload();
    BiPluginCallBack<List<AttributeKvEntry>, List<AttributeKvEntry>> callback = new BiPluginCallBack<List<AttributeKvEntry>, List<AttributeKvEntry>>() {

        @Override
        public void onSuccess(PluginContext ctx, List<AttributeKvEntry> clientAttributes, List<AttributeKvEntry> sharedAttributes) {
            BasicGetAttributesResponse response = BasicGetAttributesResponse.onSuccess(request.getMsgType(), request.getRequestId(), BasicAttributeKVMsg.from(clientAttributes, sharedAttributes));
            ctx.reply(new ResponsePluginToRuleMsg(msg.getUid(), tenantId, ruleId, response));
        }

        @Override
        public void onFailure(PluginContext ctx, Exception e) {
            log.error("Failed to process get attributes request", e);
            ctx.reply(new ResponsePluginToRuleMsg(msg.getUid(), tenantId, ruleId, BasicStatusCodeResponse.onError(request.getMsgType(), request.getRequestId(), e)));
        }
    };
    getAttributeKvEntries(ctx, msg.getDeviceId(), DataConstants.CLIENT_SCOPE, request.getClientAttributeNames(), callback.getV1Callback());
    getAttributeKvEntries(ctx, msg.getDeviceId(), DataConstants.SHARED_SCOPE, request.getSharedAttributeNames(), callback.getV2Callback());
}
Also used : AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) ResponsePluginToRuleMsg(org.thingsboard.server.extensions.api.plugins.msg.ResponsePluginToRuleMsg)

Example 2 with ResponsePluginToRuleMsg

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

the class SqsMessageHandler method sendMessageToFifoQueue.

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

Example 3 with ResponsePluginToRuleMsg

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

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

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

the class TelemetryRuleMsgHandler method handleTelemetryUploadRequest.

@Override
public void handleTelemetryUploadRequest(PluginContext ctx, TenantId tenantId, RuleId ruleId, TelemetryUploadRequestRuleToPluginMsg msg) {
    TelemetryUploadRequest request = msg.getPayload();
    List<TsKvEntry> tsKvEntries = new ArrayList<>();
    for (Map.Entry<Long, List<KvEntry>> entry : request.getData().entrySet()) {
        for (KvEntry kv : entry.getValue()) {
            tsKvEntries.add(new BasicTsKvEntry(entry.getKey(), kv));
        }
    }
    ctx.saveTsData(msg.getDeviceId(), tsKvEntries, msg.getTtl(), new PluginCallback<Void>() {

        @Override
        public void onSuccess(PluginContext ctx, Void data) {
            ctx.reply(new ResponsePluginToRuleMsg(msg.getUid(), tenantId, ruleId, BasicStatusCodeResponse.onSuccess(request.getMsgType(), request.getRequestId())));
            subscriptionManager.onLocalSubscriptionUpdate(ctx, msg.getDeviceId(), SubscriptionType.TIMESERIES, s -> prepareSubscriptionUpdate(request, s));
        }

        @Override
        public void onFailure(PluginContext ctx, Exception e) {
            log.error("Failed to process telemetry upload request", e);
            ctx.reply(new ResponsePluginToRuleMsg(msg.getUid(), tenantId, ruleId, BasicStatusCodeResponse.onError(request.getMsgType(), request.getRequestId(), e)));
        }
    });
}
Also used : DeviceId(org.thingsboard.server.common.data.id.DeviceId) RuleId(org.thingsboard.server.common.data.id.RuleId) java.util(java.util) DataConstants(org.thingsboard.server.common.data.DataConstants) org.thingsboard.server.common.msg.core(org.thingsboard.server.common.msg.core) UpdateAttributesRequestRuleToPluginMsg(org.thingsboard.server.extensions.api.plugins.msg.UpdateAttributesRequestRuleToPluginMsg) SubscriptionManager(org.thingsboard.server.extensions.core.plugin.telemetry.SubscriptionManager) ResponsePluginToRuleMsg(org.thingsboard.server.extensions.api.plugins.msg.ResponsePluginToRuleMsg) BasicAttributeKVMsg(org.thingsboard.server.common.msg.kv.BasicAttributeKVMsg) Collectors(java.util.stream.Collectors) TenantId(org.thingsboard.server.common.data.id.TenantId) SubscriptionType(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionType) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) Slf4j(lombok.extern.slf4j.Slf4j) GetAttributesRequestRuleToPluginMsg(org.thingsboard.server.extensions.api.plugins.msg.GetAttributesRequestRuleToPluginMsg) PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) DefaultRuleMsgHandler(org.thingsboard.server.extensions.api.plugins.handlers.DefaultRuleMsgHandler) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) PluginCallback(org.thingsboard.server.extensions.api.plugins.PluginCallback) TelemetryUploadRequestRuleToPluginMsg(org.thingsboard.server.extensions.api.plugins.msg.TelemetryUploadRequestRuleToPluginMsg) Subscription(org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) ResponsePluginToRuleMsg(org.thingsboard.server.extensions.api.plugins.msg.ResponsePluginToRuleMsg)

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