KURA: how to change the MQTT messages format

人盡茶涼 提交于 2019-12-11 02:58:00

问题


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

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

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.

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.


回答1:


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);
        }
    }
}

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" />

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



来源:https://stackoverflow.com/questions/31743221/kura-how-to-change-the-mqtt-messages-format

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!