Testing door sensor graphing

This commit is contained in:
2021-10-11 16:23:18 +02:00
parent 2ba0266fca
commit a4ce890a36
3 changed files with 125 additions and 1 deletions

View File

@@ -27,6 +27,7 @@ if($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_GET["period"])) {
$period = (string)htmlspecialchars($_GET["period"]);
if ($period == "day") {
$startTime = time() - (60*60*24);
} else if ($period == "week") {
@@ -37,6 +38,11 @@ if($_SERVER['REQUEST_METHOD'] === 'POST') {
}
$lines = $door->getEntriesAfter($startTime);
if (isset($_GET["period"]) && (bool)htmlspecialchars($_GET["edgeonly"])) {
//Ignore repeats
$lines = getChanges($lines);
}
echo json_encode([
'status' => "OK",
'entries' => $lines
@@ -70,4 +76,18 @@ function handleSetState() {
$door->createEvent((int)($event->time), (bool)($event->isDoorOpen));
echo '{"status": "OK"}';
}
}
function getChanges($items) {
$prevState = 2;
$res = [];
foreach($items as $item) {
if ($item["open"] !== $prevState) {
array_push($res, $item);
$prevState = $item["open"];
}
}
return $res;
}