This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatissima.js
104 lines (90 loc) · 2.21 KB
/
latissima.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
const EventEmittter = require('events');
const { Board, Relay, Pin } = require('johnny-five');
const Promise = require('bluebird');
const Tessel = require('tessel-io');
const PRESS_DURATION = 500;
class Latissima extends EventEmittter {
// Board
board = undefined;
// Relays
espressoRelay = undefined;
grandeRelay = undefined;
powerRelay = undefined;
// Others
// The coffee machine is automatically on when you flip the master switch
isOn = true;
// No this isn't a teapot
isTeapot = false;
// Available Additions
additions = [];
// Coffee Types
static Types = {
espresso: 1,
grande: 2
};
constructor(debug = false) {
super();
this.board = new Board({
io: new Tessel(),
repl: debug,
debug: debug
});
this.board.on('ready', () => {
this.initializePins();
this.emit('ready');
});
}
initializePins() {
this.espressoRelay = new Relay({
pin: 'a4',
type: 'NO'
});
this.espressoRelay.close();
this.grandeRelay = new Relay({
pin: 'a5',
type: 'NO'
});
this.grandeRelay.close();
this.powerRelay = new Relay({
pin: 'a6',
type: 'NO'
});
this.powerRelay.close();
}
pressPower() {
if (!this.isOn) {
// it needs to heat up for ~20 seconds
// TODO: Remove this when we can read if the machine is on
setTimeout(() => {
this.isOn = true;
}, 21 * 1000)
} else {
this.isOn = false;
}
return this.pressButton(this.powerRelay);
}
// Convinience function to press different coffee types
press(type) {
switch(type) {
case Latissima.Types.espresso:
return this.pressButton(this.espressoRelay);
case Latissima.Types.grande:
return this.pressButton(this.grandeRelay);
default:
return Promise.reject(new Error("Could not find Type"));
}
}
// Emulate the push of a button by opening
// the respective relay, wait for a certain
// time and closing it again
pressButton(relay) {
return new Promise((resolve, reject) => {
relay.open();
setTimeout(() => {
relay.close();
resolve();
}, PRESS_DURATION);
});
}
}
module.exports = { Latissima };