Scripting: Discover and Consuming Things

Starting the Discovery Process

First, open the script module, create a new script, and open the script editor. These steps are described in detail here: Writing Scripts.

To get access to the WoT functionality, include the ELCO WoT module next. Therefore, add this line at the top of the script:

const WoT = require("elco-wot");

In order to search for things, it is necessary to create a so-called fragment that describes the thing. In this case, search for a thing with the title TemperatureSensor and the type virtual.virtual (for all available thing types, please refer to Everything is a Thing):

let fragment = {
    title: "TemperatureSensor",
    type: "virtual.virtual"
};

Now, use the fragment to initialize a ThingDiscovery object that helps to control the discovery process.

let discovery = WoT.discover({
    fragment: fragment
});

Next, pass the ThingDiscovery object to a function that will be written and explained in the subsequent section:

discoverAndConsumeThing(discovery);

Checking the Results

Define the function discoverAndConsumeThing:

async function discoverAndConsumeThing(discovery) {
    // the following code goes here
}

Then, use the discovery object to start the discovery process by calling the start() function:

discovery.start();

If the thing fragment defined above matches multiple things the result contains more than one element. For example, this will be the case if multiple things with the name TemperatureSensor and the type virtual.virtual exist. Therefore, add a loop to iterate over all results:

while (!discovery.done) {
    // the following code goes here
}

As a next step, get a result from the discovery object. Therefore, initialize an empty thing description first and then try to assign the next thing found by the discovery. For simplicity reasons, the error handling is not covered here but can be found in the Putting it all Together section below.

let thingDescription = {};
try {
    thingDescription = await discovery.next();
} catch (err) {
    // for error handling check the whole script in "Putting it all together" below
}

If no error occurred, the thingDescription variable now contains a ThingDescription of the TemperatureSensor thing. Since it is only a description, we need to consume it in order to be able to read and write properties. The next section describes how to do this.

Consuming the Thing

As stated before, the discovery only provides a ThingDescription object. To be able to actually read, write and observe properties, it is necessary to consume this description first. Therefore, initialize an empty consumedThing variable inside the discoverAndConsumeThing function first:

let consumedThing = {};

Next, use consume the thing description with the WoT.consume function. If an error occurs, print it to the console and cancel the process.

try {
    consumedThing = await WoT.consume(thingDescription);
} catch (err) {
    console.log(err);
    return;
}

If everything went well, use the consumedThing to read, write and observe properties as described in Reading, Writing and Observing Properties now.

Putting it all together

Following all the steps above correctly, the script should look like this:

const WoT = require("elco-wot");

//create a fragment that describes the thing
let fragment = {
    title: "TemperatureSensor",
    type: "virtual.virtual"
};

// use the fragment to initialize the ThingDiscovery object
let discovery = WoT.discover({
    fragment: fragment
});

// call the discoverAndConsumeThing method defined below
discoverAndConsumeThing(discovery);

// this method implements the actual thing discovery and consumption
async function discoverAndConsumeThing(discovery) {

    // start the discovery process
    discovery.start();

    // while there are new results:
    while (!discovery.done) {

        // initialize an empty thing description
        let thingDescription = {};

        // try to get the next thing description and handle errors if they occur
        try {
            thingDescription = await discovery.next();
        } catch (err) {
            if (err.message === "not found") {
                // No thing matching the fragment could be found
                discovery.stop();
                console.log("Thing was not found.");
            } else {
                // Some other error occurred, print it to the console
                discovery.stop();
                console.log(err.message);
            }
            // return on an error
            return;
        }

        // initialize an empty consumed thing
        let consumedThing = {};

        //consume the thing description and handle errors if they occur
        try {
            consumedThing = await WoT.consume(thingDescription);
        } catch (err) {
            // Some other error occurred. Print it to the console and return
            console.log(err);
            return;
        }

        console.log("Discovered thing: " + consumedThing.getThingDescription().title);

        // Here you can observe, read and write the properties of the consumedThing
    }
}

Testing the Script

Upload () and start () the script (see Process Data inside the IoTHub with Scripts for details on how to do this). The Output tab will be opened and after a while, the message Discovered thing: TemperatureSensor should appear:

Output
Output of discover thing

Click at the Refresh Output button () to get the latest output.

Last Updated: