forked from hajekmi/myjablotron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myjablotron.class.php
374 lines (323 loc) · 9.31 KB
/
myjablotron.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
/*
* Class MyJablotron - JA100
* Michal Hajek <[email protected]>
* 27.1.2018
*/
class MyJablotron {
private $curlHandle; // curl handle (keepalive)
private $curlResponse; // output from curl
private $debug = false; // enable or disable debug
private $debugFile = 'php://stdout'; // debug file
private $username; // username
private $password; // password
private $serviceId; // service ID
private $errors; // errors
public function __construct($username, $password) {
$this->username = $username;
$this->password = $password;
if( ! defined('MY_COOKIE_FILE')) {
define('MY_COOKIE_FILE', '/tmp/cookies.txt');
}
if(is_file(MY_COOKIE_FILE)) {
unlink(MY_COOKIE_FILE);
}
}
/**
* Cleanup curl and cookies file
*/
public function __destruct() {
if(is_resource($this->curlHandle)) {
curl_close($this->curlHandle);
$this->curlHandle = null;
}
if(is_file(MY_COOKIE_FILE)) {
unlink(MY_COOKIE_FILE);
}
}
/**
* Set debug to STDOUT
* @param $enable - boolean Enable or disable debug
* @return null
*/
public function debug($enable = true, $file = 'php://stdout') {
$this->debug = $enable;
$this->debugFile = $file;
}
/**
* Login and get service Id
* @return boolean
*/
public function login() {
$this->curl('https://www.jablonet.net/ajax/login.php', 'login='.urlencode($this->username).'&heslo='.urlencode($this->password).'&aStatus=200&loginType=Login');
$output = $this->curlGetResponse('output');
if($output !== false) {
$json = json_decode($output, true);
if(isset($json['status']) && $json['status'] == 200) {
$this->curl('https://www.jablonet.net/cloud', null);
$output = $this->curlGetResponse('output');
if(preg_match('~www.jablonet.net/app/ja100\?service=([0-9]+)~', $output, $m)) {
$this->serviceId = $m[1];
$this->curl('https://www.jablonet.net/app/ja100?service='.$this->serviceId, null);
return true;
}
else {
$this->errors[] = 'Not found service';
return false;
}
}
else {
// Wrong password
$this->errors[] = 'Wrong password';
return false;
}
}
else {
// Something wrong
$this->errors[] = 'Something wrong';
return false;
}
}
/**
* Send PGM signal on section
* @param $sectionName - string Section name (ex. PGM_1)
* @return boolean
*/
public function sendPGMSignal($sectionName, $pin) {
return $this->lock($sectionName, $pin);
}
/**
* Lock section (status 1)
* @param $sectionName - string Section name (ex. STATE_3)
* @param $pin - string PIN
* @return boolean
*/
public function lock($sectionName, $pin) {
$this->curl('https://www.jablonet.net/app/ja100/ajax/ovladani2.php', 'section='.urlencode($sectionName).'&status=1&code='.urlencode($pin));
$output = $this->curlGetResponse('output');
$json = json_decode($output, true);
if(isset($json['result']) && ( $json['result'] == 0 || $json['result'] == 1 )) {
return true;
}
else {
$this->errors[] = 'Unable lock';
return false;
}
}
/**
* Lock section bypass (status 4)
* @param $sectionName - string Section name (ex. STATE_3)
* @param $pin - string PIN
* @return boolean
*/
public function lockBypass($sectionName, $pin) {
$this->curl('https://www.jablonet.net/app/ja100/ajax/ovladani2.php', 'section='.urlencode($sectionName).'&status=4&code='.urlencode($pin));
$output = $this->curlGetResponse('output');
$json = json_decode($output, true);
if(isset($json['result']) && ( $json['result'] == 0 || $json['result'] == 1 )) {
return true;
}
else {
$this->errors[] = 'Unable lock';
return false;
}
}
/**
* UnLock section
* @param $sectionName - string Section name (ex. STATE_3)
* @param $pin - string PIN
* @return boolean
*/
public function unlock($sectionName, $pin) {
$this->curl('https://www.jablonet.net/app/ja100/ajax/ovladani2.php', 'section='.urlencode($sectionName).'&status=0&code='.urlencode($pin));
$output = $this->curlGetResponse('output');
$json = json_decode($output, true);
if(isset($json['result']) && ( $json['result'] == 0 || $json['result'] == 1 )) {
return true;
}
else {
$this->errors[] = 'Unable unlock';
return false;
}
}
/**
* Get Keyboard
* @return array
*/
public function getKeyboards() {
$this->curl('https://www.jablonet.net/app/ja100/ajax/stav.php', null);
$output = $this->curlGetResponse('output');
$json = json_decode($output, true);
if(isset($json['status']) && $json['status'] == 200) {
return $json['moduly'];
}
else {
$this->errors[] = 'Unable get keyboards';
return false;
}
}
/**
* Get Section
* @return array
*/
public function getSection() {
$this->curl('https://www.jablonet.net/app/ja100/ajax/stav.php', 'activeTab=section');
$output = $this->curlGetResponse('output');
$json = json_decode($output, true);
if(isset($json['status']) && $json['status'] == 200) {
return $json['sekce'];
}
else {
$this->errors[] = 'Unable get sections';
return false;
}
}
/**
* Get PG Section
* @return array
*/
public function getPGM() {
$this->curl('https://www.jablonet.net/app/ja100/ajax/stav.php', null);
$output = $this->curlGetResponse('output');
$json = json_decode($output, true);
if(isset($json['status']) && $json['status'] == 200) {
return $json['pgm'];
}
else {
$this->errors[] = 'Unable get sections';
return false;
}
}
/**
* Return lock or unlock section
* @param $sectionName - string Section Name (ex. STATE_3)
* @return boolean - true (lock), false (unlock)
*/
public function checkStatusSection($sectionName) {
$sections = $this->getKeyboards();
foreach($sections as $keyboardIds) {
foreach($keyboardIds as $a) {
if($a['stateName'] == $sectionName) {
return ($a['stav'] == 1 ? true : false);
}
}
}
}
/**
* Get all statuses
* @return array
*/
public function getAllStatuses() {
$this->curl('https://www.jablonet.net/app/ja100/ajax/stav.php', null);
$output = $this->curlGetResponse('output');
$json = json_decode($output, true);
if(isset($json['status']) && $json['status'] == 200) {
print_r($json);
}
else {
$this->errors[] = 'Unable get status';
return false;
}
}
/**
* Get History without paging
* @return array
*/
public function getHistory() {
$this->curl('https://www.jablonet.net/app/ja100/switch.php?switch=history', null);
$output = $this->curlGetResponse('output');
$output = explode("\n", $output);
$data = Array();
$i = 0;
$day = '';
foreach($output as $line) {
// Day I.
if(preg_match('~<span class="small_day">([0-9]+.*)</span>~', $line, $m)) {
$day = $m[1];
$data[$i]['day'] = $m[1];
}
// Day II.
if(preg_match('~<span class="before_day">([0-9]+.*)</span>~', $line, $m)) {
$day = $m[1];
$data[$i]['day'] = $m[1];
}
// Fill missing day
if( ! isset($data[$i]['day'])) {
$data[$i]['day'] = $day;
}
// ON | OFF
if(preg_match('~class=\'(OFF|ON|DISARM|ARM)\'~', $line, $m)) {
$data[$i]['on_off'] = $m[1];
}
// Time
if(preg_match('~<div class="time">([0-9]+:[0-9]+:?[0-9]*)~', $line, $m)) {
$data[$i]['time'] = $m[1];
}
// Who activated
if(preg_match('~<div class="name">([a-z]*)</div>~', $line, $m)) {
$data[$i]['name'] = $m[1];
}
// Name section (place)
if(preg_match('~<span class="span_place">(.*)</span>~', $line, $m)) {
$data[$i]['place'] = $m[1];
$i++;
}
}
return $data;
}
/**
* Get Errors
* @return - array Errors
*/
public function getErrors() {
return $this->errors;
}
/**
* Curl helper
* @param $url - string url address
* @param $postString - string post fields
* @return null
*/
private function curl($url, $postString) {
if( ! is_resource($this->curlHandle)) {
$this->curlHandle = curl_init();
}
if($this->debug) {
file_put_contents($this->debugFile, date('Y-m-d H:i:s')." CURL: Connecting $url\n", FILE_APPEND);
}
curl_setopt($this->curlHandle, CURLOPT_URL, $url);
curl_setopt($this->curlHandle, CURLOPT_TIMEOUT, 60);
curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curlHandle, CURLOPT_COOKIESESSION, true);
curl_setopt($this->curlHandle, CURLOPT_COOKIEJAR, MY_COOKIE_FILE);
curl_setopt($this->curlHandle, CURLOPT_COOKIEFILE, MY_COOKIE_FILE);
if(strlen($postString)>0) {
curl_setopt($this->curlHandle, CURLOPT_POST, 1);
curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, $postString);
if($this->debug) {
file_put_contents($this->debugFile, date('Y-m-d H:i:s')." CURL: Posting data $postString\n", FILE_APPEND);
}
}
$this->curlResponse['output'] = curl_exec($this->curlHandle);
$this->curlResponse['httpCode'] = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
if($this->debug) {
file_put_contents($this->debugFile, date('Y-m-d H:i:s')." CURL: HTTP CODE ".$this->curlResponse['httpCode']."\n", FILE_APPEND);
file_put_contents($this->debugFile, date('Y-m-d H:i:s')." CURL: Response ".$this->curlResponse['output']."\n", FILE_APPEND);
file_put_contents($this->debugFile, date('Y-m-d H:i:s')." ------------------------------------------------\n", FILE_APPEND);
}
}
/**
* Curl get response output and http code
* @param $data - string output or httpCode
* @return string
*/
private function curlGetResponse($data=null) {
if(is_null($data)) {
return $this->curlResponse;
}
else {
return $this->curlResponse[$data];
}
}
}
?>