} */ public static function generatePublicPanelList(): array { $base = self::getBaseUrl(); $result = [ 'server_name' => 'To be filled by O.E.M.', 'panels' => [], ]; $res = Database::simpleQuery("SELECT paneluuid, panelname FROM locationinfo_panel WHERE ispublic = 1"); foreach ($res as $row) { $result['panels'][] = [ 'uuid' => $row['paneluuid'], 'title' => $row['panelname'], 'url' => $base . $row['paneluuid'], ]; } return $result; } /** * Register an external device with the server. */ public static function registerDevice(): array { $deviceuuid = Request::any('deviceuuid', Request::REQUIRED, 'string'); if (!Util::isValidUuid($deviceuuid)) { http_response_code(400); return ['error' => 'Invalid external device UUID']; } $paneluuid = Request::any('paneluuid', null, 'string'); $titletext = Request::any('title', '', 'string'); $ret = Database::exec("INSERT INTO locationinfo_externaldevice (deviceuuid, paneluuid, title, registertime, registerip) VALUES (:deviceuuid, :paneluuid, :title, UNIX_TIMESTAMP(), :ip)", [ 'deviceuuid' => $deviceuuid, 'paneluuid' => $paneluuid, 'title' => $titletext, 'ip' => Util::getClientIp() ], true); if ($ret === false) { http_response_code(409); return ['error' => 'Failed to register external device; device uuid already registered, or invalid paneluuid']; } return ['success' => 'External device registered successfully']; } /** * To be called every 5 minutes by a running external device. * We'll consider devices that didn't report in for more than 10 * minutes as offline. * This also returns an array with configuration information to * the device, and the app running on the device should then check * if any of the parameters changed and react to this accordingly. * @return array{url: string, brightness_pct: int, reboot_hour: int} */ public static function checkinCallback(): array { $row = Database::queryFirst("SELECT deviceuuid, isregistered, configjson FROM locationinfo_externaldevice WHERE deviceuuid = :deviceuuid", ['deviceuuid' => Request::any('deviceuuid', Request::REQUIRED, 'string')]); if ($row === false) { http_response_code(404); return ['error' => 'Unknown external panel uuid, please register first.']; } if ($row['paneluuid'] === null) { http_response_code(202); return ['error' => 'Your external device uuid has been submitted for registration, please wait for an Administrator to set up a configuration.']; } $config = json_decode($row['configjson'], true); if (!is_array($config)) { $config = []; } $config['time_ms'] = ceil(microtime(true) * 1000); $config['url'] = self::getBaseUrl() . $row['paneluuid']; Database::exec("UPDATE locationinfo_externaldevice SET lastseen = UNIX_TIMESTAMP() WHERE deviceuuid = :deviceuuid", ['deviceuuid' => $row['deviceuuid']]); return $config; } }