|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Commands; |
| 4 | + |
| 5 | +$commonFile = $testing ? __DIR__ . "/tests/OptFppWwwCommonMock.php" : "/opt/fpp/www/common.php"; |
| 6 | +require_once $commonFile; |
| 7 | + |
| 8 | +interface ShowPulseCommandHandlerInterface |
| 9 | +{ |
| 10 | + protected function execute(); |
| 11 | +} |
| 12 | + |
| 13 | +abstract class BaseCommandHandler |
| 14 | +{ |
| 15 | + private $token; |
| 16 | + private $showUuid; |
| 17 | + private $websiteApiUrl; |
| 18 | + |
| 19 | + protected function fppHttpRequest($route, $method = "GET", $data = null) |
| 20 | + { |
| 21 | + $headers = array(); |
| 22 | + |
| 23 | + array_push($headers, "Authorization: Bearer $this->token"); |
| 24 | + |
| 25 | + $url = "https://127.0.0.1/api/"; |
| 26 | + return $this->httpRequest($url, $route, $method, $data); |
| 27 | + } |
| 28 | + |
| 29 | + protected function httpRequest($url, $route, $method = "GET", $data = null) |
| 30 | + { |
| 31 | + $headers = array(); |
| 32 | + |
| 33 | + |
| 34 | + array_push($headers, "Content-Type: application/json"); |
| 35 | + array_push($headers, "Accept: application/json"); |
| 36 | + |
| 37 | + $url = $url . $route; |
| 38 | + |
| 39 | + $ch = curl_init($url); |
| 40 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); |
| 41 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 42 | + |
| 43 | + $method = strtoupper($method); |
| 44 | + if ($method === "POST" || $method === "PUT") { |
| 45 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
| 46 | + } |
| 47 | + |
| 48 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
| 49 | + |
| 50 | + $response = curl_exec($ch); |
| 51 | + |
| 52 | + if ($response === false) { |
| 53 | + return $this->logError("cURL error: " . curl_error($ch)); |
| 54 | + } |
| 55 | + |
| 56 | + curl_close($ch); |
| 57 | + |
| 58 | + if ($response === null) { |
| 59 | + return $this->logError("Bad response returned."); |
| 60 | + } |
| 61 | + |
| 62 | + return json_decode($response, true); |
| 63 | + } |
| 64 | + |
| 65 | + protected function getShowUuid() |
| 66 | + { |
| 67 | + return $this->showUuid; |
| 68 | + } |
| 69 | + |
| 70 | + public function logError($message) |
| 71 | + { |
| 72 | + $currentDateTime = date('Y-m-d h:i:s A'); |
| 73 | + error_log("$currentDateTime: $message"); |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + protected function isNullOrEmpty($value) |
| 78 | + { |
| 79 | + return is_null($value) || empty($value); |
| 80 | + } |
| 81 | + |
| 82 | + protected function isNotNullOrEmpty($value) |
| 83 | + { |
| 84 | + return !$this->isNullOrEmpty($value); |
| 85 | + } |
| 86 | + |
| 87 | + protected function loadConfiguration(): bool |
| 88 | + { |
| 89 | + $configFile = GetDirSetting("uploads") . "/showpulse.json"; |
| 90 | + $contents = file_get_contents($configFile); |
| 91 | + |
| 92 | + if ($contents === false) { |
| 93 | + return $this->logError("Configuration file not found or unable to be loaded. Download configuration file contents from the Light Show Pulse website. Then restart FPPD."); |
| 94 | + } |
| 95 | + |
| 96 | + $json = json_decode($contents, false); |
| 97 | + |
| 98 | + $this->showUuid = $json->show_id; |
| 99 | + $this->token = $json->token; |
| 100 | + $this->websiteApiUrl = $json->host; |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + protected function getStatusFromFpp() |
| 105 | + { |
| 106 | + $fppStatus = $this->httpRequest(true, "fppd/status"); |
| 107 | + |
| 108 | + if ($this->isNullOrEmpty($fppStatus)) { |
| 109 | + return $this->logError("Unable to get latest status from FPP."); |
| 110 | + } |
| 111 | + |
| 112 | + return $fppStatus; |
| 113 | + } |
| 114 | + |
| 115 | + protected function postStatusToWebsite($statusDto) |
| 116 | + { |
| 117 | + $response = $this->httpRequest( |
| 118 | + false, |
| 119 | + "show-statuses/add/" . $this->getShowUuid(), |
| 120 | + "POST", |
| 121 | + $statusDto |
| 122 | + ); |
| 123 | + |
| 124 | + if ($response->failed) { |
| 125 | + return $this->logError("Unable to update show status. " . $response->message); |
| 126 | + } |
| 127 | + |
| 128 | + return $response; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +final class ShowPulseConstant |
| 133 | +{ |
| 134 | + public const FPP_STATUS_IDLE = 0; |
| 135 | + public const GRACEFUL_RESTART = "GRACEFUL RESTART"; |
| 136 | + public const GRACEFUL_SHUTDOWN = "GRACEFUL SHUTDOWN"; |
| 137 | + public const GRACEFUL_STOP = "GRACEFUL STOP"; |
| 138 | + public const HIGH_PRIORITY = 10; |
| 139 | + public const IMMEDIATE_RESTART = "IMMEDIATE RESTART"; |
| 140 | + public const IMMEDIATE_SHUTDOWN = "IMMEDIATE SHUTDOWN"; |
| 141 | + public const IMMEDIATE_STOP = "IMMEDIATE STOP"; |
| 142 | + public const MAX_FAILURES_ALLOWED = 5; |
| 143 | + public const SLEEP_SHORT_VALUE = 5; |
| 144 | + public const DAEMON_FILE = "/home/fpp/media/plugins/show-pulse-fpp/daemon.run"; |
| 145 | +} |
| 146 | + |
| 147 | +final class ShowPulseApiResponseDto |
| 148 | +{ |
| 149 | + public $success; |
| 150 | + public $failed; |
| 151 | + public $message; |
| 152 | + public $data; |
| 153 | +} |
0 commit comments