Skip to content

Commit 37f7957

Browse files
committed
update routes; code cleanup
1 parent 8c1dffc commit 37f7957

7 files changed

+109
-79
lines changed

ShowPulseBase.php

+27-4
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected function httpRequest($url, $method = "GET", $data = null, $headers = a
7373

7474
public function saveSetting($key, $value)
7575
{
76-
if (empty($key) || is_null($key)) {
76+
if ($this->isNullOrEmpty($key)) {
7777
throw new Exception("Setting key was not specified");
7878
}
7979

@@ -85,7 +85,7 @@ public function saveSetting($key, $value)
8585

8686
public function readSetting($key)
8787
{
88-
if (empty($key) || is_null($key)) {
88+
if ($this->isNullOrEmpty($key)) {
8989
return false;
9090
}
9191

@@ -126,7 +126,30 @@ protected function executeFppCommand($command, $data = array())
126126

127127
public function logError($data)
128128
{
129-
$currentDateTime = date('Y-m-d h:i:s A');
130-
error_log("$currentDateTime: $data");
129+
130+
}
131+
132+
public function isNullOrEmpty($value)
133+
{
134+
return is_null($value) || empty($value);
135+
}
136+
137+
public function isNotNullOrEmpty($value)
138+
{
139+
return !$this->isNullOrEmpty($value);
140+
}
141+
142+
public function idleStatusValue()
143+
{
144+
return "idle";
131145
}
132146
}
147+
148+
149+
final class ShowPulseResponseDto
150+
{
151+
public $success;
152+
public $failed;
153+
public $message;
154+
public $data;
155+
}

ShowPulseDaemon.php

+1-14
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,5 @@
88

99
$worker = new ShowPulseWorker();
1010
while (true) {
11-
try {
12-
$worker->getWebsiteApiKey();
13-
$worker->getFppStatus();
14-
$worker->postStatus();
15-
$worker->insertNextRequest();
16-
$sleepTime = $worker->calculateSleepTime();
17-
sleep($sleepTime);
18-
$worker->resetFailureCount();
19-
} catch (Exception $e) {
20-
$worker->logFailure($e->getMessage());
21-
$sleepTime = $worker->exponentialSleepTime();
22-
$worker->increaseFailureCount();
23-
sleep($sleepTime);
24-
}
11+
$worker->execute();
2512
}

ShowPulseSettings.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public function save()
4040
$playlistDirectory = GetDirSetting("playlists");
4141
$playlistJson = file_get_contents($playlistDirectory . "/" . $this->playlist);
4242

43-
$url = $this->websiteUrl("song_options/playeradd");
44-
$this->httpRequest($url, "POST", $playlistJson, $this->getWebsiteAuthorizationHeaders());
43+
$url = $this->websiteUrl("shows/add-options");
44+
$this->httpRequest($url, "PUT", $playlistJson, $this->getWebsiteAuthorizationHeaders());
4545

4646
return array('success' => true, 'message' => "Settings updated successfully.");
4747
} catch (Exception $e) {

ShowPulseWorker.php

+76-56
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ final class ShowPulseWorker extends ShowPulseBase
3434
private $fppStatus;
3535
private $failureCount;
3636
private $lastSequence;
37-
private $lastUpdated;
38-
private $statusResponse;
37+
private $lastStatusCheckTime;
38+
private $lastRequestCheckTime;
3939

4040
public function __construct()
4141
{
4242
$this->failureCount = 0;
4343
$this->fppStatus = null;
4444
$this->lastSequence = null;
45-
$this->statusResponse = null;
46-
$this->lastUpdated = time() - $this->fifteenMinutesAgo();
45+
$this->lastStatusCheckTime = time() - $this->fifteenMinutesAgo();
46+
$this->lastRequestCheckTime = time() - $this->fifteenMinutesAgo();
4747
}
4848

4949
public function getFailureCount()
@@ -56,11 +56,11 @@ public function fifteenMinutesAgo()
5656
return time() - 900;
5757
}
5858

59-
public function logFailure($exceptionMessage)
59+
public function logError($message)
6060
{
6161
if ($this->isBelowMaxFailureThreshold()) {
62-
$message = $exceptionMessage . " (Attempt " . $this->failureCount . ")";
63-
$this->logError($message);
62+
$currentDateTime = date('Y-m-d h:i:s A');
63+
error_log("$currentDateTime: $message (Attempt $this->failureCount)");
6464
}
6565
}
6666

@@ -69,37 +69,21 @@ public function getFppStatus()
6969
$url = $this->fppUrl("fppd/status");
7070
$this->fppStatus = $this->httpRequest($url);
7171

72-
if (is_null($this->fppStatus)) {
72+
if ($this->isNullOrEmpty($this->fppStatus)) {
7373
throw new Exception("Unable to get latest status from FPP.");
7474
}
7575
}
7676

77-
public function getMediaMetaData($filename = null)
78-
{
79-
if (is_null($filename) || empty($filename)) {
80-
return null;
81-
}
82-
83-
$url = $this->fppUrl("media/$filename/meta");
84-
return $this->httpRequest($url);
85-
}
86-
8777
public function isTestingOrOfflinePlaylist()
8878
{
89-
if (is_null($this->fppStatus)) {
79+
if ($this->isNullOrEmpty($this->fppStatus)) {
9080
return false;
9181
}
9282

9383
$playlistName = strtolower($this->fppStatus->current_playlist->playlist);
9484
return strpos($playlistName, 'test') >= 0 || strpos($playlistName, 'offline') >= 0;
9585
}
9686

97-
public function exponentialSleepTime()
98-
{
99-
$defaultDelay = 2;
100-
return pow(2, $this->failureCount) * $defaultDelay;
101-
}
102-
10387
public function resetFailureCount()
10488
{
10589
$this->failureCount = 0;
@@ -135,77 +119,98 @@ public function sleepLongValue()
135119
public function postStatus()
136120
{
137121
if (
138-
($this->lastSequence === $this->fppStatus->current_sequence && $this->lastUpdated < $this->fifteenMinutesAgo()) ||
122+
($this->lastSequence === $this->fppStatus->current_sequence && $this->lastStatusCheckTime < $this->fifteenMinutesAgo()) ||
139123
$this->isTestingOrOfflinePlaylist()
140124
) {
141-
$this->statusResponse = null;
142125
return;
143126
}
144127

145128
$warningCount = count($this->fppStatus->warnings);
146129
$statusDto = new StatusDto($warningCount, $this->fppStatus->current_sequence, $this->fppStatus->status_name);
147130

148-
if (!empty($this->fppStatus->current_song)) {
149-
$metaData = $this->getMediaMetaData($this->fppStatus->current_song);
131+
if ($this->isNotNullOrEmpty($this->fppStatus->current_song)) {
132+
$url = $this->fppUrl("media/" . $this->fppStatus->current_song . "/meta");
133+
$metaData = $this->httpRequest($url);
150134

151-
if (!is_null($metaData) && !is_null($metaData->format->tags)) {
135+
if ($this->isNotNullOrEmpty($metaData) && $this->isNotNullOrEmpty($metaData->format->tags)) {
152136
$statusDto->assignMedia($metaData->format->tags->title, $metaData->format->tags->artist);
153137
}
154138
}
155139

156140
$url = $this->websiteUrl("statuses/add");
157-
$this->statusResponse = $this->httpRequest($url, "POST", $statusDto, $this->getWebsiteAuthorizationHeaders());
141+
$response = $this->httpRequest($url, "POST", $statusDto, $this->getWebsiteAuthorizationHeaders());
158142

159-
if ($this->statusResponse->failed) {
160-
$this->logError("Unable to update show status. " . $this->statusResponse->messsage);
161-
return;
143+
if ($response->failed) {
144+
throw new Exception("Unable to update show status. " . $response->message);
162145
}
163146

164147
$this->lastSequence = $this->fppStatus->current_sequence;
165-
$this->lastUpdated = time();
148+
$this->lastStatusCheckTime = time();
166149
}
167150

168-
public function insertNextRequest()
151+
/**
152+
* @var ShowPulseResponseDto @responseDto
153+
*
154+
*/
155+
public function getAndInsertNextRequest()
169156
{
170-
if (is_null($this->statusResponse) || is_null($this->statusResponse->data)) {
157+
if (
158+
($this->lastSequence === $this->fppStatus->current_sequence && $this->lastRequestCheckTime < $this->fifteenMinutesAgo()) ||
159+
$this->isTestingOrOfflinePlaylist()
160+
) {
171161
return;
172162
}
173163

174-
switch ($this->statusResponse->data->sequence) {
164+
$url = $this->websiteUrl("jukebox_requests/next");
165+
$responseDto = $this->httpRequest($url, "GET", null, $this->getWebsiteAuthorizationHeaders());
166+
167+
if (is_null($responseDto) || $responseDto->failed) {
168+
$this->logError($responseDto->message);
169+
return;
170+
}
171+
172+
if (is_null($responseDto->data)) {
173+
$this->lastRequestCheckTime = time();
174+
return;
175+
}
176+
177+
switch ($responseDto->data) {
175178
case "SP_STOP_IMMEDIATELY":
176-
$this->stopPlaylistImmediately();
179+
$this->stopPlaylist(false);
177180
break;
178181

179182
case "SP_RESTART_IMMEDIATELY":
180-
$this->stopPlaylistImmediately();
183+
$this->stopPlaylist(false);
181184
$this->systemRestart();
182185
break;
183186

184187
case "SP_SHUTDOWN_IMMEDIATELY":
185-
$this->stopPlaylistImmediately();
188+
$this->stopPlaylist(false);
186189
$this->systemShutdown();
187190
break;
188191

189192
case "SP_STOP_GRACEFULLY":
190-
$this->stopPlaylistGracefully(false);
193+
$this->stopPlaylist(false);
191194
break;
192195

193196
case "SP_RESTART_GRACEFULLY":
194-
$this->stopPlaylistGracefully(true);
197+
$this->stopPlaylist(true);
195198
$this->systemRestart();
196199
break;
197200

198201
case "SP_SHUTDOWN_GRACEFULLY":
199-
$this->stopPlaylistGracefully(true);
202+
$this->stopPlaylist(true);
200203
$this->systemShutdown();
201204
break;
202205

203206
default:
204207
$this->executeFppCommand(
205208
"Insert Playlist After Current",
206-
array($this->statusResponse->data->sequence, "-1", "-1", "false")
209+
array($responseDto->data->sequence, "-1", "-1", "false")
207210
);
208211
}
212+
213+
$this->lastRequestCheckTime = time();
209214
}
210215

211216
private function systemRestart()
@@ -220,21 +225,15 @@ private function systemShutdown()
220225
$this->httpRequest($url);
221226
}
222227

223-
private function stopPlaylistImmediately()
228+
private function stopPlaylist($isGracefulStop)
224229
{
225-
$url = $this->fppUrl("playlists/stop");
230+
$url = $isGracefulStop ? $this->fppUrl("playlists/stopgracefully") : $this->fppUrl("playlists/stop");
226231
$this->httpRequest($url);
227-
}
228232

229-
private function stopPlaylistGracefully($waitForIdleState)
230-
{
231-
$url = $this->fppUrl("playlists/stopgracefully");
232-
$this->httpRequest($url);
233-
234-
while ($waitForIdleState) {
233+
while ($isGracefulStop) {
235234
$this->getFppStatus();
236235

237-
if ($this->fppStatus->status_name === "idle") {
236+
if ($this->fppStatus->status_name === $this->idleStatusValue()) {
238237
break;
239238
}
240239

@@ -248,6 +247,27 @@ public function calculateSleepTime()
248247
return $this->sleepShortValue();
249248
}
250249

251-
return $this->fppStatus->status_name === "idle" ? $this->sleepLongValue() : $this->sleepShortValue();
250+
return $this->fppStatus->status_name === $this->idleStatusValue() ? $this->sleepLongValue() : $this->sleepShortValue();
251+
}
252+
253+
public function execute()
254+
{
255+
try {
256+
$this->getWebsiteApiKey();
257+
$this->getFppStatus();
258+
$this->postStatus();
259+
$this->getAndInsertNextRequest();
260+
$sleepTime = $this->calculateSleepTime();
261+
sleep($sleepTime);
262+
$this->resetFailureCount();
263+
} catch (Exception $e) {
264+
$this->logError($e->getMessage());
265+
266+
$defaultDelay = 2;
267+
$sleepTime = $this->getFailureCount() * $defaultDelay;
268+
269+
$this->increaseFailureCount();
270+
sleep($sleepTime);
271+
}
252272
}
253273
}

commands/RequestsDisableClearCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function execute()
1616
return;
1717
}
1818

19-
$url = $this->websiteUrl("shows/clearoff");
19+
$url = $this->websiteUrl("shows/clear-off");
2020
$response = $this->httpRequest($url, 'PUT', null, $this->getWebsiteAuthorizationHeaders());
2121
}
2222
}

commands/RequestsDisableCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function execute()
1616
return;
1717
}
1818

19-
$url = $this->websiteUrl("shows/requestoff");
19+
$url = $this->websiteUrl("shows/request-off");
2020
$response = $this->httpRequest($url, 'PUT', null, $this->getWebsiteAuthorizationHeaders());
2121
}
2222
}

commands/RequestsEnableCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function execute()
1616
return;
1717
}
1818

19-
$url = $this->websiteUrl("shows/requeston");
19+
$url = $this->websiteUrl("shows/request-on");
2020
$response = $this->httpRequest($url, 'PUT', null, $this->getWebsiteAuthorizationHeaders());
2121
}
2222
}

0 commit comments

Comments
 (0)