-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.php
205 lines (192 loc) · 6.6 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
<?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
/**
* LMSDiscovery Klasse implementiert.
*
* @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 $Devices
*/
class LMSDiscovery extends IPSModule
{
use \SqueezeBox\DebugHelper;
/**
* Create
*
* @return void
*/
public function Create()
{
parent::Create();
}
/**
* ApplyChanges
*
* @return void
*/
public function ApplyChanges()
{
$this->RegisterMessage(0, IPS_KERNELSTARTED);
parent::ApplyChanges();
}
/**
* 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);
}
$Devices = $this->DiscoverDevices();
if ((count($Devices) == 0) && IPS_GetOption('NATSupport')) {
$Form['actions'][2]['visible'] = true;
$this->SendDebug('FORM', json_encode($Form), 0);
$this->SendDebug('FORM', json_last_error_msg(), 0);
return json_encode($Form);
}
$IPSDevices = $this->GetIPSInstances();
$Values = [];
foreach ($Devices as $IPAddress => $Device) {
$AddValue = [
'IPAddress' => $IPAddress,
'servername' => $Device['ENAME'],
'name' => 'Logitech Media Server (' . $Device['ENAME'] . ')',
'version' => $Device['VERS'],
'instanceID' => 0
];
$InstanceID = array_search($IPAddress, $IPSDevices);
if ($InstanceID === false) {
$InstanceID = array_search(strtolower($Device['Host']), $IPSDevices);
if ($InstanceID !== false) {
$AddValue['IPAddress'] = $Device['Host'];
}
}
if ($InstanceID !== false) {
unset($IPSDevices[$InstanceID]);
$AddValue['name'] = IPS_GetLocation($InstanceID);
$AddValue['instanceID'] = $InstanceID;
}
$AddValue['create'] = [
[
'moduleID' => '{35028918-3F9C-4524-9FB4-DBAF429C6E18}',
'configuration' => new stdClass()
],
[
'moduleID' => '{96A9AB3A-2538-42C5-A130-FC34205A706A}',
'configuration' => [
'Webport' => (int) $Device['JSON']
]
],
[
'moduleID' => '{3CFF0FD9-E306-41DB-9B5A-9D06D38576C3}',
'configuration' => [
'Host' => $AddValue['IPAddress'],
'Port' => 9090
]
]
];
$Values[] = $AddValue;
}
foreach ($IPSDevices as $InstanceID => $IPAddress) {
$Values[] = [
'IPAddress' => $IPAddress,
'version' => '',
'servername' => '',
'name' => IPS_GetLocation($InstanceID),
'instanceID' => $InstanceID
];
}
$Form['actions'][1]['values'] = $Values;
$this->SendDebug('FORM', json_encode($Form), 0);
$this->SendDebug('FORM', json_last_error_msg(), 0);
return json_encode($Form);
}
/**
* GetIPSInstances
*
* @return array
*/
private function GetIPSInstances(): array
{
$InstanceIDList = IPS_GetInstanceListByModuleID('{35028918-3F9C-4524-9FB4-DBAF429C6E18}');
$Devices = [];
foreach ($InstanceIDList as $InstanceID) {
$Splitter = IPS_GetInstance($InstanceID)['ConnectionID'];
if ($Splitter > 0) {
$IO = IPS_GetInstance($Splitter)['ConnectionID'];
if ($IO > 0) {
$Devices[$InstanceID] = strtolower(IPS_GetProperty($IO, 'Host'));
}
}
}
$this->SendDebug('IPS Devices', $Devices, 0);
return $Devices;
}
/**
* DiscoverDevices
*
* @return array
*/
private function DiscoverDevices(): array
{
$this->SendDebug('Discover', $this->Translate('Background discovery of Logitech Media Servers'), 0);
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if (!$socket) {
$this->SendDebug('Discover', $this->Translate('Error on create socket'), 0);
return [];
}
socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, ['sec' => 1, 'usec' => 100000]);
socket_bind($socket, '0.0.0.0', 0);
$message = "\x65\x49\x50\x41\x44\x00\x4e\x41\x4d\x45\x00\x4a\x53\x4f\x4e\x00\x56\x45\x52\x53\x00\x55\x55\x49\x44\x00\x4a\x56\x49\x44\x06\x12\x34\x56\x78\x12\x34";
$this->SendDebug('Search', $message, 1);
if (@socket_sendto($socket, $message, strlen($message), 0, '255.255.255.255', 3483) === false) {
return [];
}
usleep(100000);
$i = 50;
$buf = '';
$IPAddress = '';
$Port = 0;
$DeviceData = [];
while ($i) {
$ret = @socket_recvfrom($socket, $buf, 2048, 0, $IPAddress, $Port);
if ($ret === false) {
break;
}
if ($ret === 0) {
$i--;
continue;
}
$this->SendDebug('Receive', $buf, 0);
$Serach = ['UUID', 'ENAME', 'VERS', 'JSON'];
foreach ($Serach as $Key) {
$start = strpos($buf, $Key);
if ($start !== false) {
$DeviceData[$IPAddress][$Key] = substr($buf, $start + strlen($Key) + 1, ord($buf[$start + strlen($Key)]));
}
}
$DeviceData[$IPAddress]['Host'] = gethostbyaddr($IPAddress);
}
socket_close($socket);
$this->SendDebug('Found', $DeviceData, 0);
return $DeviceData;
}
}