Skip to content

Commit

Permalink
refactor: use type-checked JS instead of TS for quickstart examples
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Feb 27, 2024
1 parent 0b7b108 commit 9cf49fc
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 403 deletions.
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
29 changes: 21 additions & 8 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,39 @@
*
* 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;
servient.addServer(
new binding_http_1.HttpServer({
port: httpPort,
new HttpServer({
port: 8081,
})
);
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
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

0 comments on commit 9cf49fc

Please sign in to comment.