Skip to content

Commit aa2c323

Browse files
committed
update README, minor edits on contributions from @PFKimmerle
1 parent 5cbfc65 commit aa2c323

File tree

1 file changed

+46
-40
lines changed

1 file changed

+46
-40
lines changed

README.md

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -320,23 +320,19 @@ See a list of currently supported possibilities while using this package [below]
320320
321321
### Using APIs and Thing Descriptions
322322

323-
```
324-
http://localhost:8000/<thing-name>/resources/wot-td
325-
http://localhost:8000/<thing-name>/resources/wot-td?ignore_errors=true
326-
```
327-
328323
The HTTP API may be autogenerated or adjusted by the user. If your plan is to develop a truly networked system, it is recommended to learn more and
329-
use [Thing Descriptions](https://www.w3.org/TR/wot-thing-description11) to describe your hardware (This is optional and one can still use a classic HTTP client). A Thing Description will be automatically generated if absent as shown in JSON examples above or can be supplied manually. The default end point to fetch thing descriptions are: <br> `http(s)://<host name>/<instance name of the thing>/resources/wot-td` <br>
330-
If there are errors in generation of Thing Description
331-
(mostly due to JSON non-complaint types), one could use: <br> `http(s)://<host name>/<instance name of the thing>/resources/wot-td?ignore_errors=true`
324+
use [Thing Descriptions](https://www.w3.org/TR/wot-thing-description11) to describe your hardware (This is optional and one can still use a classic HTTP client). A Thing Description will be automatically generated if absent as shown in JSON examples above or can be supplied manually. The default end point to fetch thing descriptions are:
332325

333-
(client docs will be updated here next, also check official docs)
326+
```
327+
http(s)://<host-name>/<instance-name-of-the-thing>/resources/wot-td
328+
http(s)://<host-name>/<instance-name-of-the-thing>/resources/wot-td?ignore_errors=true
329+
```
334330

335-
### Consuming Thing Descriptions using Node-WoT (Javascript)
331+
If there are errors in generation of Thing Description (mostly due to JSON non-complaint types), use the second endpoint which may generate at least a partial but useful Thing Description.
336332

337-
`hololinked` servers expose Thing Descriptions (TDs) which are compatible with Web of Things clients like [Node-WoT](https://github.com/eclipse-thingweb/node-wot). A TD is automatically generated and can be fetched from:
333+
### Consuming Thing Descriptions using node-wot (Javascript)
338334

339-
Example TD for a device instance named `spectrometer`:
335+
The Thing Descriptions (TDs) can be consumed with Web of Things clients like [node-wot](https://github.com/eclipse-thingweb/node-wot). Suppose an example TD for a device instance named `spectrometer` is available at the following endpoint:
340336

341337
```
342338
http://localhost:8000/spectrometer/resources/wot-td
@@ -352,52 +348,62 @@ const servient = new Servient();
352348
servient.addClientFactory(new HttpClientFactory());
353349

354350
servient.start().then((WoT) => {
355-
fetch("http://localhost:8000/spectrometer/resources/wot-td")
356-
.then((res) => res.json())
357-
.then((td) => WoT.consume(td))
358-
.then((thing) => {
359-
thing.readProperty("integration_time").then(async(interactionOutput) => {
360-
console.log("Integration Time: ", await interactionOutput.value());
361-
});
362-
351+
fetch("http://localhost:8000/spectrometer/resources/wot-td")
352+
.then((res) => res.json())
353+
.then((td) => WoT.consume(td))
354+
.then((thing) => {
355+
thing.readProperty("integration_time").then(async(interactionOutput) => {
356+
console.log("Integration Time: ", await interactionOutput.value());
357+
})
358+
)});
363359
```
364-
This works with both http:// and https:// URLs. If you're using HTTPS, just make sure the server certificate is valid or trusted by the client.
360+
This works with both `http://` and `https://` URLs. If you're using HTTPS, just make sure the server certificate is valid or trusted by the client.
365361

366-
```
367-
servient.addClientFactory(new Wot.Http.HttpsClientFactory({ allowSelfSigned : true }))
362+
```js
363+
const HttpsClientFactory = require("@node-wot/binding-http").HttpsClientFactory;
364+
servient.addClientFactory(new HttpsClientFactory({ allowSelfSigned : true }))
368365
```
369366
You can see an example [here](https://gitlab.com/hololinked/examples/clients/node-clients/phymotion-controllers-app/-/blob/main/src/App.tsx?ref_type=heads#L77).
370367

371368
After consuming the TD, you can:
372369

373-
<details>
374-
375-
thing.readProperty("integration_time").then(value => {
376-
console.log("Integration Time:", value);
377-
});
370+
<details open>
371+
<summary>Read Property</summary>
372+
373+
`thing.readProperty("integration_time").then(async(interactionOutput) => {
374+
console.log("Integration Time:", await interactionOutput.value());
375+
});`
376+
</details>
377+
<details open>
378+
<summary>Write Property</summary>
378379

379-
thing.writeProperty("integration_time", 2000).then(() => {
380+
`thing.writeProperty("integration_time", 2000).then(() => {
380381
console.log("Integration Time updated");
381-
});
382+
});`
383+
</details>
384+
<details open>
385+
<summary>Invoke Action</summary>
382386

383-
thing.invokeAction("connect", { serial_number: "S14155" }).then(() => {
387+
`thing.invokeAction("connect", { serial_number: "S14155" }).then(() => {
384388
console.log("Device connected");
385-
});
386-
387-
thing.subscribeEvent("intensity_measurement_event", (data) => {
388-
console.log("Received event:", data);
389-
});
389+
});`
390390
</details>
391+
<details open>
392+
<summary>Subscribe to Event</summary>
391393

392-
> Based on verified examples from the [hololinked examples repository](https://github.com/hololinked-dev/examples/tree/main/client-examples/node-wot) and the [ThingWeb node-wot project](https://github.com/eclipse-thingweb/node-wot).
394+
`thing.subscribeEvent("intensity_measurement_event", async (interactionOutput) => {
395+
console.log("Received event:", await interactionOutput.value());
396+
});`
397+
</details>
393398

399+
Try out the above code snippets with an online example [using this TD].(http://examples.hololinked.net/simulations/spectrometer/resources/wot-td)
394400
> Note: due to reverse proxy buffering, subscribeEvent may take up to 1 minute to receive data. All other operations work fine.
395401

396-
In React, these calls can be placed inside `useEffect` and the client passed via `useContext`.
397-
402+
In React, the Thing Description may be fetched inside `useEffect` hook, the client passed via `useContext` hook and the individual operations can be performed in their own callbacks attached to user elements.
398403
<details>
399-
404+
<summary>Links to Examples</summary>
400405
For React examples using Node-WoT, refer to:
406+
401407
- [example1](https://gitlab.com/hololinked/examples/clients/node-clients/phymotion-controllers-app/-/blob/main/src/App.tsx?ref_type=heads#L96)
402408
- [example2](https://gitlab.com/hololinked/examples/clients/node-clients/phymotion-controllers-app/-/blob/main/src/components/movements.tsx?ref_type=heads#L54)
403409
</details>

0 commit comments

Comments
 (0)