pridana kontrola zmien hladiny a odosielanie notifikacie na Telegram
This commit is contained in:
@ -2,11 +2,72 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
function respondJson(int $statusCode, array $payload): void
|
||||
require_once __DIR__.'/functions.inc.php';
|
||||
|
||||
function loadPreviousRecord(string $filename): ?array
|
||||
{
|
||||
http_response_code($statusCode);
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode($payload) . PHP_EOL;
|
||||
if (!is_file($filename)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$lines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
if ($lines === false || count($lines) < 2) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// The current record has already been appended, so the previous one is the line before it.
|
||||
$previous = json_decode($lines[count($lines) - 2], true);
|
||||
if (!is_array($previous)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $previous;
|
||||
}
|
||||
|
||||
function levelsChanged(?array $previous, array $current): bool
|
||||
{
|
||||
if ($previous === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (['level1', 'level2', 'level3', 'level4'] as $name) {
|
||||
if (!array_key_exists($name, $previous) || (int) $previous[$name] !== (int) $current[$name]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function isValidLevelState(array $levels): bool
|
||||
{
|
||||
$lowerLevelInactive = false;
|
||||
foreach (['level1', 'level2', 'level3', 'level4'] as $name) {
|
||||
if ((int) $levels[$name] === 0) {
|
||||
$lowerLevelInactive = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($lowerLevelInactive) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function calculateFilledPercent(array $levels): int
|
||||
{
|
||||
$activeLevels = 0;
|
||||
foreach (['level1', 'level2', 'level3', 'level4'] as $name) {
|
||||
if ((int) $levels[$name] !== 1) {
|
||||
break;
|
||||
}
|
||||
|
||||
$activeLevels++;
|
||||
}
|
||||
|
||||
return $activeLevels * 25;
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
@ -83,6 +144,23 @@ if ($written === false) {
|
||||
exit;
|
||||
}
|
||||
|
||||
$previousRecord = loadPreviousRecord($filename);
|
||||
if (levelsChanged($previousRecord, $record)) {
|
||||
// Notify only when the level state changed compared to the previous saved measurement.
|
||||
if (!isValidLevelState($record)) {
|
||||
notifyUsers(sprintf(
|
||||
'Chyba merania hladiny kondenzátu klimatizácie (level1=%d, level2=%d, level3=%d, level4=%d)',
|
||||
$record['level1'],
|
||||
$record['level2'],
|
||||
$record['level3'],
|
||||
$record['level4']
|
||||
));
|
||||
} else {
|
||||
$filledPercent = calculateFilledPercent($record);
|
||||
notifyUsers(sprintf('Nádržka kondenzu klimatizácie: %d%%', $filledPercent));
|
||||
}
|
||||
}
|
||||
|
||||
respondJson(200, [
|
||||
'success' => true,
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user