-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckForActions.php
More file actions
82 lines (63 loc) · 2.03 KB
/
Copy pathCheckForActions.php
File metadata and controls
82 lines (63 loc) · 2.03 KB
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
<?php
use duncan3dc\Sonos\Controller;
use GuzzleHttp\Client;
use duncan3dc\Sonos\Network;
class CheckForActions
{
public $network;
public function __construct()
{
$this->network = new Network;
}
public function run()
{
$client = new Client;
$endpoint = getenv('SONOS_ENDPOINT') . '/webhooks/sonos/state';
$response = $client->get($endpoint, [
'verify' => false,
]);
$result = $response->getBody()->getContents();
$this->handleResult($result);
}
public function handleResult($result)
{
$array = json_decode($result, true)['speakers'] ?? [];
foreach ($array as $controller => $actionContents) {
$this->performAction($controller, $actionContents);
}
}
public function performAction($controller, $actionContents)
{
$controllerIp = $controller;
$action = $actionContents['custom_state'];
if ($this->notAlreadyExecuted($actionContents['order_uuid'])) {
$controller = $this->network->getControllerByIp($controllerIp);
if ($controller instanceof Controller) {
match ($action) {
'PLAYING' => $controller->play(),
'PAUSED' => $controller->pause(),
default => null
};
echo 'Performed action ' . $action . ' on ' . $controllerIp . PHP_EOL;
}
}
}
public function notAlreadyExecuted($orderUuid)
{
$directory = __DIR__ . '/orders/';
$file = $directory . 'orders' . '.json';
if (!file_exists($file)) {
// Create File
file_put_contents($file, json_encode([]));
}
$contents = file_get_contents($file);
$json = json_decode($contents, true);
if (in_array($orderUuid, $json)) {
return false;
} else {
$json[] = $orderUuid;
file_put_contents($file, json_encode($json));
return true;
}
}
}