Device Context
- When an IoT sensor reports to the platform, a new Device data structure is created, which will hold all the meta information like, created time, last reported time, last protocol channel it reported etc. And also a separate key-value storage space is created for each and every device where you can store device related information of any datatype
- Context Variable: device
- Only Applicable To: Message Rules, Binary Rules & Domain Rules
- Even though an instance of the device is available in the message and binary rules, devices can also be fetched from other types of rules using Domain Context.
Device Context Methods & Fields
// Retrieve the Device object
public Device get();
// Creates a Device Group and returns the context
public DeviceGroupContext createGroup(long groupId, String name);
// Retrieves a DeviceGroup with specified id
public DeviceGroupContext group(long groupId);
// Send a raw string data to this device
public void send(String data);
// Send a map as JSON string data
public void send(Map<Object, Object> jsonMap);
// Send a raw binary data
public void send(byte[] data);
// Sends a well formatted command with JSON header and data (*Boodskap preferred*)
public void send(int commandId, Map<Object, Object> jsonMap);
// Retrieve a stored property, return data is formatted if a format is specified while storing
// Returns one of [String, byte[], Map<String, Object>]
public Object property(String name) ;
// Retrieve a stored property, return data is converted if a format is specified while storing
// Returns one of [String, byte[], Map<String, Object], if not found def value is returned
public Object getProperty(String name, String def);
// Stores a named property property with AS_IS format
public void property(String name, String value);
// Stores a named property as JSON map
public void property(String name, Map<Object, Object> values);
// Stores a named property with one of the formats <JSON, HEX, BASE64, AS_IS>. Upon retrieval
// JSON properties are returned as Map<String,Object>
// HEX properties are hex decoded and returned as byte[]
// BASE64 properties are base64 decoded and returned as byte[]
public void property(String format, String name, String value);
// Stores a boolean value in the lookup table
public DeviceContext put(String name, boolean value);
// Stores a byte value in the lookup table
public DeviceContext put(String name, byte value);
// Stores a short value in the lookup table
public DeviceContext put(String name, short value);
// Stores a integer value in the lookup table
public DeviceContext put(String name, int value) ;
// Stores a float value in the lookup table
public DeviceContext put(String name, float value);
// Stores a double value in the lookup table
public DeviceContext put(String name, double value);
// Stores a long value in the lookup table
public DeviceContext put(String name, long value);
// Stores a String value in the lookup table
public DeviceContext put(String name, String value);
// Stores a UUID value in the lookup table
public DeviceContext put(String name, UUID value);
// Stores a byte[] value in the lookup table
public DeviceContext put(String name, byte[] value) ;
// Stores a Map<String,Object> value in the lookup table
public DeviceContext put(String name, Map<String, Object> value) ;
// Retrieve a stored lookup value in its original format
public Object get(String name);
// Retrieve a stored lookup value in its original format, if not found returns the passed def value
public Object get(String name, Object def);
Example
def inited = device.get("inited", false)
if(!inited){
device.property("statistics", ['counter':0]);
}
def stats = device.property("statistics");
stats.counter = ++stats.counter;
long diff = (utils.millis() - device.get().getReportedStamp().getTime())/1000;
stats['connected'] = (diff <= 180) ? true : false;
//update the property
device.property("statistics", stats); //Property storage
device.put('inited', true); //Lookup variable
Updated about 5 years ago