forked from bmoesdijk/daikin-control-UI-updated
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
72 lines (66 loc) · 2.19 KB
/
Copy pathapi.php
File metadata and controls
72 lines (66 loc) · 2.19 KB
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
<?php
/*
* Thin HTTP glue between the browser and the Daikin wifi module.
* Most code for this is in daikin_client.php (network I/O) and
* daikin_response_parser.php (response decoding).
*/
require_once __DIR__ . '/daikin_client.php';
require_once __DIR__ . '/daikin_response_parser.php';
require_once __DIR__ . '/timer_store.php';
header('Content-Type: application/json');
$method = $_SERVER['REQUEST_METHOD'];
$unit_ip = $_GET['unit_ip'] ?? '';
if ($method === 'POST') {
$body = json_decode(file_get_contents('php://input'), true);
if (!is_array($body)) {
http_response_code(400);
echo json_encode(['error' => 'request body must be a JSON object']);
exit;
}
try {
$raw = daikin_post($unit_ip, '/aircon/set_control_info', $body);
$parsed = daikin_response_parse($raw);
echo json_encode($parsed);
} catch (DaikinClientException $e) {
http_response_code(503);
echo json_encode(['error' => $e->getMessage()]);
} catch (DaikinParseException $e) {
http_response_code(502);
echo json_encode(['error' => $e->getMessage()]);
}
exit;
}
if ($method === 'GET') {
$uri = $_GET['uri'] ?? '';
$allowed_uris = [
'/aircon/get_sensor_info',
'/aircon/get_control_info',
'/aircon/get_week_power',
'/aircon/get_year_power',
'/aircon/get_week_power_ex',
'/aircon/get_year_power_ex',
];
if (!in_array($uri, $allowed_uris, true)) {
http_response_code(405);
echo json_encode(['error' => 'unsupported uri']);
exit;
}
try {
$raw = daikin_get($unit_ip, $uri);
$parsed = daikin_response_parse($raw);
$json = json_encode($parsed);
if ($uri === '/aircon/get_control_info') {
last_control_cache_write($unit_ip, $json);
}
echo $json;
} catch (DaikinClientException $e) {
http_response_code(503);
echo json_encode(['error' => $e->getMessage()]);
} catch (DaikinParseException $e) {
http_response_code(502);
echo json_encode(['error' => $e->getMessage()]);
}
exit;
}
http_response_code(405);
echo json_encode(['error' => 'method not allowed']);