From e3bfa1131cbe2f2f4bcf2d78b9647aebf0ec51c1 Mon Sep 17 00:00:00 2001 From: danielpeintner Date: Wed, 29 Jan 2020 09:57:14 +0100 Subject: [PATCH] feat: create readme for CoAP binding package see https://github.com/eclipse/thingweb.node-wot/issues/157 --- packages/binding-coap/README.md | 90 +++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 packages/binding-coap/README.md diff --git a/packages/binding-coap/README.md b/packages/binding-coap/README.md new file mode 100644 index 000000000..24eb307be --- /dev/null +++ b/packages/binding-coap/README.md @@ -0,0 +1,90 @@ +# CoAP Protocol Binding of node-wot + +## Getting Started + +In the following examples it is shown how to use the CoAP binding of node-wot. + +### Prerequisites +* `npm install @node-wot/core` +* `npm install @node-wot/binding-coap` + +### Client Example + +The client example tries to connect to a TestThing via CoAP and reads a property `string`. The ThingDescription is located under the follwing CoAP uri coap://plugfest.thingweb.io:5683/TestThing. + +`node example-client.js` + +``` +// example-client.js +Servient = require("@node-wot/core").Servient +CoapClientFactory = require("@node-wot/binding-coap").CoapClientFactory + +Helpers = require("@node-wot/core").Helpers + +// create Servient and add CoAP binding +let servient = new Servient(); +servient.addClientFactory(new CoapClientFactory(null)); + +let wotHelper = new Helpers(servient); +wotHelper.fetch("coap://plugfest.thingweb.io:5683/TestThing").then(async (td) => { + // using await for serial execution (note 'async' in then() of fetch()) + try { + servient.start().then((WoT) => { + WoT.consume(td).then((thing) => { + // read a property "string" and print the value + thing.readProperty("string").then((s) => { + console.log(s); + }); + }); + }); + } catch (err) { + console.error("Script error:", err); + } +}).catch((err) => { console.error("Fetch error:", err); }); +``` + +### Server Example + +The server example produces a thing that allows for setting a property `count`. The thing is reachable through CoAP. Additional additional handlers could be added. + +`node example-server.js` + +``` +// example-server.js +Servient = require("@node-wot/core").Servient +CoapServer = require("@node-wot/binding-coap").CoapServer + +Helpers = require("@node-wot/core").Helpers + +// create Servient add HTTP binding +let servient = new Servient(); +servient.addServer(new CoapServer()); + +servient.start().then((WoT) => { + WoT.produce({ + "@context": "https://www.w3.org/2019/wot/td/v1", + title: "MyCounter", + properties: { + count: { + type: "integer" + } + } + }).then((thing) => { + console.log("Produced " + thing.title); + thing.writeProperty("count", 0) + + thing.expose().then(() => { + console.info(thing.title + " ready"); + console.info("TD : " + JSON.stringify(thing.getThingDescription())); + thing.readProperty("count").then((c) => { + console.log("cound is " + c); + }); + }); + }); +}); +``` + + +### More Details + +see https://github.com/eclipse/thingweb.node-wot/