-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
376 lines (345 loc) · 10.8 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*eslint-env node*/
//------------------------------------------------------------------------------
// node.js starter application for Bluemix
//------------------------------------------------------------------------------
// This application uses express as its web server
// for more info, see: http://expressjs.com
var express = require('express');
var http = require('http');
var bodyParser = require('body-parser');
var jsonParser = bodyParser.json();
var textParser = bodyParser.text();
var mqtt = require('mqtt')
var _ = require('lodash');
// var client = mqtt.connect('mqtt:192.168.86.10')
var client = mqtt.connect('mqtt:scottchapman.no-ip.org')
var swaggerUI = require('swagger-ui-express');
var swaggerDoc = require('./swagger.json');
var OneColor = require('onecolor');
var boolifyString = require('boolify-string');
// colors
// bri uint8 The brightness value to set the light to.
// Brightness is a scale from 1 (the minimum the light is capable of) to 254 (the maximum). Note: a brightness of 1 is not off.
//
// e.g. “brightness”: 60 will set the light to a specific brightness
//
// Optional
// hue uint16 The hue value to set light to.
// The hue value is a wrapping value between 0 and 65535. Both 0 and 65535 are red, 25500 is green and 46920 is blue.
//
// e.g. “hue”: 50000 will set the light to a specific hue.
var colors = {
red: {bri: 254, hue: 0, sat: 254},
green: {bri: 254, hue: 25500, sat: 254},
blue: {bri: 254, hue: 46920, sat: 254},
}
var hubKeys = {
"9.32.241.244": "N6ZMYDGSMy4dyJi2M8xB4JJ6uP49bwM77TLa149S",
"9.32.235.253": "N6ZMYDGSMy4dyJi2M8xB4JJ6uP49bwM77TLa149S",
"9.53.24.197": "XurcL3CkWz6xhAPswke9X8dq10oTAUysogbee8ra"
};
// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');
// create a new express server
var app = express();
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerDoc));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));
// body parser
// app.use(bodyParser.json());
// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
app.get('/Hub/:hubname', function(req,res) {
var hubname = req.params.hubname;
if (hubs.hasOwnProperty(hubname)) {
res.status(200).send(hubs[hubname]).end();
}
else {
res.status(400).send({status: "hubname not found"}).end();
}
})
app.get('/Hubs', function(req,res) {
res.status(200).send(_.keys(hubs)).end();
})
function GetLightByName(lightname, callback) {
var found = false;
console.log("Looking for light: " + lightname);
_.keys(hubs).forEach(function (hub) {
if (found) return;
_.keys(hubs[hub].lights).forEach(function (lightnum) {
if (found) return;
var light = hubs[hub].lights[lightnum];
console.log("Checking: " + light.name);
if (light.name.toUpperCase() === lightname.toUpperCase()) {
console.log("found!");
light.hub = hubs[hub].config;
light.number = lightnum;
callback(null, light);
found = true;
}
})
});
if (!found) callback(true, "No light found");
}
app.get('/Light/:lightname', function(req,res) {
var lightname = req.params.lightname;
GetLightByName(lightname, function(err, light) {
if (err) {
console.log("NOT found!");
res.status(400).send({status: "Light not found"}).end();
}
else {
console.log("found!");
res.status(200).send(light).end();
}
})
})
function expand(state, callback) {
var error = null;
if (state.hasOwnProperty("color")) {
var color = OneColor(state.color);
if (color) {
_.assign(state, toHSB(color));
delete state.color;
}
else {
error = "No such color: " + state.color
}
}
if (!state.hasOwnProperty("On"))
state.on = true;
if (!state.hasOwnProperty("transitiontime"))
state.transitiontime = 0;
callback(error,state);
}
var statusColors = {
"green": "green",
"red": "red",
"good": "green",
"bad": "red",
"up": "green",
"down": "red",
}
var statusLightList = [
"Scott Bloom",
"Jim Bloom",
"Bruce Bloom",
"David Bloom",
"Miguel Bloom",
"Vijay Bloom"
];
function setPipelineStatusLights(status) {
status = status.toLowerCase();
if (statusColors.hasOwnProperty(status)) {
var body = {color: statusColors[status]};
statusLightList.forEach(function(lightname) {
GetLightByName(lightname, function(err, light) {
if (!err) {
expand(body, function(error, state) {
if (!error) {
var message = {
state: state,
light: light,
key: hubKeys[light.hub.ipaddress],
};
console.dir(message);
client.publish('SetLightState',JSON.stringify(message));
}
})
}
})
})
}
}
app.post('/PipelineStatus', jsonParser, function(req,res) {
var body = req.body;
if (body.hasOwnProperty("Status")) {
integrationPipelineStatus = body.Status;
setPipelineStatusLights(integrationPipelineStatus);
res.status(200).send({status: "Accepted"}).end();
}
else {
res.status(400).send({status: "Bad Request"}).end();
}
})
app.get('/PipelineStatus', jsonParser, function(req,res) {
res.status(200).send({Status: integrationPipelineStatus}).end();
})
app.post('/Light/:lightname', jsonParser, function(req,res) {
var lightname = req.params.lightname;
var body = req.body;
GetLightByName(lightname, function(err, light) {
if (!err) {
expand(body, function(error, state) {
if (!error) {
var message = {
state: state,
light: light,
key: hubKeys[light.hub.ipaddress],
};
client.publish('SetLightState',JSON.stringify(message));
res.status(200).send({status: "Light State Update Request sent"}).end();
console.log("Sending Message!");
console.dir(message);
}
else {
res.status(400).send(error).end();
}
})
}
else {
console.log("NOT found!");
res.status(400).send({status: "Light not found"}).end();
}
})
})
app.post('/Power/:lightname/:state', function(req,res) {
var lightname = req.params.lightname;
var state = req.params.state;
GetLightByName(lightname, function(err, light) {
if (!err) {
var body = {"on": boolifyString(state)};
expand(body, function(error, state) {
if (!error) {
var message = {
state: state,
light: light,
key: hubKeys[light.hub.ipaddress],
};
client.publish('SetLightState',JSON.stringify(message));
res.status(200).send({status: "Light State Update Request sent"}).end();
console.log("Sending Message!");
console.dir(message);
}
else {
console.log("Error: " + error);
res.status(400).send(error);
}
})
}
else {
console.log("NOT found!");
res.status(400).send({status: "Light not found"}).end();
}
})
})
function toHSB(color) {
return {
hue: Math.round(color.hue()*65535),
bri: Math.round(color.lightness() * 254),
sat: Math.round(color.saturation() * 254)
}
}
/*
app.post('/Color/:lightname', textParser, function(req,res) {
var lightname = req.params.lightname;
var body = req.body;
var color = OneColor(body);
if (color) {
GetLightByName(lightname, function(err, light) {
if (!err) {
var message = {
state: toHSB(color),
light: light,
key: hubKeys[light.hub.ipaddress],
};
client.publish('SetLightState',JSON.stringify(message));
res.status(200).send({status: "Light State Update Request sent"}).end();
console.log("Sending Message!");
console.dir(message);
found = true;
}
else {
console.log("NOT found!");
res.status(400).send({status: "Light not found"}).end();
}
})
}
else {
console.log("Color not found");
res.status(400).send({status: "color not found"}).end();
}
})
*/
app.post('/Color/:lightname/:color', textParser, function(req,res) {
var lightname = req.params.lightname;
var body = {color: req.params.color};
GetLightByName(lightname, function(err, light) {
if (!err) {
expand(body, function(error, state) {
if (!error) {
var message = {
state: state,
light: light,
key: hubKeys[light.hub.ipaddress],
};
client.publish('SetLightState',JSON.stringify(message));
res.status(200).send({status: "Light State Update Request sent"}).end();
console.log("Sending Message!");
console.dir(message);
found = true;
}
else {
console.log("error: " + error);
res.status(400).send(error).end();
}
})
}
else {
console.log("NOT found!");
res.status(400).send({status: "Light not found"}).end();
}
})
})
/*
app.post('/SetColor', function (req,res) {
var body = req.body;
var required = ['color','light'];
console.log("Yay!");
console.dir(body);
if (required.length == _.intersection(required,_.keys(body)).length) {
if (colors.hasOwnProperty(body.color)) {
var color = colors[body.color];
color.on = true;
color.transitiontime = 0;
client.publish(body.light,JSON.stringify(color));
color.light = body.light;
client.publish('lights',JSON.stringify(color));
res.status(200).send({status: "All set!"}).end();
}
else {
res.status(400).send({status: "color not supported"}).end();
}
}
else {
res.status(400).send({status: "missing required fields (color, light)"}).end();
}
})
*/
var integrationPipelineStatus = "Down";
var hubs = {};
client.subscribe('HubConfig/+');
client.on('message', function(topic, message) {
try {
var obj = JSON.parse(message);
if (obj.hasOwnProperty('config') && obj.config.hasOwnProperty('whitelist'))
delete obj.config.whitelist;
hubs[obj.config.name] = obj;
console.log("Got config for: " + obj.config.name);
}
catch (err) {
console.log("ERROR: " + err);
}
// console.dir(obj);
})
// start server on the specified port and binding host
var httpServer = http.createServer(app).listen(appEnv.port, '0.0.0.0', function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});