Skip to content

Commit

Permalink
Merge pull request eclipse-thingweb#1229 from eclipse-thingweb/ege-si…
Browse files Browse the repository at this point in the history
…mple-coffee-machine

Events for simple coffee machine
  • Loading branch information
relu91 authored Feb 13, 2024
2 parents 56c01a2 + 3b1136c commit af74c9d
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 35 deletions.
79 changes: 63 additions & 16 deletions examples/quickstart/simple-coffee-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ 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
core_1.Helpers.setStaticAddress(staticAddress);
let waterAmount = 1000;
let beansAmount = 1000;
let milkAmount = 1000;
Expand Down Expand Up @@ -73,8 +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: {
type: "array",
items: {
type: "string",
enum: ["water", "beans", "milk"],
},
},
},
},
Expand All @@ -99,6 +116,16 @@ servient.start().then((WoT) => {
waterAmount = waterAmount - 10;
beansAmount = beansAmount - 10;
thing.emitPropertyChange("resources");
const resourceEvent = [];
if (waterAmount <= 10) {
resourceEvent.push("water");
}
if (beansAmount <= 10) {
resourceEvent.push("beans");
}
if (resourceEvent.length > 0) {
thing.emitEvent("resourceEmpty", resourceEvent);
}
return undefined;
}
} else if (coffeeType === "cappuccino") {
Expand All @@ -110,6 +137,19 @@ servient.start().then((WoT) => {
beansAmount = beansAmount - 20;
milkAmount = milkAmount - 10;
thing.emitPropertyChange("resources");
const resourceEvent = [];
if (waterAmount <= 10) {
resourceEvent.push("water");
}
if (beansAmount <= 10) {
resourceEvent.push("beans");
}
if (milkAmount <= 10) {
resourceEvent.push("milk");
}
if (resourceEvent.length > 0) {
thing.emitEvent("resourceEmpty", resourceEvent);
}
return undefined;
}
} else if (coffeeType === "americano") {
Expand All @@ -120,6 +160,16 @@ servient.start().then((WoT) => {
waterAmount = waterAmount - 30;
beansAmount = beansAmount - 10;
thing.emitPropertyChange("resources");
const resourceEvent = [];
if (waterAmount <= 10) {
resourceEvent.push("water");
}
if (beansAmount <= 10) {
resourceEvent.push("beans");
}
if (resourceEvent.length > 0) {
thing.emitEvent("resourceEmpty", resourceEvent);
}
return undefined;
}
} else {
Expand All @@ -129,25 +179,22 @@ 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");
if (selectedResource.indexOf("water") !== -1) {
waterAmount = 1000;
}
if (selectedResource.indexOf("beans") !== -1) {
beansAmount = 1000;
}
if (selectedResource.indexOf("milk") !== -1) {
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);
});
})
.catch((e) => {
Expand Down
84 changes: 65 additions & 19 deletions packages/examples/src/quickstart/simple-coffee-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,8 +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: {
type: "array",
items: {
type: "string",
enum: ["water", "beans", "milk"],
},
},
},
},
Expand All @@ -107,6 +124,16 @@ servient.start().then((WoT) => {
waterAmount = waterAmount - 10;
beansAmount = beansAmount - 10;
thing.emitPropertyChange("resources");
const resourceEvent: Array<string> = [];
if (waterAmount <= 10) {
resourceEvent.push("water");
}
if (beansAmount <= 10) {
resourceEvent.push("beans");
}
if (resourceEvent.length > 0) {
thing.emitEvent("resourceEmpty", resourceEvent);
}
return undefined;
}
} else if (coffeeType === "cappuccino") {
Expand All @@ -118,6 +145,19 @@ servient.start().then((WoT) => {
beansAmount = beansAmount - 20;
milkAmount = milkAmount - 10;
thing.emitPropertyChange("resources");
const resourceEvent: Array<string> = [];
if (waterAmount <= 10) {
resourceEvent.push("water");
}
if (beansAmount <= 10) {
resourceEvent.push("beans");
}
if (milkAmount <= 10) {
resourceEvent.push("milk");
}
if (resourceEvent.length > 0) {
thing.emitEvent("resourceEmpty", resourceEvent);
}
return undefined;
}
} else if (coffeeType === "americano") {
Expand All @@ -128,6 +168,16 @@ servient.start().then((WoT) => {
waterAmount = waterAmount - 30;
beansAmount = beansAmount - 10;
thing.emitPropertyChange("resources");
const resourceEvent: Array<string> = [];
if (waterAmount <= 10) {
resourceEvent.push("water");
}
if (beansAmount <= 10) {
resourceEvent.push("beans");
}
if (resourceEvent.length > 0) {
thing.emitEvent("resourceEmpty", resourceEvent);
}
return undefined;
}
} else {
Expand All @@ -136,29 +186,25 @@ 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);
switch (selectedResource) {
case "water":
waterAmount = 1000;
break;
case "beans":
beansAmount = 1000;
break;
case "milk":
milkAmount = 1000;
break;
default:
throw new Error("Wrong refill input");
if (selectedResource!.indexOf("water") !== -1) {
waterAmount = 1000;
}
if (selectedResource!.indexOf("beans") !== -1) {
beansAmount = 1000;
}
if (selectedResource!.indexOf("milk") !== -1) {
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);
});
})
.catch((e) => {
Expand Down

0 comments on commit af74c9d

Please sign in to comment.