Skip to content
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

refactor: use type-checked JS instead of TS for quickstart examples #1215

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion examples/quickstart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ These Things are:
- Presence sensor that emits an event when a person is detected.
- Smart clock that runs 60 times faster than real time, where 1 hour happens in 1 minute.

These Things are hosted on plugfest.thingweb.io but can be also self-hosted.
These Things are hosted under https://www.thingweb.io/services, but can also be self-hosted.
17 changes: 12 additions & 5 deletions examples/quickstart/presence-sensor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"use strict";
/********************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
Expand All @@ -13,13 +12,21 @@
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/

// @ts-check

"use strict";

// This is an example Thing script which is a simple presence detector
// It fires an event when it detects a person (mocked as every 5 second)
const core_1 = require("@node-wot/core");
const binding_mqtt_1 = require("@node-wot/binding-mqtt");

const { Servient } = require("@node-wot/core");
const { MqttBrokerServer } = require("@node-wot/binding-mqtt");

// create Servient add MQTT binding with port configuration
const servient = new core_1.Servient();
servient.addServer(new binding_mqtt_1.MqttBrokerServer({ uri: "mqtt://test.mosquitto.org" }));
const servient = new Servient();
servient.addServer(new MqttBrokerServer({ uri: "mqtt://test.mosquitto.org" }));

servient.start().then((WoT) => {
WoT.produce({
title: "PresenceSensor",
Expand Down
56 changes: 41 additions & 15 deletions examples/quickstart/simple-coffee-machine.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"use strict";
/********************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
Expand All @@ -13,25 +12,41 @@
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/

// @ts-check

"use strict";

// This is an example Thing script which is a simple coffee machine.
// You can order coffee and see the status of the resources
const core_1 = require("@node-wot/core");
const binding_http_1 = require("@node-wot/binding-http");

const { Servient, Helpers } = require("@node-wot/core");
const { HttpServer } = require("@node-wot/binding-http");

// create Servient add HTTP binding with port configuration
const servient = new core_1.Servient();
const servient = new Servient();

// const staticAddress = "plugfest.thingweb.io";
const staticAddress = "localhost"; // use this for testing locally
const httpPort = 8081;
const port = 8081;

servient.addServer(
new binding_http_1.HttpServer({
port: httpPort,
new HttpServer({
port,
})
);
core_1.Helpers.setStaticAddress(staticAddress);

Helpers.setStaticAddress(staticAddress);

let waterAmount = 1000;
let beansAmount = 1000;
let milkAmount = 1000;
// promisify timeout since it does not return a promise

/**
* Function to promisify timeout since it does not return a promise
*
* @param {number} ms
*/
function timeout(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
Expand Down Expand Up @@ -177,24 +192,35 @@ servient.start().then((WoT) => {
}
});
thing.setActionHandler("refill", async (params, options) => {
const selectedResource = await params.value();
console.info("received refill order of ", selectedResource);
if (selectedResource.indexOf("water") !== -1) {
const selectedResources = await params.value();

if (!Array.isArray(selectedResources)) {
console.log(`Received invalid refill order of ${selectedResources}`);
return undefined;
}

console.log(`Received refill order ${selectedResources}`);

if (!selectedResources.includes("water")) {
waterAmount = 1000;
}
if (selectedResource.indexOf("beans") !== -1) {

if (!selectedResources.includes("beans")) {
beansAmount = 1000;
}
if (selectedResource.indexOf("milk") !== -1) {

if (!selectedResources.includes("milk")) {
milkAmount = 1000;
}

thing.emitPropertyChange("resources");

return undefined;
});
// expose the thing
thing.expose().then(() => {
console.info(thing.getThingDescription().title + " ready");
console.info("TD available at http://" + staticAddress + ":" + httpPort);
console.info(`TD available at http://${staticAddress}:${port}/coffee-machine`);
});
})
.catch((e) => {
Expand Down
28 changes: 22 additions & 6 deletions examples/quickstart/smart-clock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"use strict";
/********************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
Expand All @@ -13,15 +12,28 @@
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/

// @ts-check

"use strict";

// This is an example Thing which is a smart clock that runs 60 times faster than real time, where 1 hour happens in 1 minute.
const core_1 = require("@node-wot/core");
const binding_coap_1 = require("@node-wot/binding-coap");

const { Servient, Helpers } = require("@node-wot/core");
const { CoapServer } = require("@node-wot/binding-coap");

// create Servient add CoAP binding with port configuration
const servient = new core_1.Servient();
servient.addServer(new binding_coap_1.CoapServer(5686));
core_1.Helpers.setStaticAddress("plugfest.thingweb.io"); // comment this out if you are testing locally
const servient = new Servient();
servient.addServer(new CoapServer({ port: 5686 }));

Helpers.setStaticAddress("plugfest.thingweb.io"); // comment this out if you are testing locally

let minuteCounter = 0;
let hourCounter = 0;

/**
* @param {WoT.ExposedThing} thing
*/
async function timeCount(thing) {
for (minuteCounter = 0; minuteCounter < 59; minuteCounter++) {
// if we have <60, we can get a 15:60.
Expand All @@ -32,11 +44,13 @@ async function timeCount(thing) {
hour: hourCounter,
minute: minuteCounter,
});

hourCounter++;
if (hourCounter === 24) {
hourCounter = 0;
}
}

servient.start().then((WoT) => {
WoT.produce({
title: "Smart Clock",
Expand Down Expand Up @@ -71,11 +85,13 @@ servient.start().then((WoT) => {
minute: minuteCounter,
};
});

timeCount(thing);
setInterval(async () => {
timeCount(thing);
thing.emitPropertyChange("time");
}, 61000); // if this is 60s, we never leave the for loop

// expose the thing
thing.expose().then(() => {
console.info(thing.getThingDescription().title + " ready");
Expand Down
10 changes: 0 additions & 10 deletions packages/examples/src/quickstart/README.md

This file was deleted.

64 changes: 0 additions & 64 deletions packages/examples/src/quickstart/presence-sensor.ts

This file was deleted.

Loading
Loading