Use Home Assistant notifications

This commit is contained in:
seo
2026-06-21 07:30:39 +09:00
parent 81a24530d6
commit 49394a8ee5
15 changed files with 234 additions and 2371 deletions
+22 -78
View File
@@ -1275,7 +1275,7 @@ function signed_delta_compact(float $value, int $decimals, string $suffix = ''):
return $sign . number_format($value, $decimals) . $suffix;
}
function push_process_name(array $row): string
function display_process_name(array $row): string
{
$service = (string)($row['service'] ?? '');
if ($service !== '' && $service !== 'N/A') {
@@ -1344,7 +1344,7 @@ function expected_process_text(array $processes): string
}
if ($cpu !== null && (float)($cpu['cpu_percent'] ?? 0) >= 1.0) {
return 'CPU ' . push_process_name($cpu);
return 'CPU ' . display_process_name($cpu);
}
$mem = null;
@@ -1358,7 +1358,7 @@ function expected_process_text(array $processes): string
}
if ($mem !== null) {
return 'MEM ' . push_process_name($mem);
return 'MEM ' . display_process_name($mem);
}
return 'CPU/MEM 원인 후보 없음';
@@ -1398,17 +1398,17 @@ function expected_process_detail_text(array $processes): string
}
if ($cpu !== null && $mem !== null && process_identity($mem) === process_identity($cpu)) {
return push_process_name($cpu);
return display_process_name($cpu);
}
$parts = [];
if ($cpu !== null) {
$parts[] = sprintf('CPU %.1f%% %s', (float)($cpu['cpu_percent'] ?? 0), push_process_name($cpu));
$parts[] = sprintf('CPU %.1f%% %s', (float)($cpu['cpu_percent'] ?? 0), display_process_name($cpu));
}
if ($mem !== null) {
$parts[] = sprintf('RAM %.1f%% %s', (float)($mem['mem_percent'] ?? 0), push_process_name($mem));
$parts[] = sprintf('RAM %.1f%% %s', (float)($mem['mem_percent'] ?? 0), display_process_name($mem));
}
return $parts === [] ? '원인 후보 없음' : implode(' / ', $parts);
@@ -1439,7 +1439,7 @@ function notice_downward_only(float $tempDelta, float $rpmDelta): bool
return true;
}
function send_fan_spike_push(array $spike, array $fan, array $system, array $processes): array
function send_fan_spike_notify(array $spike, array $fan, array $system, array $processes): array
{
$tempDelta = (float)($spike['temp_delta'] ?? 0);
$rpmDelta = (float)($spike['rpm_delta'] ?? 0);
@@ -1476,13 +1476,18 @@ function send_fan_spike_push(array $spike, array $fan, array $system, array $pro
. number_format((float)($fan['pwm'] ?? 0), 0)
. (notice_downward_only($tempDelta, $rpmDelta) ? '' : "\n원인 후보: " . expected_process_detail_text($processes));
return send_push_payload([
return send_ha_notify([
'title' => '시스템 유의사항',
'body' => $body,
'message' => $body,
'level' => 'warning',
'url' => '/',
'tag' => 'system-notice-' . date('YmdHi'),
'created_at' => date('Y-m-d H:i:s'),
'tag' => 'control-system-notice',
'data' => [
'channel' => 'control_system',
'notification_icon' => 'mdi:fan-alert',
'color' => '#f59e0b',
'sticky' => 'false',
'renotify' => 'true',
'summary' => $spike['summary'] ?? '',
'rpm_delta' => $spike['rpm_delta'] ?? 0,
'pwm_delta' => $spike['pwm_delta'] ?? 0,
@@ -1492,16 +1497,9 @@ function send_fan_spike_push(array $spike, array $fan, array $system, array $pro
]);
}
function latest_system_notice_push_epoch(): int
function latest_system_notice_notify_epoch(): int
{
$stmt = db()->query("
SELECT UNIX_TIMESTAMP(MAX(created_at))
FROM push_event_logs
WHERE event = 'send_success'
AND meta LIKE '%\"tag\":\"system-notice-%'
");
return (int)($stmt->fetchColumn() ?: 0);
return latest_ha_notify_epoch_by_tag('control-system-notice');
}
function fan_spike_history(int $limit = 100): array
@@ -1981,14 +1979,14 @@ function collect_snapshot(bool $applyFan = true): array
$fanSpike = fan_spike_analysis($history, $fan, $system, $processes);
if (!empty($fanSpike['active'])) {
$latestNoticePush = latest_system_notice_push_epoch();
$latestNoticeNotify = latest_system_notice_notify_epoch();
$spikeLogId = add_fan_spike_log($fanSpike, $fan, $system, $processes);
if ($spikeLogId > 0) {
$fanSpike['log_id'] = $spikeLogId;
if ($latestNoticePush <= 0 || time() - $latestNoticePush >= 600) {
$fanSpike['push'] = send_fan_spike_push($fanSpike, $fan, $system, $processes);
if ($latestNoticeNotify <= 0 || time() - $latestNoticeNotify >= 600) {
$fanSpike['notify'] = send_fan_spike_notify($fanSpike, $fan, $system, $processes);
} else {
$fanSpike['push'] = [
$fanSpike['notify'] = [
'sent' => 0,
'failed' => 0,
'skipped' => 'system_notice_cooldown',
@@ -2040,60 +2038,6 @@ function control_api_dispatch(): void
]);
}
if ($action === 'push_devices') {
json_out([
'ok' => true,
'data' => [
'devices' => push_device_rows(),
'summary' => push_health_summary(),
],
]);
}
if ($action === 'send_push_healthcheck') {
require_csrf();
json_out([
'ok' => true,
'data' => [
'result' => send_push_healthcheck_if_due(24, true),
'devices' => push_device_rows(),
'summary' => push_health_summary(),
],
]);
}
if ($action === 'push_status') {
json_out([
'ok' => true,
'data' => push_subscription_status((string)($_GET['endpoint'] ?? $_POST['endpoint'] ?? '')),
]);
}
if ($action === 'delete_push_device') {
delete_push_device((string)($_POST['endpoint_hash'] ?? ''));
json_out([
'ok' => true,
'data' => [
'devices' => push_device_rows(),
'summary' => push_health_summary(),
],
]);
}
if ($action === 'delete_push_endpoint') {
delete_push_endpoint((string)($_POST['endpoint'] ?? ''));
json_out([
'ok' => true,
'data' => [
'devices' => push_device_rows(),
'summary' => push_health_summary(),
],
]);
}
if ($action === 'fan') {
$mode = (string)($_POST['mode'] ?? 'auto');
$pwm = max(0, min(255, (int)($_POST['pwm'] ?? 120)));