setTimestamp(time()) ->sub(new \DateInterval('P1D')); } elseif ($period == 'week') { $startTime = (new \DateTimeImmutable()) ->setTimestamp(time()) ->sub(new \DateInterval('P1W')); } else { echo '{"status": "error", "message": "Invalid period"}'; exit; } $lines = $door->getEntriesAfter($startTime); if (isset($_GET['edgeonly']) && (bool) htmlspecialchars($_GET['edgeonly'])) { // Ignore repeats $lines = getChanges($lines); } echo json_encode([ 'status' => 'OK', 'entries' => array_map( function ($line) { return [ 'time' => $line->getTimestamp(), 'open' => $line->isOpen(), ]; }, $lines ), ]); } else { // Only last entry $line = $door->getCurrent(); if (is_null($line)) { echo '{"status": "error", "message": "No door data"}'; exit; } echo json_encode([ 'status' => 'OK', 'time' => $line->getTimestamp(), 'open' => $line->isOpen(), ]); } } function handleSetState(): void { 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"}'; exit; } if ((!isset($event->isDoorOpen)) || (!is_bool($event->isDoorOpen))) { echo '{"status": "error", "message": "Invalid door state"}'; exit; } $time = (new \DateTimeImmutable())->setTimestamp((int) $event->time); $door->createEvent($time, $event->isDoorOpen); echo '{"status": "OK"}'; } function getChanges($items) { $prevState = 2; $res = []; foreach ($items as $item) { if ($item->isOpen() !== $prevState) { $res[] = $item; $prevState = $item->isOpen(); } } return $res; }