-
Notifications
You must be signed in to change notification settings - Fork 310
/
eqLogic.class.php
2070 lines (1940 loc) · 68.6 KB
/
eqLogic.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
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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* This file is part of Jeedom.
*
* Jeedom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Jeedom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jeedom. If not, see <http://www.gnu.org/licenses/>.
*/
/* * ***************************Includes********************************* */
require_once __DIR__ . '/../../core/php/core.inc.php';
class eqLogic {
/* * *************************Attributs****************************** */
const UIDDELIMITER = '__';
protected $id;
protected $name;
protected $logicalId = '';
protected $generic_type;
protected $object_id = null;
protected $eqType_name;
protected $isVisible = 0;
protected $isEnable = 0;
protected $configuration;
protected $timeout = 0;
protected $category;
protected $display;
protected $order = 9999;
protected $comment;
protected $tags;
protected $_debug = false;
protected $_object = null;
protected $_needRefreshWidget = false;
protected $_timeoutUpdated = false;
protected $_batteryUpdated = false;
protected $_changed = false;
protected $_cmds = array();
protected static $_templateArray = array();
/* * ***********************Méthodes statiques*************************** */
public static function getAllTags() {
$values = array();
$sql = 'SELECT tags
FROM eqLogic
WHERE tags IS NOT NULL
AND tags!=""';
$results = DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL);
$return = array();
foreach ($results as $result) {
$tags = explode(',', $result['tags']);
foreach ($tags as $tag) {
$return[$tag] = $tag;
}
}
return $return;
}
/**
* @param int|string $_id
* @return void|eqLogic void if $_id is not valid else the eqLogic
*/
public static function byId($_id) {
if ($_id == '') {
return;
}
$values = array(
'id' => $_id,
);
$sql = 'SELECT ' . DB::buildField(__CLASS__) . '
FROM eqLogic
WHERE id=:id';
return self::cast(DB::Prepare($sql, $values, DB::FETCH_TYPE_ROW, PDO::FETCH_CLASS, __CLASS__));
}
private static function cast($_inputs) {
if (is_object($_inputs) && class_exists($_inputs->getEqType_name())) {
$return = cast($_inputs, $_inputs->getEqType_name());
if (method_exists($return, 'decrypt')) {
$return->decrypt();
}
return $return;
}
if (is_array($_inputs)) {
$return = array();
foreach ($_inputs as $input) {
$return[] = self::cast($input);
}
return $return;
}
return $_inputs;
}
public static function all($_onlyEnable = false) {
$sql = 'SELECT ' . DB::buildField(__CLASS__, 'el') . '
FROM eqLogic el
LEFT JOIN object ob ON el.object_id=ob.id';
if ($_onlyEnable) {
$sql .= ' AND isEnable=1';
}
$sql .= ' ORDER BY ob.name,el.name';
return self::cast(DB::Prepare($sql, array(), DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, __CLASS__));
}
public static function byObjectId($_object_id, $_onlyEnable = true, $_onlyVisible = false, $_eqType_name = null, $_logicalId = null, $_orderByName = false, $_onlyHasCmds = false) {
$values = array();
$sql = 'SELECT ' . DB::buildField(__CLASS__) . '
FROM eqLogic';
if ($_object_id === null) {
$sql .= ' WHERE (object_id IS NULL OR object_id = -1)';
} else {
$values['object_id'] = $_object_id;
$sql .= ' WHERE object_id=:object_id';
}
if ($_onlyEnable) {
$sql .= ' AND isEnable = 1';
}
if ($_onlyVisible) {
$sql .= ' AND isVisible = 1';
}
if ($_eqType_name !== null) {
$values['eqType_name'] = $_eqType_name;
$sql .= ' AND eqType_name=:eqType_name';
}
if ($_logicalId !== null) {
$values['logicalId'] = $_logicalId;
$sql .= ' AND logicalId=:logicalId';
}
if ($_onlyHasCmds !== false) {
$sql .= ' AND id IN (SELECT eqLogic_id FROM cmd WHERE 1=1';
if (is_array($_onlyHasCmds)) {
if (isset($_onlyHasCmds['type'])) {
$values['cmd_type'] = $_onlyHasCmds['type'];
$sql .= ' AND type=:cmd_type';
}
if (isset($_onlyHasCmds['subType'])) {
$values['cmd_subType'] = $_onlyHasCmds['subType'];
$sql .= ' AND subType=:cmd_subType';
}
}
$sql .= ' )';
}
if ($_orderByName) {
$sql .= ' ORDER BY `name`';
} else {
$sql .= ' ORDER BY `order`,category';
}
if ($_eqType_name != null && class_exists($_eqType_name)) {
return DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, $_eqType_name);
}
return self::cast(DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, __CLASS__));
}
/**
* @param string $_logicalId
* @param string $_eqType_name the plugin class name
* @param bool $_multiple default value=false
* @return eqLogic|eqLogic[] eqLogic if $_multiple is false else eqLogic[]
*/
public static function byLogicalId($_logicalId, $_eqType_name, $_multiple = false) {
$values = array(
'logicalId' => $_logicalId,
'eqType_name' => $_eqType_name,
);
$sql = 'SELECT ' . DB::buildField(__CLASS__) . '
FROM eqLogic
WHERE logicalId=:logicalId
AND eqType_name=:eqType_name';
if ($_multiple) {
if (class_exists($_eqType_name)) {
return DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, $_eqType_name);
}
return self::cast(DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, __CLASS__));
}
if (class_exists($_eqType_name)) {
return DB::Prepare($sql, $values, DB::FETCH_TYPE_ROW, PDO::FETCH_CLASS, $_eqType_name);
}
return self::cast(DB::Prepare($sql, $values, DB::FETCH_TYPE_ROW, PDO::FETCH_CLASS, __CLASS__));
}
/**
* @param string $_eqType_name the plugin class name
* @param bool $_onlyEnable
* @return eqLogic[]
*/
public static function byType($_eqType_name, $_onlyEnable = false) {
$values = array(
'eqType_name' => $_eqType_name,
);
$sql = 'SELECT ' . DB::buildField(__CLASS__, 'el') . '
FROM eqLogic el
LEFT JOIN object ob ON el.object_id=ob.id
WHERE eqType_name=:eqType_name ';
if ($_onlyEnable) {
$sql .= ' AND isEnable=1';
}
$sql .= ' ORDER BY ob.name,el.name';
if (class_exists($_eqType_name)) {
return DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, $_eqType_name);
}
return self::cast(DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, __CLASS__));
}
public static function byCategorie($_category) {
$values = array(
'category' => '%"' . $_category . '":1%',
'category2' => '%"' . $_category . '":"1"%',
);
$sql = 'SELECT ' . DB::buildField(__CLASS__) . '
FROM eqLogic
WHERE category LIKE :category
OR category LIKE :category2
ORDER BY name';
return self::cast(DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, __CLASS__));
}
public static function byTypeAndSearchConfiguration($_eqType_name, $_configuration, $_onlyEnable=false, $_onlyVisible=false) {
if (is_array($_configuration)) {
$values = array(
'eqType_name' => $_eqType_name,
'configuration' => json_encode($_configuration),
);
$sql = 'SELECT ' . DB::buildField(__CLASS__) . '
FROM eqLogic
WHERE eqType_name=:eqType_name';
if ($_onlyEnable) {
$sql .= ' AND isEnable=1';
}
if ($_onlyVisible) {
$sql .= ' AND isVisible=1';
}
$sql .= ' AND JSON_CONTAINS(configuration,:configuration)
ORDER BY name';
if ($_eqType_name != null && class_exists($_eqType_name)) {
return DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, $_eqType_name);
}
return self::cast(DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, __CLASS__));
}
$values = array(
'eqType_name' => $_eqType_name,
'configuration' => '%' . $_configuration . '%',
);
$sql = 'SELECT ' . DB::buildField(__CLASS__) . '
FROM eqLogic
WHERE eqType_name=:eqType_name';
if ($_onlyEnable) {
$sql .= ' AND isEnable=1';
}
if ($_onlyVisible) {
$sql .= ' AND isVisible=1';
}
$sql .= '
AND configuration LIKE :configuration
ORDER BY name';
if ($_eqType_name != null && class_exists($_eqType_name)) {
return DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, $_eqType_name);
}
return self::cast(DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, __CLASS__));
}
public static function byTypeAndSearhConfiguration($_eqType_name, $_configuration, $_onlyEnable=false, $_onlyVisible=false) {
trigger_error('eqLogic::byTypeAndSearhConfiguration() is deprecated since Core v4.4, eqLogic::byTypeAndSearchConfiguration() has been introduced since Core v4.1', E_USER_DEPRECATED);
return self::byTypeAndSearchConfiguration($_eqType_name, $_configuration, $_onlyEnable, $_onlyVisible);
}
public static function searchByString($_search) {
$values = array(
'search' => '%' . $_search . '%'
);
$sql = 'SELECT ' . DB::buildField(__CLASS__) . '
FROM eqLogic
WHERE name LIKE :search or logicalId LIKE :search or eqType_name LIKE :search or comment LIKE :search or tags LIKE :search';
return self::cast(DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, __CLASS__));
}
public static function searchConfiguration($_configuration, $_eqType_name = null) {
if (!is_array($_configuration)) {
$values = array(
'configuration' => '%' . $_configuration . '%',
);
$sql = 'SELECT ' . DB::buildField(__CLASS__) . '
FROM eqLogic
WHERE configuration LIKE :configuration';
} else {
$values = array(
'configuration' => '%' . $_configuration[0] . '%',
);
$sql = 'SELECT ' . DB::buildField(__CLASS__) . '
FROM eqLogic
WHERE configuration LIKE :configuration';
for ($i = 1; $i < count($_configuration); $i++) {
$values['configuration' . $i] = '%' . $_configuration[$i] . '%';
$sql .= ' OR configuration LIKE :configuration' . $i;
}
}
if ($_eqType_name !== null) {
$values['eqType_name'] = $_eqType_name;
$sql .= ' AND eqType_name=:eqType_name ';
}
$sql .= ' ORDER BY name';
if ($_eqType_name != null && class_exists($_eqType_name)) {
return DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, $_eqType_name);
}
return self::cast(DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, __CLASS__));
}
public static function listByTypeAndCmdType($_eqType_name, $_typeCmd, $subTypeCmd = '') {
if ($subTypeCmd == '') {
$values = array(
'eqType_name' => $_eqType_name,
'typeCmd' => $_typeCmd,
);
$sql = 'SELECT DISTINCT(el.id),el.name
FROM eqLogic el
INNER JOIN cmd c ON c.eqLogic_id=el.id
WHERE eqType_name=:eqType_name
AND c.type=:typeCmd
ORDER BY name';
return DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL);
} else {
$values = array(
'eqType_name' => $_eqType_name,
'typeCmd' => $_typeCmd,
'subTypeCmd' => $subTypeCmd,
);
$sql = 'SELECT DISTINCT(el.id),el.name
FROM eqLogic el
INNER JOIN cmd c ON c.eqLogic_id=el.id
WHERE eqType_name=:eqType_name
AND c.type=:typeCmd
AND c.subType=:subTypeCmd
ORDER BY name';
return DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL);
}
}
public static function listByObjectAndCmdType($_object_id, $_typeCmd, $subTypeCmd = '') {
$values = array();
$sql = 'SELECT DISTINCT(el.id),el.name
FROM eqLogic el
INNER JOIN cmd c ON c.eqLogic_id=el.id
WHERE ';
if ($_object_id === null) {
$sql .= ' object_id IS NULL ';
} elseif ($_object_id != '') {
$values['object_id'] = $_object_id;
$sql .= ' object_id=:object_id ';
}
if ($subTypeCmd != '') {
$values['subTypeCmd'] = $subTypeCmd;
$sql .= ' AND c.subType=:subTypeCmd ';
}
if ($_typeCmd != '' && $_typeCmd != 'all') {
$values['type'] = $_typeCmd;
$sql .= ' AND c.type=:type ';
}
$sql .= ' ORDER BY name ';
return DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL);
}
public static function allType() {
$sql = 'SELECT distinct(eqType_name) as type
FROM eqLogic';
return DB::Prepare($sql, array(), DB::FETCH_TYPE_ALL);
}
public static function checkAlive() {
foreach (eqLogic::byTimeout(1, true) as $eqLogic) {
$sendReport = false;
$cmds = $eqLogic->getCmd();
foreach ($cmds as $cmd) {
$sendReport = true;
}
$logicalId = 'noMessage' . $eqLogic->getId();
if ($sendReport) {
$noReponseTimeLimit = $eqLogic->getTimeout();
if (count(message::byPluginLogicalId('core', $logicalId)) == 0) {
if ($eqLogic->getStatus('lastCommunication', date('Y-m-d H:i:s')) < date('Y-m-d H:i:s', strtotime('-' . $noReponseTimeLimit . ' minutes' . date('Y-m-d H:i:s')))) {
$message = __('Attention', __FILE__) . ' ' . $eqLogic->getHumanName();
$message .= ' ' . __('n\'a pas envoyé de message depuis plus de', __FILE__) . ' ' . $noReponseTimeLimit . ' ' . __('min (vérifiez les piles)', __FILE__);
$action = '<a href="/' . $eqLogic->getLinkToConfiguration() . '">' . __('Equipement', __FILE__) . '</a>';
$prevStatus = $eqLogic->getStatus('timeout', 0);
$eqLogic->setStatus('timeout', 1);
if (config::byKey('alert::addMessageOnTimeout') == 1 && $prevStatus == 0) {
message::add('core', $message, $action, $logicalId);
}
$cmds = explode(('&&'), config::byKey('alert::timeoutCmd'));
if (count($cmds) > 0 && trim(config::byKey('alert::timeoutCmd')) != '' && $prevStatus == 0) {
foreach ($cmds as $id) {
$cmd = cmd::byId(str_replace('#', '', $id));
if (is_object($cmd)) {
$cmd->execCmd(array(
'title' => '[' . config::byKey('name', 'core', 'JEEDOM') . '] : ' . $message,
'message' => config::byKey('name', 'core', 'JEEDOM') . ' : ' . $message,
));
}
}
}
}
} else {
if ($eqLogic->getStatus('lastCommunication', date('Y-m-d H:i:s')) > date('Y-m-d H:i:s', strtotime('-' . $noReponseTimeLimit . ' minutes' . date('Y-m-d H:i:s')))) {
foreach (message::byPluginLogicalId('core', $logicalId) as $message) {
$message->remove();
}
$eqLogic->setStatus('timeout', 0);
}
}
}
}
}
public static function byTimeout($_timeout = 0, $_onlyEnable = false) {
$values = array(
'timeout' => $_timeout,
);
$sql = 'SELECT ' . DB::buildField(__CLASS__) . '
FROM eqLogic
WHERE timeout>=:timeout';
if ($_onlyEnable) {
$sql .= ' AND isEnable=1';
}
return self::cast(DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, __CLASS__));
}
public static function byObjectNameEqLogicName($_object_name, $_eqLogic_name) {
if ($_object_name == __('Aucun', __FILE__)) {
$values = array(
'eqLogic_name' => $_eqLogic_name,
);
$sql = 'SELECT ' . DB::buildField(__CLASS__) . '
FROM eqLogic
WHERE name=:eqLogic_name
AND object_id IS NULL';
} else {
$values = array(
'eqLogic_name' => $_eqLogic_name,
'object_name' => $_object_name,
);
$sql = 'SELECT ' . DB::buildField(__CLASS__, 'el') . '
FROM eqLogic el
INNER JOIN object ob ON el.object_id=ob.id
WHERE el.name=:eqLogic_name
AND ob.name=:object_name';
}
return self::cast(DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, __CLASS__));
}
public static function toHumanReadable($_input) {
if (is_object($_input)) {
$reflections = array();
$uuid = spl_object_hash($_input);
if (!isset($reflections[$uuid])) {
$reflections[$uuid] = new ReflectionClass($_input);
}
$reflection = $reflections[$uuid];
$properties = $reflection->getProperties();
foreach ($properties as $property) {
$property->setAccessible(true);
$value = $property->getValue($_input);
$property->setValue($_input, self::toHumanReadable($value));
$property->setAccessible(false);
}
return $_input;
}
if (is_array($_input)) {
foreach ($_input as $key => $value) {
$_input[$key] = self::toHumanReadable($value);
}
return $_input;
}
$text = $_input;
preg_match_all("/#eqLogic([0-9]*)#/", $text, $matches);
foreach ($matches[1] as $eqLogic_id) {
if (is_numeric($eqLogic_id)) {
$eqLogic = self::byId($eqLogic_id);
if (is_object($eqLogic)) {
$text = str_replace('#eqLogic' . $eqLogic_id . '#', '#' . $eqLogic->getHumanName() . '#', $text);
}
}
}
return $text;
}
public static function fromHumanReadable($_input) {
$isJson = false;
if (is_json($_input)) {
$isJson = true;
$_input = json_decode($_input, true);
}
if (is_object($_input)) {
$reflections = array();
$uuid = spl_object_hash($_input);
if (!isset($reflections[$uuid])) {
$reflections[$uuid] = new ReflectionClass($_input);
}
$reflection = $reflections[$uuid];
$properties = $reflection->getProperties();
foreach ($properties as $property) {
$property->setAccessible(true);
$value = $property->getValue($_input);
$property->setValue($_input, self::fromHumanReadable($value));
$property->setAccessible(false);
}
return $_input;
}
if (is_array($_input)) {
foreach ($_input as $key => $value) {
$_input[$key] = self::fromHumanReadable($value);
}
if ($isJson) {
return json_encode($_input, JSON_UNESCAPED_UNICODE);
}
return $_input;
}
$text = $_input;
preg_match_all("/#\[(.*?)\]\[(.*?)\]#/", $text, $matches);
if (count($matches) == 3) {
$countMatches = count($matches[0]);
for ($i = 0; $i < $countMatches; $i++) {
if (isset($matches[1][$i]) && isset($matches[2][$i])) {
$eqLogic = self::byObjectNameEqLogicName($matches[1][$i], $matches[2][$i]);
if (isset($eqLogic[0]) && is_object($eqLogic[0])) {
$text = str_replace($matches[0][$i], '#eqLogic' . $eqLogic[0]->getId() . '#', $text);
}
}
}
}
return $text;
}
/**
* byString
*
* @param string $_string
* @return \eqLogic
*/
public static function byString($_string) {
$eqLogic = self::byId(str_replace(array('#', 'eqLogic'), '', self::fromHumanReadable($_string)));
if (!is_object($eqLogic)) {
throw new Exception(__('L\'équipement n\'a pas pu être trouvé :', __FILE__) . ' ' . $_string . ' => ' . self::fromHumanReadable($_string));
}
return $eqLogic;
}
public static function clearCacheWidget() {
foreach ((self::all()) as $eqLogic) {
$eqLogic->emptyCacheWidget();
}
}
public static function generateHtmlTable($_nbLine, $_nbColumn, $_options = array()) {
$return = array('html' => '', 'replace' => array());
if (!isset($_options['styletd'])) {
$_options['styletd'] = '';
}
if (!isset($_options['center'])) {
$_options['center'] = 0;
}
if (!isset($_options['styletable'])) {
$_options['styletable'] = '';
}
$return['html'] .= '<table style="' . $_options['styletable'] . '" class="tableCmd" data-line="' . $_nbLine . '" data-column="' . $_nbColumn . '">';
$return['html'] .= '<tbody>';
for ($i = 1; $i <= $_nbLine; $i++) {
$return['html'] .= '<tr>';
for ($j = 1; $j <= $_nbColumn; $j++) {
$styletd = (isset($_options['style::td::' . $i . '::' . $j]) && $_options['style::td::' . $i . '::' . $j] != '') ? $_options['style::td::' . $i . '::' . $j] : '';
$styletd = $_options['styletd'] . $styletd;
$classTd = ($styletd != '') ? 'tableCmdcss' : '';
$attrs = '';
$style = '';
if(trim($styletd) != ''){
foreach (explode(';',$styletd) as $value) {
if(strpos($value, '=') !== false){
$attrs .= $value;
}else{
$style .= $value.';';
}
}
}
$return['html'] .= '<td class="' . $classTd . (($_options['center'] == 1) ? ' tableCenter' : '') . '" style="' . $style . '" '.$attrs.' data-line="' . $i . '" data-column="' . $j . '">';
if (isset($_options['text::td::' . $i . '::' . $j])) {
$return['html'] .= $_options['text::td::' . $i . '::' . $j];
}
$return['html'] .= '#cmd::' . $i . '::' . $j . '#';
$return['html'] .= '</td>';
$return['tag']['#cmd::' . $i . '::' . $j . '#'] = '';
}
$return['html'] .= '</tr>';
}
$return['html'] .= '</tbody>';
$return['html'] .= '</table>';
return $return;
}
/* * *********************Méthodes d'instance************************* */
public function batteryWidget($_version = 'dashboard') {
$html = '';
$level = 'good';
$niveau = '3';
$battery = $this->getConfiguration('battery_type', 'none');
$batteryTime = $this->getConfiguration('batterytime', 'NA');
$batterySince = 'NA';
if ($batteryTime != 'NA') {
$batterySince = round((strtotime(date("Y-m-d")) - strtotime(date("Y-m-d", strtotime($batteryTime)))) / 86400, 1);
}
if (strpos($battery, ' ') !== false) {
$battery = mb_substr(strrchr($battery, " "), 1);
}
$plugins = $this->getEqType_name();
$object_name = 'Aucun';
if (is_object($this->getObject())) {
$object_name = $this->getObject()->getName();
}
if ($this->getStatus('battery') <= $this->getConfiguration('battery_danger_threshold', config::byKey('battery::danger'))) {
$level = 'critical';
$niveau = '0';
} else if ($this->getStatus('battery') <= $this->getConfiguration('battery_warning_threshold', config::byKey('battery::warning'))) {
$level = 'warning';
$niveau = '1';
} else if ($this->getStatus('battery') <= 75) {
$niveau = '2';
}
$classAttr = $level . ' ' . $battery . ' ' . $plugins . ' ' . $object_name;
$idAttr = $level . '__' . $battery . '__' . $plugins . '__' . $object_name;
$html .= '<div class="eqLogic eqLogic-widget text-center ' . $classAttr . '" id="' . $idAttr . '" data-eqlogic_id="' . $this->getId() . '">';
$eqName = $this->getName();
if ($_version == 'mobile') {
$html .= '<div class="widget-name"><span class="name">' . $eqName . '</span><span class="object">' . $object_name . '</span></div>';
} else {
$html .= '<div class="#battery widget-name"><a href="' . $this->getLinkToConfiguration() . '">' . $eqName . '</a><br><span>' . $object_name . '</span></div>';
}
$html .= '<div class="jeedom-batterie">';
$html .= '<i class="icon jeedom-batterie' . $niveau . '"></i>';
$html .= '<span>' . $this->getStatus('battery', -2) . '%</span>';
$html .= '</div>';
$html .= '<div>' . __('Le', __FILE__) . ' ' . date("Y-m-d H:i:s", strtotime($this->getStatus('batteryDatetime', __('inconnue', __FILE__)))) . '</div>';
$html .= '<br>';
$html .= '<span class="pull-left pluginName">' . ucfirst($this->getEqType_name()) . '</span>';
if ($_version == 'mobile') {
$html .= '<span class="pull-left batteryTime">';
} else {
$html .= '<span class="pull-left batteryTime cursor">';
}
if ($this->getConfiguration('battery_danger_threshold') != '' || $this->getConfiguration('battery_warning_threshold') != '') {
$html .= '<i class="icon techno-fingerprint41 pull-right" title="' . __('Seuil manuel défini', __FILE__) . '"></i>';
}
if ($batteryTime != 'NA') {
$text = __('Pile(s) changée(s) il y a', __FILE__) . ' ';
$text .= ($batterySince > 1) ? $batterySince . __('jours', __FILE__) . ' (' . $batteryTime . ')' : $batterySince . __('jour', __FILE__) . ' (' . $batteryTime . ')';
$html .= '<i class="icon divers-calendar2" title="' . $text . '"></i><span> (' . $batterySince . 'j)</span>';
} else {
$html .= '<i class="icon divers-calendar2" title="' . __('Pas de date de changement de pile(s) renseignée', __FILE__) . '"></i>';
}
$html .= '</span>';
if ($this->getConfiguration('battery_type', '') != '') {
$html .= '<span class="pull-right" title="' . __('Piles', __FILE__) . '">' . $this->getConfiguration('battery_type', '') . '</span>';
}
$html .= '</div>';
return $html;
}
public function checkAndUpdateCmd($_logicalId, $_value, $_updateTime = null) {
if ($this->getIsEnable() == 0) {
return false;
}
$cmd = is_object($_logicalId) ? $_logicalId : $this->getCmd('info', $_logicalId);
if (!is_object($cmd)) {
return false;
}
$oldValue = $cmd->execCmd();
if ($oldValue !== $cmd->formatValue($_value) || $oldValue === '') {
$cmd->event($_value, $_updateTime);
return true;
}
if ($_updateTime !== null && $_updateTime !== false) {
if (strtotime($cmd->getCollectDate()) < strtotime($_updateTime)) {
$cmd->event($_value, $_updateTime);
return true;
}
return false;
} else if ($cmd->getConfiguration('repeatEventManagement', 'never') == 'always') {
$cmd->event($_value, $_updateTime);
return true;
}
if ($_updateTime !== false) {
$cmd->setCache('collectDate', date('Y-m-d H:i:s'));
$this->setStatus(array('lastCommunication' => date('Y-m-d H:i:s'), 'timeout' => 0));
}
return false;
}
public function copy($_name) {
$eqLogicCopy = clone $this;
$eqLogicCopy->setName($_name);
$eqLogicCopy->setId('');
$eqLogicCopy->save();
foreach (($eqLogicCopy->getCmd()) as $cmd) {
$cmd->remove();
}
$cmd_link = array();
foreach (($this->getCmd()) as $cmd) {
$cmdCopy = clone $cmd;
$cmdCopy->setId('');
$cmdCopy->setEqLogic_id($eqLogicCopy->getId());
$cmdCopy->save();
$cmd_link[$cmd->getId()] = $cmdCopy;
}
foreach (($this->getCmd()) as $cmd) {
if (!isset($cmd_link[$cmd->getId()])) {
continue;
}
if ($cmd->getValue() != '' && isset($cmd_link[$cmd->getValue()])) {
$cmd_link[$cmd->getId()]->setValue($cmd_link[$cmd->getValue()]->getId());
$cmd_link[$cmd->getId()]->save();
}
}
return $eqLogicCopy;
}
public function getTableName() {
return 'eqLogic';
}
public function hasOnlyEventOnlyCmd() {
return true;
}
public function preToHtml($_version = 'dashboard', $_default = array(), $_noCache = false) {
global $JEEDOM_INTERNAL_CONFIG;
if ($_version == '') {
throw new Exception(__('La version demandée ne peut pas être vide (mobile, dashboard ou scénario)', __FILE__));
}
if (!$this->hasRight('r') || !$this->getIsEnable()) {
return '';
}
if (!$_noCache && config::byKey('widget::disableCache', 'core', 0) == 0) {
$mc = cache::byKey('widgetHtml' . $this->getId() . $_version);
if (trim($mc->getValue()) != '') {
return preg_replace("/" . preg_quote(self::UIDDELIMITER) . "(.*?)" . preg_quote(self::UIDDELIMITER) . "/", self::UIDDELIMITER . mt_rand() . self::UIDDELIMITER, $mc->getValue());
}
}
$translate_category = '';
foreach ($JEEDOM_INTERNAL_CONFIG['eqLogic']['category'] as $key => $value) {
if ($this->getCategory($key, 0) == 1) {
$translate_category .= $value['name'] . ',';
}
}
$translate_category = trim($translate_category, ',');
$name_display = $this->getName();
$uid = 'eqLogic' . $this->getId() . self::UIDDELIMITER . mt_rand() . self::UIDDELIMITER;
$replace = array(
'#id#' => $this->getId(),
'#name#' => $this->getName(),
'#name_display#' => $name_display,
'#eqLink#' => $this->getLinkToConfiguration(),
'#category#' => $this->getPrimaryCategory(),
'#translate_category#' => $translate_category,
'#style#' => '',
'#logicalId#' => $this->getLogicalId(),
'#object_name#' => (is_object($this->getObject())) ? $this->getObject()->getName() : __('Aucun', __FILE__),
'#height#' => $this->getDisplay('height', 'auto'),
'#width#' => $this->getDisplay('width', 'auto'),
'#uid#' => $uid,
'#refresh_id#' => '',
'#version#' => $_version,
'#alert_name#' => '',
'#alert_icon#' => '',
'#eqType#' => $this->getEqType_name(),
'#custom_layout#' => ($this->widgetPossibility('custom::layout')) ? 'allowLayout' : '',
'#tags#' => $this->getTags(),
'#generic_type#' => $this->getGenericType(),
'#isVerticalAlign#' => (config::byKey('interface::advance::vertCentering', 'core', 0) == 1) ? 'verticalAlign' : '',
'#class#' => '',
'#divGraphInfo#' => '',
'#panelLink#' => '',
);
$_version = jeedom::versionAlias($_version);
if ($this->getConfiguration('panelLink') != '') {
if ($_version == 'dashboard') {
$replace['#panelLink#'] = 'index.php?v=d&m=' . $this->getEqType_name() . '&p=' . $this->getConfiguration('panelLink');
} else {
$replace['#panelLink#'] = 'index.php?v=m&m=' . $this->getEqType_name() . '&p=' . $this->getConfiguration('panelLink');
}
}
//Automatic width when first displayed:
if ($replace['#width#'] == 'auto') {
$replace['#width#'] = '230px';
}
if ($this->getAlert() != '') {
$alert = $this->getAlert();
$replace['#alert_name#'] = $alert['name'];
$replace['#alert_icon#'] = $alert['icon'];
$replace['#background-color#'] = $alert['color'];
}
$refresh_cmd = $this->getCmd('action', 'refresh');
if (!is_object($refresh_cmd)) {
foreach ($this->getCmd('action') as $cmd) {
if ($cmd->getConfiguration('isRefreshCmd') == 1) {
$refresh_cmd = $cmd;
break;
}
}
}
if (is_object($refresh_cmd) && $refresh_cmd->getIsVisible() == 1) {
$replace['#refresh_id#'] = $refresh_cmd->getId();
}
//Custom parameter css class:
if (is_array($this->getDisplay('parameters')) && count($this->getDisplay('parameters')) > 0) {
foreach ($this->getDisplay('parameters') as $key => $value) {
if ($key == $_version . '_class') {
$replace['#class#'] = $value;
continue;
}
$replace['#' . $key . '#'] = $value;
}
}
$replace['#style#'] = trim($replace['#style#'], ';');
if (is_array($this->widgetPossibility('parameters')) && count($this->widgetPossibility('parameters')) > 0) {
foreach ($this->widgetPossibility('parameters') as $pKey => $parameter) {
if (!isset($parameter['allow_displayType']) || !isset($parameter['type'])) {
continue;
}
if (is_array($parameter['allow_displayType']) && !in_array($_version, $parameter['allow_displayType'])) {
continue;
}
if ($parameter['allow_displayType'] === false) {
continue;
}
$default = '';
if (isset($parameter['default'])) {
$default = $parameter['default'];
}
if ($this->getDisplay('advanceWidgetParameter' . $pKey . $_version . '-default', 1) == 1) {
$replace['#' . $pKey . '#'] = $default;
continue;
}
switch ($parameter['type']) {
case 'color':
if ($this->getDisplay('advanceWidgetParameter' . $pKey . $_version . '-transparent', 0) == 1) {
$replace['#' . $pKey . '#'] = 'transparent';
} else {
$replace['#' . $pKey . '#'] = $this->getDisplay('advanceWidgetParameter' . $pKey . $_version, $default);
}
break;
default:
$replace['#' . $pKey . '#'] = $this->getDisplay('advanceWidgetParameter' . $pKey . $_version, $default);
break;
}
}
}
//History graph:
if ($this->getDisplay('backGraph::info', 0) != 0 && is_object(cmd::byId($this->getDisplay('backGraph::info')))) {
$doNotHighlightGraphCmd = (config::byKey('interface::advance::doNotHighlightGraphCmd') == 1) ? 'true' : 'false';
$replace['#divGraphInfo#'] = '<div class="eqlogicbackgraph" data-cmdid="' . $this->getDisplay('backGraph::info') . '" data-format="' . $this->getDisplay('backGraph::format', 'day') . '" data-type="' . $this->getDisplay('backGraph::type', 'areaspline') . '" data-color="' . $this->getDisplay('backGraph::color', '#4572A7') . '"></div><script>jeedom.eqLogic.initGraphInfo("' . $uid . '", ' . $doNotHighlightGraphCmd . ')</script>';
$height = $this->getDisplay('backGraph::height', '0');
if ($height != '0') {
//$replace['#isVerticalAlign#'] = 0;
$replace['#divGraphInfo#'] = str_replace('data-cmdid=', 'style="height:' . $height . 'px;" data-cmdid=', $replace['#divGraphInfo#']);
$replace['#divGraphInfo#'] = str_replace('eqlogicbackgraph', 'eqlogicbackgraph fixedbackgraph', $replace['#divGraphInfo#']);
}
}
return $replace;
}
public function toHtml($_version = 'dashboard') {
$replace = $this->preToHtml($_version);
if (!is_array($replace)) {
return $replace;
}
$_version = jeedom::versionAlias($_version);
$replace['#calledFrom#'] = __CLASS__;
switch ($this->getDisplay('layout::' . $_version)) {
case 'table':
$replace['#eqLogic_class#'] = 'eqLogic_layout_table';
$table = self::generateHtmlTable($this->getDisplay('layout::' . $_version . '::table::nbLine', 1), $this->getDisplay('layout::' . $_version . '::table::nbColumn', 1), $this->getDisplay('layout::' . $_version . '::table::parameters'));
foreach ($this->getCmd(null, null, true) as $cmd) {
if (isset($replace['#refresh_id#']) && $cmd->getId() == $replace['#refresh_id#']) {
continue;
}
$tag = '#cmd::' . $this->getDisplay('layout::' . $_version . '::table::cmd::' . $cmd->getId() . '::line', 1) . '::' . $this->getDisplay('layout::' . $_version . '::table::cmd::' . $cmd->getId() . '::column', 1) . '#';
if ($cmd->getDisplay('forceReturnLineBefore', 0) == 1) {
$table['tag'][$tag] .= '<div class="break"></div>';
}
$table['tag'][$tag] .= $cmd->toHtml($_version, '');
if ($cmd->getDisplay('forceReturnLineAfter', 0) == 1) {
$table['tag'][$tag] .= '<div class="break"></div>';
}
}
$replace['#cmd#'] = template_replace($table['tag'], $table['html']);
break;
default:
$replace['#eqLogic_class#'] = 'eqLogic_layout_default';
$cmd_html = '';
foreach ($this->getCmd(null, null, true) as $cmd) {
if (isset($replace['#refresh_id#']) && $cmd->getId() == $replace['#refresh_id#']) {
continue;
}
if ($_version == 'dashboard' && $cmd->getDisplay('forceReturnLineBefore', 0) == 1) {
$cmd_html .= '<div class="break"></div>';
}
$cmd_html .= $cmd->toHtml($_version, '');
if ($_version == 'dashboard' && $cmd->getDisplay('forceReturnLineAfter', 0) == 1) {
$cmd_html .= '<div class="break"></div>';
}
}
$replace['#cmd#'] = $cmd_html;
break;
}
if (!isset(self::$_templateArray[$_version])) {
self::$_templateArray[$_version] = getTemplate('core', $_version, 'eqLogic');
}
return $this->postToHtml($_version, template_replace($replace, self::$_templateArray[$_version]));
}
public function postToHtml($_version, $_html) {
if (config::byKey('widget::disableCache', 'core', 0) == 0) {
cache::set('widgetHtml' . $this->getId() . $_version, $_html);
}
return $_html;
}
public function emptyCacheWidget() {
if (config::byKey('widget::disableCache', 'core', 0) == 0) {
$mc = cache::byKey('widgetHtml' . $this->getId() . 'mobile');
$mc->remove();
$mc = cache::byKey('widgetHtml' . $this->getId() . 'dashboard');
$mc->remove();
}
}
public function getAlert() {
global $JEEDOM_INTERNAL_CONFIG;
$hasAlert = '';
$maxLevel = 0;
foreach ($JEEDOM_INTERNAL_CONFIG['alerts'] as $key => $data) {
if ($this->getStatus($key, 0) != 0 && $JEEDOM_INTERNAL_CONFIG['alerts'][$key]['level'] > $maxLevel) {
$hasAlert = $data;
$maxLevel = $JEEDOM_INTERNAL_CONFIG['alerts'][$key]['level'];
}
}
return $hasAlert;
}
public function getMaxCmdAlert() {
$return = 'none';
$max = 0;
global $JEEDOM_INTERNAL_CONFIG;
foreach ($this->getCmd('info') as $cmd) {
$cmdLevel = $cmd->getCache('alertLevel');
if (!isset($JEEDOM_INTERNAL_CONFIG['alerts'][$cmdLevel])) {
continue;
}
if ($JEEDOM_INTERNAL_CONFIG['alerts'][$cmdLevel]['level'] > $max) {
$return = $cmdLevel;
$max = $JEEDOM_INTERNAL_CONFIG['alerts'][$cmdLevel]['level'];
}
}
return $return;
}
public function getShowOnChild() {
return false;
}
public function remove() {
foreach (($this->getCmd()) as $cmd) {
$cmd->remove();
}
viewData::removeByTypeLinkId('eqLogic', $this->getId());
dataStore::removeByTypeLinkId('eqLogic', $this->getId());
$this->emptyCacheWidget();
cache::delete('eqLogicCacheAttr' . $this->getId());
cache::delete('eqLogicStatusAttr' . $this->getId());
jeedom::addRemoveHistory(array('id' => $this->getId(), 'name' => $this->getHumanName(), 'date' => date('Y-m-d H:i:s'), 'type' => 'eqLogic'));
return DB::remove($this);
}
public function save($_direct = false) {