generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LED_Strip.ts
308 lines (241 loc) · 11.1 KB
/
LED_Strip.ts
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
/* eslint-disable max-len */
import { Service, PlatformAccessory, CharacteristicValue} from 'homebridge';
import { LEDStripsPlatform } from './platform';
import { Color } from './Color';
import { BluetoothLED } from './BluetoothLED';
import noble = require('@abandonware/noble');
import { DayLight } from './DayLight';
/**
* Platform Accessory
* An instance of this class is created for each accessory your platform registers
* Each accessory may expose multiple services of different service types.
*/
export class LED_Strip {
private led: Service;
private rainbow: Service;
private light_of_day: Service;
private bluetoothLED: BluetoothLED | undefined;
private dayLight : DayLight | undefined;
private serviceUUID : string;
private main_states = {
On: false,
Color : new Color(0, 0, 0),
};
private rainbow_states = {
On: false,
Color : new Color(0, 0, 0),
cycle_time_modifier: 0.0,
};
private light_of_day_states = {
On: false,
Brightness : 0.0,
};
private parameters = {
rainbow_cycle_time: 15.0, // Time in seconds to complete a full rainbow cycle
rainbow_update_interval: 50,
HomeKitUpdateInterval: 200,
};
private color_correction = {
r:2,
g:2.8,
b:2.8,
};
constructor(
private readonly platform: LEDStripsPlatform,
private readonly accessory: PlatformAccessory,
) {
// Set accessory information
//this.accessory.getService(this.platform.Service.AccessoryInformation)!
//.setCharacteristic(this.platform.Characteristic.Manufacturer, 'Unknown')
//.setCharacteristic(this.platform.Characteristic.Model, 'Unknown')
//.setCharacteristic(this.platform.Characteristic.SerialNumber, 'Unknown');
this.serviceUUID = accessory.context.device.serviceUUID;
this.parameters.rainbow_cycle_time = accessory.context.device.rainbowCycle;
// Prepare LED
this.bluetoothLED = new BluetoothLED(this.serviceUUID);
this.bluetoothLED.onConnect.add(() => this.platform.log.info('Connected to ' + this.accessory.displayName));
this.bluetoothLED.onConnect.add(() => this.updateLED());
this.bluetoothLED.onDisconnect.add(() => this.platform.log.info('Disconnected to ' + this.accessory.displayName));
//this.bluetoothLED.onDisconnect.add(() => this.led.setHiddenService(true));
//this.bluetoothLED.onDisconnect.add(() => this.led.setHiddenService(false));
this.dayLight = new DayLight(accessory.context.device.longitude, accessory.context.device.latitude);
noble.on('scanStart', () => this.platform.log.debug('Started Scanning...'));
noble.on('scanStop', () => this.platform.log.debug('Stopped Scanning...'));
noble.on('warning', (message: string) => this.platform.log.warn('Noble: ' + message));
noble.on('error', (message: string) => this.platform.log.error('Noble: ' + message));
this.led = this.accessory.getService('LED') || this.accessory.addService(this.platform.Service.Lightbulb, 'LED', 'LED-main');
// Register Handlers for On/Off, Hue, Saturation Characteristics
this.led.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setLEDOn.bind(this))
.onGet(this.getLEDOn.bind(this));
this.led.getCharacteristic(this.platform.Characteristic.Hue)
.onSet(this.setLEDHue.bind(this))
.onGet(this.getLEDHue.bind(this));
this.led.getCharacteristic(this.platform.Characteristic.Saturation)
.onSet(this.setLEDSaturation.bind(this))
.onGet(this.getLEDSaturation.bind(this));
this.led.getCharacteristic(this.platform.Characteristic.Brightness)
.onSet(this.setLEDBrightness.bind(this))
.onGet(this.getLEDBrightness.bind(this));
this.rainbow = this.accessory.getService('Rainbow light') || this.accessory.addService(this.platform.Service.Lightbulb, 'Rainbow light', 'LED-rainbow');
this.rainbow.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setRainbowOn.bind(this))
.onGet(this.getRainbowOn.bind(this));
this.rainbow.getCharacteristic(this.platform.Characteristic.Hue)
.onGet(this.getRainbowHue.bind(this));
this.rainbow.getCharacteristic(this.platform.Characteristic.Saturation)
.onGet(this.getRainbowSaturation.bind(this));
this.rainbow.getCharacteristic(this.platform.Characteristic.Brightness)
.onSet(this.setRainbowBrightness.bind(this))
.onGet(this.getRainbowBrightness.bind(this));
this.light_of_day = this.accessory.getService('Light of day') || this.accessory.addService(this.platform.Service.Lightbulb, 'Light of day', 'light_of_day');
this.light_of_day.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setLightOfDayOn.bind(this))
.onGet(this.getLightOfDayOn.bind(this));
this.light_of_day.getCharacteristic(this.platform.Characteristic.Hue)
.onGet(this.getLightOfDayHue.bind(this));
this.light_of_day.getCharacteristic(this.platform.Characteristic.Saturation)
.onGet(this.getLightOfDaySaturation.bind(this));
this.light_of_day.getCharacteristic(this.platform.Characteristic.Brightness)
.onSet(this.setLightOfDayBrightness.bind(this))
.onGet(this.getLightOfDayBrightness.bind(this));
setInterval(() => {
if (this.rainbow_states.On) {
this.rainbow.getCharacteristic(this.platform.Characteristic.Hue).updateValue(this.rainbow_states.Color.hue);
this.rainbow.getCharacteristic(this.platform.Characteristic.Saturation).updateValue(this.rainbow_states.Color.saturation);
this.rainbow.getCharacteristic(this.platform.Characteristic.Brightness).updateValue(this.rainbow_states.Color.brightness);
}
if (this.light_of_day_states.On && this.dayLight) {
this.rainbow.getCharacteristic(this.platform.Characteristic.Hue).updateValue(this.dayLight.color.hue);
this.rainbow.getCharacteristic(this.platform.Characteristic.Saturation).updateValue(this.dayLight.color.saturation);
this.rainbow.getCharacteristic(this.platform.Characteristic.Brightness).updateValue(this.dayLight.color.brightness);
}
}, this.parameters.HomeKitUpdateInterval);
// Update the rainbow mode when active on regular interval
setInterval(() => {
if (this.rainbow_states.On) {
this.rainbow_states.Color.hue = (this.rainbow_states.Color.hue + 0.36 * this.parameters.rainbow_update_interval * this.rainbow_states.cycle_time_modifier / (this.parameters.rainbow_cycle_time)) % 360;
this.rainbow_states.Color.saturation = 100;
}
if (this.rainbow_states.On || this.light_of_day_states.On) {
this.updateLED();
}
}, this.parameters.rainbow_update_interval);
}
/*
private updateRainbowMode() {
if (!this.states.RainbowMode) {
return;
}
this.setHue(Number(this.getHue()) + 0.36 * this.parameters.rainbow_update_interval / this.parameters.rainbow_cycle_time);
this.setSaturation(100);
this.updateColor();
}
*/
private updateLED() {
let color = new Color(0, 0, 0);
if (this.main_states.On) {
if (this.rainbow_states.On) {
color = this.rainbow_states.Color;
color.brightness = this.main_states.Color.brightness;
} else {
color = this.main_states.Color;
}
}
if (this.light_of_day_states.On) {
if (this.dayLight) {
this.platform.log.debug('Time: ' + this.dayLight.time);
this.platform.log.debug('Sunrise: ' + this.dayLight.sunrise);
this.platform.log.debug('Sunset: ' + this.dayLight.sunset);
}
const maxBrightness = 100 - color.brightness;
const dayLightColor = this.dayLight ? this.dayLight.color : new Color(0, 0, 0);
dayLightColor.brightness = Math.min(dayLightColor.brightness * (this.light_of_day_states.Brightness / 100.0), maxBrightness);
if (this.dayLight) {
//this.platform.log.debug('Daylight color: r: ' + this.dayLight.sun_light_color.red + ', g: ' + this.dayLight.sun_light_color.green + ', b:' + this.dayLight.sun_light_color.blue);
}
color = new Color(color.red + dayLightColor.red, color.green + dayLightColor.green, color.blue + dayLightColor.blue);
}
color = color.getLUTCorrected(this.color_correction.r, this.color_correction.g, this.color_correction.b);
//this.platform.log.debug('Color: r: ' + color.red + ', g: ' + color.green + ', b:' + color.blue);
if (this.bluetoothLED && this.bluetoothLED.connected) {
this.bluetoothLED.color = color;
}
}
/**
* Handle "SET" requests from HomeKit
* These are sent when the user changes the state of an accessory, for example, turning on a Light bulb.
*/
async getLEDOn(): Promise<CharacteristicValue> {
return this.main_states.On;
}
async getLEDBrightness(): Promise<CharacteristicValue> {
return this.main_states.Color.brightness;
}
async getLEDHue(): Promise<CharacteristicValue> {
return this.main_states.Color.hue;
}
async getLEDSaturation(): Promise<CharacteristicValue> {
return this.main_states.Color.saturation;
}
async setLEDOn(value: CharacteristicValue) {
this.main_states.On = value as boolean;
this.updateLED();
}
async setLEDBrightness(value: CharacteristicValue) {
this.main_states.Color.brightness = value as number;
this.updateLED();
}
async setLEDHue(value: CharacteristicValue) {
this.main_states.Color.hue = value as number;
this.updateLED();
}
async setLEDSaturation(value: CharacteristicValue) {
this.main_states.Color.saturation = value as number;
this.updateLED();
}
async getRainbowOn(): Promise<CharacteristicValue> {
return this.rainbow_states.On;
}
async getRainbowBrightness(): Promise<CharacteristicValue> {
return this.rainbow_states.cycle_time_modifier * 100.0;
}
async getRainbowHue(): Promise<CharacteristicValue> {
return this.rainbow_states.Color.hue;
}
async getRainbowSaturation(): Promise<CharacteristicValue> {
return this.rainbow_states.Color.saturation;
}
async setRainbowOn(value: CharacteristicValue) {
const tempValue = this.rainbow_states.On;
this.rainbow_states.On = value as boolean;
if ((value as boolean) && !tempValue) {
this.rainbow_states.Color = new Color(this.main_states.Color.red, this.main_states.Color.green, this.main_states.Color.blue, this.main_states.Color.alpha);
}
this.updateLED();
}
async setRainbowBrightness(value: CharacteristicValue) {
this.rainbow_states.cycle_time_modifier = (value as number) / 100.0;
this.updateLED();
}
async getLightOfDayOn(): Promise<CharacteristicValue> {
return this.light_of_day_states.On;
}
async getLightOfDayBrightness(): Promise<CharacteristicValue> {
return this.light_of_day_states.Brightness;
}
async getLightOfDayHue(): Promise<CharacteristicValue> {
return this.dayLight ? this.dayLight.color.hue : 0;
}
async getLightOfDaySaturation(): Promise<CharacteristicValue> {
return this.dayLight ? this.dayLight.color.saturation : 0;
}
async setLightOfDayOn(value: CharacteristicValue) {
this.light_of_day_states.On = value as boolean;
this.updateLED();
}
async setLightOfDayBrightness(value: CharacteristicValue) {
this.light_of_day_states.Brightness = (value as number);
this.updateLED();
}
}