A .NET Standard 2.0 implementation of the W3C Web of Things (WoT) Scripting API
$ dotnet add package WoT.Net.CoreA .NET Standard 2.0 implementation of the W3C Web of Things (WoT) Scripting API, inspired by node-wot
The W3C Web of Things is a standardization effort that aims to facilitate the interoperability between the highly fragmented IoT technologies. Instead of imposing new technologies, WoT aims to provide a standardized model for modeling and describing the capabilities and the network/web interface that any IoT entity is providing. In the context of WoT, the capabilities of any entity are modeled as one of three interaction affordances:
Each entity is then capable of describing its own WoT interface using a standardized description format called the Thing Description (TD), a JSON-LD document that is both highly human- and machine-readable and contains the entity's WoT model and any additional related metadata.
Our long-term goal here is to provide the .NET Standard 2.0 stack that fully implements the Scripting API, which would facilitate rapid development of WoT applications, as well as also facilitating the integration of the WoT stack in Unity. Our short-term goal is to implement the functionalities of a WoT Consumer, i.e. the functionalities needed to fetch a TD and consume it to interact with the entity it describes. We will focus first on HTTP/S Things but aim to implement functionality for CoAP, CoAPS, and MQTT in the future.
This library provides a .NET Standard 2.0 implementation of the W3C WoT Scripting API. It provides the following functionalities:
You can install the library via NuGet Package Manager Console by running the following command:
Install-Package WoT.Net.Core
or by using the .NET CLI:
dotnet add package WoT.Net.Core
To use this library you first need to install it and then install any protocol bindings you want to use.
Here is a simple example showing how to use this library to consume a Thing Description (TD) and interact with the entity it describes using the HTTP/S protocol binding.
This example shows how to read properties, invoke actions, and subscribe to events of a Thing Description that is hosted at http://plugfest.thingweb.io:80/http-data-schema-thing/.
using WoT.Core.Implementation;
using WoT.Binding.Http;
using WoT.Core.Definitions.TD;
using Newtonsoft.Json;
Consumer consumer = new();
HttpClientConfig clientConfig = new()
{
AllowSelfSigned = true
};
consumer.AddClientFactory(new HttpClientFactory(new HttpClientConfig()));
consumer.AddClientFactory(new HttpsClientFactory(clientConfig));
consumer.Start();
ThingDescription td = await consumer.RequestThingDescription("http://plugfest.thingweb.io:80/http-data-schema-thing/");
ConsumedThing consumedThing = (ConsumedThing)consumer.Consume(td);
// Read a boolean
bool boolean = await (await consumedThing.ReadProperty<bool>("bool")).Value();
Console.WriteLine("Read a boolean: " + boolean);
// Read an integer
int integer = await (await consumedThing.ReadProperty<int>("int")).Value();
Console.WriteLine("Read an integer: " + integer);
// Read a number
float number = await (await consumedThing.ReadProperty<float>("num")).Value();
Console.WriteLine("Read a number: " + number);
// Read a string
string str = await (await consumedThing.ReadProperty<string>("string")).Value();
Console.WriteLine("Read a string: " + str);
// Read an array
object[] array = await (await consumedThing.ReadProperty<object[]>("array")).Value();
Console.WriteLine("Read an array: " + JsonConvert.SerializeObject(array));
// Read an object
Dictionary<string, object> obj = await (await consumedThing.ReadProperty<Dictionary<string, object>>("object")).Value();
Console.WriteLine("Read an object: " + JsonConvert.SerializeObject(obj));
// Invoke Action with no input and no output
await consumedThing.InvokeAction("void-void");
// Invoke Action with an input and no output
await consumedThing.InvokeAction("int-void", 1);
// Invoke Action with no input but an output
var outputBuffer = await (await consumedThing.InvokeAction<int>("void-int")).ArrayBuffer();
// Buffer to string
string outputJson = System.Text.Encoding.UTF8.GetString(outputBuffer);
// Deserialize JSON
int output = JsonConvert.DeserializeObject<int>(outputJson);
Console.WriteLine("Output of 'void-int' action was: " + output);
// Invoke Action with an input and an output
int output2 = await (await consumedThing.InvokeAction<int, int>("int-int", 4)).Value();
Console.WriteLine("Output of 'void-int' action was: " + output2);
if (consumedThing != null)
{
// Subscribe to Event
var sub = await consumedThing.SubscribeEvent<int>("on-int", async (output) =>
{
Console.WriteLine("Event: Received on-int event");
Console.WriteLine($"Value received: {await output.Value()}");
Console.WriteLine("---------------------");
});
var task = Task.Run(async () =>
{
while (sub.Active)
{
// Get random integer between 0 and 100
Random random = new();
int randomInt = random.Next(0, 100);
Console.WriteLine($"Writing int {randomInt}");
Console.WriteLine("---------------------");
// Write an int
await consumedThing.WriteProperty("int", randomInt);
await Task.Delay(TimeSpan.FromSeconds(1));
}
return;
});
Task stopTask = Task.Delay(TimeSpan.FromSeconds(10)).ContinueWith(async (task) => { await sub.Stop(); });
await task;
}
Console.WriteLine("Done");
For a more detailed step-by-step explanation and documentation, feel free to visit our documentation page.
The library provides a way to extend the consumer implementation by adding different protocol bindings.
This can be done by implementing the IClient and IClientFactory interfaces, described in the WoT.Core.Definitions.Client namespace.
... more in the future