Skip to content

UniDA Device Driver Howto

Victor Sonora Pombo edited this page Mar 4, 2015 · 2 revisions

Writing an adapter that links any device with a Java implementation with UniDA is quite simple. All that is needed is to implement the IUniDADeviceDriver, using values taken from DogOnt ontology for state ids, functionality ids, command ids, or state value ids when it makes sense.

As an example, the relevant implementation of a simple/dummy random number generator UniDA Device Driver is reproduced here:

public class RandomNumberGeneratorDeviceDriver extends AbstractUniDADeviceDriver implements Runnable
{

    private static final long DELAY = 5000;
    
    private static final int BOUND = 100000;
    
    private static final String STATE = "http://elite.polito.it/ontologies/dogont.owl#NumericValueState";

    private static final String INTEGER_STATE_VALUE = "http://elite.polito.it/ontologies/dogont.owl#IntegerStateValue";
    
    private boolean active = false;
    
    private int currentNumber = 0;
    

    @Override
    public void run()
    {
        this.active = true;

        try
        {

            while (this.active)
            {
                
                Thread.sleep(DELAY);
                this.currentNumber = new Random().nextInt(BOUND);
            }

        } catch (InterruptedException ex)
        {
            Logger.getLogger(RandomNumberGeneratorDeviceDriver.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public DeviceState queryDeviceState(DeviceID did, String stateId) throws DeviceErrorException
    {
        if (stateId.equals(STATE))
        {
            this.currentNumber = new Random().nextInt(BOUND);
            return this.createState(this.currentNumber);
        }
        return null;
    }

    @Override
    public DeviceState[] queryDeviceStates(DeviceID did) throws DeviceErrorException
    {
        DeviceState[] states = new DeviceState[1];
        states[0] = queryDeviceState(did, STATE);
        return states;
    }
    
    private DeviceState createState(int value)
    {      

        return new DeviceState(
                new DeviceStateMetadata(
                        STATE, new DeviceStateValue[]
                        {
                            new DeviceStateValue(INTEGER_STATE_VALUE, "")
                        }),
                new DeviceStateValue(INTEGER_STATE_VALUE, String.valueOf(value)));
    }

   ( … )

}