uprava zapisovani casu do deye (nevyvolava self_sustain), notifikace na discord pri zmene rezimu
This commit is contained in:
@@ -3,7 +3,9 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from datetime import datetime
|
||||
|
||||
import asyncpg
|
||||
import httpx
|
||||
|
||||
from app.config import get_settings
|
||||
@@ -11,6 +13,80 @@ from app.config import get_settings
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _discord_level_for_mode_change(activated_by: str) -> str:
|
||||
if activated_by == "system:mismatch":
|
||||
return "critical"
|
||||
if activated_by.startswith("system:"):
|
||||
return "warning"
|
||||
return "info"
|
||||
|
||||
|
||||
async def notify_operating_mode_changed(
|
||||
site_code: str,
|
||||
previous_mode: str,
|
||||
new_mode: str,
|
||||
activated_by: str,
|
||||
notes: str | None,
|
||||
*,
|
||||
level: str | None = None,
|
||||
) -> None:
|
||||
lvl = level or _discord_level_for_mode_change(activated_by)
|
||||
note_line = f"\nPoznámka: {notes}" if notes else ""
|
||||
msg = (
|
||||
f"Přepnutí provozního režimu – lokalita `{site_code}`\n"
|
||||
f"**{previous_mode}** → **{new_mode}**\n"
|
||||
f"Aktivoval: `{activated_by}`{note_line}"
|
||||
)
|
||||
await send_discord(msg, level=lvl)
|
||||
|
||||
|
||||
async def run_fn_set_mode_with_discord(
|
||||
conn: asyncpg.Connection,
|
||||
site_id: int,
|
||||
mode_code: str,
|
||||
activated_by: str,
|
||||
valid_until: datetime | None,
|
||||
notes: str | None,
|
||||
*,
|
||||
notify_level: str | None = None,
|
||||
) -> str:
|
||||
"""
|
||||
Zavolá ems.fn_set_mode. Při skutečné změně režimu pošle Discord (pokud je webhook).
|
||||
Vrátí aktuální mode_code z DB po volání.
|
||||
"""
|
||||
prev = await conn.fetchval(
|
||||
"SELECT mode_code FROM ems.site_operating_mode WHERE site_id = $1",
|
||||
site_id,
|
||||
)
|
||||
await conn.execute(
|
||||
"SELECT ems.fn_set_mode($1, $2, $3, $4, $5)",
|
||||
site_id,
|
||||
mode_code,
|
||||
activated_by,
|
||||
valid_until,
|
||||
notes,
|
||||
)
|
||||
new = await conn.fetchval(
|
||||
"SELECT mode_code FROM ems.site_operating_mode WHERE site_id = $1",
|
||||
site_id,
|
||||
)
|
||||
if new is None:
|
||||
new = mode_code
|
||||
if prev is not None and prev != new:
|
||||
site_code = await conn.fetchval(
|
||||
"SELECT code FROM ems.site WHERE id = $1", site_id
|
||||
)
|
||||
await notify_operating_mode_changed(
|
||||
site_code or str(site_id),
|
||||
str(prev),
|
||||
str(new),
|
||||
activated_by,
|
||||
notes,
|
||||
level=notify_level,
|
||||
)
|
||||
return str(new)
|
||||
|
||||
|
||||
async def send_discord(message: str, level: str = "info") -> bool:
|
||||
"""
|
||||
Pošle notifikaci na Discord webhook.
|
||||
@@ -65,6 +141,21 @@ async def notify_self_sustain_activated(site_code: str, reason: str) -> None:
|
||||
await send_discord(msg, level="critical")
|
||||
|
||||
|
||||
async def notify_modbus_clock_verify_exhausted(
|
||||
site_code: str,
|
||||
asset_code: str,
|
||||
written: tuple[int, int, int],
|
||||
actual: tuple[int, int, int],
|
||||
) -> None:
|
||||
msg = (
|
||||
f"Modbus **systémový čas 62–64** – po 3 neúspěšných ověřeních **bez** přepnutí režimu.\n"
|
||||
f"Lokalita `{site_code}`, zařízení `{asset_code}`\n"
|
||||
f"Zapsáno: `{written}` | Přečteno: `{actual}`\n"
|
||||
f"Doporučení: zkontrolovat firmware/RS485; režim EMS se nemění automaticky."
|
||||
)
|
||||
await send_discord(msg, level="critical")
|
||||
|
||||
|
||||
async def notify_daily_economics(
|
||||
site_code: str,
|
||||
day: str,
|
||||
|
||||
Reference in New Issue
Block a user