You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sorry for posting this late, but I couldn't join the last two days of the PF and I didn't have time to create a proper project description.
In this plug-fest, I played a little bit with the third proposal of the Hypermedia protocol for actions. I created a repository with a bunch of testing scripts that I used to evaluate the idea.
Here I'm reporting the consumer side because I think it helps to understand the advantages of this new approach.
Greenfield
For new WoT aware ExposedThings the consumer can just assume that action may return an ActionDescription and use it to interact further with the action instance. Notice how no id tracking is need, the code flow is really similar to what the consumer already does for a normal WebThing:
WoTHelpers.fetch("http://localhost:8080/testingactions").then(asynctd=>{constthing=awaitWoT.consume(td);if(!td.actions.longRunning||td.actions.longRunning.output.model.href!=="http://localhost:8888/model.tm.json"){console.log("Long Running action is synchronous");awaitthing.invokeAction("longRunning");return;}console.log("Long Running action is asynchronous");constactionInstance=await(awaitthing.invokeAction("longRunning")).value();console.log("Action invoked",actionInstance.id);// from here on I can use the action as if it is a webthingconstactionThingHandler=awaitWoT.consume(actionInstance);console.log("Monitoring",actionInstance.id);constinterval=setInterval(async()=>{try{conststatus=await(awaitactionThingHandler.readProperty("status")).value();console.log("Status",status);if(status.status==="completed"||status.status==="cancelled"){console.log("Action has stopped:",status.status);clearInterval(interval);}if(Math.random()<0.1){// cancel with action with 10% probabilityconsole.log("Cancelling action");awaitactionThingHandler.invokeAction("cancel");}}catch(error){console.log("Can't read",error);}},1000);}).catch(e=>{console.log(e);});
Brownfield
For brownfield ExposedThings (i.e., devices that already have their own API and we cannot return an Action Description from an API call). The consumer has to do additional operations to obtain the final ActionDescription but I think it is reasonably easy to then interact with the action instance.
Here's the script:
constpointer=require('jsonpointer');WoTHelpers.fetch("http://localhost:8080/testingactions").then(asynctd=>{constthing=awaitWoT.consume(td);if(!td.actions.longRunning||td.actions.longRunning.output.model.href!=="http://localhost:3000/model-brown.tm.json"){console.log("Long Running action is synchronous");awaitthing.invokeAction("longRunning");return;}console.log("Long Running action is asynchronous");if(!td.actions.longRunning.output.model.mappings){console.log("Long Running action has not mappings -> is a green field device skipping");console.log("See example: consumer.js");return;}constactionInstance=await(awaitthing.invokeAction("longRunning")).value();console.log("Action invoked",actionInstance.id);//Create a "Action Description" from actionInstance and tmconsttm=awaitWoTHelpers.fetch(td.actions.longRunning.output.model.href);lettmString=JSON.stringify(tm);constmappings=td.actions.longRunning.output.model.mappings;for(constpintd.actions.longRunning.output.model.mappings){constvariable=mappings[p];constvalue=pointer.get(actionInstance,p);constexp=newRegExp(`{{${variable}}}`,'g');tmString=tmString.replace(exp,value);}constactionDescriptor=JSON.parse(tmString);// action description contains the TD that can be used to interact with the actionconstactionThingHandler=awaitWoT.consume(actionDescriptor);constinterval=setInterval(async()=>{try{conststatus=await(awaitactionThingHandler.readProperty("status")).value();console.log("Status",status);if(status.status==="completed"||status.status==="cancelled"){console.log("Action has stopped:",status.status);clearInterval(interval);}if(Math.random()<0.1){// cancel with action with 10% probabilityconsole.log("Cancelling action");awaitactionThingHandler.invokeAction("cancel");}}catch(error){console.log("Can't read",error);}},1000);}).catch(e=>{console.log(e);});
I want to underline that all of this already works with node-wot as it is, no API changes are needed.
The text was updated successfully, but these errors were encountered:
Sorry for posting this late, but I couldn't join the last two days of the PF and I didn't have time to create a proper project description.
In this plug-fest, I played a little bit with the third proposal of the Hypermedia protocol for actions. I created a repository with a bunch of testing scripts that I used to evaluate the idea.
Here I'm reporting the consumer side because I think it helps to understand the advantages of this new approach.
Greenfield
For new WoT aware ExposedThings the consumer can just assume that action may return an ActionDescription and use it to interact further with the action instance. Notice how no id tracking is need, the code flow is really similar to what the consumer already does for a normal WebThing:
Brownfield
For brownfield ExposedThings (i.e., devices that already have their own API and we cannot return an Action Description from an API call). The consumer has to do additional operations to obtain the final ActionDescription but I think it is reasonably easy to then interact with the action instance.
Here's the script:
I want to underline that all of this already works with
node-wot
as it is, no API changes are needed.The text was updated successfully, but these errors were encountered: