Accessing Data from Other Resources
This topic describes how data from other resources, can be read programmatically using an Edge Action, and how to write a script to read a given resource via the Datahub
object.
The previous topic described how to create a basic Edge Action on an Octave-enabled edge device that executes JavaScript to process data.
This topic covers how to implement the following logic within an Edge Action:
- reading data from an 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 light sensor in an Edge Action and send the data to the cloud.
Note
Before you continue, you must first have an Observation that reads from the
/redSensor/light/value
Resource, and has been set set to Send to an Edge Action.
- Navigate to Device > Edge Actions.
- Click Add Edge Action.
- Click on Source Observation and select the Observation corresponding to the
/redSensor/light/value
Resource. - Click on Edge action name and enter a descriptive name.
- Click on the Documentation tab. Octave provides a number of example scripts that you can use as a starting point.
- Select Read from Datahub and copy and paste the code into the Code section. The code should look as follows:
function(event) {
var reading = Datahub.read('/redSensor/light/value', 0).value;
var light_reading = reading;
}
Note
This code reads the current level of light from the light sensor. The value returned from
read()
is a JSON object which includes both the value and a timestamp.
The second parameter named timeout must be set to 0 (zero).
- Add the following line to the function:
return {
'cl://': [light_reading],
}
This line 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.
- Click Save.
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 the Datahub's Query()
method in the Edge Action's script, 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.
- Use the steps in Reading Simple Sensors to set up an Edge Action.
- Replace the code in Action Runner that invokes
Datahub.read()
so that it invokesDatahub.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 = Datahub.query('my_observation', 'mean', 10000)
Sending New Events
To specify where data is sent to from an Edge Action, set the target in the JSON returned by the Edge Action's script 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.
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]
}
}
Updated over 4 years ago