-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
119 lines (116 loc) · 3.34 KB
/
index.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
var express = require('express');
var bodyParser = require("body-parser")
var gpio = require('gpio');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var redPin = 21,
yellowPin = 16,
greenPin = 20;
var port = 3000;
var states = { red: 0, yellow: 0, green: 0 };
var last = 0
var cycle = function(color, frequency, millis, current) {
if (current) {
color.reset();
} else {
color.set();
}
millis -= frequency;
if (millis > 0) {
setTimeout(function() { cycle(color, frequency, millis, current ? 0 : 1) }, frequency);
} else {
green.set();
yellow.set();
red.set();
}
}
var green = gpio.export(greenPin, {
direction: "out",
ready: function() {
green.set();
green.on("change", function(val) {
states.green = val ? 0 : 1;
})
}
});
var yellow = gpio.export(yellowPin, {
direction: "out",
ready: function() {
yellow.set();
yellow.on("change", function(val) {
states.yellow = val ? 0 : 1;
})
}
});
var red = gpio.export(redPin, {
direction: "out",
ready: function() {
red.set();
red.on("change", function(val) {
states.red = val ? 0 : 1;
})
}
});
app.all('/:light/:power', function(req, res) {
lightPower(req.params.light, req.params.power, req.query.frequency, req.query.millis);
res.jsonp(null);
});
app.get('/states', function(req, res) {
res.jsonp(states);
});
app.post('/slack', function(req, res) {
var text = req.body.text.replace("light ", "").split(" ")
if (text[0] === "hammertime" || (text[0] === "hammer" && text[1] === "time")) {
lightPower("red", "just", "47", "2000");
lightPower("yellow", "just", "48", "2000");
lightPower("green", "just", "49", "2000");
} else {
lightPower(text[0], text[1], null, null);
}
});
var port = 3000;
app.listen(port, function() {
console.log('Arkalight listening on port ' + port + "!");
});
function lightPower(light, power, frequency, millis) {
last = Date.now();
if (light === "all") {
if (power === "on") {
red.reset();
green.reset();
yellow.reset();
} else if (power === "off") {
red.set();
green.set();
yellow.set();
}
} else {
let pickedLight = light === "green" ? green : (light === "yellow" ? yellow : (light === "red" ? red : null))
if (power === "on") {
pickedLight.reset();
} else if (power === "off") {
pickedLight.set();
} else if (power === "just") {
pickedLight !== red ? red.set() : red.reset();
pickedLight !== yellow ? yellow.set() : yellow.reset();
pickedLight !== green ? green.set() : green.reset();
pickedLight.reset();
if (frequency && millis) {
setTimeout(function() {
cycle(pickedLight, frequency, millis);
}, frequency);
}
}
}
}
setInterval(function() {
var current = Date.now();
console.log("Interval for shutoff");
console.log("Current: "+current);
console.log("Last: "+last);
if (current - last > 600000) {
console.log("Shutoff!!")
lightPower("all", "off");
}
}, 30000)