-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIftttController.php
86 lines (70 loc) · 2.64 KB
/
IftttController.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
<?php
namespace App\Controllers;
use Slim\Http\Request;
use Slim\Http\Response;
class IftttController
{
private $appCommand;
private $lifx;
private $ifttt;
/**
* IftttController constructor.
*
* @param $appCommand
* @param $lifx
* @param $ifttt
*/
public function __construct($appCommand, $lifx, $ifttt)
{
$this->appCommand = $appCommand;
$this->lifx = $lifx;
$this->ifttt = $ifttt;
}
/**
* @param Request $request
* @param Response $response
*
* @return Response
*/
public function __invoke(Request $request, Response $response)
{
$payload = $request->getParsedBody();
// Handle app events
if ($payload->event === 'app_command') {
// Enable plex webhooks
if ($payload->command === 'enable_plex_webhooks') {
$this->appCommand->changeSetting('plex.webhooks', 'enabled');
return $response->withJson('Plex webhooks enabled.');
}
// Disable plex webhooks
if ($payload->command === 'disable_plex_webhooks') {
$this->appCommand->changeSetting('plex.webhooks', 'disabled');
return $response->withJson('Plex webhooks disabled.');
}
}
// Handle home events
if ($payload->event === 'home_command') {
// Movie time!
// 1. Re-enable Plex webhooks so lights respond (in case they are disabled)
// 2. Activate the LIFX Movie Time scene over 5 seconds
// 3. Turn on the Kasa smart plug for the TV, receiver, and speakers
// 4. Tell Harmony to activate the Shield TV activity
if ($payload->command === 'activate_movie_time') {
$this->appCommand->changeSetting('plex.webhooks', 'enabled');
$this->lifx->activateScene('movie_time', 25);
$this->ifttt->trigger('turn_tv_plug_on');
$this->ifttt->trigger('start_shield_activity');
return $response->withJson($payload->command . ' webhook fired.');
}
// Bed time!
// 1. Turn off the Kasa smart plug for the TV, receiver, and speakers
// 2. Fade all the lights off over 15 minutes
if ($payload->command === 'activate_bed_time') {
$this->ifttt->trigger('turn_tv_plug_off');
$this->ifttt->trigger('fade_all_lights_off');
return $response->withJson($payload->command . ' webhook fired.');
}
}
return $response->withJson($payload->command . ' unhandled.', 422);
}
}