-
Notifications
You must be signed in to change notification settings - Fork 0
/
System.Functions.ips.php
839 lines (797 loc) · 28.2 KB
/
System.Functions.ips.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
<?php
declare(strict_types=1);
################################################################################
# Script: System.Functions.ips.php
# Version: 4.0.20231005
# Author: Heiko Wilknitz (@Pitti)
#
# Basisfunktionen für einfache Scripterstellung!
#
# ------------------------------ Changelog -------------------------------------
#
# 09.05.2019 - Initalversion (v1.0)
# 01.08.2022 - CreateIdent hinzugefügt (v2.0)
# 21.01.2023 - Definition der WWX-Konstante (v2.1)
# 15.03.2023 - RegisterArchive, UnregisterArchive, UnregisterProfil,
# ExtractGuid, CreateEventByNameFromTo
# CreateIdent um Option erweitert
# CreateProfilInteger korrigiert (ACHTUNG)
# Dokumentation umgestellt (v3.0)
# 05.10.2023 - GetObjectByIdent und GetObjectByName hinzugefügt
# GetCategoryByName, GetEventByName, GetScriptByName
# GetDummyByName, GetDummyByIdent
# GetPopupByName, GetPopupByIdent,
# GetVariableByName und GetVariableByName hinzugefügt (v4.0)
# 04.03.2024 - Kleine Anpassungen für Events (v4.1)
#
################################################################################
// Globale Definition der Bibliothek
define('WWX_FUNCTIONS', true);
/**
* Liefert die GUID für den übergebene Modul bzw. Aktion
*
* @param string $name Name des IPS Moduls oder Automation
*
* @return string GUID, e.g. '{43192F0B-135B-4CE7-A0A7-1475603F3060}'
*/
function ExtractGuid($name)
{
// Modules
$guids = IPS_GetModuleList();
$result = false;
foreach ($guids as $guid) {
$module = IPS_GetModule($guid);
if ($module['ModuleName'] == $name) {
return $guid;
}
}
// Actions/Automations
$actions = json_decode(IPS_GetActions(), true);
$matching = [];
foreach ($actions as $action) {
if ($action['caption'] == $name && count($action['form']) == 0) {
$matching[] = $action;
}
}
if (count($matching) == 1) {
return $matching[0]['id'];
}
throw new Exception('GUID does not exist for ' . $name);
}
/**
* Erzeugt aus dem übergebenen Namen ein IPS konformen IDENT
*
* @param string $name Name für den Variablen-Ident
* @param bool $lower Wenn true, dann nur nur kleingeschriebene Idents.
*
* @return string Ident
*/
function CreateIdent($name, $lower = false)
{
$umlaute = ['/ä/', '/ö/', '/ü/', '/Ä/', '/Ö/', '/Ü/', '/ß/'];
$replace = ['ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue', 'ss'];
$ident = preg_replace($umlaute, $replace, $name);
// idents immer klein?!?
if ($lower) {
$ident = strtolower($ident);
}
return preg_replace('/[^a-z0-9_]+/i', '', $ident);
}
/**
* Liefert die ID eines Objektes unterhalb {id} mit dem Ident {ident}.
*
* @param int $id ID unter welchem das Objekt gesucht werden soll.
* @param string $ident Ident des zu suchenden Objektes
* @param bool $internal Nutzung der internen Ident-Funktion
*
* @return int|false ID des gefundenen Objektes, andernfalls false.
*/
function GetObjectByIdent($id, $ident, $internal = true)
{
if ($internal) {
$ident = CreateIdent($ident);
}
return @IPS_GetObjectIDByIdent($ident, $id);
}
/**
* Liefert die ID eines Objektes unterhalb {id} mit dem Namen {name}.
*
* @param int $id ID unter welchem das Objekt gesucht werden soll.
* @param string $name Name des zu suchenden Objektes
*
* @return int|false ID des gefundenen Objektes, andernfalls false.
*/
function GetObjectByName($id, $name)
{
return @IPS_GetObjectIDByName($name, $id);
}
/**
* Erzeugt eine Kategorie unterhalb {id} mit dem Namen {name}
* Existiert die Kategorie schon wird diese zurückgeliefert.
*
* @param int $id ID unter welchem die Kategorie erzeugt werden soll.
* @param string $name Name der zu erzeugenden Kategorie
* @param int $pos 0 gleich Standard, sonst neue Sortierreihenfolgenummer {pos}
* @param string $icon Zuweisung eines Icons mit dem übergebenen Namen
*
* @return int ID der bestehenden oder neu angelegten Kategorie
*/
function CreateCategoryByName($id, $name, $pos = 0, $icon = '')
{
$cid = GetCategoryByName($id, $name);
if ($cid === false) {
$cid = IPS_CreateCategory();
IPS_SetName($cid, $name);
IPS_SetParent($cid, $id);
IPS_SetPosition($cid, $pos);
IPS_SetIcon($cid, $icon);
}
return $cid;
}
/**
* Liefert die ID eine Kategorie unterhalb {id} mit dem Namen {name}.
*
* @param int $id ID unter welchem die Kategorie erzeugt werden soll.
* @param string $name Name der zu erzeugenden Kategorie
*
* @return int|false ID der gefundenen Kategorie, andernfalls false.
*/
function GetCategoryByName($id, $name)
{
return @IPS_GetCategoryIDByName($name, $id);
}
/**
* Erzeugt ein Dummy Modul unterhalb {id} mit dem Namen {name}
* Existiert das Modul schon wird diese zurückgeliefert.
*
* @param int $id ID unter welchem das Dummy Modulserzeugt werden soll.
* @param string $name Name des zu erzeugenden Dummy Moduls
* @param int $pos 0 gleich Standard, sonst neue Sortierreihenfolgenummer {pos}
* @param string $icon Zuweisung eines Icons mit dem übergebenen Namen
*
* @return int ID des bestehenden oder neu angelegten Dummy Moduls
*/
function CreateDummyByName($id, $name, $pos = 0, $icon = '')
{
return CreateDummyByIdent($id, $name, $name, $pos, $icon);
}
/**
* Liefert die ID eines Dummy Moduls unterhalb {id} mit dem Namen {name}.
*
* @param int $id ID unter welchem das Dummy Moduls erzeugt wurde.
* @param string $name Name des zu suchenden Dummy Moduls
*
* @return int|false ID des gefundenen Dummy Moduls, andernfalls false.
*/
function GetDummyByName($id, $name)
{
return GetObjectByName($id, $name);
}
/**
* Erzeugt ein Dummy Modul unterhalb {id} mit dem Namen {name} und Ident {ident}
* Existiert das Modul schon wird diese zurückgeliefert.
*
* @param int $id ID unter welchem das Dummy Moduls erzeugt werden soll.
* @param string $ident Ident des zu erzeugenden Dummy Moduls
* @param string $name Name des zu erzeugenden Dummy Moduls
* @param int $pos 0 gleich Standard, sonst neue Sortierreihenfolgenummer {pos}
* @param string $icon Zuweisung eines Icons mit dem übergebenen Namen
*
* @return int ID des bestehenden oder neu angelegten Dummy Moduls
*/
function CreateDummyByIdent($id, $ident, $name, $pos = 0, $icon = '')
{
$ident = CreateIdent($ident);
$did = GetObjectByIdent($id, $ident);
if ($did === false) {
$did = IPS_CreateInstance(ExtractGuid('Dummy Module'));
IPS_SetName($did, $name);
IPS_SetIdent($did, $ident);
IPS_SetParent($did, $id);
IPS_SetPosition($did, $pos);
IPS_SetIcon($did, $icon);
}
return $did;
}
/**
* Liefert die ID eines Dummy Moduls unterhalb {id} mit dem Ident {ident}.
*
* @param int $id ID unter welchem das Dummy Moduls erzeugt wurde.
* @param string $ident Ident des zu suchenden Dummy Moduls
*
* @return int|false ID des gefundenen Dummy Moduls, andernfalls false.
*/
function GetDummyByIdent($id, $ident)
{
return GetObjectByIdent($id, $ident);
}
/**
* Erzeugt ein Popup Modul unterhalb {id} mit dem Namen {name}
* Existiert das Modul schon wird diese zurückgeliefert.
*
* @param int $id ID unter welchem das Popup Moduls erzeugt werden soll.
* @param string $name Name des zu erzeugenden Popup Moduls
* @param int $pos 0 gleich Standard, sonst neue Sortierreihenfolgenummer {pos}
* @param string $icon Zuweisung eines Icons mit dem übergebenen Namen
*
* @return int ID des bestehenden oder neu angelegten Popup Moduls
*/
function CreatePopupByName($id, $name, $pos = 0, $icon = '')
{
return CreatePopupByName($id, $name, $name, $pos, $icon);
}
/**
* Liefert die ID eines Popup Moduls unterhalb {id} mit dem Namen {name}.
*
* @param int $id ID unter welchem das Popup Modul erzeugt wurde.
* @param string $name Name des zu suchenden Popup Moduls
*
* @return int|false ID des gefundenen Popup Moduls, andernfalls false.
*/
function GetPopupByName($id, $name)
{
return GetObjectByName($id, $name);
}
/**
* Erzeugt ein Popup Modul unterhalb {id} mit dem Namen {name} und Ident {ident}
* Existiert das Modul schon wird diese zurückgeliefert.
*
* @param int $id ID unter welchem das Popup Moduls erzeugt werden soll.
* @param string $ident Ident des zu erzeugenden Popup Moduls
* @param string $name Name des zu erzeugenden Popup Moduls
* @param int $pos 0 gleich Standard, sonst neue Sortierreihenfolgenummer {pos}
* @param string $icon Zuweisung eines Icons mit dem übergebenen Namen
*
* @return int ID des bestehenden oder neu angelegten Popup Moduls
*/
function CreatePopupByIdent($id, $ident, $name, $pos = 0, $icon = '')
{
$ident = CreateIdent($ident);
$pid = GetObjectByIdent($id, $ident);
if ($pid === false) {
$pid = IPS_CreateInstance(ExtractGuid('Popup Module'));
IPS_SetName($pid, $name);
IPS_SetIdent($pid, $ident);
IPS_SetParent($pid, $id);
IPS_SetPosition($pid, $pos);
IPS_SetIcon($pid, $icon);
}
return $pid;
}
/**
* Liefert die ID eines Popup Moduls unterhalb {id} mit dem Ident {ident}.
*
* @param int $id ID unter welchem das Popup Modul erzeugt wurde.
* @param string $ident Ident des zu suchenden Popup Moduls
*
* @return int|false ID des gefundenen Popup Moduls, andernfalls false.
*/
function GetPopupByIdent($id, $ident)
{
return GetObjectByIdent($id, $ident);
}
/**
* Erzeugt eine Variable unterhalb {id} mit dem Namen {name} vom Typ {type}
* Existiert die Variable schon wird diese zurückgeliefert.
*
* @param int $id ID unter welchem die Variable erzeugt werden soll.
* @param string $name Name der zu erzeugenden Variable
* @param int $type Typ der Variable (0 = Boolean, 1 = Integer, 2 = Float, 3 = String)
* @param int $pos 0 gleich Standard, sonst neue Sortierreihenfolgenummer {pos}
* @param string $icon Zuweisung eines Icons mit dem übergebenen Namen
* @param string $profil Leer für kein Profil, sonst Profilnamen
* @param int $action ID der benutzerdefinierten Aktion (Auslösung via Webfront)
*
* @return int ID der bestehenden oder neu angelegten Variable
*/
function CreateVariableByName($id, $name, $type, $pos = 0, $icon = '', $profile = '', $action = null)
{
$vid = GetVariableByName($id, $name);
if ($vid === false) {
$vid = IPS_CreateVariable($type);
IPS_SetParent($vid, $id);
IPS_SetName($vid, $name);
IPS_SetPosition($vid, $pos);
IPS_SetIcon($vid, $icon);
if ($profile !== '') {
IPS_SetVariableCustomProfile($vid, $profile);
}
if ($action != null) {
IPS_SetVariableCustomAction($vid, $action);
}
}
return $vid;
}
/**
* Liefert die ID einer Variable unterhalb {id} mit dem Namen {name}.
*
* @param int $id ID unter welchem die Variable erzeugt wurde.
* @param string $name Name der zu suchenden Variable
*
* @return int|false ID der gefundenen Variable, andernfalls false.
*/
function GetVariableByName($id, $name)
{
return @IPS_GetVariableIDByName($name, $id);
}
/**
* Erzeugt eine Variable unterhalb {id} mit dem Namen {name} vom Typ {type}
* Existiert die Variable schon wird diese zurückgeliefert.
*
* @param int $id ID unter welchem die Variable erzeugt werden soll.
* @param string $ident Ident der zu erzeugenden Variable
* @param string $name Name der zu erzeugenden Variable
* @param int $type Typ der Variable (0 = Boolean, 1 = Integer, 2 = Float, 3 = String)
* @param int $pos 0 gleich Standard, sonst neue Sortierreihenfolgenummer {pos}
* @param string $icon Zuweisung eines Icons mit dem übergebenen Namen
* @param string $profil Leer für kein Profil, sonst Profilnamen
* @param int $action ID der benutzerdefinierten Aktion (Auslösung via Webfront)
*
* @return int ID der bestehenden oder neu angelegten Variable
*/
function CreateVariableByIdent($id, $ident, $name, $type, $pos = 0, $icon = '', $profile = '', $action = null)
{
$ident = CreateIdent($ident);
$vid = GetObjectByIdent($id, $ident);
if ($vid === false) {
$vid = IPS_CreateVariable($type);
IPS_SetParent($vid, $id);
IPS_SetName($vid, $name);
IPS_SetIdent($vid, $ident);
IPS_SetPosition($vid, $pos);
IPS_SetIcon($vid, $icon);
if ($profile !== '') {
IPS_SetVariableCustomProfile($vid, $profile);
}
if ($action != null) {
IPS_SetVariableCustomAction($vid, $action);
}
}
return $vid;
}
/**
* Liefert die ID einer Variable unterhalb {id} mit dem Ident {ident}.
*
* @param int $id ID unter welchem die Variable erzeugt wurde.
* @param string $ident Ident der zu suchenden Variable
*
* @return int|false ID der gefundenen Variable, andernfalls false.
*/
function GetVariableByIdent($id, $ident)
{
return GetObjectByIdent($id, $ident);
}
/**
* Erzeugt ein Event unterhalb {id} mit dem Namen {name} um Zeit {time}
* Existiert das Event schon wird diese zurückgeliefert.
* Hinweis: $time = mktime(hour, minute, second);
*
* @param int $id ID unter welchem das Event erzeugt werden soll.
* @param string $name Name des zu erzeugenden Events
* @param int $time Uhrzeit an welchen das Event eintreten soll.
*
* @return int ID des bestehenden oder neu erzeugten Events
*/
function CreateEventByName($id, $name, $time = 0)
{
$eid = GetEventByName($id, $name);
if (($eid === false) && ($time > 0)) {
// Eventtyp = Zyklisch (1)
$eid = IPS_CreateEvent(1);
IPS_SetParent($eid, $id);
IPS_SetName($eid, $name);
IPS_SetPosition($eid, -2);
IPS_SetHidden($eid, true);
}
if ($time > 0) {
// Datumstyp = Tägliche (2), Datumsintervall = keins (1), Datumstage = keine (0), Datumstagesintervall = keine (0), Zeittyp = Einmalig (0), Zeitintervall = keine (0)
IPS_SetEventCyclic($eid, 2, 1, 0, 0, 0, 0);
IPS_SetEventCyclicDateFrom($eid, (int) date('j', $time), (int) date('n', $time), (int) date('Y', $time));
IPS_SetEventCyclicDateTo($eid, 0, 0, 0);
IPS_SetEventCyclicTimeFrom($eid, (int) date('H', $time), (int) date('i', $time), (int) date('s', $time));
IPS_SetEventCyclicTimeTo($eid, 0, 0, 0);
IPS_SetEventActive($eid, true);
if (function_exists('IPS_SetEventAction')) {
IPS_SetEventAction($eid, ExtractGuid('Run Automation'), []);
}
}
return $eid;
}
/**
* Erzeugt ein Event unterhalb {id} mit dem Namen {name} in der Zeit {from} bis {to}
* Existiert das Event schon wird diese zurückgeliefert.
* Hinweis: $from/$to = mktime(hour, minute, second);
*
* @param int $id ID unter welchem das Event erzeugt werden soll.
* @param string $name Name des zu erzeugenden Events
* @param int $type Zeittyp (1 Sekündlich,2 Minütlichm, 3 Stündlich)
* @param int $interval Zeitinterval (1 Alle X Sekunden, 2 Alle X Minuten, 3 Alle X Stunden)
* @param int $from Uhrzeit an welchen das Event starten soll.
* @param int $time Uhrzeit an welchen das Event stoppen soll.
*
* @return int ID des bestehenden oder neu erzeugten Events
*/
function CreateEventByNameFromTo($id, $name, $type, $interval, $from, $to)
{
$eid = GetEventByName($id, $name);
if (($eid === false) && ($type > 0) && ($interval > 0)) {
// Eventtyp = Zyklisch (1)
$eid = IPS_CreateEvent(1);
IPS_SetParent($eid, $id);
IPS_SetName($eid, $name);
IPS_SetPosition($eid, -2);
IPS_SetHidden($eid, true);
}
if ($from > 0 && $to > 0) {
// Datumstyp = Tägliche (2), Datumsintervall = keins (1), Datumstage = keine (0), Datumstagesintervall = keine (0), Zeittyp = Einmalig (0), Zeitintervall = keine (0)
IPS_SetEventCyclic($eid, 2, 1, 0, 0, $type, $interval);
IPS_SetEventCyclicTimeFrom($eid, (int) date('H', $from), (int) date('i', $from), (int) date('s', $from));
IPS_SetEventCyclicTimeTo($eid, (int) date('H', $to), (int) date('i', $to), (int) date('s', $to));
IPS_SetEventActive($eid, true);
if (function_exists('IPS_SetEventAction')) {
IPS_SetEventAction($eid, ExtractGuid('Run Automation'), []);
}
}
return $eid;
}
/**
* Erzeugt einen Timer unterhalb {id} mit dem Namen {name} um die Zeit {time}
* Existiert der Timer schon wird diese zurückgeliefert.
* Tägliche Ausführung: $time in Minuten
* Einmalige Ausführung: $time mit mktime(hour, minute, second) übergeben.
*
* @param int $id ID unter welchem das Timers erzeugt werden soll.
* @param string $name Name des zu erzeugenden Timers
* @param int $time Uhrzeit bzw. Zeitpunkt des Timers.
*
* @return int ID des bestehenden oder neu erzeugten Timers
*/
function CreateTimerByName($id, $name, $time = 0, $repeat = true)
{
$eid = GetEventByName($id, $name);
if (($eid === false) && ($time > 0)) {
// Eventtyp = Zyklisch (1)
$eid = IPS_CreateEvent(1);
IPS_SetParent($eid, $id);
IPS_SetName($eid, $name);
IPS_SetPosition($eid, -1);
IPS_SetHidden($eid, true);
}
if (($eid !== false) && ($time > 0)) {
if ($repeat == true) {
// 0 = Tägliche Ausführung
IPS_SetEventCyclic($eid, 0, 1, 0, 0, 2, $time);
$now = time();
IPS_SetEventCyclicDateFrom($eid, (int) date('j', $now), (int) date('n', $now), (int) date('Y', $now));
} else {
// Einmalige Ausführung
IPS_SetEventCyclic($eid, 1, 0, 0, 0, 0, 0);
IPS_SetEventCyclicDateFrom($eid, (int) date('j', $time), (int) date('n', $time), (int) date('Y', $time));
IPS_SetEventCyclicDateTo($eid, (int) date('j', $time), (int) date('n', $time), (int) date('Y', $time));
IPS_SetEventCyclicTimeFrom($eid, (int) date('H', $time), (int) date('i', $time), (int) date('s', $time));
IPS_SetEventCyclicTimeTo($eid, (int) date('H', $time), (int) date('i', $time), (int) date('s', $time));
}
IPS_SetEventActive($eid, true);
if (function_exists('IPS_SetEventAction')) {
IPS_SetEventAction($eid, ExtractGuid('Run Automation'), []);
}
}
if (($eid !== false) && ($time == -1)) {
IPS_SetEventActive($eid, false);
}
return $eid;
}
/**
* Liefert die ID eines Events bzw. Timers unterhalb {id} mit dem Namen {name}.
*
* @param int $id ID unter welchem das Event erzeugt wurde.
* @param string $name Name des zu suchenden Events oder Timers.
*
* @return int|false ID des gefundenen Events, andernfalls false.
*/
function GetEventByName($id, $name)
{
return @IPS_GetEventIDByName($name, $id);
}
/**
* Eine Funktion um ein Skript im Script-Verzeichnis zu erzeugen
*
* @param int $id ID unter welchem das Skript erzeugt werden soll.
* @param string $name Name des zu erzeugenden Scripts
*
* @return int ID des bestehenden oder neu erzeugten Scripts
*/
function CreateScriptByName($id, $name)
{
$sid = GetScriptByName($id, $name);
if ($sid === false) {
$sid = IPS_CreateScript(0);
IPS_SetName($sid, $name);
IPS_SetParent($sid, $id);
}
return $sid;
}
/**
* Liefert die ID eines Skriptes unterhalb {id} mit dem Namen {name}.
*
* @param int $id ID unter welchem das Skript erzeugt wurde.
* @param string $name Name des zu suchenden Skriptes.
*
* @return int|false ID des gefundenen Skriptes, andernfalls false.
*/
function GetScriptByName($id, $name)
{
return @IPS_GetScriptIDByName($name, $id);
}
/**
* Erzeugt ein WebHook mit Path {webhook} und Target {id}
*
* @param string $webhook Path des Ebhooks
* @param int $id Ziel ID (Target) des Webhooks
*
* @return void Funktion liefert keinen Rückgabewert.
*/
function RegisterHook($webhook, $id)
{
$ids = IPS_GetInstanceListByModuleID(ExtractGuid('WebHook Control'));
if (count($ids) > 0) {
$hooks = json_decode(IPS_GetProperty($ids[0], 'Hooks'), true);
$found = false;
foreach ($hooks as $index => $hook) {
if ($hook['Hook'] == $webhook) {
if ($hook['TargetID'] == $id) {
return;
}
$hooks[$index]['TargetID'] = $id;
$found = true;
}
}
if (!$found) {
$hooks[] = ['Hook' => $webhook, 'TargetID' => $id];
}
IPS_SetProperty($ids[0], 'Hooks', json_encode($hooks));
IPS_ApplyChanges($ids[0]);
}
}
/**
* Unregister a web hook.
*
* @param string $hook path of the web hook.
*/
function UnregisterHook($hook)
{
$ids = IPS_GetInstanceListByModuleID(ExtractGuid('WebHook Control'));
if (count($ids) > 0) {
$hooks = json_decode(IPS_GetProperty($ids[0], 'Hooks'), true);
$found = false;
foreach ($hooks as $key => $value) {
if ($value['Hook'] == $hook) {
$found = true;
$this->SendDebug('UnregisterHook', $hook . $this->InstanceID);
break;
}
}
// Unregister
if ($found == true) {
array_splice($hooks, $key, 1);
IPS_SetProperty($ids[0], 'Hooks', json_encode($hooks));
IPS_ApplyChanges($ids[0]);
}
}
}
/**
* Aktiviert das Logging einer Variable im Archiv.
*
* @param int $id ID der zu loggenden Variable
* @param bool $default True für standard Aggregationstyp, sonst Zähler
* @param bool $zero true wenn Nullwerte ignoriert werden sollen.
*
* @return void Funktion liefert keinen Rückgabewert.
*/
function RegisterArchive($id, $default = true, $zero = false)
{
$ids = IPS_GetInstanceListByModuleID(ExtractGuid('Archive Control'));
if (count($ids) > 0) {
AC_SetLoggingStatus($ids[0], $id, true);
AC_SetAggregationType($ids[0], $id, ($default ? 0 : 1));
if ($zero) {
AC_SetCounterIgnoreZeros($ids[0], $id, true);
}
}
}
/**
* Deaktiviert das Logging einer Variable im Archiv.
*
* @param int $id ID der nicht mehr zu loggenden Variable
*
* @return void Funktion liefert keinen Rückgabewert.
*/
function UnregisterArchive($id)
{
$ids = IPS_GetInstanceListByModuleID(ExtractGuid('Archive Control'));
if (count($ids) > 0) {
AC_SetLoggingStatus($ids[0], $id, false);
}
}
/**
* Erzeugt ein Variablenprofil vom Typ {type} mit Namen {name}
*
* @param string $name Name des Profiles
* @param int $type Typ des Profils (0 = Boolean, 1 = Integer, 2 = Float, 3 = String)
*
* @return void Funktion liefert keinen Rückgabewert.
*/
function CreateProfile($name, $type)
{
if (!IPS_VariableProfileExists($name)) {
IPS_CreateVariableProfile($name, $type);
} else {
$profile = IPS_GetVariableProfile($name);
if ($profile['ProfileType'] != $type) {
throw new Exception('Variable profile type does not match for profile ' . $name);
}
}
}
/**
* Erzeugt ein Boolean-Variablenprofil (Boolean-Typ = 0)
*
* @param string $name Name des Profiles
* @param string $icon Assoziertes Standard-Icon
* @param string $prefix Prefix Text
* @param string $suffix Suffix Text
* @param array $asso Array mit Profilassoziationen
*
* @return void Funktion liefert keinen Rückgabewert.
*/
function CreateProfileBoolean($name, $icon, $prefix, $suffix, $asso)
{
CreateProfile($name, 0);
IPS_SetVariableProfileIcon($name, $icon);
IPS_SetVariableProfileText($name, $prefix, $suffix);
if (($asso !== null) && (count($asso) !== 0)) {
foreach ($asso as $ass) {
IPS_SetVariableProfileAssociation($name, $ass[0], $ass[1], $ass[2], $ass[3]);
}
}
}
/**
* Erzeugt ein Integer-Variablenprofil (Integer-Typ = 1)
*
* @param string $name Name des Profiles
* @param string $icon Assoziertes Standard-Icon
* @param string $prefix Prefix Text
* @param string $suffix Suffix Text
* @param int $minvalue Minimalwert welchen die Variable annehmen kann
* @param int $maxvalue Maximalwert welchen die Variable annehmen kann
* @param int $step Schrittweite in welchen sich der Wert erhöhen oder verringern kann
* @param array $asso Array mit Profilassoziationen
*
* @return void Funktion liefert keinen Rückgabewert.
*/
function CreateProfileInteger($name, $icon, $prefix, $suffix, $minvalue, $maxvalue, $step, $asso = null)
{
CreateProfile($name, 1);
IPS_SetVariableProfileIcon($name, $icon);
IPS_SetVariableProfileText($name, $prefix, $suffix);
IPS_SetVariableProfileValues($name, $minvalue, $maxvalue, $step);
if (($asso !== null) && (count($asso) !== 0)) {
foreach ($asso as $ass) {
IPS_SetVariableProfileAssociation($name, $ass[0], $ass[1], $ass[2], $ass[3]);
}
}
}
/**
* Erzeugt ein Float-Variablenprofil (Float-Typ = 2)
*
* @param string $name Name des Profiles
* @param string $icon Assoziertes Standard-Icon
* @param string $prefix Prefix Text
* @param string $suffix Suffix Text
* @param int $minvalue Minimalwert welchen die Variable annehmen kann
* @param int $maxvalue Maximalwert welchen die Variable annehmen kann
* @param int $step Schrittweite in welchen sich der Wert erhöhen oder verringern kann
* @param int $digits Anzahl Nachkommastellen die angezeigt werden sollen
* @param array $asso Array mit Profilassoziationen
*
* @return void Funktion liefert keinen Rückgabewert.
*/
function CreateProfileFloat($name, $icon, $prefix, $suffix, $minvalue, $maxvalue, $step, $digits, $asso = null)
{
CreateProfile($name, 2);
IPS_SetVariableProfileIcon($name, $icon);
IPS_SetVariableProfileText($name, $prefix, $suffix);
IPS_SetVariableProfileDigits($name, $digits);
/*
if(($asso == null) && (count($asso) == 0)){
$minvalue = 0;
$maxvalue = 0;
}
*/
IPS_SetVariableProfileValues($name, $minvalue, $maxvalue, $step);
if (($asso !== null) && (count($asso) !== 0)) {
foreach ($asso as $ass) {
IPS_SetVariableProfileAssociation($name, $ass[0], $ass[1], $ass[2], $ass[3]);
}
}
}
/**
* Erzeugt ein String-Variablenprofil (String-Typ = 3)
*
* @param string $name Name des Profiles
* @param string $icon Assoziertes Standard-Icon
* @param string $prefix Prefix Text
* @param string $suffix Suffix Text
* @param array $asso Array mit Profilassoziationen
*
* @return void Funktion liefert keinen Rückgabewert.
*/
function CreateProfileString($name, $icon, $prefix, $suffix, $asso)
{
CreateProfile($name, 3);
IPS_SetVariableProfileIcon($name, $icon);
IPS_SetVariableProfileText($name, $prefix, $suffix);
if (($asso !== null) && (count($asso) !== 0)) {
foreach ($asso as $ass) {
IPS_SetVariableProfileAssociation($name, $ass[0], $ass[1], $ass[2], $ass[3]);
}
}
}
/**
* Löscht ein Variablenprofile, sofern es nicht noch verwendet wird.
*
* @param string $name Name des Profiles
*
* @return void true im Erfolgsfall, anderenfalls false.
*/
function UnregisterProfile($name)
{
if (!IPS_VariableProfileExists($name)) {
return false;
}
foreach (IPS_GetVariableList() as $vid) {
if (IPS_GetVariable($vid)['VariableCustomProfile'] == $name) {
return false;
}
if (IPS_GetVariable($vid)['VariableProfile'] == $name) {
return false;
}
}
return IPS_DeleteVariableProfile($name);
}
/**
* Debug Ausgabe von Variablen, Feldern & Objecten auf die Konsole
*
* @param string $msg Texthinweis zum Datenobjekt
* @param mixed $data Datenobjekt welches auszugeben ist
* @param string|null $pre Prefix, welcher den Datentyp näher beschreibt
*
* @return void Funktion liefert keinen Rückgabewert.
*/
function EchoDebug($msg, $data, $pre = null)
{
// Safty Check for use!
if (!isset($GLOBALS['DEBUG'])) {
echo "!!! For Debug Output define global variable 'DEBUG' (true|false) !!!";
return;
}
// only if Debug enabled
if ($GLOBALS['DEBUG']) {
if (is_object($data)) {
foreach ($data as $key => $value) {
EchoDebug($msg, $value, $key);
}
} elseif (is_array($data)) {
if ($pre !== null) {
echo $msg . ': ' . $pre . '(array) =>' . PHP_EOL;
}
foreach ($data as $key => $value) {
EchoDebug($msg, $value, $key);
}
} elseif (is_bool($data)) {
echo $msg . ': ' . ($pre !== null ? $pre . ' => ' : '') . ($data ? 'TRUE' : 'FALSE') . PHP_EOL;
} else {
echo $msg . ': ' . ($pre !== null ? $pre . ' => ' : '') . $data . PHP_EOL;
}
}
}
################################################################################