diff --git a/README.md b/README.md index 61f7e1e..dc610cb 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,82 @@ rapid development of WoT applications for devices running Windows OS, including 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 Things but aim to implement functionality for HTTPS, CoAP, CoAPS, and MQTT in the future. +## Getting Started +An example showing how to use this library +```csharp +using Newtonsoft.Json; +using WoT; +using WoT.Definitions; +using WoT.Implementation; + +SimpleHTTPConsumer consumer = new SimpleHTTPConsumer(); +ThingDescription td = await consumer.RequestThingDescription("http://plugfest.thingweb.io:8083/testthing"); +SimpleConsumedThing consumedThing = await consumer.Consume(td) as SimpleConsumedThing; + +// Read a boolean +bool boolean = await (await consumedThing.ReadProperty("bool")).Value(); +Console.WriteLine("Read a boolean: " + boolean); +// Read an integer +int integer = await (await consumedThing.ReadProperty("int")).Value(); +Console.WriteLine("Read an integer: " + integer); +// Read a number +float number = await (await consumedThing.ReadProperty("num")).Value(); +Console.WriteLine("Read a number: " +number); +// Read a string +string str = await (await consumedThing.ReadProperty("string")).Value(); +Console.WriteLine("Read a string: " + str); +// Read an array +object[] array = await (await consumedThing.ReadProperty("array")).Value(); +Console.WriteLine("Read a string: " + JsonConvert.SerializeObject(array)); +// Read an object +Dictionary obj = await (await consumedThing.ReadProperty>("object")).Value(); +Console.WriteLine("Read a string: " + 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 +int output = await (await consumedThing.InvokeAction("void-int")).Value(); +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", 4)).Value(); +Console.WriteLine("Output of 'void-int' action was: " + output2); + +if (consumedThing != null) +{ + + // Subscribe to Event + var sub = await consumedThing.SubscribeEvent("on-int", async (output) => + { + Console.WriteLine("Event:"); + Console.WriteLine(await output.Value()); + Console.WriteLine("---------------------"); + }); + + var task = Task.Run(async () => + { + Console.WriteLine("Here"); + Console.WriteLine(consumedThing.HasActiveListeners); + while (consumedThing.HasActiveListeners) { + // Write an int + await consumedThing.WriteProperty("int", 5); + 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](https://tum-esi.github.io/WoT.Net/). + ## Roadmap - [X] TD Deserializing and Parsing - [X] HTTP Consumer (works only with `"application\json"`, `"longpoll"` and no security)