From 4353ddc6fb8f7d49397cd9e2078de3e9100b7be2 Mon Sep 17 00:00:00 2001 From: almostengr Date: Tue, 29 Oct 2024 04:52:51 -0500 Subject: [PATCH] #26 moved unshared logic; #36 retrieve weather from NWS --- commands/BaseCommand.php | 152 +------------ commands/DaemonCommand.php | 199 +++++++++++++++--- commands/RequestedSelectionsClearCommand.php | 2 +- .../RequestedSelectionsGetNextCommand.php | 24 --- commands/SelectionOptionsAddCommand.php | 2 +- commands/ShowPulseCommandInterface.php | 2 +- commands/ShowPulseConfigurationResponse.php | 2 +- commands/ShowPulseResponseDto.php | 2 +- commands/ShowsAcceptRequestsCommand.php | 2 +- commands/ShowsRejectRequestsCommand.php | 2 +- 10 files changed, 186 insertions(+), 203 deletions(-) delete mode 100644 commands/RequestedSelectionsGetNextCommand.php diff --git a/commands/BaseCommand.php b/commands/BaseCommand.php index 101cfa1..52beb23 100644 --- a/commands/BaseCommand.php +++ b/commands/BaseCommand.php @@ -1,20 +1,17 @@ loadConfiguration(); } - /** - * @param ShowPulseSelectionResponseDto $selectionResponseDto - * @param mixed $fppStatus - */ - protected function postNextRequestedSelectionToFpp($data, $fppStatus) - { - if (is_null($data)) { - return false; - } - - switch ($data['playlist_name']) { - case self::IMMEDIATE_STOP: - $this->stopPlaylist(); - $this->postStatusToWebsite($fppStatus, self::IMMEDIATE_STOP); - break; - - case self::IMMEDIATE_RESTART: - $this->stopPlaylist(); - $this->postStatusToWebsite($fppStatus, self::IMMEDIATE_RESTART); - $this->systemRestart(); - break; - - case self::IMMEDIATE_SHUTDOWN: - $this->stopPlaylist(); - $this->postStatusToWebsite($fppStatus, self::IMMEDIATE_SHUTDOWN); - $this->systemShutdown(); - break; - - case self::GRACEFUL_STOP: - $this->gracefulStopPlaylist(); - $this->postStatusToWebsite($fppStatus, self::GRACEFUL_STOP); - break; - - case self::GRACEFUL_RESTART: - $this->gracefulStopPlaylist(); - $this->postStatusToWebsite($fppStatus, self::GRACEFUL_RESTART); - $this->systemRestart(); - break; - - case self::GRACEFUL_SHUTDOWN: - $this->gracefulStopPlaylist(); - $this->postStatusToWebsite($fppStatus, self::GRACEFUL_SHUTDOWN); - $this->systemShutdown(); - break; - - default: - $command = "Insert Playlist After Current"; - if ($selectionResponseDto->getPriority() === self::HIGH_PRIORITY) { - $command = "Insert Playlist Immediate"; - } - - $args = $command; - $data = array($selectionResponseDto->getSequenceFilename(), "-1", "-1", "false"); - foreach ($data as $value) { - $args .= "/$value"; - } - - $this->fppHttpRequest($args, "GET", $args); - } - - return true; - } - - private function systemRestart() - { - return $this->fppHttpRequest("system/restart"); - } - - private function systemShutdown() - { - return $this->fppHttpRequest("system/shutdown"); - } - - private function stopPlaylist() - { - return $this->fppHttpRequest("playlists/stop"); - } - - private function gracefulStopPlaylist() - { - $this->fppHttpRequest("playlists/stopgracefully"); - - $maxLoops = 180; // 180 = 5 seconds loops during 5 minutes - for ($i = 0; $i < $maxLoops; $i++) { - $latestStatus = $this->getStatusFromFpp(); - - if ($latestStatus->status === self::FPP_STATUS_IDLE_ID) { - break; - } - - sleep(5); - } - } - protected function fppHttpRequest($route, $method = "GET", $data = null) { $fppUrl = "https://127.0.0.1/api/"; @@ -126,7 +29,8 @@ protected function fppHttpRequest($route, $method = "GET", $data = null) protected function webHttpRequest($route, $method = "GET", $data = null) { $url = $this->configuration->getWebsiteUrl() . "/" . $route; - return $this->httpRequest($url, $method, $data, $this->configuration->getTokenAsHeader()); + $response = $this->httpRequest($url, $method, $data, $this->configuration->getTokenAsHeader()); + return new ShowPulseResponseDto($response); } protected function nwsHttpRequest($route) @@ -205,46 +109,11 @@ protected function completed() echo "Done"; } - /** - * Summary of getNextRequestFromWebsite - * @var ShowPulseResponseDto $responseDto - * @return ShowPulseSelectionResponseDto|bool - */ - private function getNextRequestedSelectionFromWebsite() - { - $responseDto = $this->webHttpRequest( - "api/requested-selections/view-next/" . $this->configuration->getShowId() - ); - - return new ShowPulseResponseDto($responseDto); - } - - protected function getNextRequestedSelection($fppStatus) - { - $selectionResponse = $this->getNextRequestedSelectionFromWebsite(); - - if ($selectionResponse->getData() === null) { - $selectionResponse = $this->getRandomSelection(); - } - - $this->postNextRequestedSelectionToFpp($selectionResponse->getData(), $fppStatus, $this->configuration); - - return $selectionResponse; - } - - private function getRandomSelection() - { - $responseDto = $this->webHttpRequest( - "api/selection-options/view-random/" . $this->configuration->getShowId() - ); - - return new ShowPulseSelectionResponseDto($responseDto); - } - protected function getShow() { $response = $this->webHttpRequest("api/shows/view/" . $this->configuration->getShowId()); - return new ShowPulseResponseDto($response); + // return new ShowPulseResponseDto($response); + return $response; } protected function updateShow($data) @@ -254,7 +123,8 @@ protected function updateShow($data) 'PUT', $data ); - return new ShowPulseResponseDto($response); + // return new ShowPulseResponseDto($response); + return $response; } protected function rejectSelectionRequests() diff --git a/commands/DaemonCommand.php b/commands/DaemonCommand.php index 1996361..10940ed 100644 --- a/commands/DaemonCommand.php +++ b/commands/DaemonCommand.php @@ -1,15 +1,23 @@ lastWeatherUpdateTime = time() - self::ONE_HOUR_IN_SECONDS; } + private function shouldPostStatus($fppStatus) + { + return + !is_null($fppStatus) && + $this->lastSequence === $fppStatus->current_sequence && + $this->lastSong === $fppStatus->current_song; + } + + private function checkPlayerFrozen($fppStatus) + { + return $this->lastSequence === $fppStatus->current_sequence && $this->lastSecondsPlayed === $fppStatus->seconds_played; + } + + private function getCurrentWeather() + { + $timeDifference = time() - $this->lastWeatherUpdateTime; + + if ($this->show === null || $this->show->weather_station_identifier === null || $timeDifference < self::ONE_HOUR_IN_SECONDS) { + return; + } + + $url = "stations/" . $this->show->weather_station_identifier . "/observations/latest"; + $this->latestWeather = $this->nwsHttpRequest($url, "GET"); + $this->lastWeatherUpdateTime = time(); + } + + /** + * Summary of getNextRequestFromWebsite + * @var ShowPulseResponseDto $responseDto + * @return ShowPulseSelectionResponseDto|bool + */ + private function getNextRequestedSelectionFromWebsite() + { + $responseDto = $this->webHttpRequest( + "api/requested-selections/view-next/" . $this->configuration->getShowId() + ); + + // return new ShowPulseResponseDto($responseDto); + return $responseDto; + } + + private function getRandomSelection() + { + $responseDto = $this->webHttpRequest( + "api/selection-options/view-random/" . $this->configuration->getShowId() + ); + + return $responseDto; + // return new ShowPulseResponseDto($responseDto); + } + + /** + * @param ShowPulseSelectionResponseDto $selectionResponseDto + * @param mixed $fppStatus + */ + protected function postNextRequestedSelectionToFpp($data, $fppStatus) + { + if (is_null($data)) { + return false; + } + + switch ($data['playlist_name']) { + case self::IMMEDIATE_STOP: + $this->stopPlaylist(); + $this->postStatusToWebsite($fppStatus, self::IMMEDIATE_STOP); + break; + + case self::IMMEDIATE_RESTART: + $this->stopPlaylist(); + $this->postStatusToWebsite($fppStatus, self::IMMEDIATE_RESTART); + $this->systemRestart(); + break; + + case self::IMMEDIATE_SHUTDOWN: + $this->stopPlaylist(); + $this->postStatusToWebsite($fppStatus, self::IMMEDIATE_SHUTDOWN); + $this->systemShutdown(); + break; + + case self::GRACEFUL_STOP: + $this->gracefulStopPlaylist(); + $this->postStatusToWebsite($fppStatus, self::GRACEFUL_STOP); + break; + + case self::GRACEFUL_RESTART: + $this->gracefulStopPlaylist(); + $this->postStatusToWebsite($fppStatus, self::GRACEFUL_RESTART); + $this->systemRestart(); + break; + + case self::GRACEFUL_SHUTDOWN: + $this->gracefulStopPlaylist(); + $this->postStatusToWebsite($fppStatus, self::GRACEFUL_SHUTDOWN); + $this->systemShutdown(); + break; + + default: + $command = "Insert Playlist After Current"; + if ($data['priority'] === self::HIGH_PRIORITY) { + $command = "Insert Playlist Immediate"; + } + + $args = $command; + $data = array($data['playlist_name'], "-1", "-1", "false"); + foreach ($data as $value) { + $args .= "/$value"; + } + + $this->fppHttpRequest($args, "GET", $args); + } + + return true; + } + + private function systemRestart() + { + return $this->fppHttpRequest("system/restart"); + } + + private function systemShutdown() + { + return $this->fppHttpRequest("system/shutdown"); + } + + private function stopPlaylist() + { + return $this->fppHttpRequest("playlists/stop"); + } + + private function gracefulStopPlaylist() + { + $this->fppHttpRequest("playlists/stopgracefully"); + + $maxLoops = 180; // 180 = 5 seconds loops during 5 minutes + for ($i = 0; $i < $maxLoops; $i++) { + $latestStatus = $this->getStatusFromFpp(); + + if ($latestStatus->status === self::FPP_STATUS_IDLE_ID) { + break; + } + + sleep(5); + } + } + public function execute() { if (file_exists(self::DAEMON_FILE)) { @@ -50,16 +203,26 @@ public function execute() if ($hasStatusUpdate) { $this->getCurrentWeather(); // $this->postStatusToWebsite($fppStatus, null, $this->latestWeather); - + $statusDto = new ShowPulseStatusRequestDto($fppStatus); - $statusDto->setWeatherObservation($this->latestWeather); + if ($this->latestWeather !== null) { + $statusDto->setWeatherObservation($this->latestWeather); + } + $this->postStatusToWebsite($statusDto); $this->lastSequence = $fppStatus->current_sequence; $this->lastSong = $fppStatus->current_song; $this->lastSecondsPlayed = $fppStatus->seconds_played; - $this->getNextRequestedSelection($fppStatus); + // $this->getNextRequestedSelection($fppStatus); + + $selectionResponse = $this->getNextRequestedSelectionFromWebsite(); + if ($selectionResponse->getData() === null) { + $selectionResponse = $this->getRandomSelection(); + } + + $this->postNextRequestedSelectionToFpp($selectionResponse->getData(), $fppStatus, $this->configuration); } else if ($isPlayerFrozen) { throw new Exception("Player appears to be frozen."); } @@ -67,7 +230,7 @@ public function execute() sleep(5); $failureCount = 0; } catch (Exception $exception) { - if ($failureCount < self::MAX_FAILURES_ALLOWED) { + if ($failureCount < self::MAX_FAILURES_ALLOWED + 3) { $failureCount++; $this->logError($exception->getMessage()); } @@ -82,32 +245,6 @@ public function execute() $this->completed(); } - - private function shouldPostStatus($fppStatus) - { - return - !is_null($fppStatus) && - $this->lastSequence === $fppStatus->current_sequence && - $this->lastSong === $fppStatus->current_song; - } - - private function checkPlayerFrozen($fppStatus) - { - return $this->lastSequence === $fppStatus->current_sequence && $this->lastSecondsPlayed === $fppStatus->seconds_played; - } - - private function getCurrentWeather() - { - $timeDifference = time() - $this->lastWeatherUpdateTime; - - if ($timeDifference < self::ONE_HOUR_IN_SECONDS || $this->show === null || $this->show->station_identifier === null) { - return; - } - - $url = "/stations/" . $this->show->station_identifier . "/observations/latest"; - $this->latestWeather = $this->nwsHttpRequest($url, "GET"); - $this->lastWeatherUpdateTime = time(); - } } $command = new DaemonCommand(); diff --git a/commands/RequestedSelectionsClearCommand.php b/commands/RequestedSelectionsClearCommand.php index c34160b..b268193 100644 --- a/commands/RequestedSelectionsClearCommand.php +++ b/commands/RequestedSelectionsClearCommand.php @@ -1,6 +1,6 @@ getStatusFromFpp(); - $this->getNextRequestedSelection($fppStatus); - $this->completed(); - } catch (Exception $exception) { - $this->logError($exception->getMessage()); - } - } -} - -$command = new RequestedSelectionsGetNextCommand(); -$command->execute(); diff --git a/commands/SelectionOptionsAddCommand.php b/commands/SelectionOptionsAddCommand.php index d27d610..5c52fa0 100644 --- a/commands/SelectionOptionsAddCommand.php +++ b/commands/SelectionOptionsAddCommand.php @@ -1,6 +1,6 @@