|
1
|
|
|
<?php |
|
2
|
|
|
namespace Route4Me; |
|
3
|
|
|
|
|
4
|
|
|
use Route4Me\Common; |
|
5
|
|
|
/** |
|
6
|
|
|
* class MyQ |
|
7
|
|
|
* |
|
8
|
|
|
* Offers authentication to MyQ API, and access to garage door open/close/status functions |
|
9
|
|
|
* |
|
10
|
|
|
*/ |
|
11
|
|
|
class MyQException extends \Exception {} |
|
12
|
|
|
class MyQ { |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** @var string|null $username contains the username used to authenticate with the MyQ API */ |
|
15
|
|
|
protected $username = null; |
|
16
|
|
|
/** @var string|null $password contains the password used to authenticate with the MyQ API */ |
|
17
|
|
|
protected $password = null; |
|
18
|
|
|
/** @var string|null $appId is the application ID used to register with the MyQ API */ |
|
19
|
|
|
protected $appId = 'NWknvuBd7LoFHfXmKNMBcgajXtZEgKUh4V7WNzMidrpUUluDpVYVZx+xT4PCM5Kx'; |
|
20
|
|
|
//protected $appId = 'Vj8pQggXLhLy0WHahglCD4N1nAkkXQtGYpq2HrHD7H1nvmbT55KqtN6RSF4ILB%2fi'; |
|
|
|
|
|
|
21
|
|
|
/** @var string|null $securityToken is the auth token returned after a successful login */ |
|
22
|
|
|
protected $securityToken = null; |
|
23
|
|
|
/** @var string|null $userAgent is the User-Agent header value sent with each API request */ |
|
24
|
|
|
protected $userAgent = 'Chamberlain/3.4.1'; |
|
25
|
|
|
/** @var string|null $culture is the API culture code for the API */ |
|
26
|
|
|
protected $culture = 'en'; |
|
27
|
|
|
/** @var string|null $contentType is the content type used for all cURL requests */ |
|
28
|
|
|
protected $contentType = 'application/json'; |
|
29
|
|
|
/** @var array $headers contain HTTP headers for cURL requests */ |
|
30
|
|
|
protected $_headers = array(); |
|
31
|
|
|
protected $_deviceId = null; |
|
32
|
|
|
protected $_locationName = null; |
|
33
|
|
|
protected $_doorName = null; |
|
34
|
|
|
protected $_loginUrl = 'https://myqexternal.myqdevice.com/api/v4/User/Validate'; |
|
35
|
|
|
protected $_getDeviceDetailUrl = 'https://myqexternal.myqdevice.com/api/v4/userdevicedetails/get?&filterOn=true'; |
|
36
|
|
|
protected $_putDeviceStateUrl = '/api/v4/DeviceAttribute/PutDeviceAttribute'; |
|
37
|
|
|
/** @var resource|null $_conn is the web connection to the MyQ API */ |
|
38
|
|
|
protected $_conn = null; |
|
39
|
|
|
/** |
|
40
|
|
|
* Initializes class. Optionally allows user to override variables |
|
41
|
|
|
* |
|
42
|
|
|
* @param array $params A associative array for overwriting class variables |
|
43
|
|
|
* |
|
44
|
|
|
* @return MyQ |
|
|
|
|
|
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct ($params = array()) { |
|
47
|
|
|
// Overwrite class variables |
|
48
|
|
|
foreach ($params as $k => $v) { |
|
49
|
|
|
$this->$k = $v; |
|
50
|
|
|
} |
|
51
|
|
|
// Initialize cURL request headers |
|
52
|
|
|
if (sizeof($this->_headers) == 0) { |
|
53
|
|
|
$this->_headers = array ( |
|
54
|
|
|
'MyQApplicationId' => $this->appId, |
|
55
|
|
|
'Culture' => $this->culture, |
|
56
|
|
|
'Content-Type' => $this->contentType, |
|
57
|
|
|
'User-Agent' => $this->userAgent, |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
// Initialize cURL connection |
|
61
|
|
|
$this->_init(); |
|
62
|
|
|
return $this; |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
/** |
|
65
|
|
|
* Perform a login request |
|
66
|
|
|
* |
|
67
|
|
|
* @param string|null $username Username to use when logging in |
|
68
|
|
|
* @param string|null $password Password to use for logging in |
|
69
|
|
|
* |
|
70
|
|
|
* @return MyQ |
|
71
|
|
|
*/ |
|
72
|
|
|
public function login ($username = null, $password = null) { |
|
73
|
|
|
// Set username/password if not null |
|
74
|
|
|
if (!is_null($username)) { |
|
75
|
|
|
$this->username = $username; |
|
76
|
|
|
} |
|
77
|
|
|
if (!is_null($password)) { |
|
78
|
|
|
$this->password = $password; |
|
79
|
|
|
} |
|
80
|
|
|
// confirm that we have a valid username/password |
|
81
|
|
|
$error = array(); |
|
82
|
|
|
if (is_null($this->username)) { |
|
83
|
|
|
$error[] = 'username'; |
|
84
|
|
|
} |
|
85
|
|
|
if (is_null($this->password)) { |
|
86
|
|
|
$error[] = 'password'; |
|
87
|
|
|
} |
|
88
|
|
|
if (sizeof($error) > 0) { |
|
89
|
|
|
throw new MyQException('Missing required auth credential: ' . implode(',', $error)); |
|
90
|
|
|
} |
|
91
|
|
|
$this->_login(); |
|
92
|
|
|
} |
|
93
|
|
|
public function getState () { |
|
94
|
|
|
$this->_getDetails(); |
|
95
|
|
|
$timeInState = time() - $this->_doorStateTime; |
|
|
|
|
|
|
96
|
|
|
echo implode(',', array ( |
|
97
|
|
|
$this->_locationName, |
|
98
|
|
|
$this->_doorName, |
|
99
|
|
|
$this->_doorState, |
|
|
|
|
|
|
100
|
|
|
(int)$timeInState, |
|
101
|
|
|
)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getDetails() { |
|
105
|
|
|
return $this->_getDetails(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private function _init () { |
|
109
|
|
|
if (!isset($this->_conn)) { |
|
110
|
|
|
$this->_conn = curl_init(); |
|
111
|
|
|
curl_setopt_array($this->_conn, array ( |
|
112
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
113
|
|
|
CURLOPT_ENCODING => "", |
|
114
|
|
|
CURLOPT_MAXREDIRS => 10, |
|
115
|
|
|
CURLOPT_TIMEOUT => 30, |
|
116
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
|
117
|
|
|
CURLOPT_FAILONERROR => true, |
|
118
|
|
|
CURLOPT_FOLLOWLOCATION => true, |
|
119
|
|
|
CURLOPT_FRESH_CONNECT => true, |
|
120
|
|
|
CURLOPT_FORBID_REUSE => true, |
|
121
|
|
|
CURLOPT_USERAGENT => $this->userAgent, |
|
122
|
|
|
)); |
|
123
|
|
|
} |
|
124
|
|
|
$this->_setHeaders(); |
|
125
|
|
|
} |
|
126
|
|
|
private function _setHeaders () { |
|
127
|
|
|
$headers = array(); |
|
128
|
|
|
foreach ($this->_headers as $k => $v) { |
|
129
|
|
|
$headers[] = "$k: $v"; |
|
130
|
|
|
} |
|
131
|
|
|
curl_setopt($this->_conn, CURLOPT_HTTPHEADER, $headers); |
|
132
|
|
|
} |
|
133
|
|
|
private function _login () { |
|
134
|
|
|
$this->_init(); |
|
135
|
|
|
curl_setopt($this->_conn, CURLOPT_CUSTOMREQUEST, 'POST'); |
|
136
|
|
|
curl_setopt($this->_conn, CURLOPT_URL, $this->_loginUrl); |
|
137
|
|
|
$post = json_encode(array('username' => $this->username, 'password' => $this->password)); |
|
138
|
|
|
curl_setopt($this->_conn, CURLOPT_POSTFIELDS, $post); |
|
139
|
|
|
$output = curl_exec($this->_conn); |
|
140
|
|
|
$data = json_decode($output); |
|
141
|
|
|
if ($data == false || !isset($data->SecurityToken)) { |
|
142
|
|
|
throw new MyQException("Error processing login request: $output"); |
|
143
|
|
|
} |
|
144
|
|
|
$this->_headers['SecurityToken'] = $data->SecurityToken; |
|
145
|
|
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
private function _getDetails () { |
|
148
|
|
|
$this->_init(); |
|
149
|
|
|
curl_setopt($this->_conn, CURLOPT_CUSTOMREQUEST, 'GET'); |
|
150
|
|
|
curl_setopt($this->_conn, CURLOPT_URL, $this->_getDeviceDetailUrl); |
|
151
|
|
|
$output = curl_exec($this->_conn); |
|
152
|
|
|
$data = json_decode($output); |
|
153
|
|
|
if ($data == false || !isset($data->Devices)) { |
|
154
|
|
|
throw new MyQException("Error fetching device details: $output"); |
|
155
|
|
|
} |
|
156
|
|
|
// Find our door device ID |
|
157
|
|
|
foreach ($data->Devices as $device) { |
|
158
|
|
|
if (stripos($device->MyQDeviceTypeName, "Gateway") !== false) { |
|
159
|
|
|
// Find location name |
|
160
|
|
|
foreach ($device->Attributes as $attr) { |
|
161
|
|
|
if ($attr->AttributeDisplayName == 'desc') { |
|
162
|
|
|
$this->_locationName = $attr->Value; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$this->_deviceId = $device->MyQDeviceId; |
|
168
|
|
|
foreach ($device->Attributes as $attr) { |
|
169
|
|
|
switch ($attr->AttributeDisplayName) { |
|
170
|
|
|
case 'desc': |
|
171
|
|
|
$this->_doorName = $attr->Value; |
|
172
|
|
|
break; |
|
173
|
|
|
case 'doorstate': |
|
174
|
|
|
$this->_doorState = $attr->Value; |
|
|
|
|
|
|
175
|
|
|
// UpdatedTime is a timestamp in ms, so we truncate |
|
176
|
|
|
$this->_doorStateTime = (int)$attr->UpdatedTime / 1000; |
|
177
|
|
|
break; |
|
178
|
|
|
default: |
|
179
|
|
|
continue; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
} |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.