-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWemo.class.php
More file actions
270 lines (213 loc) · 7.64 KB
/
Wemo.class.php
File metadata and controls
270 lines (213 loc) · 7.64 KB
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
<?
require_once("Debug.class.php");
require_once("WemoMeta.class.php");
/**
* Class to control a wemo device
*/
class Wemo {
private $ip;
private $port;
private $state;
private $name;
private $retryCount = 0;
private $meta;
private $debug;
const MIN_PORT = 49152;
const MAX_PORT = 49155;
const MAX_RETRIES = 10000; // try for a LOOONG time
const RETRY_SLEEP = 2;
const REQUEST_TIMEOUT = 10; // how long to wait for a response from the
// device (in seconds) before trying again
const DEVICE_FAULT = 98; // we got a fault response from the device. This
// seems like a harder error
const DEVICE_ERROR = 99; // we got an error response from the device. This
// looks to happen when a device is already in
// the requested state
/**
* Loop through some known ports that wemo uses and find out which one is
* active RIGHT NOW
*
* @return the active port numver
*/
private function findPort (){
$this->debug->log("findPort() for " . $this->ip);
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($this->meta->get("port") !== WemoMeta::KEY_NOT_SET)
$port = $this->meta->get("port");
else
$port = self::MIN_PORT - 1;
while (true){ // hotloop. Should probably add a BIG max timeout here
if ($port >= self::MAX_PORT){
$port = self::MIN_PORT;
sleep(self::RETRY_SLEEP);
}
curl_setopt($ch, CURLOPT_URL, "http://" . $this->ip . ":" . $port);
$res = curl_exec($ch);
if ($res){
$this->debug->log("found port " . $port);
$this->meta->set("port", $port);
return $port;
}
$port++;
}
return false;
}
private function constructRequest (
$action,
$body
){
$data = '<?xml version="1.0" encoding="utf-8"?>'
. '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
. '<s:Body><u:' . $action . ' xmlns:u="urn:Belkin:service:basicevent:1">' . $body . '</u:' . $action . '></s:Body></s:Envelope>';
return $data;
}
private function getGeneric (
$action
){
$this->debug->log("getGeneric(" . $action . ")");
if (strtoupper(substr($action, 0, 3)) == "GET"){
$tag = substr($action,3);
} else {
$tag = $action;
$action = "Get" . $action;
}
$res = $this->sendRequest($action);
$ret = self::parseResponse($res, $tag);
return $ret;
}
/**
* Turn the switch on or off
* @param on 1 for on, 0 for off
* @return true if the state in the response from the wemo matches the
* requested state (i.e. the switch worked), false otherwise
*/
public function setBinaryState (
$on
){
$this->debug->log ("setBinaryState(" . $on . ")");
$res = $this->sendRequest("SetBinaryState", "<BinaryState>" . $on . "</BinaryState>");
$parsedResponse = self::parseResponse($res, "BinaryState");
$this->debug->log("got response: " . $parsedResponse);
$ret = ($parsedResponse == $on);
if ($ret == true || $ret == self::DEVICE_ERROR)
$this->meta->set("state", $on);
return $ret;
}
/**
* Make a SOAP request to this wemo device
*
* This method will keep trying until it receives a valid SOAP response (up
* to self::MAX_RETRIES times)
*
* @param action The action to send. If body is not specified, this will
* also be used to construct the body
* @param body The SOAP body to send
*
* @return The SOAP response from the device, or boolean false on error
*/
private function sendRequest (
$action,
$body = null
){
$this->debug->log("sendRequest()");
$this->port = $this->findPort();
$header = "SOAPACTION: \"urn:Belkin:service:basicevent:1#" . $action . "\"";
if ($body === null)
$body = "<" . $action . ">0</" . $action. ">";
$data = $this->constructRequest($action, $body);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_USERAGENT, "");
curl_setopt($ch, CURLOPT_TIMEOUT, self::REQUEST_TIMEOUT);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, "http://" . $this->ip . ":" . $this->port . "/upnp/control/basicevent1");
curl_setopt($ch, CURLOPT_HTTPHEADER, array (
"Accept: ",
"Content-type: text/xml; charset=\"utf-8\"",
$header
));
$this->debug->log("Sending: " . $data);
$ret = curl_exec($ch);
$this->debug->log("Received: " . $ret);
if ($ret === false && $this->retries++ < self::MAX_RETRIES){
sleep(self::RETRY_SLEEP);
return $this->sendRequest($action, $body);
}
return $ret;
}
/**
* Strip out the contents of with $tag
*
* @param xml The XML response
* @param tag The XML tag to return the contents of
*/
private static function parseResponse (
$xml,
$tag
){
if (strpos($xml, "<faultstring>") !== FALSE)
return self::DEVICE_FAULT;
preg_match("/<" . $tag . ">(.*)<\/" . $tag . ">/", $xml, $matches);
$ret = false;
if ($matches[1])
$ret = (string)$matches[1];
if ($ret == "Error")
$ret = self::DEVICE_ERROR;
return $ret;
}
public function __construct (
$ip,
$debug = false,
WemoMeta &$meta
){
$this->debug = new Debug($debug);
$this->meta = $meta;
$this->debug->log("meta data is:");
$this->debug->log($this->meta);
$this->ip = $ip;
}
public function on (){
return $this->setBinaryState(1);
}
public function off (){
return $this->setBinaryState(0);
}
public function getSignalStrength (){
$signalStrength = $this->getGeneric("GetSignalStrength");
$this->meta->set("signalStrength", $signalStrength);
return $signalStrength;
}
public function getFriendlyName (){
$friendlyName = $this->getGeneric("GetFriendlyName");
$this->meta->set("friendlyName", $friendlyName);
return $friendlyName;
}
/**
* A wrapper for getBinaryState, but returns human-readable state (on or off)
* @return The response value from the Device converted to human-readable format (on or off)
*/
public function getState (){
$this->debug->log("getState()");
$res = $this->getBinaryState();
if ($res == "1")
return "on";
else
return "off";
}
/**
* Get the binary state of the wemo (on or off)
* @return The response value from the Device. This is: 0 if the device is off, 1 if the device is on
*/
public function getBinaryState (){
$this->debug->log("getBinaryState()");
$state = $this->getGeneric("GetBinaryState");
$this->meta->set("state", $state);
$this->debug->log(array("switched state to:", $state));
return $state;
}
}
?>