Define a collection owner

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-update-value-with-owner .

Description

In this tutorial you will learn how to define a collection owner. This owner will update a value when the associated collection is updated. The value will then contain the updated element from the collection. We will then use a callback to call a script each time the value is updated.

If you have never created a collection, you can follow this example to create one.

Steps

1. Define a new value

Create a value.ospp file with a TEXT type:

root/collections/owner/value.ospp

{
    "name": "Device",
    "description": "Last updated ip device",
    "type": "TEXT"
}

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.

root/collections/owner/owner.collections

{
    "schemaId": "root.collections.device",
    "filterId": "filter_status_connected",
    "type": "ON_COLLECTION"
}

1. 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 device = JSON.parse(trigger.content);
    log.info("Updated device contains : " + JSON.stringify(device) + "");

    //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"
        }
    ]
}