Custom Object Example: Converting Temperature Values

In this example, the value of an incoming data item is a temperature in degrees Fahrenheit. This custom object converts the value to degrees Celsius and then writes the new value to a data item to pass it back to the Platform.

 

Name: Data Item Action

Type: Action

Description: Retrieves a data item value from an asset. Converts the incoming temperature value from degrees Fahrenheit to degrees Celsius and writes the Celsius value back to the Platform. To adapt this example to your purposes, you can change the business logic to perform the calculation or conversion you require. You should also change the asset name in DeviceFinder.setDeviceName("SampleDevice") and change the names of the data item that is read and the data item to which the new value will be written. The places to make changes are highlighted in this color in the source code that follows.

Source Code:


import com.axeda.drm.sdk.Context;

import com.axeda.drm.sdk.data.DataValueList;

import com.axeda.drm.sdk.data.DataValue;

import com.axeda.drm.sdk.device.DataItem;

import com.axeda.drm.sdk.data.DataValueEntry;

import com.axeda.drm.sdk.device.DataItemFinder;

import com.axeda.drm.sdk.data.CurrentDataFinder;

import com.axeda.drm.sdk.device.Device;

import com.axeda.drm.sdk.device.DeviceFinder;

 

import java.util.Iterator;

import java.util.List;

 

try

{

Context systemContext = Context.create();

int farenheitInt = 0;

DataItem celsiusDataItem = null;

DeviceFinder deviceFinder = new DeviceFinder(systemContext);

deviceFinder.setDeviceName("SampleDevice");

Device device = deviceFinder.find();

 

CurrentDataFinder currentDataFinder = new CurrentDataFinder(systemContext, device);

DataValueList valueslist = currentDataFinder.find();

logger.debug("Start Data Item " + valueslist);

 

//read data item values

for (Iterator i = valueslist.iterator(); i.hasNext();)

{

DataValue value = (DataValue) i.next();

logger.debug( "Device Name : " + context.getDevice().getName() );

logger.debug(" " + value.getDataItem().getName()

+ ": " + value.asString());

if (value.getDataItem().getName() == "fahrenheit" ){

farenheitInt = value.asInteger();

}

}

 

//perform business logic

long celsiusInt = 100/(212-32) * (farenheitInt - 32 );

logger.info("Celsius " + celsiusInt );

 

DataItemFinder finder = new DataItemFinder(systemContext);

finder.setModel(context.getDevice().getModel());

finder.setDataItemName("celsius");

celsiusDataItem = finder.find();

DataValueEntry dataValueEntry = new DataValueEntry(systemContext,

device,

celsiusDataItem,

celsiusInt);

logger.info("Writing data Item value");

dataValueEntry.store();

}catch(Exception e)

{

e.printStackTrace();

}

 

Parameters: None