The Gateway SDK Device Module

Last modified: Jul 5, 2019 @ 06:44

This SDK module provides a simplified way to integrate with other standards:

  • It provides the DeviceManager for modelling the registered devices and its smart objects and resources.
  • It provides the DeviceAppIoTListener that calls the modelled resources from the DeviceManager.
  • It provides the classes Device, SmartObjectBase for modelling the device and its smart objects.
  • It provides a range of Resource classes representing different data types, like:
    • BooleanResource
    • FloatResource
    • StringResource, etc.
  • It lets the developer focus on implementation of smart objects and resources.

Example of the Gateway SDK Device Module Maven pom file:

<dependency>
  <groupId>com.ericsson.appiot</groupId>
  <artifactId>gateway-sdk-device</artifactId>
  <version>2.0.0</version>
</dependency>

Example of device module and internal device implementing the Smart Object:

public class InternalTemperature extends SmartObjectBase implements ResourceValueChangedListener {

    public InternalTemperature(Device device) {
        this(device, AppIoTModelProvider.getObjectModel(OBJECT_ID_TEMPERATURE), OBJECT_ID_TEMPERATURE, 0);
    }
    
    public InternalTemperature(Device device, ObjectModel objectModel, Integer objectId, Integer instanceId) {
        super(device, objectModel, objectId, instanceId);

        addResource(new SensorValue(this, objectModel.getResourceModel(RESOURCE_ID_SENSOR_VALUE)));
        addResource(new FloatResource(this, objectModel.getResourceModel(RESOURCE_ID_MIN_MEASURED_VALUE), 0, 0f));
        addResource(new FloatResource(this, objectModel.getResourceModel(RESOURCE_ID_MAX_MEASURED_VALUE), 0, 0f));
        addResource(new StringResource(this, objectModel.getResourceModel(RESOURCE_ID_UNITS), UNIT));

        ResetResource resetResource = new ResetResource(this, objectModel.getResourceModel(RESOURCE_ID_RESET_MIN_MAX_MEASURED_VALUES));
        resetResource.addResource(getResource(RESOURCE_ID_MIN_MEASURED_VALUE));
        resetResource.addResource(getResource(RESOURCE_ID_MAX_MEASURED_VALUE));
        addResource(resetResource);
        
        getResource(RESOURCE_ID_SENSOR_VALUE).addListener(this);
    }

    @Override
    public void onResourceValueChanged(Resource resource, Object value) {
        if(resource.getResourceId() == RESOURCE_ID_SENSOR_VALUE) {
            float measurement = (float) value;
            FloatResource min = (FloatResource) getResource(RESOURCE_ID_MIN_MEASURED_VALUE);
            FloatResource max = (FloatResource) getResource(RESOURCE_ID_MAX_MEASURED_VALUE);
            min.setValue(min.getValue() > measurement ? measurement : min.getValue());  
            max.setValue(max.getValue() < measurement ? measurement : max.getValue());
        }
    }
}

Example of device module and internal device implementing the resource:

public class SensorValue extends FloatResource implements Runnable {

    private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    private int counter = 0;

    public SensorValue(SmartObjectBase smartObject, ResourceModel resourceModel) {
        super(smartObject, resourceModel);            
        this.counter = ((int)(Math.random() * 100.0D));
    }

    @Override
    protected void onObserveRequest() throws ObserveRequestException {
      super.onObserveRequest();
      if(scheduler.isTerminated()) {
        scheduler = Executors.newScheduledThreadPool(1);
        scheduler.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS);
      }
    }
    
    @Override
    protected void onCancelObserveRequest() throws ObserveRequestException {
      super.onCancelObserveRequest();
      scheduler.shutdown();
    }

    @Override
    public void run() {
        int offset = 50;
        float measurement = (float) (offset - offset * Math.cos(this.counter / 100.0 * 3.141592653589793D));
        setValue(measurement);
        this.counter += 10;
    }
}

Example of device module and internal device:

DeviceManager deviceManager = new DeviceManager();

// Internal device for representing the gateway itself.
Device internalDevice = new Device();
SmartObjectBase internalTemperature = new InternalTemperature(internalDevice);
internalDevice.addSmartObject(internalTemperature);
deviceManager.setInternalDevice(internalDevice);

appIoTListener = new DeviceAppIoTListener(deviceManager);
appIoTGateway = new AppIoTGateway(appIoTListener);

appIoTGateway.start();

The following diagram explains the Device Module operations and structure: