-
Notifications
You must be signed in to change notification settings - Fork 0
/
QAPI.php
131 lines (105 loc) · 3.44 KB
/
QAPI.php
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
<?php
class Q
{
public $ip = '127.0.0.1';
public $port = 11600;
private $bufsize = 2048; // max accepted response (in bytes)
/***** Public methods *****/
public function Q($ip='127.0.0.1', $port=11600)
{
$this->ip = $ip;
$this->port = $port;
}
public function set_color($bulbs, $red=255, $green=255, $blue=255, $bright=100)
{
// Set the color of one or more bulbs
$this->q_send_command($this->build_json_light_control($bulbs, $red, $green, $blue, $bright, 1, 0, 9));
return true;
}
/*** FIXME: This is *really* flaky :(
public function set_brightness($bulbs, $bright=100)
{
// Set the brightness of one or more bulbs without changing the color
$this->q_send_command($this->build_json_light_control($bulbs, $red=null, $green=null, $blue=null, $bright, 1, 0, 9, $brightOnly=true));
return true;
}
***/
public function set_on($bulbs, $bright=100)
{
// Set one or more bulbs to on as a standard ('white') bulb
$this->q_send_command($this->build_json_light_control($bulbs, 255, 255, 255, $bright, 1, 0, 8));
return true;
}
public function set_off($bulbs)
{
// Set one or more bulbs to off
$this->q_send_command($this->build_json_light_control($bulbs, 0, 0, 0, 0, 0, 0, 8));
return true;
}
public function list_bulbs()
{
// List the bulbs known by a Q station
$bulbs = $this->q_send_command_expect_response("{ 'cmd':'light_list' }");
return json_decode($bulbs, true)['led'];
}
public function set_bulb_title($bulb, $title='lightX')
{
$json = "{ 'cmd':'set_title', 'sn':'" . $bulb . "','title':'" . $title . "' }";
$this->q_send_command($json);
}
public function save_lights()
{
// FIXME: Unimplemented (Not sure what this does yet!)
}
public function music_sync($bulb)
{
// FIXME: It doesn't look like there's a way to *unset* this!
$json = "{ 'cmd':'set_musicled', 'sn':'" . $bulb . "' }";
$this->q_send_command($json);
}
public function music_sync_group($group)
{
// FIXME: Unimplemented (How does this differ from normal music sync?)
}
public function delete($bulb)
{
// Delete a bulb from the Q station
// FIXME: Is there a way to re-add them? (This is untested until I find that out!)
$json = "{ 'cmd':'del_device', 'sn':'" . $bulb . "' }";
$this->q_send_command($json);
}
/***** Private methods *****/
private function q_send_command($json)
{
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$len = strlen($json);
socket_sendto($sock, $json, $len, 0, $this->ip, $this->port);
socket_close($sock);
}
private function q_send_command_expect_response($json)
{
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>1, "usec"=>0));
$len = strlen($json);
$response = '';
socket_sendto($sock, $json, $len, 0, $this->ip, $this->port);
socket_recvfrom($sock, $response, $this->bufsize, 0, $this->ip, $this->port);
socket_close($sock);
return $response;
}
private function build_json_light_control($bulbs, $red, $green, $blue, $bright, $iswitch, $matchValue, $effect, $brightOnly=false)
{
if (!is_array($bulbs))
$bulbs = array($bulbs);
$json = "{ 'cmd':'light_ctrl', ";
if (!$brightOnly)
$json .= "'r':'$red', 'g':'$green', 'b':'$blue', ";
$json .= "'bright':'$bright', ";
$json .= "'sn_list': [";
foreach ($bulbs as $bulb)
$json .= "{ 'sn':'$bulb' }, ";
$json .= "], 'iswitch':'$iswitch', 'matchValue':'$matchValue', 'effect':'$effect' }";
return $json;
}
}
?>