forked from MiniMeOSc/phpTradfri
-
Notifications
You must be signed in to change notification settings - Fork 1
/
general.php
77 lines (46 loc) · 1.68 KB
/
general.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
<?php
require_once('defines.php');
class tradfri {
private $gateway;
function __construct($user, $secret, $gwip){
$this->gateway['user'] = $user;
$this->gateway['secretkey'] = $secret;
$this->gateway['ip'] = $gwip;
}
function getGatewayData(){
return $this->gateway;
}
function query($path){
$cmd = "coap-client -m get -u '{$this->gateway['user']}' -k '{$this->gateway['secretkey']}' 'coaps://{$this->gateway['ip']}:5684/{$path}'";
$process = proc_open($cmd, [STDOUT => ['pipe', 'w'], STDERR => ['pipe', 'w']], $output);
//read the outputs
$stdout = stream_get_contents($output[STDOUT]);
$stderr = stream_get_contents($output[STDERR]);
//clean up and properly close our handles
fclose($output[STDOUT]);
fclose($output[STDERR]);
$rc = proc_close($process);
//$result = json_decode(strstr($stdout,'{"'), true);
$result = json_encode($stdout, true);
return $stdout;
//return $result;
}
function action($method, $payload, $path){
if($payload != "")
$cmd = "coap-client -m {$method} -u '{$this->gateway['user']}' -k '{$this->gateway['secretkey']}' -e '{$payload}' 'coaps://{$this->gateway['ip']}:5684/{$path}'";
else
$cmd = "coap-client -m {$method} -u '{$this->gateway['user']}' -k '{$this->gateway['secretkey']}' 'coaps://{$this->gateway['ip']}:5684/{$path}'";
return exec($cmd);
}
function getDeviceIds(){
return explode(",", trim(str_replace(['[',']'], "" ,strstr($this->query("15001"), '[65'))));
}
function getDetails($path){
return json_decode(strstr($this->query($path), '{"'), true);
}
function getName($path){
$details = $this->getDetails($path);
return trim($details[NAME]);
}
} //End of Class tradfri
?>