diff --git a/ShowPulseBase.php b/ShowPulseBase.php index 04b20d7..623a114 100644 --- a/ShowPulseBase.php +++ b/ShowPulseBase.php @@ -1,6 +1,7 @@ getWebsiteApiKey(); + $worker->getFppStatus(); + $worker->postShowStatus(); + $worker->getNextRequest(); + $sleepTime = $worker->calculateSleepTime(); + sleep($sleepTime); + $worker->resetAttemptCount(); + } catch (Exception $e) { + $worker->logError($e->getMessage()); + $sleepTime = $worker->exponentialBackoffSleep(); + $worker->increaseAttemptCount(); + sleep($sleepTime); + } +} \ No newline at end of file diff --git a/ShowPulseWorker.php b/ShowPulseWorker.php index c56f580..b4f1cc8 100644 --- a/ShowPulseWorker.php +++ b/ShowPulseWorker.php @@ -1,6 +1,7 @@ nextJukeboxRequest = null; } + public function getAttemptCount() + { + return $this->attemptCount; + } + public function getFppStatus() { $url = $this->fppUrl("fppd/status"); @@ -82,8 +88,7 @@ public function exponentialBackoffSleep() { $defaultDelay = 2; $maxDelay = 15; - $delay = min(pow(2, $this->attemptCount) * $defaultDelay, $maxDelay); - sleep($delay); + return min(pow(2, $this->attemptCount) * $defaultDelay, $maxDelay); } public function resetAttemptCount() @@ -93,7 +98,9 @@ public function resetAttemptCount() public function increaseAttemptCount() { - $this->attemptCount = $this->attemptCount < 5 ? $this->attemptCount++ : $this->attemptCount; + if ($this->attemptCount < 5) { + $this->attemptCount++; + } } public function sleepShortValue() @@ -180,25 +187,12 @@ public function postShowStatus() $this->lastSequence = $this->fppStatus->current_sequence; } - public function sleepDelay() + public function calculateSleepTime() { - $seconds = $this->fppStatus->status_name === "idle" ? $this->sleepLongValue() : $this->sleepShortValue(); - sleep($seconds); - } -} + if (is_null($this->fppStatus)) { + return $this->sleepShortValue(); + } -$worker = new ShowPulseWorker(); -while ($testing) { - try { - $worker->getWebsiteApiKey(); - $worker->getFppStatus(); - $worker->postShowStatus(); - $worker->getNextRequest(); - $worker->sleepDelay(); - $worker->resetAttemptCount(); - } catch (Exception $e) { - $worker->logError($e->getMessage()); - $worker->exponentialBackoffSleep(); - $worker->increaseAttemptCount(); + return $this->fppStatus->status_name === "idle" ? $this->sleepLongValue() : $this->sleepShortValue(); } -} \ No newline at end of file +} diff --git a/scripts/postStart.sh b/scripts/postStart.sh index cac06fa..5139b88 100755 --- a/scripts/postStart.sh +++ b/scripts/postStart.sh @@ -2,4 +2,4 @@ echo "Running Show Pulse PostStart Script" -/usr/bin/php /home/fpp/media/plugins/showpulse/ShowPulseWorker.php & +/usr/bin/php /home/fpp/media/plugins/showpulse/ShowPulseService.php & diff --git a/tests/ShowPulseWorkerTest.php b/tests/ShowPulseWorkerTest.php new file mode 100644 index 0000000..d69f1fe --- /dev/null +++ b/tests/ShowPulseWorkerTest.php @@ -0,0 +1,65 @@ +resetAttemptCount(); + + $this->assertEquals(0, $worker->getAttemptCount()); + } + + public function testIncreaseAttemptCountTwoTimes() + { + $worker = new ShowPulseWorker(); + $worker->increaseAttemptCount(); + $worker->increaseAttemptCount(); + + $this->assertEquals(2, $worker->getAttemptCount()); + } + + public function testIncreaseAttemptCountSixTimes() + { + $worker = new ShowPulseWorker(); + $worker->increaseAttemptCount(); + $worker->increaseAttemptCount(); + $worker->increaseAttemptCount(); + $worker->increaseAttemptCount(); + $worker->increaseAttemptCount(); + $worker->increaseAttemptCount(); + + $this->assertEquals(5, $worker->getAttemptCount()); + } + + public function testSleepShortValue() + { + $worker = new ShowPulseWorker(); + + $this->assertEquals(5, $worker->sleepShortValue()); + } + + public function testSleepLongValue() + { + $worker = new ShowPulseWorker(); + + $this->assertEquals(30, $worker->sleepLongValue()); + } + + public function testCalculateSleepTime() + { + $worker = new ShowPulseWorker(); + + $sleepTime = $worker->calculateSleepTime(); + + $this->assertEquals(5, $sleepTime); + } +}