forked from luxdvie/WS2812Controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
275 lines (241 loc) · 6.98 KB
/
app.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*******************************
WS2812 LED Control
A tiny, primitive app for controlling WS2812 LEDs.
Austin Brown - 2020
http://luxdvie.github.io
*******************************/
/* Common Libraries & Setup */
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var path = require("path");
var os = require('os');
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
/* The port the server will listen on */
var HTTP_PORT = 8080;
/* Global reference to the LED strip */
var strip = require("./strip.js");
/* Animation Libraries */
var xmas = require("./animations/xmas.js");
var fade = require("./animations/fade.js");
var static = require("./animations/static.js");
var rainbow = require("./animations/rainbow.js");
var control = require("./animations/control.js");
var dance = require("./animations/dance.js");
var twinkle = require("./animations/twinkle.js");
const { brightness } = require("./strip.js");
// Find the first local, ipv4 address
// This is a 'best guess' that the web server can be accessed
// via this address. Your mileage may vary!
// https://stackoverflow.com/questions/10750303/how-can-i-get-the-local-ip-address-in-node-js
// https://nodejs.org/api/os.html#os_os_networkinterfaces
var interfaces = os.networkInterfaces();
var localAddress = "";
for (var k in interfaces) {
for (var k2 in interfaces[k]) {
var address = interfaces[k][k2];
if (address.family === 'IPv4' && !address.internal) {
localAddress = address.address;
break;
}
}
}
/*****************
Web Methods
*****************/
/**
* Generic handler for an animation request.
* All requests should be in the format
* lib => name of animation package you're referencing
* method => name of method in package you're calling
* args => object containing any arguments you want to pass to the animation method
*/
app.post("/AnimationRequest", function (request, response) {
response.header("Access-Control-Allow-Origin", "*");
var lib = request.body.hasOwnProperty("lib") ? request.body.lib : "";
var instance = GetLibraryInstance(lib);
if (instance === null || typeof instance === "undefined") {
response.send("Library not found.");
return;
}
var method = request.body.hasOwnProperty("method")
? request.body.method
: null;
if (method === null) {
response.send("Method not found.");
return;
}
var args = request.body.hasOwnProperty("args") ? request.body.args : "";
if (typeof instance[method] === "function") {
var rsp = instance[method](args, strip);
response.send(rsp);
} else {
response.send("Function not found");
}
});
/**
* Home
*/
app.get("/", function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.sendFile(path.join(__dirname + "/app.html"));
});
/*****************
Admin
*****************/
app.get("/admin/reboot", function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.send("Issuing shutdown command.");
require('child_process').exec('sudo reboot', function (msg) { console.log("Issuing reboot command.") });
});
app.get("/admin/poweroff", function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.send("Issuing shutdown command.");
require('child_process').exec('sudo poweroff', function (msg) { console.log("Issuing shutdown command.") });
});
/*****************
API
*****************/
/**
* Turn on LED Strip
*/
app.get("/api/v1/on", function (req, res) {
// switch led on
res.header("Access-Control-Allow-Origin", "*");
strip.On()
res.send("1")
});
/**
* Turn off LED-Strip
*/
app.get("/api/v1/off", function (req, res) {
// switch led off
res.header("Access-Control-Allow-Origin", "*");
strip.Off()
res.send("0")
});
/**
* Get status of LED-Strip
* @returns 0 if off
* @returns 1 if on
*
*/
app.get("/api/v1/status", function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.send(`${strip.GetStripStatus()}`)
});
/**
* Get Brightness of LED-Strip as float value between 0 and 1
* 0 is off
* 1 is 100% on
* @returns float value with brightness between 0 and 1
*/
app.get("/api/v1/brightness", function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
let stripBrightness = strip.GetBrightness()
let calc = stripBrightness / 2.55
calc = Math.floor(calc)
res.send(`${calc}`)
});
/**
* Set Brightness of LED-Strip value
* @valueOfBrightness value between 0 and 100
* @returns float between 0 and 1
*/
app.get("/api/v1/brightness/:valueOfBrightness", function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
let reqBrightness = req.params.valueOfBrightness
if (reqBrightness === "0") {
strip.Off()
res.send("0")
} else {
let brightness_255 = Math.ceil(reqBrightness * 2.55)
//let brightness_response = (reqBrightness / 100)
strip.SetBrightness(brightness_255)
res.send(`${reqBrightness}`)
}
});
app.post("/api/v1/brightness/:valueOfBrightness", function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
let reqBrightness = req.params.valueOfBrightness
if (reqBrightness === "0") {
strip.Off()
res.send("0")
} else {
let brightness_255 = Math.ceil(reqBrightness * 2.55)
//let brightness_response = (reqBrightness / 100)
strip.SetBrightness(brightness_255)
res.send(`${reqBrightness}`)
}
});
app.get("/api/v1/color", function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
let color = strip.color
res.send(`${color}`)
});
app.get("/api/v1/color/:valueOfColor", function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
let color = req.params.valueOfColor
let dec_color = parseInt(color, 16)
strip.SetStripColor(dec_color)
res.send(`${color}`)
});
app.post("/api/v1/color/:valueOfColor", function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
let color = req.params.valueOfColor
let dec_color = parseInt(color, 16)
strip.SetStripColor(dec_color)
res.send(`${color}`)
});
/*****************
Common
*****************/
/**
* Find the correct reference to a specific library name;
* @param {string} key The name of the library to find
*/
function GetLibraryInstance(key) {
var lib = null;
switch (key) {
case "static":
lib = static;
break;
case "xmas":
lib = xmas;
break;
case "fade":
lib = fade;
break;
case "rainbow":
lib = rainbow;
break;
case "control":
lib = control;
break;
case "dance":
lib = dance;
break;
case "twinkle":
lib = twinkle;
break;
}
return lib;
}
/*****************
Startup
*****************/
var server = app.listen(HTTP_PORT, function () {
console.log("***************************");
console.log(" WS2812 CONTROLLER STARTUP ");
console.log(" Web Server listening at the location below, or by host name and port. ");
console.log(" http://" + localAddress + ":" + HTTP_PORT);
console.log("***************************");
//Run the 'Rainbow' routing on startup
var rainbowInstance = GetLibraryInstance("rainbow");
if (rainbowInstance) {
rainbowInstance.GoRainbow("", strip);
}
});