-
Notifications
You must be signed in to change notification settings - Fork 2
/
coffeeMachine.js
309 lines (238 loc) · 10.1 KB
/
coffeeMachine.js
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
'use strict';
const net = require('net');
const http = require('http');
const url = require('url');
const Smarter = require('./smarter.js');
const winston = require('winston');
function scheduleCallback(subscriptions, payload) {
const now = Date.now();
const schedule = sub => setTimeout(() => doCallback(sub, payload), 0);
for (let subscription of subscriptions.values()) {
if (subscription.expiry >= now) {
winston.info('Scheduling callback for subscription %s', subscription.subscriptionId);
schedule(subscription);
}
}
}
function doCallback(subscription, payload) {
winston.info('Executing callback for subscription %s', subscription.subscriptionId);
const uri = url.parse(subscription.callbackUrl);
const options = {
"host": uri.hostname,
"port": uri.port,
"path": uri.path,
"method": "POST",
"headers": {
"SID" : subscription.subscriptionId,
"Content-Type" : "application/json",
}
};
const body = JSON.stringify(payload);
const request = http.request(options, res => {
winston.info('Callback to subscription %s status code %s', subscription.subscriptionId, res.statusCode);
});
request.on('error', (e) => {
winston.info('Callback to subscription %s failed with error %s', subscription.subscriptionId, e);
});
request.end(body);
}
class CoffeeMachine
{
constructor(mac, ip) {
this.id = CoffeeMachine.idFromMac(mac);
this.mac = mac;
this.ip = ip;
this.name = "Coffee Machine";
this.status = null;
this.isConnected = false;
this.subscriptions = new Map();
}
updateIp(ip) {
if (this.ip === ip) return;
winston.info('IP address has changed for machine %s to %s', this.id, this.ip, ip);
this.ip = ip;
this.disconnect();
setTimeout(() => this.connect(), 5000);
}
connect() {
if (this.isConnected) return;
winston.info('Connecting to machine %s', this.ip);
this.client = net.createConnection({ port:Smarter.port, host:this.ip, timeout:6000 });
this.client.on('lookup', (err, address, family, host) => {
winston.warning('Lookup %s %s %s %s', err, address, family, host);
// TODO
});
this.client.on('connect', () => {
winston.info('Connected to machine %s', this.ip);
this.isConnected = true;
});
this.client.on('error', (error) => {
winston.error('Error in stream from machine %s, %s', this.ip, error);
// TODO
});
this.client.on('timeout', () => {
winston.warning('Timeout');
this.client.destroy();
this.isConnected = false;
setTimeout(() => this.connect(), 5000);
});
this.client.on('end', () => {
this.isConnected = false;
winston.info('Disconnected from machine %s', this.ip);
});
this.client.on('data', (data) => {
if (!data || data.length === 0)
return;
if (data[0] === Smarter.acknowledgementReplyByte)
{
winston.info('Received acknowledgement message from machine %s - %s', this.ip, data.join(','));
if (data[1] === Smarter.acknowledgementSuccessByte) {
// do nothing
}
if (data[1] === Smarter.acknowledgementNoCarafeByte) {
winston.info('Preparing error subscription callbacks');
const payload = { id: this.id, error: 'No carafe' };
scheduleCallback(this.subscriptions, payload);
}
}
else if (data[0] === Smarter.statusReplyByte)
{
winston.info('Received status message from machine %s - %s', this.ip, data.join(','));
// not sure what isReady is... don't think it's "machine is ready to brew"
// it might be "your coffee is ready" as it doesn't stay set that long
// this.isReady = (data[1] & 4) >= 1;
// not useful
// this.isCycleComplete = (data[1] & 32) >= 1;
// combine these two properties into something more useful
const isGrindInProgress = (data[1] & 8) >= 1;
const isWaterPumpInProgress = (data[1] & 16) >= 1;
const oldstatus = this.status;
this.status = {
isBrewing : isGrindInProgress || isWaterPumpInProgress,
isCarafeDetected : (data[1] & 1) >= 1,
isGrind : (data[1] & 2) >= 1,
isHotplateOn : (data[1] & 64) >= 1,
waterLevel : (data[2] & 15),
strength : (data[4] & 3),
cups : (data[5] & 15)
};
if (oldstatus !== null &&
oldstatus.isBrewing === this.status.isBrewing &&
oldstatus.isCarafeDetected === this.status.isCarafeDetected &&
oldstatus.isGrind === this.status.isGrind &&
oldstatus.isHotplateOn === this.status.isHotplateOn &&
oldstatus.waterLevel === this.status.waterLevel &&
oldstatus.strength === this.status.strength &&
oldstatus.cups === this.status.cups)
return;
winston.info('Status has changed, preparing status subscription callbacks');
const payload = { id: this.id, status: this.status };
scheduleCallback(this.subscriptions, payload);
}
});
}
disconnect() {
if (!this.isConnected) return;
winston.info('Disconnecting from machine %s', this.ip);
this.client.end();
this.isConnected = false;
}
setStrength(strength, callback) {
const strengthAsInt = parseInt(strength);
if (isNaN(strengthAsInt) || strengthAsInt < 0 || strengthAsInt > 2) {
return callback("'strength' must be 0 (weak), 1 (medium), or 2 (strong)");
}
winston.info('Setting strength to %s', strengthAsInt);
const command = new Buffer([Smarter.strengthRequestByte, strengthAsInt, Smarter.messageTerminator]);
this.sendCommand(command, callback);
}
setCups(cups, callback) {
const cupsAsInt = parseInt(cups);
if (isNaN(cupsAsInt) || cupsAsInt < 1 || cupsAsInt > 12) {
return callback("'cups' must be a number between 1 to 12 inclusive");
}
winston.info('Setting cups to %s', cupsAsInt);
const command = new Buffer([Smarter.cupsRequestByte, cupsAsInt, Smarter.messageTerminator]);
this.sendCommand(command, callback);
}
setGrind(isGrind, callback) {
if (isGrind !== true && isGrind !== false) {
return callback("'isGrind' must be true (on) or false (off)");
}
if (this.status.isGrind === isGrind) {
return callback();
}
winston.info('Setting grind to %s', isGrind);
const command = new Buffer([Smarter.toggleGrindRequestByte, Smarter.messageTerminator]);
this.sendCommand(command, callback);
}
brewOn(isGrind, cups, strength, callback) {
if (isGrind !== true && isGrind !== false) {
return callback("'isGrind' must be true (on) or false (off)");
}
const isGrindAsInt = isGrind ? 1 : 0;
const cupsAsInt = parseInt(cups);
if (isNaN(cupsAsInt) || cupsAsInt < 1 || cupsAsInt > 12) {
return callback("'cups' must be a number between 1 to 12 inclusive");
}
const strengthAsInt = parseInt(strength);
if (isNaN(strengthAsInt) || strengthAsInt < 0 || strengthAsInt > 2) {
return callback("'strength' must be 0 (weak), 1 (medium), or 2 (strong)");
}
winston.info('Brewing coffee with grind %s, cups %s, strength %s', isGrindAsInt, cupsAsInt, strengthAsInt);
const command = new Buffer([Smarter.brewOnRequestByte, cupsAsInt, strengthAsInt, 0x5 /*unknown*/, isGrindAsInt, Smarter.messageTerminator]);
this.sendCommand(command, callback);
}
brewOnDefault(callback) {
winston.info('Brewing coffee with default settings');
const command = new Buffer([Smarter.brewOnDefaultRequestByte, Smarter.messageTerminator]);
this.sendCommand(command, callback);
}
brewOff(callback) {
winston.info('Stopping coffee brew');
const command = new Buffer([Smarter.brewOffRequestByte, 0, Smarter.messageTerminator]);
this.sendCommand(command, callback);
}
hotplateOn(mins, callback) {
var minsAsInt = 5;
if (mins !== undefined) {
minsAsInt = parseInt(mins);
if (isNaN(minsAsInt) || minsAsInt < 1 || minsAsInt > 30) {
return callback("'mins' must be a number between 1 to 30 inclusive");
}
}
winston.info('Turning on hotplate for %s mins', minsAsInt);
const command = new Buffer([Smarter.hotplateOnRequestByte, minsAsInt, Smarter.messageTerminator]);
this.sendCommand(command, callback);
}
hotplateOff(callback) {
winston.info('Turning off hotplate');
const command = new Buffer([Smarter.hotplateOffRequestByte, Smarter.messageTerminator]);
this.sendCommand(command, callback);
}
sendCommand(command, callback) {
if (!this.isConnected) {
return callback("Not connected");
}
this.client.write(command);
callback();
}
addSubscription(subscriptionId, timeoutInMs, callbackUrl) {
const expiry = Date.now() + timeoutInMs;
const obj = { subscriptionId, expiry, callbackUrl };
this.subscriptions.set(subscriptionId, obj);
}
toApiDevice() {
return {
id : this.id,
mac : this.mac,
ip : this.ip,
name : this.name,
status : this.status
};
}
static idFromMac(mac) {
return mac.replace(/:/g, '').toLowerCase();
}
}
module.exports = CoffeeMachine;