use of org.thingsboard.server.extensions.mqtt.action.MqttActionMsg 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);
}
}
Aggregations