-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwemo_link.class.php
357 lines (334 loc) · 12.3 KB
/
wemo_link.class.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
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
<?php
/**
* Wemo Link
*
* Originaly a bash script
* Original author: [email protected]
*
* Modified 7/13/2014 by Donald Burr
* email: <[email protected]>
* web: <http://DonaldBurr.com>
*
* Modified 05/12/2014 by Jack Lawry
* email: <[email protected]>
* web: <http://www.jacklawry.co.uk>
*
* Modified 31/05/2015 by Wagner Oliveira
* * Fixed Port parameter and added Support for WeMo Link LED Bulbs
* email: <[email protected]>
* web: <http://guino.home.insightbb.com>
*
* Ported to PHP by Matthew Burns
*/
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/**
* IP address of your wemo bridge
* This should be static
*/
define('WEMO_IP', '192.168.0.14');
/**
* This is the port that we will connect to
* It will either be 49152 or 49153
*/
define('WEMO_PORT', '49153');
/**
* Where the cache of the device is to be stored
*/
define('WEMO_CACHE', __DIR__.'/cache.json');
/**
* WemoLink
* @author Matthew Burns <mazodude>
* @package WemoLink
*/
class WemoLink
{
/**
* info
* Holds all the info about the wemo system
* @var object
*/
private $info;
/**
* Constuctor
*/
function __construct()
{
}
/**
* wemoInit
* Loads the cache or creates it
*/
public function wemoInit()
{
$this->info = $this->loadCache();
if ($this->info===false) {
$this->info = new stdClass();
$this->info->device = new stdClass();
$this->info->devices = new stdClass();
$this->info->device = $this->getSetup();
$this->info->devices = $this->getDevices();
$this->writeCache($this->info);
}
}
/**
* loadCache
* Loads the cache from the file
* @return object JSON object of wemo info
*/
private function loadCache()
{
if (file_exists(WEMO_CACHE)) {
$fp = fopen(WEMO_CACHE, 'r');
$json = fread($fp, filesize(WEMO_CACHE));
fclose($fp);
return json_decode($json);
} else {
return false;
}
}
/**
* writeCache
* Writes the cache out to a file
* @param object $object object to save to file
*/
private function writeCache($object)
{
$fp = fopen(WEMO_CACHE, 'w');
fwrite($fp, json_encode($object));
fclose($fp);
}
/**
* getSetup
* Grabs the setup file from the wemo an returns it as an object
* @return object setup info from wemo
*/
private function getSetup()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://".WEMO_IP.":".WEMO_PORT."/setup.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $this->parseXML($response);
}
/**
* getDevices
* Gets the list of devices from the wemo
* @return object object of devices from wemo
*/
private function getDevices()
{
$UDN = $this->info->device->device->UDN;
$data = '<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetEndDevices xmlns:u="urn:Belkin:service:bridge:1">
<ReqListType>SCAN_LIST</ReqListType>
<DevUDN>'.$UDN.'</DevUDN>
</u:GetEndDevices>
</s:Body>
</s:Envelope>';
$headers_array = array(
'Content-type: text/xml; charset="utf-8"',
'SOAPACTION: "urn:Belkin:service:bridge:1#GetEndDevices"',
'Accept: ',
);
$url = "http://".WEMO_IP.":".WEMO_PORT."/upnp/control/bridge1";
$response = $this->sendCurl($url,$data,$headers_array);
// Fix the xml
$response = str_replace('<', '<', $response);
$response = str_replace('>', '>', $response);
$response = str_replace('"', '"', $response);
$response = str_replace(':', '', $response);
// To be valid xml, the xml string must be at the front
$response = str_replace('<?xml version="1.0" encoding="utf-8"?>', '', $response);
$response = '<?xml version="1.0" encoding="utf-8"?>'.$response;
// return $response;
$response = $this->parseXML($response)->sBody->uGetEndDevicesResponse->DeviceLists
->DeviceLists->DeviceList->DeviceInfos;
return $response;
}
/**
* turnOn
* Turns a light on
* @param string $light Name of the light
* @param int $brightness brightness level to set
*/
public function turnOn($light,$brightness=255)
{
$lightname = $light;
$light = $this->bulbNameToID($light);
$isgroup = 'NO';
$data = '<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:SetDeviceStatus xmlns:u="urn:Belkin:service:bridge:1">
<DeviceStatusList><?xml version="1.0" encoding="UTF-8"?><DeviceStatus><DeviceID>'.$light.'</DeviceID><CapabilityID>10008</CapabilityID><CapabilityValue>'.$brightness.':0</CapabilityValue><IsGroupAction>'.$isgroup.'</IsGroupAction></DeviceStatus></DeviceStatusList>
</u:SetDeviceStatus>
</s:Body>
</s:Envelope>';
$headers_array = array(
'Content-type: text/xml; charset="utf-8"',
'SOAPACTION: "urn:Belkin:service:bridge:1#SetDeviceStatus"',
'Accept: ',
);
$url = "http://".WEMO_IP.":".WEMO_PORT."/upnp/control/bridge1";
$response = $this->sendCurl($url,$data,$headers_array);
$this->getStatus($lightname);
}
/**
* turnOff
* Turns off a light
* @param string $light Name of the light
*/
public function turnOff($light)
{
$lightname = $light;
$light = $this->bulbNameToID($light);
$isgroup = 'NO';
$data = '<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:SetDeviceStatus xmlns:u="urn:Belkin:service:bridge:1">
<DeviceStatusList><?xml version="1.0" encoding="UTF-8"?><DeviceStatus><DeviceID>'.$light.'</DeviceID><CapabilityID>10008</CapabilityID><CapabilityValue>0:0</CapabilityValue><IsGroupAction>'.$isgroup.'</IsGroupAction></DeviceStatus></DeviceStatusList>
</u:SetDeviceStatus>
</s:Body>
</s:Envelope>';
$headers_array = array(
'Content-type: text/xml; charset="utf-8"',
'SOAPACTION: "urn:Belkin:service:bridge:1#SetDeviceStatus"',
'Accept: ',
);
$url = "http://".WEMO_IP.":".WEMO_PORT."/upnp/control/bridge1";
$response = $this->sendCurl($url,$data,$headers_array);
$this->getStatus($lightname);
}
/**
* lightStatus
* Gets the light status of a light
* This will also allow you to get the real status of a light
* the second time you connect to it
* @param string $light Name of the light
* @return JSON JSON of the status
*/
public function lightStatus($light)
{
$response = $this->getStatus($light);
// Fix the xml
$response = str_replace('<', '<', $response);
$response = str_replace('>', '>', $response);
$response = str_replace('"', '"', $response);
$response = str_replace(':', '', $response);
// To be valid xml, the xml string must be at the front
$response = str_replace('<?xml version="1.0" encoding="utf-8"?>', '', $response);
$response = '<?xml version="1.0" encoding="utf-8"?>'.$response;
$response = $this->parseXML($response)->sBody->uGetDeviceStatusResponse->DeviceStatusList
->DeviceStatusList->DeviceStatus->CapabilityValue[0];
if ($response) {
$status = explode(',', $response);
$onOff = $status[0];
$brightness = substr($status[1], 0, -1);
echo json_encode(array(
'status' => $onOff,
'brightness' => $brightness
));
}
}
/**
* getStatus
* Send the curl to the wemo to get the status of the light
* @param string $light Name of the light
* @return string Response from the wemo
*/
private function getStatus($light)
{
$light = $this->bulbNameToID($light);
$data = '<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetDeviceStatus xmlns:u="urn:Belkin:service:bridge:1">
<DeviceIDs>'.$light.'</DeviceIDs>
</u:GetDeviceStatus>
</s:Body>
</s:Envelope>';
$headers_array = array(
'Content-type: text/xml; charset="utf-8"',
'SOAPACTION: "urn:Belkin:service:bridge:1#GetDeviceStatus"',
'Accept: ',
);
$url = "http://".WEMO_IP.":".WEMO_PORT."/upnp/control/bridge1";
$response = $this->sendCurl($url,$data,$headers_array);
return $response;
}
/**
* sendCurl
* Curl wrapper for sending to wemo
* @param string $url url and port to connect to
* @param array $data Array of the post to send
* @param array $headers_array Array of header to send
*/
private function sendCurl($url,$data,$headers_array)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers_array);
$response = trim(curl_exec($ch));
curl_close($ch);
return $response;
}
/**
* bulbNameToID
* Changes the bulb string to an id that the wemo understands
* @param string $light Name of the light
* @return int ID of light
*/
private function bulbNameToID($light)
{
foreach ($this->info->devices->DeviceInfo as $key => $value) {
if ($value->FriendlyName==$light) {
return $value->DeviceID;
}
}
return false;
}
/**
* parseXML
* wrapper for simplexml
* @param string $string xml from wemo
* @return object object of xml
*/
private function parseXML($string)
{
$object = simplexml_load_string($string);
return $object;
}
/**
* ping
* Pings the wemo to see if it will respond
* @return bool Returns true if up, false if down
*/
public function ping()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://".WEMO_IP.":".WEMO_PORT);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode >= 200 && $httpcode <500){
return true;
} else {
return false;
}
}
}
?>