Kura DataService Simple implementation

298 Views Asked by At

I tried make a simple implementation of using Kura DataService

Here is the java class I made LampuPintar.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);
            }
        } 

    }
}

and this is the component definition file, 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>

I tried make a project with those files, I successfully created the .dp file and installed it to Kura Web UI, but it seems showed nothing and not send anything to the broker (I checked in the mosquitto broker console).

What's wrong with those codes ?? or something miss from those code to make it complete and work properly ??

Thanks.

1

There are 1 best solutions below

0
On

Have you checked the log files (/var/log/kura.log and /var/log/kura-console.log)? Are you seeing your "Publish ok" message in the log? You can also check your bundle with the OSGi console (telnet localhost 5002) using the 'ss' and 'ls' commands. This will show if the bundle and resolved correctly.

I would also add the DataServiceListener [1]. This will help track events with the DataService.

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