-
Notifications
You must be signed in to change notification settings - Fork 2
/
light_control.pl
executable file
·413 lines (340 loc) · 11.5 KB
/
light_control.pl
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#! /usr/bin/perl -w
## Version 0.4 (9/9/15)
#
# Edit SQLite DB file path in get_dbsource subroutine
use strict;
use Getopt::Mixed;
use DBI;
use IO::Socket;
my ($optLight, $optState, $optDelay, $optBrightness, $optTransInterval, $optColour, $optHue, $optNotification, $optEnforceTime);
Getopt::Mixed::init( 'l=s s=s d=i b=i t=s c=i h=i n=i e=s light>l state>s delay>d brightness>b interval>t colour>c hue>h notification>n enforcetime>e');
while( my( $option, $value, $pretty ) = Getopt::Mixed::nextOption() ) {
OPTION: {
$option eq 'l' and do {
$optLight = $value;
last OPTION;
};
$option eq 's' and do {
$optState = $value if $value;
$optState = lc $optState;
$optState = 0 if $optState eq 'off';
$optState = 1 if $optState eq 'on';
$optState = 9 if $optState eq 'toggle';
last OPTION;
};
$option eq 'd' and do {
$optDelay = $value if $value;
last OPTION;
};
$option eq 'b' and do {
$optBrightness = $value if $value;
last OPTION;
};
$option eq 't' and do {
$optTransInterval = $value if $value;
last OPTION;
};
$option eq 'c' and do {
$optColour = $value if $value;
last OPTION;
};
$option eq 'h' and do {
$optHue = $value if $value;
last OPTION;
};
$option eq 'n' and do {
$optNotification = $value if $value;
last OPTION;
};
$option eq 'e' and do {
$optEnforceTime = $value if $value;
last OPTION;
};
}
}
Getopt::Mixed::cleanup();
die "No light specified via -l or --light.\n" unless $optLight;
checkTime() if $optEnforceTime; ## If enforce time flag parsed, check is 'night time' before allowing lights
my %light = getLightDB($optLight);
if ( defined $optNotification ) {
sendNotification($optNotification,$light{zone},$light{type},$light{host},$light{state},$light{brightness},$light{colour},$light{hue});
}
if ( defined $optState && $optState != $light{state} ) {
my $newState = changeLightState($optState,$optDelay,$light{zone},$light{type},$light{host},$light{state});
changeLightStateDB($light{id},$newState);
}
if ( defined $optBrightness && $light{state} != 0 ) {
my $newBrightness = changeLightBrightness($optBrightness,$light{zone},$light{type},$light{host},$light{brightness},$optTransInterval);
changeLightBrightnessDB($light{id},$newBrightness);
}
sub changeLightState {
my $optState = shift;
my $optDelay = shift;
my $zone = shift;
my $type = shift;
my $host = shift;
my $state = shift;
my ($offHex, $onHex);
sleep $optDelay if $optDelay;
my $socks = IO::Socket::INET->new(
Proto => 'udp',
PeerPort => 8899,
PeerAddr => $host,
) or die "It didn't work. $! \n";
if ( $optState == 9 ) {
## State option is 'toggle', make state opposite of current state
$optState = 0 if $state == 1;
$optState = 1 if $state == 0;
}
if ( $optState == 0 ) {
## Turn off lights
my $offHex = getHexOnOffValue($zone,$type,0);
$socks->send($offHex) or die "Nope, can't send that. $! \n";
select(undef, undef, undef, 0.10); ## Delay 100ms
$socks->send($offHex) or die "Nope, can't send that. $! \n";
select(undef, undef, undef, 0.10); ## Delay 100ms
$socks->send($offHex) or die "Nope, can't send that. $! \n";
$state = $optState
}
elsif ( $optState == 1 ) {
## Turn on light UDP call
my $onHex = getHexOnOffValue($zone,$type,1);
$socks->send($onHex) or die "Nope, can't send that. $! \n";
select(undef, undef, undef, 0.10); ## Delay 100ms
$socks->send($onHex) or die "Nope, can't send that. $! \n";
select(undef, undef, undef, 0.10); ## Delay 100ms
$socks->send($onHex) or die "Nope, can't send that. $! \n";
$state = $optState
}
return $state;
}
sub changeLightBrightness {
my $optBrightness = shift;
my $zone = shift;
my $type = shift;
my $host = shift;
my $brightness = shift;
my $optTransInterval = shift;
my ($brightenHex, $dimHex);
my $interval;
my $onHex = getHexOnOffValue($zone,$type,1);
if ( $optTransInterval ) {
$interval = $optTransInterval;
}
else {
$interval = 0.10;
}
my $socks = IO::Socket::INET->new(
Proto => 'udp',
PeerPort => 8899,
PeerAddr => $host,
) or die "It didn't work. $! \n";
if ( $type eq 'DW' ) {
$optBrightness = "10" if $optBrightness > 10;
if ( $optBrightness > $brightness ) {
## Increase brightness
$brightenHex = "\x3C\x00\x55";
my $count = $optBrightness - $brightness;
$socks->send($onHex) or die "Nope, can't send that. $! \n";
select(undef, undef, undef, 0.10); ## Delay 100ms
for (my $i=0; $i <= $count; $i++) {
$socks->send($brightenHex) or die "Nope, can't send that. $! \n";
select(undef, undef, undef, $interval); ## Delay predefined interval
}
}
elsif ( $optBrightness < $brightness ) {
## Decrease brightness
$dimHex = "\x34\x00\x55";
my $count = $brightness - $optBrightness;
$socks->send($onHex) or die "Nope, can't send that. $! \n";
select(undef, undef, undef, 0.10); ## Delay 100ms
for (my $i=0; $i <= $count; $i++) {
$socks->send($dimHex) or die "Nope, can't send that. $! \n";
select(undef, undef, undef, $interval); ## Delay predefined interval
}
}
return $optBrightness;
}
elsif ( $type eq 'RGBW' ) {
$optBrightness = "25" if $optBrightness > 25;
if ( $optBrightness > $brightness ) {
$brightness = $brightness + 1;
my %brightnessHexValues = getBrightnessHexValuesDB();
$socks->send($onHex) or die "Nope, can't send that. $! \n";
select(undef, undef, undef, 0.10); ## Delay 100ms
if ( defined $optTransInterval ) {
## Transition interval specified, step through brightness intervals
for (my $i=$brightness; $i <= $optBrightness; $i++) {
my $hexValue = pack "H*",$brightnessHexValues{$i};
$socks->send($hexValue) or die "Nope, can't send that. $! \n";
select(undef, undef, undef, $interval); ## Delay predefined interval
}
}
else {
## No transition, go straight to new brightness
my $hexValue = $brightnessHexValues{$optBrightness};
$socks->send($hexValue) or die "Nope, can't send that. $! \n";
select(undef, undef, undef, $interval); ## Delay predefined interval
$socks->send($hexValue) or die "Nope, can't send that. $! \n";
}
}
elsif ( $optBrightness < $brightness ) {
$brightness = $brightness - 1;
my %brightnessHexValues = getBrightnessHexValuesDB();
$socks->send($onHex) or die "Nope, can't send that. $! \n";
select(undef, undef, undef, 0.10); ## Delay 100ms
if ( defined $optTransInterval ) {
## Transition interval specified, step through brightness intervals
for (my $i=$brightness; $i >= $optBrightness; $i--) {
my $hexValue = pack "H*",$brightnessHexValues{$i};
$socks->send($hexValue) or die "Nope, can't send that. $! \n";
select(undef, undef, undef, $interval); ## Delay predefined interval
}
}
else {
## No transition, go straight to new brightness
my $hexValue = $brightnessHexValues{$optBrightness};
$socks->send($hexValue) or die "Nope, can't send that. $! \n";
select(undef, undef, undef, $interval); ## Delay predefined interval
$socks->send($hexValue) or die "Nope, can't send that. $! \n";
}
}
return $optBrightness;
}
}
sub sendNotification {
my $optNotification = shift;
my $zone = shift;
my $type = shift;
my $host = shift;
my $state = shift;
my $brightness = shift;
my $colour = shift;
my $hue = shift;
my $offHex = getHexOnOffValue($zone,$type,0);
my $onHex = getHexOnOffValue($zone,$type,1);
my $socks = IO::Socket::INET->new(
Proto => 'udp',
PeerPort => 8899,
PeerAddr => $host,
) or die "It didn't work. $! \n";
## Flash twice notification
$socks->send($onHex) if $state == 0;
select(undef, undef, undef, 0.10); ## Delay 100ms
$socks->send($onHex) if $state == 0;
select(undef, undef, undef, 0.10); ## Delay 100ms
$socks->send($onHex);
select(undef, undef, undef, 0.20); ## Delay 200ms
$socks->send($offHex);
select(undef, undef, undef, 0.20); ## Delay 200ms
$socks->send($onHex);
select(undef, undef, undef, 0.20); ## Delay 200ms
$socks->send($offHex);
select(undef, undef, undef, 0.20); ## Delay 200ms
$socks->send($onHex) if $state == 1;
select(undef, undef, undef, 0.10); ## Delay 100ms
$socks->send($offHex) if $state == 0;
}
sub getHexOnOffValue {
my $zone = shift;
my $type = shift;
my $state = shift;
my $hexValue = "error";
if ( $type eq 'DW' && $state == 0 ) {
## Dual White OFF HEX Values
$hexValue = "\x3B\x00\x55" if $zone == 1;
$hexValue = "\x33\x00\x55" if $zone == 2;
$hexValue = "\x3A\x00\x55" if $zone == 3;
$hexValue = "\x36\x00\x55" if $zone == 4;
}
elsif ( $type eq 'DW' && $state == 1 ) {
## Dual White ON HEX Values
$hexValue = "\x38\x00\x55" if $zone == 1;
$hexValue = "\x3D\x00\x55" if $zone == 2;
$hexValue = "\x37\x00\x55" if $zone == 3;
$hexValue = "\x32\x00\x55" if $zone == 4;
}
elsif ( $type eq 'RGBW' && $state == 0 ) {
## RGBW OFF HEX Values
$hexValue = "\x46\x00\x55" if $zone == 1;
$hexValue = "\x48\x00\x55" if $zone == 2;
$hexValue = "\x4A\x00\x55" if $zone == 3;
$hexValue = "\x4C\x00\x55" if $zone == 4;
}
elsif ( $type eq 'RGBW' && $state == 1 ) {
## RGBW ON HEX Values
$hexValue = "\x45\x00\x55" if $zone == 1;
$hexValue = "\x47\x00\x55" if $zone == 2;
$hexValue = "\x49\x00\x55" if $zone == 3;
$hexValue = "\x4B\x00\x55" if $zone == 4;
}
return $hexValue;
}
sub checkTime {
use Time::localtime;
my $hour = localtime->hour();
if ( $hour <= 19 && $hour >= 7 ) {
## Is 'daytime' between 7AM and 7PM, do not allow lights and exit
exit;
}
}
sub getLightDB {
$optLight = shift;
my %light;
my $ds = get_datasource();
my $dbh = DBI->connect($ds) || die "DBI::errstr";
my ($id, $name, $zone, $type, $host, $state, $brightness, $colour, $hue);
my $query = $dbh->prepare("select ID, NAME, ZONE, TYPE, HOST, STATE, BRIGHTNESS, COLOUR, HUE from LIGHTS where NAME = '$optLight'") || die "DBI::errstr";
$query->execute;
$query->bind_columns(\$id,\$name,\$zone,\$type,\$host,\$state,\$brightness,\$colour,\$hue);
while ($query->fetchrow_arrayref()) {
$light{"id"}=$id;
$light{"name"}=$name;
$light{"zone"}=$zone;
$light{"type"}=$type;
$light{"host"}=$host;
$light{"state"}=$state;
$light{"brightness"}=$brightness;
$light{"colour"}=$colour;
$light{"hue"}=$hue;
}
$query->finish();
return %light;
}
sub getBrightnessHexValuesDB {
my ($key, $value);
my %brightnessHexValues;
my $ds = get_datasource();
my $dbh = DBI->connect($ds) || die "DBI::errstr";
my $query = $dbh->prepare("select key, HEX_value from RGBW_BRIGHTNESS_HEX_VALUES order by key") || die "DBI::errstr";
$query->execute();
$query->bind_columns(\$key,\$value);
while ($query->fetch) {
$brightnessHexValues{"$key"}=$value;
}
$query->finish();
return %brightnessHexValues;
}
sub changeLightStateDB {
my $id = shift;
my $state = shift;
my $ds = get_datasource();
my $dbh = DBI->connect($ds) || die "DBI::errstr";
my $query = $dbh->prepare("update LIGHTS set STATE = '$state' where ID = '$id'") || die "DBI::errstr";
$query->execute();
$query->finish();
}
sub changeLightBrightnessDB {
my $id = shift;
my $brightness = shift;
my $ds = get_datasource();
my $dbh = DBI->connect($ds) || die "DBI::errstr";
my $query = $dbh->prepare("update LIGHTS set BRIGHTNESS = '$brightness' where ID = '$id'") || die "DBI::errstr";
$query->execute();
$query->finish();
}
sub get_datasource {
my $db_location = "/usr/local/bin/lights/lights.db";
my $ds = "DBI:SQLite:dbname=$db_location";
return $ds;
}