Kura DataService简单实现

时间:2017-05-20 14:39:13

标签: eclipse osgi mqtt iot kura

我尝试过使用Kura DataService的简单实现

这是我制作的 LampuPintar.java

的java类
package org.eclipse.kura.smarthome.lampupintar;
import org.eclipse.kura.data.DataService;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LampuPintar {
    private DataService m_dataservice;

    private static final Logger s_logger = LoggerFactory.getLogger(LampuPintar.class);

    private static final String APP_ID = "lampupintar";

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

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

    protected void activate(ComponentContext componentContext) {
        s_logger.info("Bundle " + APP_ID + " has started!");
        s_logger.debug(APP_ID + ": This is a debug message.");
    }

    protected void deactivate(ComponentContext componentContext) {
        s_logger.info("Bundle " + APP_ID + " has stopped!");
    }

    public void publish() {
        String topic = "smarthome/lampupintar";
        String payload = "Hello";
        int qos = 2;
        boolean retain = false;
        for (int i=0; i<20;i++){
            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);
            }
        } 

    }
}

这是组件定义文件 component.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="org.eclipse.kura.smarthome.lampupintar">
<implementation class="org.eclipse.kura.smarthome.lampupintar.LampuPintar"/>
<reference bind="setDataService" 
    cardinality="1..1" 
    interface="org.eclipse.kura.data.DataService" 
    name="DataService" 
    policy="static" 
    unbind="unsetDataService"/>
</scr:component>

我尝试使用这些文件创建一个项目,我成功创建了.dp文件并将其安装到Kura Web UI,但似乎没有显示任何内容并且没有向代理发送任何内容(我在mosquitto代理控制台中检查过)。 / p>

这些代码有什么问题?还是从这些代码中遗漏了一些东西以使其完整和正常工作?

感谢。

1 个答案:

答案 0 :(得分:0)

您是否检查过日志文件(/var/log/kura.log和/var/log/kura-console.log)?你看到了你的发布好吗&#34;日志中的消息?您还可以使用&#39; ss&#39;来检查您的捆绑包与OSGi控制台(telnet localhost 5002)。和&#39; ls&#39;命令。这将显示捆绑包是否正确解析。

我还要添加DataServiceListener [1]。这将有助于使用DataService跟踪事件。

[1] http://download.eclipse.org/kura/docs/api/3.0.0/apidocs/org/eclipse/kura/data/listener/DataServiceListener.html