Virtual Resources
Virtual Resources are resources which are user-defined and are not associated with a physical measurement or device property. Virtual resources are useful for:
- capturing and monitoring state information
- storing global values for edge processing
- aggregating multiple resource values together into a single resource that can be monitored in a single stream
The following subsections describe how to create, read/write, and store state information in virtual resources within Octave.
Creating a Virtual Resource
Virtual Resource are added to your device from the Device > Resources screen.
- Navigate to Device > Resources
- Click the Add Virtual Resource button
![vr_1.png 1013](https://files.readme.io/b844253-vr_1.png)
- Define the Virtual resource
In the following example we create a Virtual Resource of type JSON and we don't provide any Configured value.
Click Add and Apply to finalize the creation.
![vr_2.png 768](https://files.readme.io/da1fe4d-vr_2.png)
The configuration is sent to the device, which will create the Virtual Resource, it will appear after few seconds in the Resource tree:
![vr_3.png 1126](https://files.readme.io/057d798-vr_3.png)
Write in a Virtual Resource
The instruction to write in the Virtual Resource /virtual/report from an edge action is:
function(event) {
// edge action code
// ...
var report = {
"temperature": temperature,
"pressure": pressure,
"location": { "lat": lat, "lon": lon },
"light": light,
"signal_bars": bars,
"cell_technology": technology
}
return {
"vr://report": [report] // or
"dh://virtual/report/value": [report]
}
}
Let's update the Virtual Resource we created previously with sensor readings we gather from our device, so we create a device report, we can send to the cloud periodically.
Here are some examples for mangOH red:
mangOH red
function(event) {
var temperature = Datahub.read("/redSensor/imu/temp/value",0).value;
var pressure = Datahub.read("/redSensor/pressure/value",0).value;
var lat = 0;
var lon = 0;
var pos = Datahub.read("/location/coordinates/value",0);
if (pos) {
lat = pos.lat;
lon = pos.lon
}
var light = Datahub.read("/redSensor/light/value",0).value;
var bars = Datahub.read("/util/cellular/signal/value",0).value.bars;
var technology = Datahub.read("/util/cellular/signal/value",0).value.rat;
var report = {
"temperature": temperature,
"pressure": pressure,
"location": { "lat": lat, "lon": lon },
"light": light,
"signal_bars": bars,
"cell_technology": technology
}
return {
"vr://report": [report]
}
}
Read a Virtual Resource
The instruction to read the value of a Virtual Resource is:
function(event) {
//edge action code
// ...
var report = Datahub.read("/virtual/report/value",0).value;
// report will contain the JSON object
}
Observe the Virtual Resource
Like any other resource, a Virtual Resource can be observed:
![vr_4.png 766](https://files.readme.io/800a5ad-vr_4.png)
In this example, the Virtual Resource updates are reported in the stream report of your device:
![vr_5.png 1054](https://files.readme.io/48f6075-vr_5.png)
Use Virtual Resources to store state
Virtual Resources configured in /virtual/config are persisted in the non volatile memory of the device. Check out the section about Resources for more details.
In the following example we use a Virtual Resource to store a state, and send an alert to the Cloud.
- Navigate tio the Device > Resources screen
- Add Virtual Resource lightThreshold of type Numeric, with Configured value of 1500
- Add another Virtual Resource tooMuchLight of type Boolean false
- Apply the changes
![vr_7.png 696](https://files.readme.io/2a0f43f-vr_7.png)
- Add an observation to the resource redSensor/light/value to an edge action
![vr_6.png 765](https://files.readme.io/a420507-vr_6.png)
- Write the following edge action to detect and report light alerts
function(event) {
var light_reading = event.value;
var light_threshold = Datahub.read("/virtual/lightThreshold/value",0).value;
var previous_state = Datahub.read("/virtual/tooMuchLight/value",0).value;
if(previous_state===false) {
if(light_reading >= light_threshold) {
//send alert and store state
return {
"cl://": [{"alert": "tooMuchLight", "lightReading": light_reading, "lightThreshold": light_threshold}],
"vr://tooMuchLight": [true]
}
}
// else do nothing: no alert
}
// else do nothing, alert already raised
}
- You can change the light intensity on your device until the event "toMuchLight" is sent to the Cloud
![vr_8.png 1105](https://files.readme.io/50e81b9-vr_8.png)
The alert is sent only once.
You can also check that the Virtual Resource tooMuchLight value has changed to true:
![vr_9.png 559](https://files.readme.io/9c62abc-vr_9.png)
- You can now reset the status of the alert sending a command to the device, setting false to the Virtual Resource tooMuchLight:
- Navigate to the Device > Resources screen
- Locate the the virtual resource tooMuchLight
- Click the Send command button
![vr_10.png 1319](https://files.readme.io/994d75b-vr_10.png)
- Enter the new value. The screen is showing the previous command you sent.
- and click the Send button
![vr_11.png 769](https://files.readme.io/a6476fa-vr_11.png)
- You can see the command is actually sent checking the Recent changes widget in the Device details page.
![vr_12.png 552](https://files.readme.io/e64ac02-vr_12.png)
- And also confirm the virtual resource tooMuchLight now reports false in the Device > Resources screen:
![vr_13.png 697](https://files.readme.io/969cf32-vr_13.png)
More details on the Virtual Resources
Check our Resources reference for more information about resources in general, and persistence of Virtual Resources too.
Updated almost 5 years ago