Skip to content

Commit

Permalink
refactor: use type-check JS instead of TS for quickstart examples
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 12, 2024
1 parent 8adf840 commit 463128e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 356 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
27 changes: 20 additions & 7 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,22 +12,36 @@
*
* 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();
servient.addServer(
new binding_http_1.HttpServer({
new HttpServer({
port: 8081,
})
);
core_1.Helpers.setStaticAddress("plugfest.thingweb.io"); // comment this out if you are testing locally

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

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.

167 changes: 0 additions & 167 deletions packages/examples/src/quickstart/simple-coffee-machine.ts

This file was deleted.

Loading

0 comments on commit 463128e

Please sign in to comment.