forked from MiniMeOSc/phpTradfri
-
Notifications
You must be signed in to change notification settings - Fork 1
/
devices.php
215 lines (143 loc) · 5.47 KB
/
devices.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
<?php
require_once('general.php');
class tradfridevices extends tradfri
{
function getDimmer($Id){
$dimid = $this->getDetails("15001/$Id");
return $dimid['3311']['0'][DIMMER];
}
function getIds(){
return explode(",", trim(str_replace(['[',']'], "" ,strstr($this->query("15001"), '[65'))));
}
function getIdbyName($name){
$Ids = $this->getIds() ?? [];
foreach($Ids as $Id){
$Idname = strtolower(trim($this->getName("15001/$Id")));
$Idname = str_replace(['ä','ö','ü','ß'], ['ae','oe','ue','ss'], $Idname);
if(strcasecmp(trim($name), $Idname) == 0)
$output = $Id;
}
if(isset($output))
return $output;
else
return NULL;
}
function getTypeId($Id){
$tid = $this->getDetails("15001/$Id");
return $tid[TYPE];
}
function getPowerStatus($Id){
$psid = $this->getDetails("15001/$Id");
switch ($psid[TYPE]) {
case TYPE_LIGHT:
return $psid['3311']['0'][ONOFF];
break;
case TYPE_CONTROL_OUTLET:
return $psid['3312']['0'][ONOFF];
break;
default:
return NULL;
break;
}
}
function poweroff($path){
switch($this->getTypeId($path)){
case TYPE_LIGHT:
$payload = '{ "3311": [{ "5850": 0 }] }';
break;
case TYPE_CONTROL_OUTLET:
$payload = '{ "3312": [{ "5850": 0 }] }';
break;
default:
$payload = NULL;
}
if (!is_null($payload)){
$this->action("put", $payload, "15001/$path");
if($this->getPowerStatus($path) == 0)
return $this->getName("15001/$path")." wurde ausgeschaltet";
else
return $this->getName("15001/$path")." konnte nicht ausgeschaltet werden";
}
else
return $this->getName("15001/$device")." konnte nicht ausgeschaltet werden, da es keine Lampe ist";
}
function poweron($path){
switch($this->getTypeId($path)){
case TYPE_LIGHT:
$payload = '{ "3311": [{ "5850": 1 }] }';
break;
case TYPE_CONTROL_OUTLET:
$payload = '{ "3312": [{ "5850": 1 }] }';
break;
default:
$payload = NULL;
}
if (!is_null($payload)){
$this->action("put", $payload, "15001/$path");
if($this->getPowerStatus($path) == 1)
return $this->getName("15001/$path")." wurde eingeschaltet";
else
return $this->getName("15001/$path")." konnte nicht eingeschaltet werden";
}
else
return $this->getName("15001/$device")." konnte nicht eingeschaltet werden, da es keine Lampe ist";
}
function setDimmer($path, $dimmer, $transition = NULL){
if($this->getTypeId($path) == TYPE_LIGHT){
$dim = round(254 * (int)str_replace("%", "", trim($dimmer)) / 100, 0);
$payload = is_null($transition) ? '{ "3311": [{ "5851" : '.$dim.' }] }' : '{ "3311": [{ "5851": '.$dim.', "5712": '.$transition.' }] }';
$this->action("put", $payload, "15001/$path");
if($this->getDimmer($path) == $dim)
return $this->getName("15001/$path")." wurde auf {$dimmer} gedimmt";
else
return $this->getName("15001/$path")." konnte nicht auf {$dimmer} gedimmt werden";
}
else
return $this->getName("15001/$device")." konnte nicht gedimmt werden, da es keine Lampe ist";
}
function statuscontroloutlet(){
$Ids = $this->getIds();
sort($Ids, SORT_NUMERIC);
foreach($Ids as $device){
$details = $this->getDetails("15001/$device");
if($details[TYPE] == TYPE_CONTROL_OUTLET){
$output[] = array("id" => $device,"name" => $details[NAME], "type" => $details['3']['1'], "firmware" => $details['3']['3'], "lastseen" => date('H:i:s d.m.Y', $details[LAST_SEEN]), "lastseenunix" => $details[LAST_SEEN], "power" => $details['3312']['0'][ONOFF]);
}
}
return $output;
}
function statusremotecontrol(){
$Ids = $this->getIds();
sort($Ids, SORT_NUMERIC);
foreach($Ids as $device){
$details = $this->getDetails("15001/$device");
if($details[TYPE] == TYPE_REMOTE_CONTROL || $details[TYPE] == TYPE_MOTION_SENSOR || $details[TYPE] == TYPE_SYMFONISK_CONTROLLER){
$output[] = array("id" => $device,"name" => $details[NAME], "type" => $details['3']['1'], "battery" => $details['3']['9'], "firmware" => $details['3']['3'], "lastseen" => date('H:i:s d.m.Y', $details[LAST_SEEN]), "lastseenunix" => $details[LAST_SEEN]);
}
}
return $output;
}
function statuslamps(){
$Ids = $this->getIds();
sort($Ids, SORT_NUMERIC);
foreach($Ids as $device){
$details = $this->getDetails("15001/$device");
if($details[TYPE] == TYPE_LIGHT){
$colorhex = isset($details[LIGHT]['0'][COLORHEX]) ? $details[LIGHT]['0'][COLORHEX] : NULL;
$colorx = isset($details[LIGHT]['0'][COLORX]) ? $details[LIGHT]['0'][COLORX] : NULL;
$colory = isset($details[LIGHT]['0'][COLORY]) ? $details[LIGHT]['0'][COLORY] : NULL;
$colortemp = isset($details[LIGHT]['0'][COLORTEMP]) ? $details[LIGHT]['0'][COLORTEMP] : NULL;
$transition = isset($details[LIGHT]['0'][TRANSITION]) ? $details[LIGHT]['0'][TRANSITION] : NULL;
//Set Dimmer to the INT Value in %
$details[LIGHT]['0'][DIMMER] = round($details[LIGHT]['0'][DIMMER] * 100 / 255);
$output[] = array("id" => $device,"name" => $details[NAME], "type" => $details['3']['1'], "power" => $details[LIGHT]['0'][ONOFF], "dimmer" => $details[LIGHT]['0'][DIMMER], "colorhex" => $colorhex, "colorx" => $colorx, "colory" => $colory, "colortemp" => $colortemp, "transition" => $transition, "firmware" => $details['3']['3'], "lastseen" => date('H:i:s d.m.Y', $details[LAST_SEEN]), "lastseenunix" => $details[LAST_SEEN]);
//Clean up
unset($colorhex, $colorx, $colory, $colortemp, $transition);
}
}
return $output;
//Clean up
unset($Ids, $device, $output);
}
}
?>