-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrink.js
97 lines (85 loc) · 2.54 KB
/
drink.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
var tessel = require('tessel');
var drink = {
busy: false,
recipes: {
// todo: atualizar para ficar igual aos slides...
maitai: [60, 0, 120, 0],
cubalibre: [70, 120, 0, 0],
hifi: [0, 0, 120, 70],
moskvasvobodno: [0, 120, 0, 70]
},
setBusyFor: function(time) {
drink.busy = true;
setTimeout(function() {
console.log("--- pronta para outro drink! ---");
drink.init();
drink.busy = false;
},time * 1000);
},
timeForRecipe: function(recipe) {
return drink.recipes[recipe].reduce(function(p, c) {
return Math.max(p, c);
},0);
},
turnPumpOn: function(number) {
console.log("Ligando bomba #"+number);
tessel.port.B.pin[number].write(0, function(error, buffer) {
if (error !== null) {
console.log(error)
console.log(JSON.stringify(error))
return error;
}
});
return true;
},
turnPumpOff: function(number) {
console.log("Desligando bomba #"+number);
tessel.port.B.pin[number].write(1, function(error, buffer) {
if (error !== null) {
return error;
}
});
return true;
},
turnPumpOnForSeconds: function(number, seconds) {
console.log("Ligando bomba #"+number + " por " + seconds + " segundos");
var status = drink.turnPumpOn(number);
if (status !== true) {
console.log("--- Problema com a bomba #" + number + ": " + status + " ---");
return status
}
setTimeout(function() {
status = drink.turnPumpOff(number);
if (!status) {
throw status
}
}, seconds * 1000);
return true;
},
mix: function(recipe) {
if (drink.busy) {
return {
status: "erro",
msg: "Ocupada fazendo drinks"
}
}
drink.recipes[recipe].map(function(seconds, pump) {
if (seconds > 0) {
drink.turnPumpOnForSeconds(pump, seconds)
}
});
var time = drink.timeForRecipe(recipe)
drink.setBusyFor(time);
return {
time: time,
status: "ok",
msg: "Fazendo " + recipe
}
},
init: function() {
[0, 1, 2, 3].forEach(function(i) {
tessel.port.B.pin[i].write(1);
})
}
}
module.exports = drink;