This repository was 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
Expand file tree
/
Copy pathsmart_bulb_controller.js
More file actions
244 lines (192 loc) · 6.61 KB
/
smart_bulb_controller.js
File metadata and controls
244 lines (192 loc) · 6.61 KB
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
/*
Code below is for handling the connection between node and the smart bulb via Bluetooth Low Energy (BLE)
*/
var SmartBulb = require('./smart_bulb');
var noble = require('noble');
//Controller class
function SmartBulbController() {
this.connected_smart_bulbs = {};
}
//adds a bulb to the connected smart bulbs list
SmartBulbController.prototype.add_bulb = function(bulb_id, smart_bulb)
{
this.connected_smart_bulbs[bulb_id] = smart_bulb;
this.status_packet_buffer = new Buffer('0F050400000005FFFF', 'hex');
}
//returns a json object representing if the bulb disconnected or not
SmartBulbController.prototype.disconnect_from_bulb = function(bulb_id)
{
var bulb = this.connected_smart_bulbs[bulb_id];
if (bulb) {
bulb.disconnect();
delete this.connected_smart_bulbs[bulb_id];
}
return { success: 'Smart bulb was disconnected.' };
}
//returns an array of uuids of the bulbs that we are connected to
//see Reference_Packets.txt on how packets are formatted
SmartBulbController.prototype.list_all_bulbs = function() {
return { bulb_ids: Object.keys(this.connected_smart_bulbs) };
}
//returns an object representing a bulb
SmartBulbController.prototype.get_bulb = function(bulb_id) {
var bulb = this.connected_smart_bulbs[bulb_id]
if(bulb)
{
var sanitized_bulb_obj = {};
sanitized_bulb_obj.hex_colour = bulb.get_colour().hex;
sanitized_bulb_obj.brightness = bulb.get_brightness();
sanitized_bulb_obj.turn_on = bulb.get_turned_on_off_status();
return sanitized_bulb_obj;
}
else
{
return { error: "Bulb with id " + bulb_id + " was does not exist!" };
}
}
//turns off the bulb
SmartBulbController.prototype.turn_off = function(bulb_id) {
var bulb = this.connected_smart_bulbs[bulb_id];
bulb.set_turned_on_off_status(false);
return this.set_brightness(bulb_id, 0);
}
//turns on the bulb
SmartBulbController.prototype.turn_on = function(bulb_id) {
var bulb = this.connected_smart_bulbs[bulb_id];
bulb.set_turned_on_off_status(true);
return this.set_brightness(bulb_id, 200);
}
SmartBulbController.prototype.update_bulb_status = function(bulb_id) {
var bulb = this.connected_smart_bulbs[bulb_id];
bulb.update_internal_data(this.status_packet_buffer);
}
//sets a colour for the bulb
SmartBulbController.prototype.set_colour = function(bulb_id, hex_colour) {
var bulb = this.connected_smart_bulbs[bulb_id];
var rgb_values = hex_to_rgb(hex_colour);
var brightness_level = bulb.get_brightness();
return this.set_colour_and_brightness(bulb_id, rgb_values.hex, brightness_level);
}
//sets the brightness for the bulb
SmartBulbController.prototype.set_brightness = function(bulb_id, brightness_level) {
var bulb = this.connected_smart_bulbs[bulb_id];
var rgb_values = bulb.get_colour();
return this.set_colour_and_brightness(bulb_id, rgb_values.hex, brightness_level);
}
//sets the colour and brightness
SmartBulbController.prototype.set_colour_and_brightness = function(bulb_id, hex_colour, brightness_level) {
var rgb_values = hex_to_rgb(hex_colour);
var bulb = this.connected_smart_bulbs[bulb_id];
bulb.set_brightness(brightness_level);
bulb.set_colour(rgb_values);
//validation for brightness levels
//values range from 0 to 200
if(brightness_level < 0)
{
brightness_level = 0;
}
else if(brightness_level > 200)
{
brightness_level = 200;
}
var raw_buffer = new Buffer(17);
raw_buffer[0] = 0x0F; //15
raw_buffer[1] = 0x0D; //13
raw_buffer[2] = 0x03; //3
raw_buffer[3] = 0x00; //empty?
//RGB and brightness
raw_buffer[4] = rgb_values.red;
raw_buffer[5] = rgb_values.green;
raw_buffer[6] = rgb_values.blue;
raw_buffer[7] = brightness_level;
//x and y coords
//no idea what this does, seems like it is related to the multiple bulb lightning?
raw_buffer[8] = 0x00;
raw_buffer[9] = 0xC8;
raw_buffer[10] = 0x00;
raw_buffer[11] = 0xC8;
raw_buffer[12] = 0x00;
raw_buffer[13] = 0x00;
//last 2 bytes are always 0xFF
raw_buffer[15] = 0xFF;
raw_buffer[16] = 0xFF;
//verification byte
raw_buffer[14] = 0x01; //start off as 1
for(var i = 2; i < 14; i++)
{
raw_buffer[14] += raw_buffer[i];
}
bulb.write_data(raw_buffer);
return this.get_bulb(bulb_id);
}
//singleton approach for the controller
smart_bulb_controller_instance = new SmartBulbController();
//smaller helper function to convert hex to rgb values
function hex_to_rgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
red: parseInt(result[1], 16),
green: parseInt(result[2], 16),
blue: parseInt(result[3], 16),
hex: hex
} : null;
}
//do not touch the functions below unless you know what you are doing
//functions below pretain to automatic find/connect of smart bulbs
//only scan for BLE devices when bluetooth device is enabled
noble.on('stateChange', function(state) {
if (state === 'poweredOn') {
console.log('Now scanning for Smart Bulbs....');
noble.startScanning();
}
else {
noble.stopScanning();
}
});
//connection handler
noble.on('discover', function(peripheral) {
var advertising_name = peripheral.advertisement.localName;
if (smart_bulb_controller_instance.connected_smart_bulbs[peripheral.uuid]) {
return;
}
//only get our bulbs (all bulbs start with the name DELIGHT)
if(advertising_name == 'DELIGHT' || advertising_name == 'BLEBULB-10')
{
console.log('Found a Revogi Smart Bulb with id ' + peripheral.uuid);
noble.stopScanning();
connect_to_bulb(peripheral);
}
});
//connect to a smart bulb
function connect_to_bulb(bulb)
{
bulb.connect(function(error) {
console.log('Connected to bulb with id ' + bulb.uuid);
bulb.discoverServices(['fff0'], function(error, services)
{
var main_service = services[0];
main_service.discoverCharacteristics(['fff3', 'fff4', 'fff6'], function(error, characteristics)
{
//get the write characteristic
var write_characteristic = characteristics[0];
//reading characteristic
var reading_characteristic = characteristics[1];
//useful later?
var friendly_name_characteristic = characteristics[2];
//add the bulb for tracking
var smart_bulb = new SmartBulb(bulb, write_characteristic, reading_characteristic, friendly_name_characteristic);
smart_bulb_controller_instance.add_bulb(bulb.uuid, smart_bulb);
smart_bulb_controller_instance.update_bulb_status(bulb.uuid);
console.log('Now tracking smart bulb with id ' + bulb.uuid);
//start scanning for more bulbs
noble.startScanning();
});
});
bulb.on('disconnect', function(error) {
console.log("Disconnected from " + bulb.uuid);
noble.stopScanning();
smart_bulb_controller_instance.disconnect_from_bulb(bulb.uuid);
noble.startScanning();
});
});
}