KURA:如何使用 DataService 接收 MQTT 消息?
KURA : how to receive MQTT messages using the DataService?
如何接收针对特定主题发布的 MQTT 消息?
我可以在 MQTT 代理上发布消息,但我不知道如何接收消息?
这是我的组件 xml:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" name="arduinoCommunicator">
<implementation class="arduinoCommunicator.ArduinoCommunicator"/>
<reference bind="setDataService" cardinality="1..1" interface="org.eclipse.kura.data.DataService" name="DataService" policy="static" unbind="unsetDataService"/>
<service>
<provide interface="org.eclipse.kura.data.DataServiceListener"/>
</service>
</scr:component>
这是我的包激活器 class,我只保留了需要的代码,我实现了 DataServiceListener,我订阅了关于 onConnectionEstablished() 的主题,我想我在函数 onMessageArrived 上收到了消息,问题是我没有看到订阅已经完成的日志,并且函数 onMessageArrived() 没有被执行:
public class ArduinoCommunicator implements DataServiceListener {
public static DataService dataService;
@Override
public void onConnectionEstablished() {
String topic="egmkey/device2/cmd/PING";
System.out.println("connection has been established");
try {
ArduinoCommunicator.dataService.subscribe(topic, 1);//egmkey/device2/cmd/PING
System.out.println("subscription done to topic"+topic);
} catch (KuraException e) {
System.out.println("failed to subscribe: "+ e);
}
}
@Override
public void onMessageArrived(String topic, byte[] payload, int qos, boolean retained) {
System.out.println("message received!!!!!!!!!!!!!!!!!!!!!!:"+topic);
}
}
为了能够在 KURA 上使用 DataService 接收 MQTT 消息,我错过了什么?谢谢
您没有正确地将 DataService 服务注入到您的组件中。下面的代码应该更接近您的实际需要:
public class ArduinoCommunicator implements DataServiceListener {
public DataService dataService;
protected setDataService(DataService dataService) {
this.dataService = dataService;
}
@Override
public void onConnectionEstablished() {
String topic = "egmkey/device2/cmd/PING";
System.out.println("connection has been established");
try {
dataService.subscribe(topic, 1);//egmkey/device2/cmd/PING
System.out.println("subscription done to topic" + topic);
} catch (KuraException e) {
System.out.println("failed to subscribe: " + e);
}
}
@Override
public void onMessageArrived(String topic, byte[] payload, int qos, boolean retained) {
System.out.println("message received!!!!!!!!!!!!!!!!!!!!!!:" + topic);
}
}
如何接收针对特定主题发布的 MQTT 消息? 我可以在 MQTT 代理上发布消息,但我不知道如何接收消息?
这是我的组件 xml:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" name="arduinoCommunicator">
<implementation class="arduinoCommunicator.ArduinoCommunicator"/>
<reference bind="setDataService" cardinality="1..1" interface="org.eclipse.kura.data.DataService" name="DataService" policy="static" unbind="unsetDataService"/>
<service>
<provide interface="org.eclipse.kura.data.DataServiceListener"/>
</service>
</scr:component>
这是我的包激活器 class,我只保留了需要的代码,我实现了 DataServiceListener,我订阅了关于 onConnectionEstablished() 的主题,我想我在函数 onMessageArrived 上收到了消息,问题是我没有看到订阅已经完成的日志,并且函数 onMessageArrived() 没有被执行:
public class ArduinoCommunicator implements DataServiceListener {
public static DataService dataService;
@Override
public void onConnectionEstablished() {
String topic="egmkey/device2/cmd/PING";
System.out.println("connection has been established");
try {
ArduinoCommunicator.dataService.subscribe(topic, 1);//egmkey/device2/cmd/PING
System.out.println("subscription done to topic"+topic);
} catch (KuraException e) {
System.out.println("failed to subscribe: "+ e);
}
}
@Override
public void onMessageArrived(String topic, byte[] payload, int qos, boolean retained) {
System.out.println("message received!!!!!!!!!!!!!!!!!!!!!!:"+topic);
}
}
为了能够在 KURA 上使用 DataService 接收 MQTT 消息,我错过了什么?谢谢
您没有正确地将 DataService 服务注入到您的组件中。下面的代码应该更接近您的实际需要:
public class ArduinoCommunicator implements DataServiceListener {
public DataService dataService;
protected setDataService(DataService dataService) {
this.dataService = dataService;
}
@Override
public void onConnectionEstablished() {
String topic = "egmkey/device2/cmd/PING";
System.out.println("connection has been established");
try {
dataService.subscribe(topic, 1);//egmkey/device2/cmd/PING
System.out.println("subscription done to topic" + topic);
} catch (KuraException e) {
System.out.println("failed to subscribe: " + e);
}
}
@Override
public void onMessageArrived(String topic, byte[] payload, int qos, boolean retained) {
System.out.println("message received!!!!!!!!!!!!!!!!!!!!!!:" + topic);
}
}