Define a collection owner with a specific target
Prerequisites
Modules
git checkout origin/osp-collections-configuration .
git checkout origin/osp-scripts-configuration .
Add this example in your configuration
You can directly use the following command to add this example into your configuration:
git checkout origin/example-collections-owner-with-target .
Description
In this tutorial you will learn how to define a collection owner with a specific targetProperty
, allowing us to set a boolean value instead of a text value.
A simpler owner example can be found here, explaining all the basics of collections owner. If you have never used a collection owner before, you might want to check this example first (recommended but not mandatory, this example should be self sufficient).
If you have never created a collection, you can follow this example to create one. This example is based on the collection create with from that previously mentioned example. We will just modify the status property to make it so it is a boolean and not an enum anymore. You can do something similar or just use any of your collections that have a boolean property within.
Steps
1. Define a new value
Create a value.ospp
file with a BOOLEAN type. In this value, we will attempt to retrieve the status of the device (status). But you can change the type of the value according to the property you want to retrieve from the collection:
root/collections/owner/value.ospp
{
"name": "Device_Status",
"description": "Last status of the Device",
"type": "BOOLEAN"
}
2. Define a collection owner to update the value
A collection owner is linked to a collection by specifying :
schemaId
: the id of the collection.filterId
: the id of the filter. If the updated document matches this filter, the value will be updated.type
: Type of operation done when an update is found.
In addition to these parameters, we also need to specify which property from the collection we want to target, using targetProperty
. To demonstrate the usage of the watchList parameter, we will also set it with a field that is different than the targeted property, just to demonstrate that the two parameters are not dependent on each other, but you could set it with the same property too.
root/collections/owner/owner.collections
{
"schemaId": "root.collections.device",
"filterId": "filter_all",
"scope": "FULL_DOCUMENT",
"watchList": [
"ip_address"
],
"targetProperty": "$.status",
"type": "ON_COLLECTION"
}
3. Write a JS script to call with the content of the updated value
In this example, we use a script to do something when the value is updated. For that, we need to have an output.scripts
.
root/collections/on_update/output.scripts
{
"scriptId": "root.collections.on_update"
}
This output is linked to this detached.scripts
.
root/collections/on_update/detached.scripts
{
"moduleId": "modules.scripts.scripts-1",
"sourceFile": "root/collections/on_update/device_updated.js",
"accessedValues": []
}
Which is linked to this script:
root/collections/on_update/device_updated.js
function main() {
let status = JSON.parse(trigger.content);
log.info("Updated device status : " + status + "");
//do something
}
main();
4. Define a callback for the value
The last step is to define a callback to link the value and the script output.
root/collections/owner/callback.ospp
{
"linkedOutputs": [
{
"outputId": "root.collections.on_update"
}
]
}