summaryrefslogtreecommitdiffstats
path: root/modules-available/locationinfo/inc/apiexternalpanels.inc.php
blob: 27b0322b70dafccb3f2f0ee146f6f74ad5d02a47 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php

/**
 * Helper functions for managing external panels,
 * which are not running on bwLehrpool, but for
 * example, an android app, and need additional
 * callbacks to be managed by slx-admin.
 */
class ApiExternalPanels
{

	private static function getBaseUrl(): string
	{
		$base = Util::shouldRedirectDomain();
		if ($base !== null) {
			$base = 'https://' . $base;
		} else {
			$base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'];
		}
		return $base . '/panel/';
	}

	/**
	 * @return array{server_name: string, panels: array<array{title: string, url: string}>}
	 */
	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;
	}

}