Wrap door status entries in dataclass
This commit is contained in:
@@ -6,6 +6,28 @@ namespace pvv\side;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
class DoorStatus {
|
||||
private DateTimeImmutable $time;
|
||||
private bool $open;
|
||||
|
||||
public function __construct(DateTimeImmutable $time, bool $open) {
|
||||
$this->time = $time;
|
||||
$this->open = $open;
|
||||
}
|
||||
|
||||
public function getTime(): DateTimeImmutable {
|
||||
return $this->time;
|
||||
}
|
||||
|
||||
public function getTimeStamp(): int {
|
||||
return $this->time->getTimestamp();
|
||||
}
|
||||
|
||||
public function isOpen(): bool {
|
||||
return $this->open;
|
||||
}
|
||||
}
|
||||
|
||||
class Door {
|
||||
private $pdo;
|
||||
|
||||
@@ -16,7 +38,7 @@ class Door {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{time: DateTimeImmutable, open: bool}[]
|
||||
* @return DoorStatus[]
|
||||
*/
|
||||
public function getAll(): array {
|
||||
$query = '
|
||||
@@ -29,19 +51,21 @@ class Door {
|
||||
$statement = $this->pdo->prepare($query);
|
||||
$statement->execute();
|
||||
|
||||
$doorEvents = [];
|
||||
foreach ($statement->fetchAll() as $row) {
|
||||
$doorEvents[] = [
|
||||
'time' => (new DateTimeImmutable)->setTimestamp((int) $row['time']),
|
||||
'open' => (bool) $row['open'],
|
||||
];
|
||||
}
|
||||
$result = array_map(
|
||||
function ($row) {
|
||||
return new DoorStatus(
|
||||
(new DateTimeImmutable)->setTimestamp((int) $row['time']),
|
||||
(bool) $row['open'],
|
||||
);
|
||||
},
|
||||
$statement->fetchAll(),
|
||||
);
|
||||
|
||||
return $doorEvents;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{time: DateTimeImmutable, open: bool}[]
|
||||
* @return DoorStatus[]
|
||||
*/
|
||||
public function getEntriesAfter(\DateTimeImmutable $startTime): array {
|
||||
$timestamp = $startTime->getTimestamp();
|
||||
@@ -58,21 +82,20 @@ class Door {
|
||||
$statement->bindParam(':startTime', $timestamp, \PDO::PARAM_INT);
|
||||
$statement->execute();
|
||||
|
||||
$doorEvents = [];
|
||||
foreach ($statement->fetchAll() as $row) {
|
||||
$doorEvents[] = [
|
||||
'time' => (new DateTimeImmutable)->setTimestamp((int) $row['time']),
|
||||
'open' => (bool) $row['open'],
|
||||
];
|
||||
}
|
||||
$result = array_map(
|
||||
function ($row) {
|
||||
return new DoorStatus(
|
||||
(new DateTimeImmutable)->setTimestamp((int) $row['time']),
|
||||
(bool) $row['open'],
|
||||
);
|
||||
},
|
||||
$statement->fetchAll(),
|
||||
);
|
||||
|
||||
return $doorEvents;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ?array{time: DateTimeImmutable, open: bool}
|
||||
*/
|
||||
public function getCurrent(): ?array {
|
||||
public function getCurrent(): ?DoorStatus {
|
||||
$query = '
|
||||
SELECT
|
||||
time,
|
||||
@@ -89,10 +112,12 @@ class Door {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'time' => (new DateTimeImmutable)->setTimestamp((int) $row['time']),
|
||||
'open' => (bool) $row['open'],
|
||||
];
|
||||
$result = new DoorStatus(
|
||||
(new DateTimeImmutable)->setTimestamp((int) $row['time']),
|
||||
(bool) $row['open'],
|
||||
);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function removeOld(): void {
|
||||
|
||||
@@ -50,13 +50,16 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'status' => 'OK',
|
||||
'entries' => array_map(function ($line) {
|
||||
return [
|
||||
'time' => $line['time']->getTimestamp(),
|
||||
'open' => $line['open'],
|
||||
];
|
||||
}, $lines),
|
||||
'status' => 'OK',
|
||||
'entries' => array_map(
|
||||
function ($line) {
|
||||
return [
|
||||
'time' => $line->getTimestamp(),
|
||||
'open' => $line->isOpen(),
|
||||
];
|
||||
},
|
||||
$lines
|
||||
),
|
||||
]);
|
||||
} else {
|
||||
// Only last entry
|
||||
@@ -65,11 +68,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
echo '{"status": "error", "message": "No door data"}';
|
||||
exit;
|
||||
}
|
||||
$line = (object) $line;
|
||||
echo json_encode([
|
||||
'status' => 'OK',
|
||||
'time' => $line->time->getTimestamp(),
|
||||
'open' => $line->open,
|
||||
'status' => 'OK',
|
||||
'time' => $line->getTimestamp(),
|
||||
'open' => $line->isOpen(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -101,9 +103,9 @@ function getChanges($items) {
|
||||
$res = [];
|
||||
|
||||
foreach ($items as $item) {
|
||||
if ($item['open'] !== $prevState) {
|
||||
if ($item->isOpen() !== $prevState) {
|
||||
$res[] = $item;
|
||||
$prevState = $item['open'];
|
||||
$prevState = $item->isOpen();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
use pvv\side\DoorStatus;
|
||||
|
||||
require_once dirname(__DIR__) . implode(\DIRECTORY_SEPARATOR, ['', 'inc', 'include.php']);
|
||||
|
||||
$translation = ['I dag', 'I morgen', 'Denne uka', 'Neste uke', 'Denne måneden', 'Neste måned'];
|
||||
@@ -10,25 +13,23 @@ $motd = $motdfetcher->getMOTD();
|
||||
|
||||
$door = new pvv\side\Door($pdo);
|
||||
$doorEntry = $door->getCurrent();
|
||||
if (!is_null($doorEntry)) {
|
||||
$doorEntry = (object) $doorEntry;
|
||||
} else {
|
||||
$doorEntry = (object) [
|
||||
'time' => new DateTimeImmutable('@0'),
|
||||
'open' => false,
|
||||
];
|
||||
if (is_null($doorEntry)) {
|
||||
$doorEntry = new DoorStatus(
|
||||
new DateTimeImmutable('@0'),
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
if ($doorEntry->time->getTimestamp() < (time() - 60 * 30)) {
|
||||
if ($doorEntry->getTimestamp() < (time() - 60 * 30)) {
|
||||
$doorStateText = 'Ingen data fra dørsensor';
|
||||
} else {
|
||||
if ($doorEntry->open) {
|
||||
if ($doorEntry->isOpen()) {
|
||||
$doorStateText = 'Døren er <b>åpen</b>';
|
||||
} else {
|
||||
$doorStateText = 'Døren er <b>ikke åpen</b>';
|
||||
}
|
||||
}
|
||||
$doorTime = $doorEntry->time->format('H:i');
|
||||
$doorTime = $doorEntry->getTime()->format('H:i');
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="no">
|
||||
@@ -64,7 +65,7 @@ $doorTime = $doorEntry->time->format('H:i');
|
||||
<a class="btn" href="om/"><li>Om PVV</li></a>
|
||||
<a class="btn focus" href="paamelding/"><li>Bli medlem!</li></a>
|
||||
<a class="btn" href="https://use.mazemap.com/#config=ntnu&v=1&zlevel=2¢er=10.406281,63.417093&zoom=19.5&campuses=ntnu&campusid=1&sharepoitype=poi&sharepoi=38159&utm_medium=longurl">Veibeskrivelse</li></a>
|
||||
<div id="doorIndicator" class="<?php echo $doorEntry->open ? 'doorIndicator_OPEN' : 'doorIndicator_CLOSED'; ?>" onclick="location.href='/door/graph.html'">
|
||||
<div id="doorIndicator" class="<?php echo $doorEntry->isOpen() ? 'doorIndicator_OPEN' : 'doorIndicator_CLOSED'; ?>" onclick="location.href='/door/graph.html'">
|
||||
<p class="doorStateText"><?php echo $doorStateText; ?></p>
|
||||
<p class="doorStateTime">(Oppdatert <?php echo $doorTime; ?>)</p>
|
||||
</div>
|
||||
|
||||
@@ -10,14 +10,14 @@ $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
$door = new pvv\side\Door($pdo);
|
||||
$doorEntry = $door->getCurrent();
|
||||
if (!is_null($doorEntry)) {
|
||||
$doorEntry = (object) $doorEntry;
|
||||
} else {
|
||||
$doorEntry = (object) [
|
||||
'time' => new DateTimeImmutable('@0'),
|
||||
'open' => false,
|
||||
];
|
||||
}
|
||||
// if (!is_null($doorEntry)) {
|
||||
// $doorEntry = (object) $doorEntry;
|
||||
// } else {
|
||||
// $doorEntry = (object) [
|
||||
// 'time' => new DateTimeImmutable('@0'),
|
||||
// 'open' => false,
|
||||
// ];
|
||||
// }
|
||||
|
||||
?>
|
||||
{
|
||||
@@ -43,9 +43,9 @@ if (!is_null($doorEntry)) {
|
||||
},
|
||||
"issue_report_channels": ["email"],
|
||||
"state": {
|
||||
"open": <?php echo $doorEntry->open ? 'true' : 'false'; ?>,
|
||||
"lastchange": <?php echo $doorEntry->time->getTimestamp(); ?>,
|
||||
"message": "<?php echo $doorEntry->open ? 'open for public, members are present' : 'closed'; ?>"
|
||||
"open": <?php echo $doorEntry->isOpen() ? 'true' : 'false'; ?>,
|
||||
"lastchange": <?php echo $doorEntry->getTimeStamp(); ?>,
|
||||
"message": "<?php echo $doorEntry->isOpen() ? 'open for public, members are present' : 'closed'; ?>"
|
||||
},
|
||||
"feeds": {
|
||||
"wiki": {
|
||||
|
||||
Reference in New Issue
Block a user