-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.php
2492 lines (2343 loc) · 89 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
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
declare(strict_types=1);
/**
* @package Squeezebox
* @file module.php
* @author Michael Tröger <micha@nall-chan.net>
* @copyright 2024 Michael Tröger
* @license https://creativecommons.org/licenses/by-nc-sa/4.0/ CC BY-NC-SA 4.0
* @version 4.00
*
*/
require_once __DIR__ . '/../libs/DebugHelper.php'; // diverse Klassen
require_once __DIR__ . '/../libs/SqueezeBoxClass.php'; // diverse Klassen
eval('declare(strict_types=1);namespace LMSSplitter {?>' . file_get_contents(__DIR__ . '/../libs/helper/BufferHelper.php') . '}');
eval('declare(strict_types=1);namespace LMSSplitter {?>' . file_get_contents(__DIR__ . '/../libs/helper/ParentIOHelper.php') . '}');
eval('declare(strict_types=1);namespace LMSSplitter {?>' . file_get_contents(__DIR__ . '/../libs/helper/SemaphoreHelper.php') . '}');
eval('declare(strict_types=1);namespace LMSSplitter {?>' . file_get_contents(__DIR__ . '/../libs/helper/VariableHelper.php') . '}');
eval('declare(strict_types=1);namespace LMSSplitter {?>' . file_get_contents(__DIR__ . '/../libs/helper/VariableProfileHelper.php') . '}');
eval('declare(strict_types=1);namespace LMSSplitter {?>' . file_get_contents(__DIR__ . '/../libs/helper/WebhookHelper.php') . '}');
/**
* LMSSplitter Klasse für die Kommunikation mit dem Logitech Media-Server (LMS).
* Erweitert IPSModule.
*
* @todo Favoriten als Tabelle oder Baum ?! für das WF
*
* @author Michael Tröger <micha@nall-chan.net>
* @copyright 2024 Michael Tröger
* @license https://creativecommons.org/licenses/by-nc-sa/4.0/ CC BY-NC-SA 4.0
*
* @version 4.00
*
* @property array $ReplyLMSData \SqueezeBox\LMSData Enthält die versendeten Befehle und speichert die Antworten.
* @property string $Buffer EmpfangsBuffer
* @property string $Host Adresse des LMS (aus IO-Parent ausgelesen)
* @property int $ParentID Die InstanzeID des IO-Parent
* @property array $Multi_Playlists Alle Datensätze der Playlisten
* @property int $ScannerID VariablenID des Scanner State
* @property string $WebHookSecretPlaylist
* @property resource|false $Socket
*
* @method bool lock(string $ident)
* @method void unlock(string $ident)
* @method void RegisterHook(string $WebHook)
* @method void UnregisterHook(string $WebHook)
* @method void SetValueBoolean(string $Ident, bool $value)
* @method void SetValueFloat(string $Ident, float $value)
* @method void SetValueInteger(string $Ident, int $value)
* @method void SetValueString(string $Ident, string $value)
* @method void RegisterProfileIntegerEx(string $Name, string $Icon, string $Prefix, string $Suffix, array $Associations, int $MaxValue = -1, float $StepSize = 0)
* @method void UnregisterProfile(string $Name)
* @method int FindIDForIdent(string $Ident)
*/
class LMSSplitter extends IPSModule
{
use \SqueezeBox\LMSHTMLTable,
\SqueezeBox\LMSSongURL,
\SqueezeBox\LMSProfile,
\SqueezeBox\DebugHelper,
\LMSSplitter\VariableProfileHelper,
\LMSSplitter\VariableHelper,
\LMSSplitter\BufferHelper,
\LMSSplitter\InstanceStatus,
\LMSSplitter\Semaphore,
\LMSSplitter\WebhookHelper {
\LMSSplitter\InstanceStatus::MessageSink as IOMessageSink; // MessageSink gibt es sowohl hier in der Klasse, als auch im Trait InstanceStatus. Hier wird für die Methode im Trait ein Alias benannt.
\LMSSplitter\InstanceStatus::RegisterParent as IORegisterParent;
\LMSSplitter\InstanceStatus::RequestAction as IORequestAction;
}
/**
* Socket
*
* @var resource
*/
private $Socket = false;
/**
* __destruct
*
* @return void
*/
public function __destruct()
{
if ($this->Socket) {
fclose($this->Socket);
}
}
/**
* Create
*
* @return void
*/
public function Create()
{
parent::Create();
$this->RequireParent('{3CFF0FD9-E306-41DB-9B5A-9D06D38576C3}');
$this->RegisterPropertyString('User', '');
$this->RegisterPropertyString('Password', '');
$this->RegisterPropertyInteger('Port', 9090);
$this->RegisterPropertyInteger('Webport', 9000);
$this->RegisterPropertyBoolean('showHTMLPlaylist', false);
$Style = $this->GenerateHTMLStyleProperty();
$this->RegisterPropertyString('Table', json_encode($Style['Table']));
$this->RegisterPropertyString('Columns', json_encode($Style['Columns']));
$this->RegisterPropertyString('Rows', json_encode($Style['Rows']));
$this->RegisterTimer('KeepAlive', 0, 'LMS_KeepAlive($_IPS["TARGET"]);');
if (IPS_GetKernelRunlevel() != KR_READY) {
$this->RegisterMessage(0, IPS_KERNELSTARTED);
}
$this->ReplyLMSData = [];
$this->Buffer = '';
$this->Multi_Playlists = [];
$this->Host = '';
$this->ParentID = 0;
$this->ScannerID = 0;
}
/**
* Destroy
*
* @return void
*/
public function Destroy()
{
if (!IPS_InstanceExists($this->InstanceID)) {
$this->UnregisterHook('/hook/LMSPlaylist' . $this->InstanceID);
$this->DeleteProfile();
}
parent::Destroy();
}
/**
* Migrate
* @param string $JSONData
* @return string
*/
public function Migrate($JSONData)
{
$Data = json_decode($JSONData);
if (property_exists($Data->configuration, 'showPlaylist')) {
$Data->configuration->showHTMLPlaylist = $Data->configuration->showPlaylist;
/**
* @todo Migrate Statusvariables Types an Profiles?
*/
$vid = $this->FindIDForIdent('Playlists');
if ($vid > 0) { //Migrate Statusvariable Playlist to HTMLPlaylist
@IPS_SetIdent($vid, 'HTMLPlaylists');
}
$this->SendDebug('Migrate', json_encode($Data), 0);
$this->LogMessage('Migrated settings:' . json_encode($Data), KL_MESSAGE);
}
return json_encode($Data);
}
/**
* ApplyChanges
*
* @return void
*/
public function ApplyChanges()
{
$this->RegisterMessage($this->InstanceID, FM_CONNECT);
$this->RegisterMessage($this->InstanceID, FM_DISCONNECT);
$this->ReplyLMSData = [];
$this->Buffer = '';
$this->Multi_Playlists = [];
$this->Host = '';
$this->ParentID = 0;
$this->ScannerID = 0;
parent::ApplyChanges();
// Buffer leeren
$this->ReplyLMSData = [];
$this->Buffer = '';
// Eigene Profile
$this->CreateProfile();
// Eigene Variablen
$this->RegisterVariableString('Version', 'Version', '', 0);
$this->RegisterVariableInteger('RescanState', 'Scanner', 'LMS.Scanner', 1);
$this->ScannerID = $this->FindIDForIdent('RescanState');
$this->RegisterMessage($this->ScannerID, VM_UPDATE);
$this->EnableAction('RescanState');
$this->RegisterVariableString('RescanInfo', $this->Translate('Rescan state'), '', 2);
$this->RegisterVariableString('RescanProgress', $this->Translate('Rescan progress'), '', 3);
$this->RegisterVariableInteger('Players', 'Number of players', '', 4);
// ServerPlaylisten
$PlaylistActive = false;
if ($this->ReadPropertyBoolean('showHTMLPlaylist')) {
$this->RegisterVariableString('HTMLPlaylists', $this->Translate('Playlists'), '~HTMLBox', 6);
$PlaylistActive = true;
} else {
$this->UnregisterVariable('HTMLPlaylists');
}
if ($PlaylistActive) {
$this->RegisterProfileIntegerEx('LMS.PlayerSelect.' . $this->InstanceID, 'Speaker', '', '', []);
$this->RegisterVariableInteger('PlayerSelect', $this->Translate('select player'), 'LMS.PlayerSelect.' . $this->InstanceID, 5);
$this->EnableAction('PlayerSelect');
$this->RegisterMessage($this->InstanceID, FM_CHILDADDED);
$this->RegisterMessage($this->InstanceID, FM_CHILDREMOVED);
} else {
$this->UnregisterVariable('PlayerSelect');
$this->UnregisterProfile('LMS.PlayerSelect.' . $this->InstanceID);
$this->UnregisterMessage($this->InstanceID, FM_CHILDADDED);
$this->UnregisterMessage($this->InstanceID, FM_CHILDREMOVED);
}
// Wenn Kernel nicht bereit, dann warten... KR_READY kommt ja gleich
if (IPS_GetKernelRunlevel() != KR_READY) {
return;
}
// ServerPlaylisten
if ($this->ReadPropertyBoolean('showHTMLPlaylist')) {
$this->RegisterHook('/hook/LMSPlaylist' . $this->InstanceID);
} else {
$this->UnregisterHook('/hook/LMSPlaylist' . $this->InstanceID);
}
// Config prüfen
$this->RegisterParent();
// Wenn Parent aktiv, dann Anmeldung an der Hardware bzw. Datenabgleich starten
if ($this->ParentID > 0) {
$this->IOChangeState(IS_ACTIVE);
}
}
/**
* MessageSink
*
* @param int $TimeStamp
* @param int $SenderID
* @param int $Message
* @param array $Data
* @return void
*/
public function MessageSink($TimeStamp, $SenderID, $Message, $Data)
{
$this->IOMessageSink($TimeStamp, $SenderID, $Message, $Data);
switch ($Message) {
case IPS_KERNELSTARTED:
$this->KernelReady();
break;
case VM_UPDATE:
if (($SenderID == $this->ScannerID) && ($Data[0] == 0)) {
$this->RefreshPlaylistBuffer();
}
break;
case FM_CHILDADDED:
case FM_CHILDREMOVED:
IPS_RunScriptText('usleep(100000);IPS_RequestAction(' . $this->InstanceID . ',\'RefreshPlayerList\',0);');
break;
}
}
/**
* GetConfigurationForParent
*
* @return string
*/
public function GetConfigurationForParent()
{
$Config['Port'] = $this->ReadPropertyInteger('Port');
$Config['UseSSL'] = false;
return json_encode($Config);
}
/**
* GetConfigurationForm
*
* @return string
*/
public function GetConfigurationForm()
{
$Form = json_decode(file_get_contents(__DIR__ . '/form.json'), true);
if ($this->GetStatus() == IS_CREATING) {
return json_encode($Form);
}
$Form['elements'][1]['objectID'] = $this->ParentID;
$Form['elements'][1]['enabled'] = ($this->ParentID > 1);
$this->SendDebug('FORM', json_encode($Form), 0);
$this->SendDebug('FORM', json_last_error_msg(), 0);
return json_encode($Form);
}
//################# Action
/**
* RequestAction
* Actionhandler der Statusvariablen. Interne SDK-Funktion.
*
* @param string $Ident Der Ident der Statusvariable.
* @param bool|float|int|string $Value Der angeforderte neue Wert.
*/
public function RequestAction($Ident, $Value)
{
if ($this->IORequestAction($Ident, $Value)) {
return;
}
switch ($Ident) {
case 'PlayerSelect':
$ProfilName = 'LMS.PlayerSelect.' . $this->InstanceID;
$Assoziations = IPS_GetVariableProfile($ProfilName)['Associations'];
switch ($Value) {
case 0: //keiner
case 100: //alle
$this->SetValueInteger('PlayerSelect', $Value);
for ($i = 2; $i < count($Assoziations); $i++) {
IPS_SetVariableProfileAssociation($ProfilName, $Assoziations[$i]['Value'], $Assoziations[$i]['Name'], $Assoziations[$i]['Icon'], ($Value == 0) ? -1 : 0x00ffff);
}
break;
default:
$All = true;
$None = true;
foreach ($Assoziations as $Assoziation) {
if ($Value == $Assoziation['Value']) {
$Assoziation['Color'] = ($Assoziation['Color'] == -1) ? 0x00ffff : -1;
IPS_SetVariableProfileAssociation($ProfilName, $Assoziation['Value'], $Assoziation['Name'], $Assoziation['Icon'], $Assoziation['Color']);
}
if ($Assoziation['Color'] == -1) {
$All = false;
}
if ($Assoziation['Color'] == 0x00ffff) {
$None = false;
}
}
if ($None) {
$this->SetValueInteger('PlayerSelect', 0);
} elseif ($All) {
$this->SetValueInteger('PlayerSelect', 100);
} else {
$this->SetValueInteger('PlayerSelect', -1);
}
break;
}
break;
case 'RescanState':
if ($Value == 1) {
$ret = $this->AbortScan();
} elseif ($Value == 2) {
$ret = $this->Rescan();
} elseif ($Value == 3) {
$ret = $this->RescanPlaylists();
} elseif ($Value == 4) {
$ret = $this->WipeCache();
}
if (($Value != 0) && (($ret === null) || ($ret === false))) {
echo $this->Translate('Error on send scanner-command');
return;
}
if ($this->GetValue('RescanState') != $Value) {
$this->SetValueInteger('RescanState', $Value);
}
break;
case 'RefreshPlayerList':
$this->RefreshPlayerList();
break;
case 'showHTMLPlaylist':
$this->UpdateFormField('Table', 'enabled', (bool) $Value);
$this->UpdateFormField('Columns', 'enabled', (bool) $Value);
$this->UpdateFormField('Rows', 'enabled', (bool) $Value);
$this->UpdateFormField('HTMLExpansionPanel', 'expanded', (bool) $Value);
return;
default:
echo $this->Translate('Invalid Ident');
break;
}
}
//################# PUBLIC
/**
* KeepAlive
* IPS-Instanz-Funktion 'LMS_KeepAlive'.
* Sendet einen listen Abfrage an den LMS um die Kommunikation zu erhalten.
*
* @return bool true wenn LMS erreichbar, sonst false.
*/
public function KeepAlive(): bool
{
$Data = new \SqueezeBox\LMSData('listen', '1');
$ret = @$this->Send($Data);
if ($ret === null) {
set_error_handler([$this, 'ModulErrorHandler']);
trigger_error($this->Translate('Error on keepalive to LMS.'), E_USER_NOTICE);
restore_error_handler();
return false;
}
if ($ret->Data[0] == '1') {
return true;
}
set_error_handler([$this, 'ModulErrorHandler']);
trigger_error($this->Translate('Error on keepalive to LMS.'), E_USER_NOTICE);
restore_error_handler();
return false;
}
/**
* SendSpecial
* IPS-Instanz-Funktion 'LMS_SendSpecial'.
* Sendet einen Anfrage an den LMS.
*
* @param string $Command Das zu sendende Kommando.
* @param string $Value Die zu sendenden Werte als JSON String.
* @return array|bool Antwort des LMS als Array, false im Fehlerfall.
*/
public function SendSpecial(string $Command, string $Value): string|array
{
$Data = json_decode($Value, true);
if ($Data === null) {
set_error_handler([$this, 'ModulErrorHandler']);
trigger_error($this->Translate('Value ist not valid JSON.'), E_USER_NOTICE);
restore_error_handler();
return false;
}
$LMSData = new \SqueezeBox\LMSData($Command, $Data);
$ret = $this->SendDirect($LMSData);
return $ret->Data;
}
/**
* RestartServer
* IPS-Instanz-Funktion 'LMS_RestartServer'.
*
* @return bool
*/
public function RestartServer(): bool
{
return $this->Send(new \SqueezeBox\LMSData('restartserver')) != null;
}
/**
* RequestState
* IPS-Instanz-Funktion 'LMS_RequestState'.
* Fragt einen Wert des LMS ab. Es ist der Ident der Statusvariable zu übergeben.
*
* @param string $Ident Der Ident der abzufragenden Statusvariable.
* @return bool True wenn erfolgreich.
*/
public function RequestState(string $Ident): bool
{
if ($Ident == '') {
set_error_handler([$this, 'ModulErrorHandler']);
trigger_error($this->Translate('Invalid Ident'));
restore_error_handler();
return false;
}
switch ($Ident) {
case 'Players':
$LMSResponse = new \SqueezeBox\LMSData(['player', 'count'], '?');
break;
case 'Version':
$LMSResponse = new \SqueezeBox\LMSData('version', '?');
break;
case 'Playlists':
return $this->RefreshPlaylistBuffer();
default:
set_error_handler([$this, 'ModulErrorHandler']);
trigger_error($this->Translate('Invalid Ident'));
restore_error_handler();
return false;
}
$LMSResponse = $this->Send($LMSResponse);
if ($LMSResponse === null) {
return false;
}
return $this->DecodeLMSResponse($LMSResponse);
}
/**
* GetAudioDirs
* IPS-Instanz-Funktion 'LMS_GetAudioDirs'.
*
* @return false|array
*/
public function GetAudioDirs(): false|array
{
$LMSData = $this->SendDirect(new \SqueezeBox\LMSData(['pref', 'mediadirs'], '?'));
if ($LMSData === null) {
return false;
}
return $LMSData->Data;
}
/**
* GetPlaylistDir
* IPS-Instanz-Funktion 'LMS_GetPlaylistDir'.
*
* @return array
*/
public function GetPlaylistDir(): false|string
{
$LMSData = $this->SendDirect(new \SqueezeBox\LMSData(['pref', 'playlistdir'], '?'));
if ($LMSData === null) {
return false;
}
return $LMSData->Data[0];
}
///////////////////////////////////////////////////////////////
// START TODO
///////////////////////////////////////////////////////////////
/**
* GetSyncGroups
* IPS-Instanz-Funktion 'LMS_GetSyncGroups'.
* Liefer ein Array welches die Gruppen mit ihren jeweiligen IPS-InstanzeIDs enthält.
*
* @return false|array Array welches so viele Elemente wie Gruppen enthält.
*/
public function GetSyncGroups(): false|array
{
$LMSData = $this->SendDirect(new \SqueezeBox\LMSData('syncgroups', '?'));
if ($LMSData == null) {
return false;
}
if (count($LMSData->Data) == 0) {
return [];
}
$AllPlayerIDs = IPS_GetInstanceListByModuleID('{118189F9-DC7E-4DF4-80E1-9A4DF0882DD7}');
$Addresses = [];
$ret = [];
foreach ($AllPlayerIDs as $DeviceID) {
$Addresses[$DeviceID] = IPS_GetProperty($DeviceID, 'Address');
}
$Data = array_chunk($LMSData->Data, 2);
foreach ($Data as $Group) {
$FoundInstanzIDs = [];
$Search = explode(',', (new \SqueezeBox\LMSTaggingData($Group[0]))->Value);
foreach ($Search as $Value) {
if (array_search($Value, $Addresses) !== false) {
$FoundInstanzIDs[] = array_search($Value, $Addresses);
}
}
if (count($FoundInstanzIDs) > 0) {
$ret[] = $FoundInstanzIDs;
}
}
return $ret;
}
///////////////////////////////////////////////////////////////
// ENDE TODO
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
// START PLAYERINFO
///////////////////////////////////////////////////////////////
/**
* GetPlayerInfo
* IPS-Instanz-Funktion 'LMS_GetPlayerInfo'.
*
* @param int $Index Der Index des Player.
* @example <code>
* $ret = LMS_GetPlayerInfo(37340 \/*[LMSSplitter]*\/,6);
* var_dump($ret);
* </code>
* @return false|array Ein assoziiertes Array mit den Daten des Players.
*/
public function GetPlayerInfo(int $Index): false|array
{
$LMSData = $this->SendDirect(new \SqueezeBox\LMSData('players', [(string) $Index, '1']));
if ($LMSData === null) {
return false;
}
$LMSData->SliceData();
$ret = (new \SqueezeBox\LMSTaggingArray($LMSData->Data))->DataArray();
if (!isset($ret['Playerid'])) {
set_error_handler([$this, 'ModulErrorHandler']);
trigger_error($this->Translate('Invalid index'), E_USER_NOTICE);
restore_error_handler();
return false;
}
$DevicesIDs = IPS_GetInstanceListByModuleID('{118189F9-DC7E-4DF4-80E1-9A4DF0882DD7}');
$FoundId = 0;
foreach ($DevicesIDs as $Device) {
if (IPS_GetProperty($Device, 'Address') == $ret['Playerid']) {
$FoundId = $Device;
}
}
$ret['Instanceid'] = $FoundId;
unset($ret['Count']);
return $ret;
}
///////////////////////////////////////////////////////////////
// START DATABASE
///////////////////////////////////////////////////////////////
/**
* Rescan
* IPS-Instanz-Funktion 'LMS_Rescan'.
* Startet einen rescan der Datenbank.
*
* @return bool True wenn erfolgreich.
*/
public function Rescan(): bool
{
return $this->Send(new \SqueezeBox\LMSData('rescan')) != null;
}
/**
* RescanPlaylists
* IPS-Instanz-Funktion 'LMS_RescanPlaylists'.
* Startet einen rescan der Datenbank für die Playlists.
*
* @return bool True wenn erfolgreich.
*/
public function RescanPlaylists(): bool
{
return $this->Send(new \SqueezeBox\LMSData('rescan', 'playlists')) != null;
}
/**
* WipeCache
* IPS-Instanz-Funktion 'LMS_WipeCache'.
* Löscht den Cache der DB.
*
* @return bool True wenn erfolgreich.
*/
public function WipeCache(): bool
{
return $this->Send(new \SqueezeBox\LMSData('wipecache')) != null;
}
/**
* AbortScan
* IPS-Instanz-Funktion 'LMS_AbortScan'.
* Bricht einen Scan der Datenbank ab.
*
* @return bool True wenn erfolgreich.
*/
public function AbortScan(): bool
{
return $this->Send(new \SqueezeBox\LMSData('abortscan')) != null;
}
/**
* GetLibraryInfo
* IPS-Instanz-Funktion 'LMS_GetLibraryInfo'.
*
* @return false|array
*/
public function GetLibraryInfo(): false|array
{
$genres = $this->Send(new \SqueezeBox\LMSData(['info', 'total', 'genres'], '?'));
if ($genres === null) {
return false;
}
$artists = $this->Send(new \SqueezeBox\LMSData(['info', 'total', 'artists'], '?'));
if ($artists === null) {
return false;
}
$albums = $this->Send(new \SqueezeBox\LMSData(['info', 'total', 'albums'], '?'));
if ($albums === null) {
return false;
}
$songs = $this->Send(new \SqueezeBox\LMSData(['info', 'total', 'songs'], '?'));
if ($songs === null) {
return false;
}
$duration = $this->Send(new \SqueezeBox\LMSData(['info', 'total', 'duration'], '?'));
if ($duration === null) {
return false;
}
$ret = [
'Genres' => (int) $genres->Data[0],
'Artists' => (int) $artists->Data[0],
'Albums' => (int) $albums->Data[0],
'Songs' => (int) $songs->Data[0],
'Duration' => (int) $duration->Data[0]
];
return $ret;
}
/**
* GetGenres
* IPS-Instanz-Funktion 'LMS_GetGenres'.
*
* @return false|array
*/
public function GetGenres(): false|array
{
return $this->GetGenresEx('');
}
/**
* GetGenresEx
* IPS-Instanz-Funktion 'LMS_GetGenresEx'.
*
* @param string $Search Suchstring
* @return false|array
*/
public function GetGenresEx(string $Search): false|array
{
$Data = [0, 100000];
if ($Search != '') {
$Data[] = 'search:' . urlencode($Search);
}
$LMSData = $this->SendDirect(new \SqueezeBox\LMSData('genres', $Data));
if ($LMSData === null) {
return false;
}
$LMSData->SliceData();
$Genres = new \SqueezeBox\LMSTaggingArray($LMSData->Data);
$Genres->Compact('Genre');
return $Genres->DataArray();
}
/**
* GetArtists
* IPS-Instanz-Funktion 'LMS_GetArtists'.
*
* @return false|array
*/
public function GetArtists(): false|array
{
return $this->GetArtistsEx('');
}
/**
* GetArtistsEx
* IPS-Instanz-Funktion 'LMS_GetArtistsEx'.
*
* @param string $Search Suchstring
* @return false|array
*/
public function GetArtistsEx(string $Search): false|array
{
$Data = [0, 100000];
if ($Search != '') {
$Data[] = 'search:' . urlencode($Search);
}
$LMSData = $this->SendDirect(new \SqueezeBox\LMSData('artists', $Data));
if ($LMSData === null) {
return false;
}
$LMSData->SliceData();
$Artists = new \SqueezeBox\LMSTaggingArray($LMSData->Data);
$Artists->Compact('Artist');
return $Artists->DataArray();
}
/**
* GetAlbums
* IPS-Instanz-Funktion 'LMS_GetAlbums'.
*
* @return false|array
*/
public function GetAlbums(): false|array
{
return $this->GetAlbumsEx('');
}
/**
* GetAlbumsEx
* IPS-Instanz-Funktion 'LMS_GetAlbumsEx'.
*
* @param string $Search Suchstring
* @return false|array
*/
public function GetAlbumsEx(string $Search): false|array
{
$Data = [0, 100000];
if ($Search != '') {
$Data[] = 'search:' . urlencode($Search);
}
$LMSData = $this->SendDirect(new \SqueezeBox\LMSData('albums', $Data));
if ($LMSData === null) {
return false;
}
$LMSData->SliceData();
$Albums = new \SqueezeBox\LMSTaggingArray($LMSData->Data);
$Albums->Compact('Album');
return $Albums->DataArray();
}
/**
* GetDirectoryByID
* IPS-Instanz-Funktion 'LMS_GetDirectoryByID'. Liefert Informationen zu einem Verzeichnis.
*
* @param int $FolderID ID des Verzeichnis welches durchsucht werden soll. 0= root
* @return false|array Array mit den Quellen oder false bei Fehler.
*/
public function GetDirectoryByID(int $FolderID): false|array
{
if ($FolderID == 0) {
$Data = ['0', 100000, 'tags:uc'];
} else {
$Data = ['0', 100000, 'tags:uc', 'folder_id:' . $FolderID];
}
$LMSData = $this->SendDirect(new \SqueezeBox\LMSData('mediafolder', $Data));
if ($LMSData == null) {
return false;
}
$LMSData->SliceData();
return (new \SqueezeBox\LMSTaggingArray($LMSData->Data))->DataArray();
}
/**
* GetDirectoryByIDRecursive
* IPS-Instanz-Funktion 'LMS_GetDirectoryByIDRecursive'. Liefert rekursiv Informationen zu einem Verzeichnis.
*
* @param int $FolderID ID des Verzeichnis welches durchsucht werden soll.
* @return false|array Array mit den Quellen oder false bei Fehler.
*/
public function GetDirectoryByIDRecursive(int $FolderID): false|array
{
if ($FolderID == 0) {
set_error_handler([$this, 'ModulErrorHandler']);
trigger_error($this->Translate('Search root recursive is not supported'), E_USER_NOTICE);
restore_error_handler();
return false;
} else {
$Data = ['0', 100000, 'tags:uc', 'folder_id:' . $FolderID];
}
$LMSData = $this->SendDirect(new \SqueezeBox\LMSData('mediafolder', $Data));
if ($LMSData == null) {
return false;
}
$LMSData->SliceData();
return (new \SqueezeBox\LMSTaggingArray($LMSData->Data))->DataArray();
}
/**
* GetDirectoryByURL
* IPS-Instanz-Funktion 'LMS_GetDirectoryByURL'. Liefert rekursiv Informationen zu einem Verzeichnis.
*
* @param string $Directory URL des Verzeichnis welches durchsucht werden soll.
* @return false|array Array mit den Quellen oder false bei Fehler.
*/
public function GetDirectoryByURL(string $Directory): false|array
{
if ($Directory == '') {
$Data = ['0', 100000, 'tags:uc'];
} else {
$Data = ['0', 100000, 'tags:uc', 'url:' . $Directory];
}
$LMSData = $this->SendDirect(new \SqueezeBox\LMSData('mediafolder', $Data));
if ($LMSData == null) {
return false;
}
$LMSData->SliceData();
return (new \SqueezeBox\LMSTaggingArray($LMSData->Data))->DataArray();
}
/**
* GetDirectoryByURLRecursive
* IPS-Instanz-Funktion 'LMS_GetDirectoryByURLRecursive'. Liefert rekursiv Informationen zu einem Verzeichnis.
*
* @param string $Directory URL des Verzeichnis welches durchsucht werden soll.
* @return false|array Array mit den Quellen oder false bei Fehler.
*/
public function GetDirectoryByURLRecursive(string $Directory): false|array
{
if ($Directory == '') {
$Data = ['0', 100000, 'recursive:1', 'tags:uc'];
} else {
$Data = ['0', 100000, 'recursive:1', 'tags:uc', 'url:' . $Directory];
}
$LMSData = $this->SendDirect(new \SqueezeBox\LMSData('mediafolder', $Data));
if ($LMSData == null) {
return false;
}
$LMSData->SliceData();
return (new \SqueezeBox\LMSTaggingArray($LMSData->Data))->DataArray();
}
/**
* GetPlaylists
* IPS-Instanz-Funktion 'LMS_GetPlaylists'.
* Liefert alle Server Playlisten.
*
* @return false|array Array mit den Server-Playlists. FALSE im Fehlerfall.
*/
public function GetPlaylists(): false|array
{
return $this->GetPlaylistsEx('');
}
/**
* GetPlaylistsEx
* IPS-Instanz-Funktion 'LMS_GetPlaylistsEx'.
* Liefert alle Server Playlisten.
*
* @param string $Search Such-String
* @return false|array Array mit den Server-Playlists. FALSE im Fehlerfall.
*/
public function GetPlaylistsEx(string $Search): false|array
{
if ($Search != '') {
$Data = [0, 100000, 'search:' . urlencode($Search), 'tags:u'];
} else {
return $this->Multi_Playlists;
}
$LMSData = $this->SendDirect(new \SqueezeBox\LMSData('playlists', $Data));
if ($LMSData === null) {
return false;
}
$LMSData->SliceData();
$Playlists = (new \SqueezeBox\LMSTaggingArray($LMSData->Data))->DataArray();
foreach ($Playlists as $Key => $Playlist) {
$Playlists[$Key]['Id'] = $Key;
$Playlists[$Key]['Tracks'] = 0;
$Playlists[$Key]['Duration'] = 0;
$LMSSongData = $this->SendDirect(new \SqueezeBox\LMSData(['playlists', 'tracks'], [0, 100000, 'playlist_id:' . $Key, 'tags:d']));
if ($LMSSongData === null) {
set_error_handler([$this, 'ModulErrorHandler']);
trigger_error(sprintf($this->Translate('Error read Playlist %d .'), $Key), E_USER_NOTICE);
restore_error_handler();
$Playlists[$Key]['Playlist'] = $Playlists[$Key]['Playlist'];
continue;
}
$LMSSongData->SliceData();
$SongInfo = new \SqueezeBox\LMSSongInfo($LMSSongData->Data);
$Playlists[$Key]['Tracks'] = $SongInfo->CountAllSongs();
$Playlists[$Key]['Duration'] = $SongInfo->GetTotalDuration();
}
return $Playlists;
}
/**
* GetPlaylist
* IPS-Instanz-Funktion 'LMS_GetPlaylist'.
* Liefert alle Songs einer Playlist.
*
* @param int $PlaylistId Die Playlist welche gelesen werden soll.
* @return false|array Array mit Songs der Playlist.
*/
public function GetPlaylist(int $PlaylistId): false|array
{
$LMSSongData = $this->SendDirect(new \SqueezeBox\LMSData(['playlists', 'tracks'], [0, 10000, 'playlist_id:' . $PlaylistId, 'tags:gladiqrRtueJINpsy']));
if ($LMSSongData === null) {
return false;
}
$LMSSongData->SliceData();
$SongInfo = new \SqueezeBox\LMSSongInfo($LMSSongData->Data);
return $SongInfo->GetAllSongs();
}
/**
* RenamePlaylist
* IPS-Instanz-Funktion 'LMS_RenamePlaylist'.
* Benennt eine Playlist um.
*
* @param int $PlaylistId Die ID der Playlist welche umbenannt werden soll.
* @param string $Name Der neue Name der Playlist.
* @return bool TRUE bei Erfolg. FALSE im Fehlerfall.
*/
public function RenamePlaylist(int $PlaylistId, string $Name): bool
{
return $this->RenamePlaylistEx($PlaylistId, $Name, false);
}
/**
* RenamePlaylistEx
* IPS-Instanz-Funktion 'LMS_RenamePlaylistEx'.
* Benennt eine Playlist um.
*
* @param int $PlaylistId Die ID der Playlist welche umbenannt werden soll.
* @param string $Name Der neue Name der Playlist.
* @param bool $Overwrite True wenn eine eventuell vorhandene Playlist überschrieben werden soll.
* @return bool TRUE bei Erfolg. FALSE im Fehlerfall.
*/
public function RenamePlaylistEx(int $PlaylistId, string $Name, bool $Overwrite): bool
{
if (!$Overwrite) {
$LMSData = $this->SendDirect(new \SqueezeBox\LMSData(['playlists', 'rename'], ['playlist_id:' . $PlaylistId, 'newname:' . $Name, 'dry_run:1']));
if ($LMSData === null) {
return false;
}
$LMSData->SliceData();
if (count($LMSData->Data) > 0) {
if ((new \SqueezeBox\LMSTaggingData($LMSData->Data[0]))->Value == $PlaylistId) {
return true;
}
set_error_handler([$this, 'ModulErrorHandler']);
trigger_error(sprintf($this->Translate('Error rename Playlist. Name already used by Playlist %s'), (new \SqueezeBox\LMSTaggingData($LMSData->Data[0]))->Value), E_USER_NOTICE);
restore_error_handler();
return false;
}
}
$LMSData = $this->SendDirect(new \SqueezeBox\LMSData(['playlists', 'rename'], ['playlist_id:' . $PlaylistId, 'newname:' . $Name, 'dry_run:0']));
if ($LMSData === null) {
return false;
}
return true;
}