-
Notifications
You must be signed in to change notification settings - Fork 0
/
StepRunner.js
121 lines (105 loc) · 3.05 KB
/
StepRunner.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
var TemperatureSensor = require('./TemperatureSensor');
var LcdDisplay = require('./LcdDisplay');
//var pushToPhone = require('./Push.js');
var tempSensor = new TemperatureSensor().start(500);
var display = new LcdDisplay();
var CONSUMPTION_TEMPERATURE = 0; // 30
console.log("Ideal tea temperature:", CONSUMPTION_TEMPERATURE);
function StepRunner(props){
this.socket = null;
this.currentStep = null;
this.currentStepReleaser = null;
for (var i in props){
this[i] = props[i];
}
var _this = this;
this.STEPS = {
0: function run(){
display.setColor(0, 0, 0);
display.writeLines(['', '']);
},
1: function run(){
display.setColor(0, 0, 255);
display.writeLines(['A cup of tea?', '']);
},
2: function run(){
display.setColor(255, 0, 0);
var lines = ['o o o o o o o o', ' o o o o o o o o'];
function bubbles(){
lines.push(lines.shift());
display.writeLines(lines);
}
var interv = setInterval(bubbles, 400);
bubbles();
return function release(){
clearInterval(interv);
};
},
3: function run(){
display.setColor(255, 255, 0);
display.writeLines(['Loading theine...', '']);
},
4: function run(){
function release(){
tempSensor.removeListener("temp", onTemperature);
};
function onTemperature(celsius){
var normTemp = Math.min(255, Math.max(0, celsius - 20) * 3);
display.setColor(parseInt(normTemp), parseInt(255 - normTemp), 0);
display.writeLines(['Cooling tea...', ' ' + ('' + celsius).substr(0, 4) + ' celsius']);
if (celsius <= CONSUMPTION_TEMPERATURE) {
release();
_this.run(5);
}
if (_this.socket)
_this.socket.emit('celsius', celsius);
}
tempSensor.on('temp', onTemperature);
return release;
},
5: function run(){
display.setColor(0, 0, 255);
display.writeLines(['Enjoy your tea!', ':-)']);
//try { pushToPhone('your tea is ready to drink!'); } catch(e) {};
}
};
}
StepRunner.prototype.release = function releaseStep(){
if (this.currentStepReleaser) {
console.log('releasing step', this.currentStep);
this.currentStepReleaser();
this.currentStepReleaser = null;
}
this.run(0, true); // back to blank state
}
StepRunner.prototype.run = function runStep(i, noRelease){
if (!noRelease) {
this.release();
}
console.log('STEP', i);
this.currentStep = i;
this.currentStepReleaser = this.STEPS[i]();
};
StepRunner.prototype.setSocket = function setSocket(socket){
this.release();
this.socket = socket;
var _this = this;
socket.on('selection', function(celsius){
_this.run(1);
});
socket.on('setBoil', function(celsius){
_this.run(2);
});
socket.on('infuse', function(seconds){
_this.run(3);
});
socket.on('setMaxCelsius', function(max){
console.log('maxCelsius <-', max);
CONSUMPTION_TEMPERATURE = parseInt(max);
_this.run(4);
});
socket.on('enjoy', function(){
_this.run(5);
});
}
module.exports = StepRunner;