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

Events for simple coffee machine #1229

Merged
merged 6 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 37 additions & 3 deletions examples/quickstart/simple-coffee-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -78,6 +78,13 @@ servient.start().then((WoT) => {
},
},
},
events: {
resourceEmpty: {
data: {
enum: ["water", "beans", "milk"],
},
},
},
})
.then((thing) => {
console.log("Produced " + thing.getThingDescription().title);
Expand All @@ -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;
egekorkan marked this conversation as resolved.
Show resolved Hide resolved
}
if (beansAmount <= 10) {
thing.emitEvent("resourceEmpty", "beans");
return undefined;
}
return undefined;
}
} else if (coffeeType === "cappuccino") {
Expand All @@ -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") {
Expand All @@ -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 {
Expand Down
34 changes: 34 additions & 0 deletions packages/examples/src/quickstart/simple-coffee-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ servient.start().then((WoT) => {
},
},
},
events: {
resourceEmpty: {
data: {
enum: ["water", "beans", "milk"],
},
},
},
})
.then((thing) => {
console.log("Produced " + thing.getThingDescription().title);
Expand All @@ -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") {
Expand All @@ -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") {
Expand All @@ -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 {
Expand Down
Loading