Skip to content

Commit

Permalink
Merge pull request #1081 from relu91/quickstart_refill
Browse files Browse the repository at this point in the history
feat(examples/quickstart): add a refill action for coffe-machine
  • Loading branch information
egekorkan authored Sep 12, 2023
2 parents 692af10 + fcdf5c9 commit e491764
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
32 changes: 29 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 = 100;
let beansAmount = 100;
let milkAmount = 100;
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));
Expand Down Expand Up @@ -70,6 +70,13 @@ servient.start().then((WoT) => {
enum: ["espresso", "cappuccino", "americano"],
},
},
refill: {
synchronous: true,
input: {
type: "string",
enum: ["water", "beans", "milk"],
},
},
},
})
.then((thing) => {
Expand Down Expand Up @@ -119,6 +126,25 @@ servient.start().then((WoT) => {
throw new Error("Wrong coffee input");
}
});
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");
}
thing.emitPropertyChange("resources");
return undefined;
});
// expose the thing
thing.expose().then(() => {
console.info(thing.getThingDescription().title + " ready");
Expand Down
34 changes: 31 additions & 3 deletions packages/examples/src/quickstart/simple-coffee-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ servient.addServer(

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

let waterAmount = 100;
let beansAmount = 100;
let milkAmount = 100;
let waterAmount = 1000;
let beansAmount = 1000;
let milkAmount = 1000;

// promisify timeout since it does not return a promise
function timeout(ms: number) {
Expand Down Expand Up @@ -76,6 +76,13 @@ servient.start().then((WoT) => {
enum: ["espresso", "cappuccino", "americano"],
},
},
refill: {
synchronous: true,
input: {
type: "string",
enum: ["water", "beans", "milk"],
},
},
},
})
.then((thing) => {
Expand Down Expand Up @@ -128,6 +135,27 @@ 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");
}

thing.emitPropertyChange("resources");
return undefined;
});

// expose the thing
thing.expose().then(() => {
console.info(thing.getThingDescription().title + " ready");
Expand Down

0 comments on commit e491764

Please sign in to comment.