-
Notifications
You must be signed in to change notification settings - Fork 3
/
30_LIFXBridge.pm
74 lines (54 loc) · 1.46 KB
/
30_LIFXBridge.pm
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
package main;
use strict;
use warnings;
use POSIX;
use vars qw($FW_ME); # webname (default is fhem), needed by Color
use Color;
use JSON::PP;
use HTTP::Request;
use LWP::UserAgent;
use SetExtensions;
use Device::LIFX;
use Data::Dumper;
sub LIFXBridge_Initialize($$)
{
my ($hash) = @_;
# Provider
$hash->{ReadFn} = "LIFXBridge_Read";
$hash->{Clients} = ":LIFXBulb:";
#Consumer
$hash->{DefFn} = "LIFXBridge_Define";
$hash->{AttrList} = "key";
}
sub LIFXBridge_Define($$)
{
my ($hash, $def) = @_;
my ($name, $type, $interval) = split("[ \t]+", $def);
if (!defined($type)) {
return "Usage: define <name> LIFXBridge [gateway search interval]";
}
$hash->{ID} = "LIFXBridge";
$hash->{NAME} = $name;
$hash->{INTERVAL} = $interval || 60;
$hash->{STATE} = 'Initialized';
$hash->{lifx}{lifx} = Device::LIFX->new();;
$hash->{FD} = fileno($hash->{lifx}{lifx}->socket());
$selectlist{$name} = $hash;
InternalTimer(gettimeofday()+$hash->{INTERVAL}, "LIFXBridge_GetUpdate", $hash, 0);
return undef;
}
sub LIFXBridge_GetUpdate($)
{
my ($hash) = @_;
$hash->{lifx}{lifx}->find_gateways();
InternalTimer(gettimeofday()+$hash->{INTERVAL}, "LIFXBridge_GetUpdate", $hash, 0);
}
sub LIFXBridge_Read($)
{
my ($hash) = $_[0];
my $lifx = $hash->{lifx}{lifx};
my $msg = $lifx->get_message();
Dispatch($hash, $msg, undef);
return undef;
}
1;