forked from bmoesdijk/daikin-control-UI-updated
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaikin_response_parser.php
More file actions
34 lines (31 loc) · 957 Bytes
/
Copy pathdaikin_response_parser.php
File metadata and controls
34 lines (31 loc) · 957 Bytes
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
<?php
/*
* Decoder for the `ret=OK,key=val,key2=val2` shape that the Daikin endpoint
* returns.
*/
class DaikinParseException extends RuntimeException {}
function daikin_response_parse($raw) {
if (!is_string($raw) || $raw === '') {
throw new DaikinParseException('empty response');
}
$result = [];
foreach (explode(',', $raw) as $pair) {
if ($pair === '') continue;
$eq = strpos($pair, '=');
if ($eq === false) {
throw new DaikinParseException("malformed pair without '=': $pair");
}
$key = substr($pair, 0, $eq);
$value = substr($pair, $eq + 1);
$result[$key] = $value;
}
return $result;
}
function daikin_response_parse_expecting_ok($raw) {
$parsed = daikin_response_parse($raw);
$ret = $parsed['ret'] ?? '(missing)';
if ($ret !== 'OK') {
throw new DaikinParseException("device returned ret=$ret");
}
return $parsed;
}