Accessing Data from Other Resources

This topic describes how data from other Resources can be read programmatically from the Datahub in your Edge Action's logic.

This topic covers how to implement the following logic within an Edge Action:

  • reading data from a sensor and writing it to the cloud.
  • applying a function to buffered data collected over a certain time range (e.g., calculating the average from a series of values buffered over two seconds).

In addition, this topic also shows different ways to programmatically route events via an Edge Action (e.g., send them to the cloud).

Reading Simple Sensors

The steps below illustrate how to read data from the mangOH Red's light sensor in an Edge Action and send that data to the cloud.

📘

Note

Before you continue, you must first have an Observation that reads from the /redSensor/light/value sensor resource on a mangOH Red, and has been configured Send to an Edge Action .

  1. Create an Edge Action. Depending on the device's firmware versions, set its Source Observation to the Observation corresponding to the /redSensor/light/value Resource, or add the Observation as a source.
  2. Modify the code to read as follows and click Save to apply it:
function(event) {
  var light_reading = Resource.readValue('/redSensor/light/value');
    
  return {
  'cl://': [light_reading]
  }
}

This code reads the current level of light from the light sensor using Resource.readValue(). The value returned from Resource.readValue() is the resource value, in this example an integer.

The return statement writes the value to the cloud as indicated by cl in the JSON that the function returns. For more information see Sending New Events. The event will be stored in the stream :/default of the device in the cloud.

Applying Functions to Events in a Buffer

An Edge Action can perform functions on events that have been buffered by an Observation. This is accomplished by invoking Datahub.Query() method in your Edge Action's logic, which allows you to specify the function (e.g. min, max, etc.) and the time window of buffered events on which to perform the function.

Follow the steps below to apply the mean function on buffered events.

  1. Use the steps in Reading Simple Sensors to set up an Edge Action.
  2. Replace the code that invokes Datahub.read() so that it invokes Datahub.query(). The following example specifies that the mean should be calculated using data that was stored in the buffer for the first 10000 seconds:
var queryResult = Observation.getMean('my_observation', 10000)

Sending New Events

To specify where data is sent to from an Edge Action, set the target in the JSON returned to cl for cloud, st for store and forward, vr for a virtual resource, or dh for another Resource on the device. The following subsections show example code for each of these cases. For additional information see the Output section of the Edge Action Runner Reference.

Sending Events to the Cloud Immediately

function(event) {
   ...
   var result = {};
   result["sample"] = 12;
   result["sensor"] = "pressure";
   ... 
   return {
      "cl://": [result],
   }
}

Sending Events to Store and Forward

function(event) {
   ...
   var result = {};
   result["sample"] = 12;
   result["sensor"] = "pressure";
   ... 
   return {
      "st://": [result],
   }
}

Sending Events to a Virtual Resource

function(event) {
   ...
   var result = {};
   result["sample"] = 12;
   result["sensor"] = "pressure";
   ... 
 
   return {
      "vr://myJSonVR": [result],
      "vr://myNumericVR": [2020]
   }
}

Update Multiple Events in a Virtual Resource using Array

function(event) {
   ...
   var events = [];
   events.push({"sample": 12, "sensor": "pressure"});
   events.push({"sample": 34, "sensor": "temperature"});
   ... 
 
   return {
      "vr://myJSonVR": [events]
   }
}

Sending Events to Another Resource

function(event) {
   ...

   return {
      "dh://lcd/txt1": ["Hello!"],
      "dh://io/myDigitalIO/value": [true]
   }
}