-
Notifications
You must be signed in to change notification settings - Fork 0
/
SonosData.php
executable file
·72 lines (59 loc) · 2.04 KB
/
SonosData.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
<?php
use duncan3dc\Sonos\Network;
use GuzzleHttp\Client;
class SonosData
{
public $network;
public function __construct()
{
$this->network = new Network;
}
public function run()
{
$speakers = $this->getNowPlayingTracks();
$this->sendTracksToWebhooks($speakers);
}
public function getNowPlayingTracks()
{
foreach ($this->network->getControllers() as $controller) {
$stateDetails = $controller->getStateDetails();
$speakers[$controller->getIp()] = [
'room' => $controller->getRoom(),
'state' => $controller->getStateName(),
'volume' => $controller->getVolume(),
'title' => $stateDetails->getTitle(),
'artist' => $stateDetails->getArtist(),
'album' => $stateDetails->getAlbum(),
'duration' => $stateDetails->getDuration()->asInt(),
'position' => $stateDetails->getPosition()->asInt(),
'cover' => $stateDetails->getAlbumArt(),
'timestamp' => time(),
];
}
return $speakers;
}
public function sendTracksToWebhooks(array $speakers)
{
$client = new Client;
$json = json_encode(['speakers' => $speakers]);
$secret = getenv('SONOS_SECRET');
$endpoint = getenv('SONOS_ENDPOINT').'/webhooks/sonos';
try {
$hash = hash_hmac('sha256', $json, $secret);
$response = $client->post($endpoint, [
'json' => $json,
'headers' => [
'X-Signature' => $hash,
],
]);
$statusCode = $response->getStatusCode();
if ($statusCode == 200) {
echo 'Endpoint succesfully send to '.$endpoint."\n";
echo 'Body '.$response->getBody()."\n";
}
} catch (\Exception $e) {
echo 'Endpoint faild to send to '.$endpoint."\n";
echo 'Error occurred: '.$e->getMessage()."\n";
}
}
}