-
Notifications
You must be signed in to change notification settings - Fork 0
/
raumfeldController.php
390 lines (304 loc) · 14.2 KB
/
raumfeldController.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
<?php
#
# This file is part of RaumfeldAlarm2.
# Learn more at: https://github.com/blaues0cke/RaumfeldAlarm
#
# Author: Thomas Kekeisen <[email protected]>
# License: This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
# To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
#
class RaumfeldController
{
protected $baseIp = null;
protected $devices = [];
protected $zones = [];
public function __construct ($baseIp = null)
{
$this->log('Initialized');
if ($baseIp)
{
$this->setBaseIp($baseIp);
$this->loadDevices();
$this->loadZones();
}
}
public function __destruct ()
{
$this->log('Everything done');
}
public function addRoomToZone ($room, $zone, $noSleep = false)
{
$this->log('Adding room to '.$room.' to zone: '.$zone);
$baseDevice = $this->findBase();
$this->httpRequest(
'http://192.168.0.10:47365/1b9b97b752e6adfd9c6efe7abf43d252c04885f86d01c90e55a401afcc86ed8d/connectRoomToZone?zoneUDN='.urlencode($zone).'&roomUDN='.urlencode($room)
);
if ($noSleep)
{
$this->log('Done');
}
else
{
$this->log('Done, waiting 2 seconds since this seems to help');
$this->sleep(2);
}
}
public function addAllRoomsToZone ($zoneUdn)
{
foreach ($this->zones as $zone)
{
foreach ($zone['rooms'] as $room)
{
$this->log('Current room: '.$room['name']);
$this->addRoomToZone($room['udn'], $zoneUdn);
}
}
}
public function addAllRoomsToFirstZone ()
{
if (count($this->zones) > 1)
{
$zoneFound = false;
foreach ($this->zones as $zone)
{
if (!empty($zone['udn']))
{
$zoneUdn = $zone['udn'];
$this->log('Adding all rooms to zone: '.$zoneUdn);
$this->addAllRoomsToZone($zoneUdn);
$zoneFound = true;
break;
}
}
if (!$zoneFound)
{
$this->log('No zone found to add all rooms to');
}
}
else
{
$this->log('Got only one zone, nothing to do');
}
}
public function fadeVolumeInAllRoomsTo ($targetVolume, $step = 1, $sleep = 3)
{
for ($volume = 1; $volume < $targetVolume; $volume += $step)
{
$this->setVolumeInAllRoomsTo($volume);
$this->log('Waiting '.$sleep.' seconds');
$this->sleep($sleep);
}
}
public function findBase ()
{
return $this->findRendererWithIpAnd($this->baseIp);
}
public function findDeviceWithIp ($ip, $type = null)
{
foreach ($this->devices as $device)
{
if (
strpos($device['location'].':', $ip.':') !== false &&
(
$type === null ||
strpos($device['type'], $type) !== false
)
)
{
return $device;
}
}
return false;
}
public function findRendererWithIpAnd ($ip)
{
return $this->findDeviceWithIp($ip, 'MediaRenderer');
}
protected function httpRequest ($url, $postData = null, $soapAction = null)
{
$this->log('Requesting '.$url);
$this->log(' '.$postData);
$this->log(' '.$soapAction);
$ch = curl_init();
$header = array(
'Content-Type: text/xml; charset="utf-8"',
'User-Agent: RaumfeldAlarm2'
);
if ($soapAction)
{
$header[] = 'SOAPAction: "'.$soapAction.'"';
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, !empty($postData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if (!empty($postData))
{
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
}
$result = curl_exec($ch);
$this->log('Done, response: '.(empty($result) ? '<empty response>' : $result));
$this->log(' ');
curl_close($ch);
return $result;
}
public function loadDevices ()
{
$newDeviceList = [];
$deviceListData = $this->httpRequest('http://'.$this->baseIp.':47365/listDevices');
preg_match_all('/<device (.*?)>(.*?)<\/device>/is', $deviceListData, $matches);
foreach ($matches[1] as $index => $match)
{
$newDevice = [];
if (!empty($matches[2]) && !empty($matches[2][$index]))
{
$newDevice['name'] = $matches[2][$index];
}
$explodedString = explode('\' ', $match);
foreach ($explodedString as $dataPart)
{
$explodedDataPart = explode('=\'', $dataPart);
$newDevice[$explodedDataPart[0]] = substr($explodedDataPart[1], 0, -1);
}
$ipFound = preg_match('/([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}:[0-9]{1,5})/is', $newDevice['location'], $matches);
if ($ipFound)
{
$newDevice['ip'] = $matches[1];
}
$newDeviceList[] = $newDevice;
}
$this->devices = $newDeviceList;
$this->log('Loaded '.count($this->devices).' devices');
}
public function loadZones ()
{
$newZoneList = [];
$zoneListData = $this->httpRequest('http://'.$this->baseIp.':47365/getZones');
preg_match_all('/<(?:zone|unassignedRooms)( .*?|)>(.*?)<\/(?:zone|unassignedRooms)>/is', $zoneListData, $matches);
foreach ($matches[1] as $index => $match)
{
$newZone = ['roomData' => $matches[2][$index]];
$newRoomList = [];
$explodedString = explode('\' ', $match);
foreach ($explodedString as $dataPart)
{
$explodedDataPart = explode('=\'', $dataPart);
if (!empty($explodedDataPart[0]))
{
$newZone[trim($explodedDataPart[0])] = str_replace('\'', '', $explodedDataPart[1]);
}
}
preg_match_all('/<room (.*?)>(.*?)<\/room>/is', $newZone['roomData'], $matches2);
foreach ($matches2[1] as $index2 => $match2)
{
$newRoom = ['rendererData' => $matches2[2][$index2]];
$newRendererList = [];
$explodedString = explode('\' ', $match2);
foreach ($explodedString as $dataPart)
{
$explodedDataPart = explode('=\'', $dataPart);
$newRoom[$explodedDataPart[0]] = $explodedDataPart[1];
}
unset($newRoom['rendererData']);
$newRoom['renderer'] = $newRendererList;
$newRoomList[] = $newRoom;
}
unset($newZone['roomData']);
$newZone['rooms'] = $newRoomList;
$newZoneList[] = $newZone;
}
$this->zones = $newZoneList;
$this->log('Loaded '.count($this->zones).' zones');
}
protected function log ($text)
{
echo '> '.$text."\n";
}
public function playRandomTuneInRadio ()
{
$radios = array
(
'playTuneInHerrMerkt',
'playTuneInJahfari',
'playTuneInRawFM'
);
$function = $radios[array_rand($radios)];
$this->$function();
}
public function playTuneInHerrMerkt ()
{
$this->log('Playing herr merkt');
$baseDevice = $this->findBase();
$this->httpRequest(
'http://'.$baseDevice['ip'].'/TransportService/Control',
'<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetAVTransportURI xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><CurrentURIMetaData><DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dlna="urn:schemas-dlna-org:metadata-1-0/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:raumfeld="urn:schemas-raumfeld-com:meta-data/raumfeld"><item restricted="1" id="0/RadioTime/Search/s-s97342" parentID="0/RadioTime/Search"><dc:title>Herr Merkt Radio</dc:title><upnp:signalStrength>100</upnp:signalStrength><raumfeld:section>RadioTime</raumfeld:section><upnp:class>object.item.audioItem.audioBroadcast.radio</upnp:class><raumfeld:ebrowse>http://opml.radiotime.com/Tune.ashx?partnerId=7aJ9pvV5&amp;formats=wma%2Cmp3%2Cogg&amp;serial=00%3A0d%3Ab9%3A24%3A50%3A14&amp;id=s97342&amp;c=ebrowse</raumfeld:ebrowse><upnp:albumArtURI>http://cdn-radiotime-logos.tunein.com/s97342q.png</upnp:albumArtURI><raumfeld:name>Station</raumfeld:name><res protocolInfo="http-get:*:audio/x-mpegurl:*">http://opml.radiotime.com/Tune.ashx?id=s97342&amp;formats=wma,mp3,ogg&amp;partnerId=7aJ9pvV5&amp;serial=00:0d:b9:24:50:14</res><raumfeld:durability>117</raumfeld:durability></item></DIDL-Lite></CurrentURIMetaData><InstanceID>0</InstanceID><CurrentURI>http://opml.radiotime.com/Tune.ashx?id=s97342&formats=wma,mp3,ogg&partnerId=7aJ9pvV5&serial=00:0d:b9:24:50:14</CurrentURI></u:SetAVTransportURI></s:Body></s:Envelope>',
'urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI'
);
$this->log('Done');
}
public function playTuneInJahfari ()
{
$this->log('Playing jahfari');
$baseDevice = $this->findBase();
$this->httpRequest(
'http://'.$baseDevice['ip'].'/TransportService/Control',
'<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetAVTransportURI xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><CurrentURIMetaData><DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dlna="urn:schemas-dlna-org:metadata-1-0/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:raumfeld="urn:schemas-raumfeld-com:meta-data/raumfeld"><item restricted="1" id="0/RadioTime/Search/s-s100890" parentID="0/RadioTime/Search"><dc:title>JAHFARI</dc:title><upnp:signalStrength>97</upnp:signalStrength><raumfeld:section>RadioTime</raumfeld:section><upnp:class>object.item.audioItem.audioBroadcast.radio</upnp:class><raumfeld:ebrowse>http://opml.radiotime.com/Tune.ashx?partnerId=7aJ9pvV5&amp;formats=wma%2Cmp3%2Cogg&amp;serial=00%3A0d%3Ab9%3A24%3A50%3A14&amp;id=s100890&amp;c=ebrowse</raumfeld:ebrowse><upnp:albumArtURI>http://cdn-radiotime-logos.tunein.com/s100890q.png</upnp:albumArtURI><raumfeld:name>Station</raumfeld:name><res protocolInfo="http-get:*:audio/x-mpegurl:*">http://opml.radiotime.com/Tune.ashx?id=s100890&amp;formats=wma,mp3,ogg&amp;partnerId=7aJ9pvV5&amp;serial=00:0d:b9:24:50:14</res><raumfeld:durability>115</raumfeld:durability></item></DIDL-Lite></CurrentURIMetaData><InstanceID>0</InstanceID><CurrentURI>http://opml.radiotime.com/Tune.ashx?id=s100890&formats=wma,mp3,ogg&partnerId=7aJ9pvV5&serial=00:0d:b9:24:50:14</CurrentURI></u:SetAVTransportURI></s:Body></s:Envelope>',
'urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI'
);
$this->log('Done');
}
public function playTuneInRawFM ()
{
$this->log('Playing raw fm');
$baseDevice = $this->findBase();
$this->httpRequest(
'http://'.$baseDevice['ip'].'/TransportService/Control',
'<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetAVTransportURI xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><CurrentURIMetaData><DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dlna="urn:schemas-dlna-org:metadata-1-0/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:raumfeld="urn:schemas-raumfeld-com:meta-data/raumfeld"><item restricted="1" id="0/RadioTime/Search/s-s82747" parentID="0/RadioTime/Search"><dc:title>Raw FM</dc:title><upnp:signalStrength>98</upnp:signalStrength><raumfeld:section>RadioTime</raumfeld:section><upnp:class>object.item.audioItem.audioBroadcast.radio</upnp:class><raumfeld:ebrowse>http://opml.radiotime.com/Tune.ashx?partnerId=7aJ9pvV5&amp;formats=wma%2Cmp3%2Cogg&amp;serial=00%3A0d%3Ab9%3A24%3A50%3A14&amp;id=s82747&amp;c=ebrowse</raumfeld:ebrowse><upnp:albumArtURI>http://cdn-radiotime-logos.tunein.com/s82747q.png</upnp:albumArtURI><raumfeld:name>Station</raumfeld:name><res protocolInfo="http-get:*:audio/x-mpegurl:*">http://opml.radiotime.com/Tune.ashx?id=s82747&amp;formats=wma,mp3,ogg&amp;partnerId=7aJ9pvV5&amp;serial=00:0d:b9:24:50:14</res><raumfeld:durability>115</raumfeld:durability></item></DIDL-Lite></CurrentURIMetaData><InstanceID>0</InstanceID><CurrentURI>http://opml.radiotime.com/Tune.ashx?id=s82747&formats=wma,mp3,ogg&partnerId=7aJ9pvV5&serial=00:0d:b9:24:50:14</CurrentURI></u:SetAVTransportURI></s:Body></s:Envelope>',
'urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI'
);
$this->log('Done');
}
public function setBaseIp ($ip)
{
$this->baseIp = $ip;
}
public function setVolumeInAllRoomsTo ($volume)
{
$this->log('Setting volume for all rooms to '.$volume);
foreach ($this->zones as $zone)
{
foreach ($zone['rooms'] as $room)
{
$this->log('Current room: '.$room['name']);
$this->setVolumeInRoomTo($room['udn'], $volume);
}
}
}
public function setVolumeInRoomTo ($room, $volume)
{
$this->log('Setting volume to '.$volume.' for room: '.$room);
$baseDevice = $this->findBase();
$this->httpRequest(
'http://'.$baseDevice['ip'].'/RenderingService/Control',
'<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetRoomVolume xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1"><Room>'.$room.'</Room><InstanceID>0</InstanceID><DesiredVolume>'.$volume.'</DesiredVolume></u:SetRoomVolume></s:Body></s:Envelope>',
'urn:schemas-upnp-org:service:RenderingControl:1#SetRoomVolume'
);
$this->log('Done');
}
public function sleep ($seconds)
{
sleep($seconds);
}
public function stopMusic ()
{
$this->log('Stopping music');
$baseDevice = $this->findBase();
$this->httpRequest(
'http://'.$baseDevice['ip'].'/TransportService/Control',
'<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:Pause xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID></u:Pause></s:Body></s:Envelope>',
'urn:schemas-upnp-org:service:AVTransport:1#Pause'
);
$this->log('Done');
}
}