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);
}
}
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() + "!");
}
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())));
}
}
Aggregations