Skip to content

Add Node-WoT client example to README #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 78 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,12 @@ See a list of currently supported possibilities while using this package [below]

> You may use a script deployment/automation tool to remote stop and start servers, in an attempt to remotely control your hardware scripts.

### A little more about Usage
### Using APIs and Thing Descriptions

```
http://localhost:8000/<thing-name>/resources/wot-td
http://localhost:8000/<thing-name>/resources/wot-td?ignore_errors=true
```

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
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>
Expand All @@ -326,6 +331,76 @@ If there are errors in generation of Thing Description

(client docs will be updated here next, also check official docs)

### Consuming Thing Descriptions using Node-WoT (Javascript)

`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:

Example TD for a device instance named `spectrometer`:

```
http://localhost:8000/spectrometer/resources/wot-td
```

Consume this TD in a Node.js script using Node-WoT:

```js
const { Servient } = require("@node-wot/core");
const HttpClientFactory = require("@node-wot/binding-http").HttpClientFactory;

const servient = new Servient();
servient.addClientFactory(new HttpClientFactory());

servient.start().then((WoT) => {
fetch("http://localhost:8000/spectrometer/resources/wot-td")
.then((res) => res.json())
.then((td) => WoT.consume(td))
.then((thing) => {
thing.readProperty("integration_time").then(async(interactionOutput) => {
console.log("Integration Time: ", await interactionOutput.value());
});

```
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.

```
servient.addClientFactory(new Wot.Http.HttpsClientFactory({ allowSelfSigned : true }))
```
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).

After consuming the TD, you can:

<details>

thing.readProperty("integration_time").then(value => {
console.log("Integration Time:", value);
});

thing.writeProperty("integration_time", 2000).then(() => {
console.log("Integration Time updated");
});

thing.invokeAction("connect", { serial_number: "S14155" }).then(() => {
console.log("Device connected");
});

thing.subscribeEvent("intensity_measurement_event", (data) => {
console.log("Received event:", data);
});
</details>

> 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).

> Note: due to reverse proxy buffering, subscribeEvent may take up to 1 minute to receive data. All other operations work fine.

In React, these calls can be placed inside `useEffect` and the client passed via `useContext`.

<details>

For React examples using Node-WoT, refer to:
- [example1](https://gitlab.com/hololinked/examples/clients/node-clients/phymotion-controllers-app/-/blob/main/src/App.tsx?ref_type=heads#L96)
- [example2](https://gitlab.com/hololinked/examples/clients/node-clients/phymotion-controllers-app/-/blob/main/src/components/movements.tsx?ref_type=heads#L54)
</details>

### Currently Supported

- control method execution and property write with a custom finite state machine.
Expand All @@ -338,11 +413,11 @@ If there are errors in generation of Thing Description
- run direct ZMQ-TCP server without HTTP details
- serve multiple objects with the same HTTP server, run HTTP Server & python object in separate processes or the same process

Again, please check examples or the code for explanations. Documentation is being activety improved.
Again, please check examples or the code for explanations. Documentation is being actively improved.

### Contributing

See [organization info](https://github.com/hololinked-dev) for details regarding contributing to this package. There is:
- discord group - [![Discord](https://img.shields.io/discord/1265289049783140464?label=Discord%20Members&logo=discord)](https://discord.com/invite/kEz87zqQXh)
- [weekly meetings](https://github.com/hololinked-dev/#monthly-meetings) and
- [project planning](https://github.com/orgs/hololinked-dev/projects/4) to discuss activities around this repository.
- [project planning](https://github.com/orgs/hololinked-dev/projects/4) to discuss activities around this repository.