KURA: how to change the MQTT messages format

2019-07-22 00:05发布

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.

标签: eclipse mqtt iot
1条回答
做自己的国王
2楼-- · 2019-07-22 00:39

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.

查看更多
登录 后发表回答