door: return datetime objects for core functions

This commit is contained in:
2026-01-13 09:27:28 +09:00
parent 044444eaa8
commit 1766cc23d6
4 changed files with 22 additions and 13 deletions

View File

@@ -4,6 +4,8 @@ declare(strict_types=1);
namespace pvv\side;
use DateTimeImmutable;
class Door {
private $pdo;
@@ -14,7 +16,7 @@ class Door {
}
/**
* @return array{time: int, open: bool}[]
* @return array{time: DateTimeImmutable, open: bool}[]
*/
public function getAll(): array {
$query = 'SELECT time, open FROM door ORDER BY time DESC';
@@ -24,7 +26,7 @@ class Door {
$doorEvents = [];
foreach ($statement->fetchAll() as $row) {
$doorEvents[] = [
'time' => (int) $row['time'],
'time' => (new DateTimeImmutable)->setTimestamp((int) $row['time']),
'open' => (bool) $row['open'],
];
}
@@ -33,19 +35,21 @@ class Door {
}
/**
* @return array{time: int, open: bool}[]
* @return array{time: DateTimeImmutable, open: bool}[]
*/
public function getEntriesAfter(\DateTimeImmutable $startTime): array {
$timestamp = $startTime->getTimestamp();
$query
= 'SELECT time, open FROM door WHERE time > :startTime ORDER BY time DESC';
$statement = $this->pdo->prepare($query);
$statement->bindParam(':startTime', $startTime->getTimestamp(), \PDO::PARAM_INT);
$statement->bindParam(':startTime', $timestamp, \PDO::PARAM_INT);
$statement->execute();
$doorEvents = [];
foreach ($statement->fetchAll() as $row) {
$doorEvents[] = [
'time' => (int) $row['time'],
'time' => (new DateTimeImmutable)->setTimestamp((int) $row['time']),
'open' => (bool) $row['open'],
];
}
@@ -54,7 +58,7 @@ class Door {
}
/**
* @return array{time: int, open: bool}
* @return array{time: DateTimeImmutable, open: bool}
*/
public function getCurrent(): array {
$query = 'SELECT time, open FROM door ORDER BY time DESC LIMIT 1';
@@ -63,7 +67,7 @@ class Door {
$row = $statement->fetch();
return [
'time' => (int) $row['time'],
'time' => (new DateTimeImmutable)->setTimestamp((int) $row['time']),
'open' => (bool) $row['open'],
];
}
@@ -72,7 +76,7 @@ class Door {
$firstValidTime = time() - 60 * 60 * 24 * self::DAYS_OF_DOOR_HISTORY;
$query = 'DELETE FROM door WHERE time < :firstValid';
$statement = $this->pdo->prepare($query);
$statement->bindParam(':firstValid', $firstValidTime, \PDO::PARAM_STR);
$statement->bindParam(':firstValid', $firstValidTime, \PDO::PARAM_INT);
$statement->execute();
}

View File

@@ -51,14 +51,19 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo json_encode([
'status' => 'OK',
'entries' => $lines,
'entries' => array_map(function ($line) {
return [
'time' => $line['time']->getTimestamp(),
'open' => $line['open'],
];
}, $lines),
]);
} else {
// Only last entry
$line = (object) $door->getCurrent();
echo json_encode([
'status' => 'OK',
'time' => $line->time,
'time' => $line->time->getTimestamp(),
'open' => $line->open,
]);
}

View File

@@ -10,7 +10,7 @@ $motd = $motdfetcher->getMOTD();
$door = new pvv\side\Door($pdo);
$doorEntry = (object) $door->getCurrent();
if ($doorEntry->time < (time() - 60 * 30)) {
if ($doorEntry->time->getTimestamp() < (time() - 60 * 30)) {
$doorStateText = 'Ingen data fra dørsensor';
} else {
if ($doorEntry->open) {
@@ -19,7 +19,7 @@ if ($doorEntry->time < (time() - 60 * 30)) {
$doorStateText = 'Døren er <b>ikke åpen</b>';
}
}
$doorTime = date('H:i', $doorEntry->time);
$doorTime = $doorEntry->time->format('H:i');
?>
<!DOCTYPE html>
<html lang="no">

View File

@@ -36,7 +36,7 @@ $doorEntry = (object) $door->getCurrent();
"issue_report_channels": ["email"],
"state": {
"open": <?php echo $doorEntry->open ? 'true' : 'false'; ?>,
"lastchange": <?php echo $doorEntry->time ? $doorEntry->time : 0; ?>,
"lastchange": <?php echo $doorEntry->time->getTimestamp(); ?>,
"message": "<?php echo $doorEntry->open ? 'open for public, members are present' : 'closed'; ?>"
},
"feeds": {