-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathsimple-coffee-machine.js
More file actions
204 lines (204 loc) · 8.14 KB
/
Copy pathsimple-coffee-machine.js
File metadata and controls
204 lines (204 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"use strict";
/********************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and
* Document License (2015-05-13) which is available at
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document.
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/
Object.defineProperty(exports, "__esModule", { value: true });
// 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");
// 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: httpPort,
})
);
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));
}
servient.start().then((WoT) => {
WoT.produce({
title: "Coffee Machine",
description: "A simple coffee machine that can be interacted over the Internet",
support: "https://github.com/eclipse-thingweb/node-wot/",
"@context": "https://www.w3.org/2022/wot/td/v1.1",
properties: {
resources: {
readOnly: true,
observable: true,
type: "object",
properties: {
water: {
type: "integer",
minimum: 0,
maximum: 1000,
},
beans: {
type: "integer",
minimum: 0,
maximum: 1000,
},
milk: {
type: "integer",
minimum: 0,
maximum: 1000,
},
},
},
},
actions: {
brew: {
synchronous: true,
input: {
type: "string",
enum: ["espresso", "cappuccino", "americano"],
},
},
refill: {
synchronous: true,
input: {
type: "array",
items: {
type: "string",
enum: ["water", "beans", "milk"],
},
},
},
},
events: {
resourceEmpty: {
data: {
type: "array",
items: {
type: "string",
enum: ["water", "beans", "milk"],
},
},
},
},
})
.then((thing) => {
console.log("Produced " + thing.getThingDescription().title);
thing.setPropertyReadHandler("resources", async () => {
return {
water: waterAmount,
beans: beansAmount,
milk: milkAmount,
};
});
thing.setActionHandler("brew", async (params, options) => {
const coffeeType = await params.value();
console.info("received coffee order of ", coffeeType);
if (coffeeType === "espresso") {
if (waterAmount <= 10 || beansAmount <= 10) {
throw new Error("Not enough water or beans");
} else {
await timeout(1000);
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") {
if (waterAmount <= 20 || beansAmount <= 25 || milkAmount <= 15) {
throw new Error("Not enough water or beans");
} else {
await timeout(2000);
waterAmount = waterAmount - 15;
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") {
if (waterAmount <= 35 || beansAmount <= 10) {
throw new Error("Not enough water or beans");
} else {
await timeout(2000);
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 {
throw new Error("Wrong coffee input");
}
});
thing.setActionHandler("refill", async (params, options) => {
const selectedResource = await params.value();
console.info("received refill order of ", selectedResource);
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) => {
console.log(e);
});
});