KURA:如何更改MQTT消息格式 [英] KURA: how to change the MQTT messages format

查看:337
本文介绍了KURA:如何更改MQTT消息格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

KURA MQTT云客户端发布符合以下公式的消息(更多详细信息):

The KURA MQTT cloud client publishes messages respecting the following formula (more details):

#account-name/#client-id/#API-ID/topic

我想以自己的格式发送MQTT消息,我不想在MQTT消息中发送帐户名称和客户端ID 。

I want to send MQTT messages with my own format, I dont want to send the account name and the client id in the MQTT message.

我该怎么做?
我已经尝试更改KURA Web界面中的配置 - > MQTTData传输,我删除了lwt.topic的内容,但没有成功。

How can I do that? I already tried to change the configuration in the KURA web interface -> MQTTData transport and I have deleted the content of "lwt.topic" but without success.

推荐答案

使用 DataService 。请求OSGi将实例注入到您的组件中。

您的组件类中要使用的示例代码:

Use the DataService directly. Ask OSGi to inject the instance into your component.
Sample code to use in your component class:

public class MyComponent {
    private DataService m_dataService;

    public void setDataService(DataService dataService) {
        m_dataService = dataService;
    }

    public void unsetDataService(DataService dataService) {
        m_dataService = null;
    }

    // activate() deactivate() and all required methods

    public void publish() {
        String topic = "your/topic";
        String payload = "Hello!";
        int qos = 0;
        boolean retain = false;
        try {
            m_dataService.publish(topic, payload.getBytes(), qos, retain, 2);
            s_logger.info("Publish ok");
        } catch (Exception e) {
            s_logger.error("Error while publishing", e);
        }
    }
}

在您的组件中 OSGI-INF / mycomponent.xml 告诉OSGi要调用哪些方法通过添加以下

In your component OSGI-INF/mycomponent.xml tell OSGi which methods to call to inject the DataService by adding the following

<reference name="DataService"
    interface="org.eclipse.kura.data.DataService"
    bind="setDataService"
    unbind="unsetDataService"
    cardinality="1..1"
    policy="static" />

然后,您可以将您需要的主题传递给 DataService.publish(.. 。)。有效负载必须转换为 byte [] 数组。

Then you can pass the topic you need to DataService.publish(...). Payloads must be converted to byte[] arrays.

这篇关于KURA:如何更改MQTT消息格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆