From 56c7e16e002b739405d048a7a15130c53db4e0d9 Mon Sep 17 00:00:00 2001 From: Ege Korkan Date: Fri, 9 Feb 2024 00:02:05 +0100 Subject: [PATCH 1/6] add event to simple coffee machine --- examples/quickstart/simple-coffee-machine.js | 40 +++++++++++++++++-- .../src/quickstart/simple-coffee-machine.ts | 34 ++++++++++++++++ 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/examples/quickstart/simple-coffee-machine.js b/examples/quickstart/simple-coffee-machine.js index dd042b3a6..849f69dd8 100644 --- a/examples/quickstart/simple-coffee-machine.js +++ b/examples/quickstart/simple-coffee-machine.js @@ -25,9 +25,9 @@ servient.addServer( }) ); core_1.Helpers.setStaticAddress("plugfest.thingweb.io"); // comment this out if you are testing locally -let waterAmount = 1000; -let beansAmount = 1000; -let milkAmount = 1000; +let waterAmount = 28; +let beansAmount = 28; +let milkAmount = 28; // promisify timeout since it does not return a promise function timeout(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); @@ -78,6 +78,13 @@ servient.start().then((WoT) => { }, }, }, + events: { + resourceEmpty: { + data: { + enum: ["water", "beans", "milk"], + }, + }, + }, }) .then((thing) => { console.log("Produced " + thing.getThingDescription().title); @@ -99,6 +106,14 @@ servient.start().then((WoT) => { waterAmount = waterAmount - 10; beansAmount = beansAmount - 10; thing.emitPropertyChange("resources"); + if (waterAmount <= 10) { + thing.emitEvent("resourceEmpty", "water"); + return undefined; + } + if (beansAmount <= 10) { + thing.emitEvent("resourceEmpty", "beans"); + return undefined; + } return undefined; } } else if (coffeeType === "cappuccino") { @@ -110,6 +125,17 @@ servient.start().then((WoT) => { beansAmount = beansAmount - 20; milkAmount = milkAmount - 10; thing.emitPropertyChange("resources"); + if (waterAmount <= 10) { + thing.emitEvent("resourceEmpty", "water"); + return undefined; + } + if (beansAmount <= 10) { + thing.emitEvent("resourceEmpty", "beans"); + return undefined; + } + if (milkAmount <= 10) { + thing.emitEvent("resourceEmpty", "milk"); + } return undefined; } } else if (coffeeType === "americano") { @@ -120,6 +146,14 @@ servient.start().then((WoT) => { waterAmount = waterAmount - 30; beansAmount = beansAmount - 10; thing.emitPropertyChange("resources"); + if (waterAmount <= 10) { + thing.emitEvent("resourceEmpty", "water"); + return undefined; + } + if (beansAmount <= 10) { + thing.emitEvent("resourceEmpty", "beans"); + return undefined; + } return undefined; } } else { diff --git a/packages/examples/src/quickstart/simple-coffee-machine.ts b/packages/examples/src/quickstart/simple-coffee-machine.ts index fbd753d85..bb980a285 100644 --- a/packages/examples/src/quickstart/simple-coffee-machine.ts +++ b/packages/examples/src/quickstart/simple-coffee-machine.ts @@ -84,6 +84,13 @@ servient.start().then((WoT) => { }, }, }, + events: { + resourceEmpty: { + data: { + enum: ["water", "beans", "milk"], + }, + }, + }, }) .then((thing) => { console.log("Produced " + thing.getThingDescription().title); @@ -107,6 +114,14 @@ servient.start().then((WoT) => { waterAmount = waterAmount - 10; beansAmount = beansAmount - 10; thing.emitPropertyChange("resources"); + if (waterAmount <= 10) { + thing.emitEvent("resourceEmpty","water") + return undefined; + } + if (beansAmount <= 10) { + thing.emitEvent("resourceEmpty", "beans"); + return undefined; + } return undefined; } } else if (coffeeType === "cappuccino") { @@ -118,6 +133,17 @@ servient.start().then((WoT) => { beansAmount = beansAmount - 20; milkAmount = milkAmount - 10; thing.emitPropertyChange("resources"); + if (waterAmount <= 10) { + thing.emitEvent("resourceEmpty", "water"); + return undefined; + } + if (beansAmount <= 10) { + thing.emitEvent("resourceEmpty", "beans"); + return undefined; + } + if (milkAmount <= 10) { + thing.emitEvent("resourceEmpty", "milk"); + } return undefined; } } else if (coffeeType === "americano") { @@ -128,6 +154,14 @@ servient.start().then((WoT) => { waterAmount = waterAmount - 30; beansAmount = beansAmount - 10; thing.emitPropertyChange("resources"); + if (waterAmount <= 10) { + thing.emitEvent("resourceEmpty", "water"); + return undefined; + } + if (beansAmount <= 10) { + thing.emitEvent("resourceEmpty", "beans"); + return undefined; + } return undefined; } } else { From fd0b505ee572a68d4743ad5c72c4caa2d14f7d60 Mon Sep 17 00:00:00 2001 From: Ege Korkan Date: Fri, 9 Feb 2024 00:06:02 +0100 Subject: [PATCH 2/6] examples: apply lint and format --- packages/examples/src/quickstart/simple-coffee-machine.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/examples/src/quickstart/simple-coffee-machine.ts b/packages/examples/src/quickstart/simple-coffee-machine.ts index bb980a285..2b10e0ba0 100644 --- a/packages/examples/src/quickstart/simple-coffee-machine.ts +++ b/packages/examples/src/quickstart/simple-coffee-machine.ts @@ -115,7 +115,7 @@ servient.start().then((WoT) => { beansAmount = beansAmount - 10; thing.emitPropertyChange("resources"); if (waterAmount <= 10) { - thing.emitEvent("resourceEmpty","water") + thing.emitEvent("resourceEmpty", "water"); return undefined; } if (beansAmount <= 10) { From 523be04d7edff96812193e461192fb5f169ede6d Mon Sep 17 00:00:00 2001 From: Ege Korkan Date: Sat, 10 Feb 2024 01:35:22 +0100 Subject: [PATCH 3/6] examples: adapt coffee machine --- examples/quickstart/simple-coffee-machine.js | 90 ++++++++++++------- .../src/quickstart/simple-coffee-machine.ts | 87 +++++++++++------- 2 files changed, 110 insertions(+), 67 deletions(-) diff --git a/examples/quickstart/simple-coffee-machine.js b/examples/quickstart/simple-coffee-machine.js index 849f69dd8..9a0199bf8 100644 --- a/examples/quickstart/simple-coffee-machine.js +++ b/examples/quickstart/simple-coffee-machine.js @@ -19,15 +19,18 @@ const core_1 = require("@node-wot/core"); const binding_http_1 = require("@node-wot/binding-http"); // create Servient add HTTP binding with port configuration const servient = new core_1.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: 8081, + port: httpPort, }) ); -core_1.Helpers.setStaticAddress("plugfest.thingweb.io"); // comment this out if you are testing locally -let waterAmount = 28; -let beansAmount = 28; -let milkAmount = 28; +core_1.Helpers.setStaticAddress(staticAddress); +let waterAmount = 1000; +let beansAmount = 1000; +let milkAmount = 1000; // promisify timeout since it does not return a promise function timeout(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); @@ -73,15 +76,22 @@ servient.start().then((WoT) => { refill: { synchronous: true, input: { - type: "string", - enum: ["water", "beans", "milk"], + type: "array", + items: { + type: "string", + enum: ["water", "beans", "milk"], + }, }, }, }, events: { resourceEmpty: { data: { - enum: ["water", "beans", "milk"], + type: "array", + items: { + type: "string", + enum: ["water", "beans", "milk"], + }, }, }, }, @@ -106,15 +116,19 @@ servient.start().then((WoT) => { waterAmount = waterAmount - 10; beansAmount = beansAmount - 10; thing.emitPropertyChange("resources"); + const resourceEvent = []; if (waterAmount <= 10) { - thing.emitEvent("resourceEmpty", "water"); - return undefined; + resourceEvent.push("water"); } if (beansAmount <= 10) { - thing.emitEvent("resourceEmpty", "beans"); + resourceEvent.push("beans"); + } + if (resourceEvent.length > 0) { + thing.emitEvent("resourceEmpty", resourceEvent); + return undefined; + } else { return undefined; } - return undefined; } } else if (coffeeType === "cappuccino") { if (waterAmount <= 20 || beansAmount <= 25 || milkAmount <= 15) { @@ -125,18 +139,22 @@ servient.start().then((WoT) => { beansAmount = beansAmount - 20; milkAmount = milkAmount - 10; thing.emitPropertyChange("resources"); + const resourceEvent = []; if (waterAmount <= 10) { - thing.emitEvent("resourceEmpty", "water"); - return undefined; + resourceEvent.push("water"); } if (beansAmount <= 10) { - thing.emitEvent("resourceEmpty", "beans"); - return undefined; + resourceEvent.push("beans"); } if (milkAmount <= 10) { - thing.emitEvent("resourceEmpty", "milk"); + resourceEvent.push("milk"); + } + if (resourceEvent.length > 0) { + thing.emitEvent("resourceEmpty", resourceEvent); + return undefined; + } else { + return undefined; } - return undefined; } } else if (coffeeType === "americano") { if (waterAmount <= 35 || beansAmount <= 10) { @@ -146,15 +164,19 @@ servient.start().then((WoT) => { waterAmount = waterAmount - 30; beansAmount = beansAmount - 10; thing.emitPropertyChange("resources"); + const resourceEvent = []; if (waterAmount <= 10) { - thing.emitEvent("resourceEmpty", "water"); - return undefined; + resourceEvent.push("water"); } if (beansAmount <= 10) { - thing.emitEvent("resourceEmpty", "beans"); + resourceEvent.push("beans"); + } + if (resourceEvent.length > 0) { + thing.emitEvent("resourceEmpty", resourceEvent); + return undefined; + } else { return undefined; } - return undefined; } } else { throw new Error("Wrong coffee input"); @@ -163,18 +185,17 @@ servient.start().then((WoT) => { thing.setActionHandler("refill", async (params, options) => { const selectedResource = await params.value(); console.info("received refill order of ", selectedResource); - switch (selectedResource) { - case "water": - waterAmount = 1000; - break; - case "beans": - beansAmount = 1000; - break; - case "milk": - milkAmount = 1000; - break; - default: - throw new Error("Wrong refill input"); + // @ts-expect-error: Property doesn't exist error + if (selectedResource.indexOf("water") !== -1) { + waterAmount = 1000; + } + // @ts-expect-error: Property doesn't exist error + if (selectedResource.indexOf("beans") !== -1) { + beansAmount = 1000; + } + // @ts-expect-error: Property doesn't exist error + if (selectedResource.indexOf("milk") !== -1) { + milkAmount = 1000; } thing.emitPropertyChange("resources"); return undefined; @@ -182,6 +203,7 @@ servient.start().then((WoT) => { // expose the thing thing.expose().then(() => { console.info(thing.getThingDescription().title + " ready"); + console.info("TD available at http://" + staticAddress + ":" + httpPort); }); }) .catch((e) => { diff --git a/packages/examples/src/quickstart/simple-coffee-machine.ts b/packages/examples/src/quickstart/simple-coffee-machine.ts index 2b10e0ba0..a35425cf6 100644 --- a/packages/examples/src/quickstart/simple-coffee-machine.ts +++ b/packages/examples/src/quickstart/simple-coffee-machine.ts @@ -21,13 +21,16 @@ import { HttpServer } from "@node-wot/binding-http"; // create Servient add HTTP binding with port configuration const servient = new Servient(); + +// const staticAddress = "plugfest.thingweb.io"; +const staticAddress = "localhost"; // use this for testing locally +const httpPort = 8081 servient.addServer( new HttpServer({ - port: 8081, + port: httpPort, }) ); - -Helpers.setStaticAddress("plugfest.thingweb.io"); // comment this out if you are testing locally +Helpers.setStaticAddress(staticAddress); let waterAmount = 1000; let beansAmount = 1000; @@ -79,15 +82,22 @@ servient.start().then((WoT) => { refill: { synchronous: true, input: { - type: "string", - enum: ["water", "beans", "milk"], + type: "array", + items: { + type: "string", + enum: ["water", "beans", "milk"], + }, }, }, }, events: { resourceEmpty: { data: { - enum: ["water", "beans", "milk"], + type: "array", + items: { + type: "string", + enum: ["water", "beans", "milk"], + }, }, }, }, @@ -114,15 +124,19 @@ servient.start().then((WoT) => { waterAmount = waterAmount - 10; beansAmount = beansAmount - 10; thing.emitPropertyChange("resources"); + const resourceEvent: Array = []; if (waterAmount <= 10) { - thing.emitEvent("resourceEmpty", "water"); - return undefined; + resourceEvent.push("water") } if (beansAmount <= 10) { - thing.emitEvent("resourceEmpty", "beans"); + resourceEvent.push("beans"); + } + if (resourceEvent.length>0){ + thing.emitEvent("resourceEmpty", resourceEvent); + return undefined; + } else { return undefined; } - return undefined; } } else if (coffeeType === "cappuccino") { if (waterAmount <= 20 || beansAmount <= 25 || milkAmount <= 15) { @@ -133,18 +147,22 @@ servient.start().then((WoT) => { beansAmount = beansAmount - 20; milkAmount = milkAmount - 10; thing.emitPropertyChange("resources"); + const resourceEvent: Array = []; if (waterAmount <= 10) { - thing.emitEvent("resourceEmpty", "water"); - return undefined; + resourceEvent.push("water"); } if (beansAmount <= 10) { - thing.emitEvent("resourceEmpty", "beans"); - return undefined; + resourceEvent.push("beans"); } if (milkAmount <= 10) { - thing.emitEvent("resourceEmpty", "milk"); + resourceEvent.push("milk"); + } + if (resourceEvent.length > 0) { + thing.emitEvent("resourceEmpty", resourceEvent); + return undefined; + } else { + return undefined; } - return undefined; } } else if (coffeeType === "americano") { if (waterAmount <= 35 || beansAmount <= 10) { @@ -154,15 +172,19 @@ servient.start().then((WoT) => { waterAmount = waterAmount - 30; beansAmount = beansAmount - 10; thing.emitPropertyChange("resources"); + const resourceEvent: Array = []; if (waterAmount <= 10) { - thing.emitEvent("resourceEmpty", "water"); - return undefined; + resourceEvent.push("water"); } if (beansAmount <= 10) { - thing.emitEvent("resourceEmpty", "beans"); + resourceEvent.push("beans"); + } + if (resourceEvent.length > 0) { + thing.emitEvent("resourceEmpty", resourceEvent); + return undefined; + } else { return undefined; } - return undefined; } } else { throw new Error("Wrong coffee input"); @@ -172,20 +194,18 @@ servient.start().then((WoT) => { thing.setActionHandler("refill", async (params, options) => { const selectedResource = await params.value(); console.info("received refill order of ", selectedResource); - switch (selectedResource) { - case "water": - waterAmount = 1000; - break; - case "beans": - beansAmount = 1000; - break; - case "milk": - milkAmount = 1000; - break; - default: - throw new Error("Wrong refill input"); + // @ts-expect-error: Property doesn't exist error + if (selectedResource!.indexOf("water") !== -1) { + waterAmount = 1000; + } + // @ts-expect-error: Property doesn't exist error + if (selectedResource!.indexOf("beans") !== -1) { + beansAmount = 1000; + } + // @ts-expect-error: Property doesn't exist error + if (selectedResource!.indexOf("milk") !== -1) { + milkAmount = 1000; } - thing.emitPropertyChange("resources"); return undefined; }); @@ -193,6 +213,7 @@ servient.start().then((WoT) => { // expose the thing thing.expose().then(() => { console.info(thing.getThingDescription().title + " ready"); + console.info("TD available at http://" + staticAddress + ":" + httpPort); }); }) .catch((e) => { From 94bb0f5453e8e7838e2e0b0d1674818b687761c1 Mon Sep 17 00:00:00 2001 From: Ege Korkan Date: Sat, 10 Feb 2024 01:36:34 +0100 Subject: [PATCH 4/6] examples: format and lint --- packages/examples/src/quickstart/simple-coffee-machine.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/examples/src/quickstart/simple-coffee-machine.ts b/packages/examples/src/quickstart/simple-coffee-machine.ts index a35425cf6..a99f0c7cc 100644 --- a/packages/examples/src/quickstart/simple-coffee-machine.ts +++ b/packages/examples/src/quickstart/simple-coffee-machine.ts @@ -24,7 +24,7 @@ const servient = new Servient(); // const staticAddress = "plugfest.thingweb.io"; const staticAddress = "localhost"; // use this for testing locally -const httpPort = 8081 +const httpPort = 8081; servient.addServer( new HttpServer({ port: httpPort, @@ -124,14 +124,14 @@ servient.start().then((WoT) => { waterAmount = waterAmount - 10; beansAmount = beansAmount - 10; thing.emitPropertyChange("resources"); - const resourceEvent: Array = []; + const resourceEvent: Array = []; if (waterAmount <= 10) { - resourceEvent.push("water") + resourceEvent.push("water"); } if (beansAmount <= 10) { resourceEvent.push("beans"); } - if (resourceEvent.length>0){ + if (resourceEvent.length > 0) { thing.emitEvent("resourceEmpty", resourceEvent); return undefined; } else { From 3c6d9fba1863dd81495db54bb7d79e60621baca7 Mon Sep 17 00:00:00 2001 From: Ege Korkan Date: Mon, 12 Feb 2024 18:24:44 +0100 Subject: [PATCH 5/6] examples: adapt feedback for return statement --- examples/quickstart/simple-coffee-machine.js | 12 +++--------- .../examples/src/quickstart/simple-coffee-machine.ts | 12 +++--------- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/examples/quickstart/simple-coffee-machine.js b/examples/quickstart/simple-coffee-machine.js index 9a0199bf8..22884dc49 100644 --- a/examples/quickstart/simple-coffee-machine.js +++ b/examples/quickstart/simple-coffee-machine.js @@ -125,10 +125,8 @@ servient.start().then((WoT) => { } if (resourceEvent.length > 0) { thing.emitEvent("resourceEmpty", resourceEvent); - return undefined; - } else { - return undefined; } + return undefined; } } else if (coffeeType === "cappuccino") { if (waterAmount <= 20 || beansAmount <= 25 || milkAmount <= 15) { @@ -151,10 +149,8 @@ servient.start().then((WoT) => { } if (resourceEvent.length > 0) { thing.emitEvent("resourceEmpty", resourceEvent); - return undefined; - } else { - return undefined; } + return undefined; } } else if (coffeeType === "americano") { if (waterAmount <= 35 || beansAmount <= 10) { @@ -173,10 +169,8 @@ servient.start().then((WoT) => { } if (resourceEvent.length > 0) { thing.emitEvent("resourceEmpty", resourceEvent); - return undefined; - } else { - return undefined; } + return undefined; } } else { throw new Error("Wrong coffee input"); diff --git a/packages/examples/src/quickstart/simple-coffee-machine.ts b/packages/examples/src/quickstart/simple-coffee-machine.ts index a99f0c7cc..ce79d3c9f 100644 --- a/packages/examples/src/quickstart/simple-coffee-machine.ts +++ b/packages/examples/src/quickstart/simple-coffee-machine.ts @@ -133,10 +133,8 @@ servient.start().then((WoT) => { } if (resourceEvent.length > 0) { thing.emitEvent("resourceEmpty", resourceEvent); - return undefined; - } else { - return undefined; } + return undefined; } } else if (coffeeType === "cappuccino") { if (waterAmount <= 20 || beansAmount <= 25 || milkAmount <= 15) { @@ -159,10 +157,8 @@ servient.start().then((WoT) => { } if (resourceEvent.length > 0) { thing.emitEvent("resourceEmpty", resourceEvent); - return undefined; - } else { - return undefined; } + return undefined; } } else if (coffeeType === "americano") { if (waterAmount <= 35 || beansAmount <= 10) { @@ -181,10 +177,8 @@ servient.start().then((WoT) => { } if (resourceEvent.length > 0) { thing.emitEvent("resourceEmpty", resourceEvent); - return undefined; - } else { - return undefined; } + return undefined; } } else { throw new Error("Wrong coffee input"); From 3b1136c03ba3068de174d8630603539c3567c210 Mon Sep 17 00:00:00 2001 From: reluc Date: Tue, 13 Feb 2024 10:57:13 +0100 Subject: [PATCH 6/6] fix(examples): do not ignore error but use cast --- examples/quickstart/simple-coffee-machine.js | 3 --- packages/examples/src/quickstart/simple-coffee-machine.ts | 5 +---- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/examples/quickstart/simple-coffee-machine.js b/examples/quickstart/simple-coffee-machine.js index 22884dc49..136e3ee1f 100644 --- a/examples/quickstart/simple-coffee-machine.js +++ b/examples/quickstart/simple-coffee-machine.js @@ -179,15 +179,12 @@ servient.start().then((WoT) => { thing.setActionHandler("refill", async (params, options) => { const selectedResource = await params.value(); console.info("received refill order of ", selectedResource); - // @ts-expect-error: Property doesn't exist error if (selectedResource.indexOf("water") !== -1) { waterAmount = 1000; } - // @ts-expect-error: Property doesn't exist error if (selectedResource.indexOf("beans") !== -1) { beansAmount = 1000; } - // @ts-expect-error: Property doesn't exist error if (selectedResource.indexOf("milk") !== -1) { milkAmount = 1000; } diff --git a/packages/examples/src/quickstart/simple-coffee-machine.ts b/packages/examples/src/quickstart/simple-coffee-machine.ts index ce79d3c9f..69fdc7c2f 100644 --- a/packages/examples/src/quickstart/simple-coffee-machine.ts +++ b/packages/examples/src/quickstart/simple-coffee-machine.ts @@ -186,17 +186,14 @@ servient.start().then((WoT) => { }); thing.setActionHandler("refill", async (params, options) => { - const selectedResource = await params.value(); + const selectedResource = (await params.value()) as Array<"water" | "beans" | "milk">; console.info("received refill order of ", selectedResource); - // @ts-expect-error: Property doesn't exist error if (selectedResource!.indexOf("water") !== -1) { waterAmount = 1000; } - // @ts-expect-error: Property doesn't exist error if (selectedResource!.indexOf("beans") !== -1) { beansAmount = 1000; } - // @ts-expect-error: Property doesn't exist error if (selectedResource!.indexOf("milk") !== -1) { milkAmount = 1000; }