-
Notifications
You must be signed in to change notification settings - Fork 3
/
module.php
352 lines (326 loc) · 10.2 KB
/
module.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
<?php
/*
* @author Ulrich Bittner
* @copyright (c) 2020, 2021
* @license CC BY-NC-SA 4.0
* @see https://github.com/ubittner/SymconNUKI
*/
/** @noinspection DuplicatedCode */
/** @noinspection PhpUnused */
declare(strict_types=1);
include_once __DIR__ . '/../libs/constants.php';
class NUKIOpener extends IPSModule
{
public function Create()
{
//Never delete this line!
parent::Create();
$this->RegisterProperties();
$this->CreateProfiles();
$this->RegisterVariables();
//Connect to NUKI bridge (Splitter)
$this->ConnectParent(NUKI_BRIDGE_GUID);
}
public function Destroy()
{
//Never delete this line!
parent::Destroy();
$this->DeleteProfiles();
}
public function ApplyChanges()
{
//Wait until IP-Symcon is started
$this->RegisterMessage(0, IPS_KERNELSTARTED);
//Never delete this line!
parent::ApplyChanges();
//Check kernel runlevel
if (IPS_GetKernelRunlevel() != KR_READY) {
return;
}
$this->GetOpenerState();
}
public function MessageSink($TimeStamp, $SenderID, $Message, $Data)
{
$this->SendDebug(__FUNCTION__, $TimeStamp . ', SenderID: ' . $SenderID . ', Message: ' . $Message . ', Data: ' . print_r($Data, true), 0);
if (!empty($Data)) {
foreach ($Data as $key => $value) {
$this->SendDebug(__FUNCTION__, 'Data[' . $key . '] = ' . json_encode($value), 0);
}
}
switch ($Message) {
case IPS_KERNELSTARTED:
$this->KernelReady();
break;
}
}
public function GetConfigurationForm()
{
$formData = json_decode(file_get_contents(__DIR__ . '/form.json'), true);
return json_encode($formData);
}
/**
* Receives data from the NUKI Bridge (splitter).
*
* @param $JSONString
* @throws Exception
*/
public function ReceiveData($JSONString)
{
$this->SendDebug(__FUNCTION__ . ' Start', 'Incomming data', 0);
$this->SendDebug(__FUNCTION__ . ' String', $JSONString, 0);
$data = json_decode(utf8_decode($JSONString));
$buffer = $data->Buffer;
$this->SendDebug(__FUNCTION__ . ' Data', json_encode($buffer), 0);
$nukiID = $buffer->nukiId;
if ($this->ReadPropertyString('OpenerUID') != $nukiID) {
$this->SendDebug(__FUNCTION__ . ' Abort', 'Data is not for this instance.', 0);
return;
}
$this->SendDebug(__FUNCTION__ . ' End', 'Data received', 0);
$this->SetOpenerState(json_encode($buffer));
}
#################### Request Action
public function RequestAction($Ident, $Value)
{
switch ($Ident) {
case 'DoorBuzzer':
$this->BuzzDoor();
break;
}
}
#################### Public
/**
* Gets the actual state of the opener.
*
* @return string
* @throws Exception
*/
public function GetOpenerState(): string
{
$nukiID = $this->ReadPropertyString('OpenerUID');
if (empty($nukiID)) {
return '';
}
if (!$this->HasActiveParent()) {
return '';
}
$data = [];
$buffer = [];
$data['DataID'] = NUKI_BRIDGE_DATA_GUID;
$buffer['Command'] = 'GetLockState';
$buffer['Params'] = ['nukiId' => (int) $nukiID, 'deviceType' => 2];
$data['Buffer'] = $buffer;
$data = json_encode($data);
$result = $this->SendDataToParent($data);
$this->SendDebug(__FUNCTION__ . ' Data', json_decode($result), 0);
if (empty($result)) {
return '';
}
$this->SetOpenerState(json_decode($result));
return json_decode($result);
}
/**
* Toggles the ring to open function of the opener.
*
* @param bool $State
* @return bool
* @throws Exception
*/
public function ToggleRingToOpen(bool $State): bool
{
//Deactivate
$lockAction = 2;
//Activate
if ($State) {
$lockAction = 1;
}
return $this->SetOpenerAction($lockAction);
}
/**
* Toggles the continuous mode of the opener.
*
* @param bool $State
* @return bool
* @throws Exception
*/
public function ToggleContinuousMode(bool $State): bool
{
//Deactivate
$lockAction = 5;
//Activate
if ($State) {
$lockAction = 4;
}
return $this->SetOpenerAction($lockAction);
}
/**
* Opens the door via buzzer.
*
* @return bool
* @throws Exception
*/
public function BuzzDoor(): bool
{
return $this->SetOpenerAction(3);
}
/**
* Set the action of the opener.
*
* @param int $Action
* @return bool
* @throws Exception
*/
public function SetOpenerAction(int $Action): bool
{
$success = false;
$nukiID = $this->ReadPropertyString('OpenerUID');
if (empty($nukiID)) {
return false;
}
if (!$this->HasActiveParent()) {
return false;
}
$data = [];
$buffer = [];
$data['DataID'] = NUKI_BRIDGE_DATA_GUID;
$buffer['Command'] = 'SetLockAction';
$buffer['Params'] = ['nukiId' => (int) $nukiID, 'lockAction' => $Action, 'deviceType' => 2];
$data['Buffer'] = $buffer;
$data = json_encode($data);
$result = json_decode(json_decode($this->SendDataToParent($data), true), true);
if (empty($result)) {
return $success;
}
if (array_key_exists('success', $result)) {
$success = $result['success'];
}
if (array_key_exists('batteryCritical', $result)) {
$this->SetValue('BatteryState', $result['batteryCritical']);
}
return $success;
}
#################### Private
private function KernelReady()
{
$this->ApplyChanges();
}
private function RegisterProperties()
{
$this->RegisterPropertyString('OpenerUID', '');
$this->RegisterPropertyString('OpenerName', '');
}
private function CreateProfiles()
{
$profile = 'NUKI.' . $this->InstanceID . '.DoorBuzzer';
if (!IPS_VariableProfileExists($profile)) {
IPS_CreateVariableProfile($profile, 1);
}
IPS_SetVariableProfileIcon($profile, '');
IPS_SetVariableProfileAssociation($profile, 0, $this->Translate('Actuate'), 'Door', 0x00FF00);
}
private function DeleteProfiles()
{
$profiles = ['DoorBuzzer'];
foreach ($profiles as $profile) {
$profileName = 'NUKI.' . $this->InstanceID . '.' . $profile;
if (@IPS_VariableProfileExists($profileName)) {
IPS_DeleteVariableProfile($profileName);
}
}
}
private function RegisterVariables()
{
//Buzzer
$profile = 'NUKI.' . $this->InstanceID . '.DoorBuzzer';
$this->RegisterVariableInteger('DoorBuzzer', $this->Translate('Door buzzer'), $profile, 10);
$this->EnableAction('DoorBuzzer');
//State
$id = @$this->GetIDForIdent('OpenerState');
$this->RegisterVariableString('OpenerState', $this->Translate('State'), '', 20);
if ($id == false) {
IPS_SetIcon($this->GetIDForIdent('OpenerState'), 'Information');
}
//Mode
$id = @$this->GetIDForIdent('OpenerMode');
$this->RegisterVariableString('OpenerMode', $this->Translate('Mode'), '', 30);
if ($id == false) {
IPS_SetIcon($this->GetIDForIdent('OpenerMode'), 'Information');
}
//Battery
$this->RegisterVariableBoolean('BatteryState', $this->Translate('Battery'), '~Battery', 40);
}
/**
* Set the state of the opener.
*
* @param string $Data
*/
private function SetOpenerState(string $Data)
{
$this->SendDebug(__FUNCTION__ . ' Data', $Data, 0);
if (empty($Data)) {
return;
}
$result = json_decode($Data, true);
if (array_key_exists('mode', $result)) {
/*
* 2 door mode, operation mode after complete setup
* 3 continuous mode, ring to open permanently active
*/
switch ($result['mode']) {
case 2:
$modeText = $this->translate('Door Mode');
break;
case 3:
$modeText = $this->translate('Continuous Mode');
break;
default:
$modeText = $this->translate('Unknown');
}
$this->SetValue('OpenerMode', $modeText);
}
if (array_key_exists('state', $result)) {
/*
* 0 untrained
* 1 online
* 2 -
* 3 rto active
* 4 -
* 5 open
* 6 -
* 7 opening
* 253 boot run
* 254 -
* 255 undefined
*/
switch ($result['state']) {
case 0:
$stateText = $this->Translate('Untrained');
break;
case 1:
$stateText = 'Online';
break;
case 3:
$stateText = $this->Translate('Ring to Open active');
break;
case 5:
$stateText = $this->Translate('Open');
break;
case 7:
$stateText = $this->Translate('Opening');
break;
case 253:
$stateText = 'Boot Run';
break;
case 255:
$stateText = $this->Translate('Undefined');
break;
default:
$stateText = $this->Translate('Unknown');
}
$this->SetValue('OpenerState', $stateText);
}
if (array_key_exists('batteryCritical', $result)) {
$this->SetValue('BatteryState', $result['batteryCritical']);
}
}
}