This repository has been archived by the owner on Nov 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
168 lines (138 loc) · 4.44 KB
/
server.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
/*
Code below is for the RESTful API
Documentation will *need* to be provided
*/
var smart_bulb = require('./smart_bulb_controller');
var express = require('express');
var app = express();
var body_parser = require('body-parser');
//enable express to use body-parse to handle POST data
app.use(body_parser.urlencoded({ extended: true }));
app.use(body_parser.json());
//get our port
var port = process.env.PORT || 3000;
//routes
var router = express.Router();
app.use('/', express.static('client'));
router.get('/api', function(request, response) {
response.json({ error: 'Please see the node-bulb-api documentation for proper routes.' });
});
router.route('/api/bulbs').get(function(request, response)
{
response.json(smart_bulb_controller_instance.list_all_bulbs());
});
router.route('/api/bulbs/:bulb_id').get(function(request, response)
{
//GET
response.json(smart_bulb_controller_instance.get_bulb(request.params.bulb_id));
}).put(function(request, response)
{
//PUT
var bulb_id = request.params.bulb_id;
var hex_colour = request.body.hex_colour;
var brightness_level = request.body.brightness;
var turn_on = request.body.turn_on;
var bulb = smart_bulb_controller_instance.get_bulb(bulb_id);
if(!bulb)
{
response.json({ error: "The bulb was not found. "});
return;
}
//only change the state of the bulb if it is 'different from the previous state'
if(turn_on != bulb.turn_on)
{
if(turn_on == 'true')
{
smart_bulb_controller_instance.turn_on(bulb_id);
}
else
{
smart_bulb_controller_instance.turn_off(bulb_id);
//do not turn the light on again, so return!
return;
}
}
if(hex_colour && brightness_level)
{
smart_bulb_controller_instance.set_colour_and_brightness(bulb_id, hex_colour, brightness_level);
}
else if(hex_colour)
{
smart_bulb_controller_instance.set_colour(bulb_id, hex_colour);
}
else if(brightness_level)
{
smart_bulb_controller_instance.set_brightness(bulb_id, brightness_level);
}
response.json(smart_bulb_controller_instance.get_bulb(bulb_id));
}).delete(function(request, response)
{
//DELETE
var bulb_id = request.params.bulb_id;
smart_bulb_controller_instance.disconnect_from_bulb(bulb_id);
});
router.route('/api/homebridge/:bulb_id/status').get(function(request, response)
{
var bulb_id = request.params.bulb_id;
var bulb = smart_bulb_controller_instance.get_bulb(bulb_id);
response.send(bulb.turn_on ? "1" : "0");
});
router.route('/api/homebridge/:bulb_id/status/:sw').get((req, res) => {
var bulb_id = req.params.bulb_id;
var sw = req.params.sw;
if (sw > 0) {
smart_bulb_controller_instance.turn_on(bulb_id);
} else {
smart_bulb_controller_instance.turn_off(bulb_id);
}
res.send('OK');
});
router.route('/api/homebridge/:bulb_id/brightness').get((req, res) => {
var bulb_id = req.params.bulb_id;
var bulb = smart_bulb_controller_instance.get_bulb(bulb_id);
res.send((bulb.brightness / 2).toString());
});
router.route('/api/homebridge/:bulb_id/brightness/:val').get((req, res) => {
var bulb_id = req.params.bulb_id;
var val = req.params.val;
smart_bulb_controller_instance.set_brightness(bulb_id, val * 2);
res.send('OK');
});
router.route('/api/homebridge/:bulb_id/color').get((req, res) => {
var bulb_id = req.params.bulb_id;
var bulb = smart_bulb_controller_instance.get_bulb(bulb_id);
res.send(bulb.hex_colour.substr(1));
});
router.route('/api/homebridge/:bulb_id/color/:val').get((req, res) => {
var bulb_id = req.params.bulb_id;
var val = req.params.val;
smart_bulb_controller_instance.set_colour(bulb_id, '#' + val);
res.send('OK');
});
//register routes
app.use('/', router);
//clean bluetooth connections by disconnecting
var already_disconnected = false;
function cleanup_bulb_connections(options, err) {
if(already_disconnected)
{
return;
}
already_disconnected = true;
console.log("Server now shutting down, closing all bulb bluetooth connections...");
var bulbs = smart_bulb_controller_instance.list_all_bulbs().bulb_ids;
bulbs.forEach(function (bulb_id)
{
smart_bulb_controller_instance.disconnect_from_bulb(bulb_id);
});
process.exit();
}
//do something when app is closing
process.on('exit', cleanup_bulb_connections.bind(null, null));
//catches ctrl+c event
process.on('SIGINT', cleanup_bulb_connections.bind(null, null));
//catches uncaught exceptions
process.on('uncaughtException', cleanup_bulb_connections.bind(null, null));
//start the server
app.listen(port);
console.log('node-bulb-api server now running on port ' + port);