Scripting: Invoke Actions from Scripts
Prerequisites
First step is to 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 by adding this import instruction at the top of the script:
const WoT = require("elco-wot");
Invoking actions
In order to call an action, you can use the InvokeAction method of an exposed or consumed thing.
thing.invokeAction("action_name", [action input]);
The invokeAction method is asynchronous and therefore returns a promise. If you want to make sure that the action has been invoked before resuming execution, add the await keyword:
await thing.invokeAction("action_name", [action input]);
Sometimes, different actions demand different input types. Check the following examples which use the most common inputs:
Empty Value
thing.invokeAction("action_name");
String Input
thing.invokeAction("action_name", "input_value");
Numeric Input
thing.invokeAction("action_name", 123456);
List Input
thing.invokeAction("action_name", ["input_item_1", "input_item_2", "input_item_3"]);
Object Input
thing.invokeAction("action_name", {"input_item": "input_item_value"});
Example script
Here is a full example about how to call actions from scripts. In this example, we will connect to an ELCO RFID Q-Series Reader device and execute a single scan for 10000 milliseconds, everything done via actions. Moreover, we will show the tags which where read during the scanning process.
const WoT = require("elco-wot");
/** Thing fragment to be discovered */
let fragment = {
title: "AssemblerScanner",
type: "elco.rfid"
};
/** Discovery object */
let discovery = WoT.discover({
fragment: fragment
});
/** Main example */
InvokeActionsExample();
async function InvokeActionsExample() {
/** Discover and consume a thing */
let consumedThing = await DiscoverAndConsumeThing(discovery);
if (consumedThing === null) {
return;
}
console.log("Discovered thing: " + consumedThing.getThingDescription().title);
try {
/** Observe a property: In this case, we will observe the list of
* scanned tags, displaying every value update of the mentioned list
*/
consumedThing.observeProperty("scan", function (newValue) {
console.log("Scanned tags: " + newValue);
});
/** Invoke action connect: If the device is not connected, then
* this action will establish a new connection with it
*/
await consumedThing.invokeAction("connect");
console.log("Invoked action: connect");
/** Invoke action single_scan: This will execute a scanning process
* for 10000 milliseconds (10 seconds)
*/
await consumedThing.invokeAction("single_scan", {"duration": 10000});
console.log("Invoked action: single_scan (duration: 10000 milliseconds)");
} catch (err) {
/** Report an error (if any) */
console.log("Calling actions error: " + err.message);
return null;
}
}
/** Extracted from: Discovering and Consuming Thing
* more information at:
* - https://wiki.elco-automation.de/display/I2UM/Discovering+and+Consuming+a+Thing
*/
async function DiscoverAndConsumeThing(discovery) {
let thingDescription = {};
discovery.start();
do {
try {
thingDescription = await discovery.next();
if (thingDescription !== null) {
discovery.stop();
}
} catch (err) {
discovery.stop();
if (err.message === "not found") {
console.log("Thing not found");
} else {
console.log(err.message);
}
return null;
}
} while (!discovery.done);
try {
let consumedThing = await WoT.consume(thingDescription);
return consumedThing;
} catch (err) {
console.log(err.message);
return null;
}
}
Output Example
Discovered thing: AssemblerScanner
Invoked action: connect
Invoked action: single_scan (duration: 10000 milliseconds)
Scanned tags:
Scanned tags: E2009A4120010AF000002551
Scanned tags: E2009A4120010AF000002551,E2009A4120010AF000001985