-
Notifications
You must be signed in to change notification settings - Fork 5
/
web.js
108 lines (96 loc) · 2.74 KB
/
web.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
var fs = require('fs');
var async = require('async');
var bar = require('./pump-controller');
var mq3 = require('./mq3-controller');
var rgb = require('./rgbled-controller');
var port = 4242;
var express = require('express');
var app = express();
var server = require('http').createServer(app, { log: false });
app.use(express.json());
app.use(express.urlencoded());
app.use(express.compress());
app.use(express.static(__dirname + '/public'));
var recipes = [];
String.prototype.endsWith = function(suffix) {
return this.match(suffix+"$") == suffix;
};
var prepareDrink = function(ingredients, cb) {
async.eachSeries(ingredients, function (i, cb) {
bar.drainIndex(i.index, i.time, function () {
cb(null);
});
}, function (err, res) {
cb(err);
});
};
fs.readdir('./recipes/', function (err, files) {
files.forEach(function (f) {
if (f.endsWith('.json')) {
fs.readFile('./recipes/' + f, function (err, data) {
if (err) {
console.log('Error: ' + err);
process.exit();
} else {
var r = JSON.parse(data);
recipes.push(r);
console.log('cocktail ' + r.name + ' added');
app.post('/api/' + r.api, function (req, res) {
prepareDrink(r.ingredients, function (err) {
res.send('ok');
});
});
}
});
}
});
});
app.get('/api/recipes', function (req,res) {
var re = [];
recipes.forEach(function (r) {
re.push({name: r.name, endpoint: '/api/' + r.api});
});
res.send(re);
});
server.listen(process.env.PORT || port);
console.log('server started on port ' + (process.env.PORT || port));
app.post('/api/purge', function()
{
console.log('Entering purge');
bar.purgeAll();
});
app.post('/api/emergency', function()
{
console.log('Entering Emergency Stop');
bar.stopAll();
});
app.get('/api/drunk', function(req,res)
{
rgb.blinkColor({'r' : 1, 'g' : 0, 'b' : 0}, 500);
mq3.getLowerValue(500, 4, function(avg_value)
{
console.log('[MQ3] Average: ' + avg_value);
console.log(mq3.getValue());
if (avg_value < 800)
{
// Green
rgb.setRgbValueTimer({'r' : 0, 'g' : 1, 'b' : 0}, 3000, true);
}
else if(avg_value >= 800 && avg_value <= 1500)
{
// Green + Blue
rgb.setRgbValueTimer({'r' : 0, 'g' : 0, 'b' : 1}, 3000, true);
}
else if (avg_value > 1500 && avg_value <= 2500)
{
// Green + Red
rgb.setRgbValueTimer({'r' : 1, 'g' : 0, 'b' : 1}, 3000, true);
}
else if (avg_value > 2500)
{
// Red
rgb.setRgbValueTimer({'r' : 1, 'g' : 0, 'b' : 0}, 3000, true);
}
res.send({'drunk' : avg_value});
});
});