getEntriesAfter($startTime); if (isset($_GET["edgeonly"]) && (bool)htmlspecialchars($_GET["edgeonly"])) { //Ignore repeats $lines = getChanges($lines); } echo json_encode([ 'status' => "OK", 'entries' => $lines ]); } else { //Only last entry $line = (object)$door->getCurrent(); echo json_encode([ 'status' => "OK", 'time' => $line->time, 'open' => $line->open ]); } } function handleSetState() { global $door; $jsonobj = file_get_contents('php://input'); $event = json_decode($jsonobj); if ((!isset($event->time)) || (!is_numeric($event->time))) { echo '{"status": "error", "message": "Invalid timestamp"}'; die(); } if ((!isset($event->isDoorOpen)) || (!is_bool($event->isDoorOpen))) { echo '{"status": "error", "message": "Invalid door state"}'; die(); } $door->createEvent((int)($event->time), $event->isDoorOpen ? 1 : 0); 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; }