app.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. """Nutikodu andmete veebiliides (FastAPI).
  2. Loeb ClickHouse'i `uus67` skeemist (zigbee andurid) ja serveerib:
  3. GET / -> veebirakendus (static/index.html)
  4. GET /api/meta -> seadmed, tz, andmete ulatus
  5. GET /api/temperatuur -> aegrida (temp + niiskus), agg=hour|day
  6. GET /api/ukseandur -> ukseolekute sündmused (tulevikuks)
  7. GET /api/vead -> häirete sündmused (tulevikuks)
  8. GET /api/latest -> viimased väärtused + lahtised häired
  9. GET /health
  10. ClickHouse'i päringud kasutavad `zigbee` kasutajat (SELECT uus67.*) ja
  11. `param_*` parameetreid (SQL-süst välistatud). Tulemused loetakse
  12. `FORMAT JSONEachRow` kujul (üks JSON-objekt rea kohta).
  13. Valikuline kaitse: WEB_ACCESS_TOKEN keskkonnamuutuja (tühi = kaitseta).
  14. """
  15. from __future__ import annotations
  16. import hmac
  17. import html
  18. import secrets
  19. import json
  20. import os
  21. import re
  22. import threading
  23. import time
  24. from collections import defaultdict
  25. from datetime import datetime, timedelta
  26. from pathlib import Path
  27. from zoneinfo import ZoneInfo
  28. import requests
  29. from fastapi import Body, Depends, FastAPI, Form, HTTPException, Query, Request
  30. from fastapi.responses import FileResponse, HTMLResponse, RedirectResponse
  31. from fastapi.staticfiles import StaticFiles
  32. from starlette.middleware.sessions import SessionMiddleware
  33. ROOT = Path(__file__).parent
  34. STATIC_DIR = ROOT / "static"
  35. TZ = ZoneInfo("Europe/Tallinn")
  36. CH_URL = os.environ.get("CH_URL", "http://clickhouse:8123").rstrip("/")
  37. CH_USER = os.environ.get("CH_USER", "zigbee")
  38. CH_PASS = os.environ.get("CH_PASS", "")
  39. CH_DB = os.environ.get("CH_DB", "uus67")
  40. WEB_ACCESS_TOKEN = os.environ.get("WEB_ACCESS_TOKEN", "")
  41. WEB_PASSWORD = os.environ.get("WEB_PASSWORD", "")
  42. WEB_SECRET_KEY = os.environ.get("WEB_SECRET_KEY", "") or secrets.token_hex(32)
  43. HA_URL = os.environ.get("HA_URL", "http://100.87.1.24:8123").rstrip("/")
  44. HA_TOKEN = os.environ.get("HA_TOKEN", "")
  45. AWAY_ENTITY = os.environ.get("AWAY_ENTITY", "input_boolean.kodust_eemal")
  46. SESSION_MAX_AGE = int(os.environ.get("SESSION_MAX_AGE", "604800"))
  47. LOGIN_MAX_ATTEMPTS = int(os.environ.get("LOGIN_MAX_ATTEMPTS", "5"))
  48. LOGIN_WINDOW_SECONDS = int(os.environ.get("LOGIN_WINDOW_SECONDS", "300"))
  49. DEFAULT_RANGE_DAYS = 7
  50. MAX_RANGE_DAYS = 3660
  51. # --- Transport (bussid + praamid) ---
  52. BUS_GRAPHQL = os.environ.get(
  53. "BUS_GRAPHQL", "https://api.peatus.ee/routing/v1/routers/estonia/index/graphql"
  54. ).rstrip("/")
  55. BUS_STOPS = [s.strip() for s in os.environ.get(
  56. "BUS_STOPS", "estonia:24703,estonia:27213").split(",") if s.strip()]
  57. PRAAM_API = os.environ.get(
  58. "PRAAM_API", "https://www.praamid.ee/online/events").rstrip("/")
  59. RII_HOIATUS_URL = os.environ.get(
  60. "RII_HOIATUS_URL", "https://www.ilmateenistus.ee/ilm/prognoosid/hoiatused/"
  61. )
  62. TRANSPORT_TTL = int(os.environ.get("TRANSPORT_TTL", "60"))
  63. UA = "Mozilla/5.0 (compatible; Nutikodu-web/1.0)"
  64. PRAAM_DIRECTIONS = {"HR": "Heltermaa → Rohuküla", "RH": "Rohuküla → Heltermaa"}
  65. SHIP_NAMES = {"TI": "Tiiu", "LE": "Leiger", "RE": "Regu"}
  66. PRAAM_STATUS = {"CONFIRMED": "plaanipärane", "CANCELLED": "tühistatud", "SUSPENDED": "seisatud"}
  67. SEA_AREAS = ["Väinameri", "Hiiu maakond", "Lääne maakond"]
  68. WIND_TYPES = ["tugev tuul", "väga tugev tuul", "torm", "merehoiatus", "tugev tuul ja lained"]
  69. _cache: dict[str, tuple[float, object]] = {}
  70. _cache_lock = threading.Lock()
  71. def cached(key: str, ttl: int, producer):
  72. """Lihtne mälusisene TTL-vahemälu väliste API-de jaoks."""
  73. now = time.monotonic()
  74. with _cache_lock:
  75. hit = _cache.get(key)
  76. if hit and now - hit[0] < ttl:
  77. return hit[1]
  78. value = producer()
  79. with _cache_lock:
  80. _cache[key] = (time.monotonic(), value)
  81. return value
  82. app = FastAPI(title="Nutikodu", version="0.2.0")
  83. app.add_middleware(
  84. SessionMiddleware,
  85. secret_key=WEB_SECRET_KEY,
  86. https_only=False,
  87. same_site="lax",
  88. max_age=SESSION_MAX_AGE,
  89. )
  90. @app.middleware("http")
  91. async def add_no_cache(request: Request, call_next):
  92. """Staatika ja API ilma vahemäluta — muidu jääb brauserisse vana JS/HTML."""
  93. response = await call_next(request)
  94. path = request.url.path
  95. if (
  96. path == "/"
  97. or path.startswith("/api/")
  98. or path.endswith((".js", ".css", ".html", ".webmanifest", ".svg"))
  99. ):
  100. response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
  101. response.headers["Pragma"] = "no-cache"
  102. return response
  103. def ch_query(query: str, params: dict[str, str] | None = None) -> list[dict]:
  104. """Käivita ClickHouse'i päring ja tagasta read JSON-objektidena."""
  105. q = {"database": CH_DB, "query": f"{query} FORMAT JSONEachRow"}
  106. for key, value in (params or {}).items():
  107. q[f"param_{key}"] = value
  108. try:
  109. resp = requests.post(
  110. f"{CH_URL}/",
  111. params=q,
  112. auth=(CH_USER, CH_PASS),
  113. timeout=20,
  114. )
  115. except requests.RequestException as exc:
  116. raise HTTPException(status_code=502, detail=f"ClickHouse'i ühendus: {exc}") from exc
  117. if resp.status_code != 200:
  118. raise HTTPException(status_code=502, detail=f"ClickHouse: {resp.text.strip()[:400]}")
  119. text = resp.text.strip()
  120. if not text:
  121. return []
  122. try:
  123. return [json.loads(line) for line in text.splitlines() if line.strip()]
  124. except json.JSONDecodeError as exc:
  125. raise HTTPException(status_code=502, detail=f"ClickHouse'i vastus: {text[:200]}") from exc
  126. _login_failures: dict[str, list[float]] = defaultdict(list)
  127. _login_lock = threading.Lock()
  128. def client_ip(request: Request) -> str:
  129. forwarded = request.headers.get("x-forwarded-for")
  130. if forwarded:
  131. return forwarded.split(",")[0].strip()
  132. return request.client.host if request.client else "?"
  133. def retry_after_seconds(ip: str) -> int:
  134. now = time.monotonic()
  135. with _login_lock:
  136. history = [t for t in _login_failures.get(ip, []) if now - t < LOGIN_WINDOW_SECONDS]
  137. _login_failures[ip] = history
  138. if len(history) >= LOGIN_MAX_ATTEMPTS:
  139. return int(LOGIN_WINDOW_SECONDS - (now - history[0])) + 1
  140. return 0
  141. def register_login_failure(ip: str) -> None:
  142. with _login_lock:
  143. _login_failures[ip].append(time.monotonic())
  144. def reset_login_failures(ip: str) -> None:
  145. with _login_lock:
  146. _login_failures.pop(ip, None)
  147. def is_authed(request: Request) -> bool:
  148. """Kas päring on autenditud: Bearer token VÕI sessiooniküpsis."""
  149. auth = request.headers.get("authorization", "")
  150. if auth.lower().startswith("bearer "):
  151. token = auth[7:].strip()
  152. if WEB_ACCESS_TOKEN and secrets.compare_digest(token, WEB_ACCESS_TOKEN):
  153. return True
  154. try:
  155. if request.session.get("auth") is True:
  156. return True
  157. except Exception:
  158. pass
  159. return False
  160. def require_auth(request: Request) -> None:
  161. if not is_authed(request):
  162. raise HTTPException(
  163. status_code=401,
  164. detail="Autentimine nõutud (Bearer token või sisselogimine)",
  165. headers={"WWW-Authenticate": "Bearer"},
  166. )
  167. def parse_dt(value: str | None, default: datetime) -> str:
  168. """Võta vastu 'YYYY-MM-DD[ HH:MM[:SS]]' või ISO 'T'-eraldajaga."""
  169. if not value:
  170. return default.strftime("%Y-%m-%d %H:%M:%S")
  171. cleaned = value.strip().replace("T", " ")
  172. for fmt in ("%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M", "%Y-%m-%d"):
  173. try:
  174. return datetime.strptime(cleaned, fmt).strftime("%Y-%m-%d %H:%M:%S")
  175. except ValueError:
  176. continue
  177. raise HTTPException(status_code=422, detail=f"Vigane kuupäev: {value}")
  178. def resolve_range(from_: str | None, to: str | None) -> tuple[str, str]:
  179. now = datetime.now(TZ)
  180. to_s = parse_dt(to, now)
  181. to_dt = datetime.strptime(to_s, "%Y-%m-%d %H:%M:%S")
  182. from_s = parse_dt(from_, to_dt - timedelta(days=DEFAULT_RANGE_DAYS))
  183. from_dt = datetime.strptime(from_s, "%Y-%m-%d %H:%M:%S")
  184. if from_dt > to_dt:
  185. raise HTTPException(status_code=422, detail="'from' peab olema enne 'to'")
  186. if (to_dt - from_dt).days > MAX_RANGE_DAYS:
  187. raise HTTPException(status_code=422, detail="Vahemik on liiga pikk")
  188. return from_s, to_s
  189. @app.get("/api/meta")
  190. async def meta() -> dict:
  191. devices = [row["device"] for row in ch_query(
  192. f"SELECT DISTINCT device FROM {CH_DB}.temperatuur ORDER BY device"
  193. )]
  194. bounds = ch_query(
  195. f"SELECT toString(min(hour)) AS min_hour, toString(max(hour)) AS max_hour, count() AS rows "
  196. f"FROM {CH_DB}.temperatuur"
  197. )
  198. first = bounds[0] if bounds else {"min_hour": None, "max_hour": None, "rows": 0}
  199. return {
  200. "tz": "Europe/Tallinn",
  201. "db": CH_DB,
  202. "devices": devices,
  203. "min": first["min_hour"],
  204. "max": first["max_hour"],
  205. "rows": first["rows"],
  206. "default_range_days": DEFAULT_RANGE_DAYS,
  207. }
  208. @app.get("/api/temperatuur")
  209. async def temperatuur(
  210. from_: str | None = Query(None, alias="from"),
  211. to: str | None = Query(None),
  212. device: str | None = Query(None),
  213. agg: str = Query("hour", pattern="^(hour|day)$"),
  214. ) -> dict:
  215. from_s, to_s = resolve_range(from_, to)
  216. params = {"from": from_s, "to": to_s}
  217. where = (
  218. f"hour BETWEEN toDateTime({{from:String}}, 'Europe/Tallinn') "
  219. f"AND toDateTime({{to:String}}, 'Europe/Tallinn')"
  220. )
  221. if device:
  222. where += " AND device = {device:String}"
  223. params["device"] = device
  224. if agg == "day":
  225. select = (
  226. "toUInt64(toUnixTimestamp(toStartOfDay(hour))) * 1000 AS t, device, "
  227. "round(avg(temperature), 2) AS temperature, round(avg(humidity), 2) AS humidity, "
  228. "max(taidetud) AS taidetud"
  229. )
  230. group = "GROUP BY t, device"
  231. else:
  232. select = (
  233. "toUInt64(toUnixTimestamp(hour)) * 1000 AS t, device, "
  234. "temperature, humidity, taidetud"
  235. )
  236. group = ""
  237. rows = ch_query(
  238. f"SELECT {select} FROM {CH_DB}.temperatuur WHERE {where} {group} ORDER BY t",
  239. params,
  240. )
  241. series: dict[str, dict] = {}
  242. for row in rows:
  243. s = series.setdefault(
  244. row["device"], {"t": [], "temperature": [], "humidity": [], "taidetud": []}
  245. )
  246. s["t"].append(int(row["t"]))
  247. s["temperature"].append(row["temperature"])
  248. s["humidity"].append(row["humidity"])
  249. s["taidetud"].append(bool(row.get("taidetud")))
  250. return {"from": from_s, "to": to_s, "agg": agg, "series": series}
  251. @app.get("/api/valisilm")
  252. async def valisilm(
  253. from_: str | None = Query(None, alias="from"),
  254. to: str | None = Query(None),
  255. agg: str = Query("hour", pattern="^(hour|day)$"),
  256. ) -> dict:
  257. from_s, to_s = resolve_range(from_, to)
  258. params = {"from": from_s, "to": to_s}
  259. where = (
  260. f"hour BETWEEN toDateTime({{from:String}}, 'Europe/Tallinn') "
  261. f"AND toDateTime({{to:String}}, 'Europe/Tallinn')"
  262. )
  263. if agg == "day":
  264. select = (
  265. "toUInt64(toUnixTimestamp(toStartOfDay(hour))) * 1000 AS t, "
  266. "round(avg(temperature), 2) AS temperature, round(avg(humidity), 2) AS humidity, "
  267. "round(avg(wind), 2) AS wind, round(sum(rain), 2) AS rain, max(taidetud) AS taidetud"
  268. )
  269. group = "GROUP BY t"
  270. else:
  271. select = (
  272. "toUInt64(toUnixTimestamp(hour)) * 1000 AS t, "
  273. "temperature, humidity, wind, rain, taidetud"
  274. )
  275. group = ""
  276. rows = ch_query(
  277. f"SELECT {select} FROM {CH_DB}.valisilm FINAL WHERE {where} {group} ORDER BY t",
  278. params,
  279. )
  280. series: dict[str, list] = {
  281. "t": [],
  282. "temperature": [],
  283. "humidity": [],
  284. "wind": [],
  285. "rain": [],
  286. "taidetud": [],
  287. }
  288. for row in rows:
  289. series["t"].append(int(row["t"]))
  290. series["temperature"].append(row["temperature"])
  291. series["humidity"].append(row["humidity"])
  292. series["wind"].append(row["wind"])
  293. series["rain"].append(row["rain"])
  294. series["taidetud"].append(bool(row.get("taidetud")))
  295. return {"from": from_s, "to": to_s, "agg": agg, "series": series}
  296. @app.get("/api/ukseandur", dependencies=[Depends(require_auth)])
  297. async def ukseandur(
  298. from_: str | None = Query(None, alias="from"),
  299. to: str | None = Query(None),
  300. device: str | None = Query(None),
  301. ) -> dict:
  302. from_s, to_s = resolve_range(from_, to)
  303. params = {"from": from_s, "to": to_s}
  304. where = (
  305. f"ts BETWEEN toDateTime({{from:String}}, 'Europe/Tallinn') "
  306. f"AND toDateTime({{to:String}}, 'Europe/Tallinn')"
  307. )
  308. if device:
  309. where += " AND device = {device:String}"
  310. params["device"] = device
  311. rows = ch_query(
  312. f"SELECT toUInt64(toUnixTimestamp(ts)) * 1000 AS t, device, contact, olek "
  313. f"FROM {CH_DB}.ukseandur WHERE {where} ORDER BY ts",
  314. params,
  315. )
  316. return {
  317. "from": from_s,
  318. "to": to_s,
  319. "events": [
  320. {"t": int(r["t"]), "device": r["device"], "contact": bool(r["contact"]), "olek": r["olek"]}
  321. for r in rows
  322. ],
  323. }
  324. @app.get("/api/vead", dependencies=[Depends(require_auth)])
  325. async def vead(
  326. from_: str | None = Query(None, alias="from"),
  327. to: str | None = Query(None),
  328. device: str | None = Query(None),
  329. ) -> dict:
  330. from_s, to_s = resolve_range(from_, to)
  331. params = {"from": from_s, "to": to_s}
  332. where = (
  333. f"ts BETWEEN toDateTime({{from:String}}, 'Europe/Tallinn') "
  334. f"AND toDateTime({{to:String}}, 'Europe/Tallinn')"
  335. )
  336. if device:
  337. where += " AND device = {device:String}"
  338. params["device"] = device
  339. rows = ch_query(
  340. f"SELECT toUInt64(toUnixTimestamp(ts)) * 1000 AS t, device, tyyp, olek, detail, vaartus "
  341. f"FROM {CH_DB}.vead WHERE {where} ORDER BY ts",
  342. params,
  343. )
  344. return {
  345. "from": from_s,
  346. "to": to_s,
  347. "events": [
  348. {
  349. "t": int(r["t"]),
  350. "device": r["device"],
  351. "tyyp": r["tyyp"],
  352. "olek": r["olek"],
  353. "detail": r["detail"],
  354. "vaartus": r["vaartus"],
  355. }
  356. for r in rows
  357. ],
  358. }
  359. @app.get("/api/liikumine", dependencies=[Depends(require_auth)])
  360. async def liikumine(
  361. from_: str | None = Query(None, alias="from"),
  362. to: str | None = Query(None),
  363. device: str | None = Query(None),
  364. ) -> dict:
  365. from_s, to_s = resolve_range(from_, to)
  366. params = {"from": from_s, "to": to_s}
  367. where = (
  368. f"ts BETWEEN toDateTime({{from:String}}, 'Europe/Tallinn') "
  369. f"AND toDateTime({{to:String}}, 'Europe/Tallinn')"
  370. )
  371. if device:
  372. where += " AND device = {device:String}"
  373. params["device"] = device
  374. rows = ch_query(
  375. f"SELECT toUInt64(toUnixTimestamp(ts)) * 1000 AS t, device, occupancy, illumination, olek "
  376. f"FROM {CH_DB}.liikumine WHERE {where} ORDER BY ts",
  377. params,
  378. )
  379. return {
  380. "from": from_s,
  381. "to": to_s,
  382. "events": [
  383. {
  384. "t": int(r["t"]),
  385. "device": r["device"],
  386. "occupancy": bool(r["occupancy"]),
  387. "illumination": r["illumination"],
  388. "olek": r["olek"],
  389. }
  390. for r in rows
  391. ],
  392. }
  393. @app.get("/api/energia", dependencies=[Depends(require_auth)])
  394. async def energia(
  395. from_: str | None = Query(None, alias="from"),
  396. to: str | None = Query(None),
  397. device: str | None = Query(None),
  398. agg: str = Query("hour", pattern="^(hour|day)$"),
  399. ) -> dict:
  400. from_s, to_s = resolve_range(from_, to)
  401. params = {"from": from_s, "to": to_s}
  402. where = (
  403. f"ts BETWEEN toDateTime({{from:String}}, 'Europe/Tallinn') "
  404. f"AND toDateTime({{to:String}}, 'Europe/Tallinn')"
  405. )
  406. if device:
  407. where += " AND device = {device:String}"
  408. params["device"] = device
  409. if agg == "day":
  410. select = (
  411. "toUInt64(toUnixTimestamp(toStartOfDay(ts))) * 1000 AS t, device, "
  412. "round(avg(power), 2) AS power, round(avg(voltage), 1) AS voltage, "
  413. "round(avg(current), 3) AS current, max(energy) AS energy"
  414. )
  415. group = "GROUP BY t, device"
  416. else:
  417. select = (
  418. "toUInt64(toUnixTimestamp(ts)) * 1000 AS t, device, "
  419. "power, voltage, current, energy"
  420. )
  421. group = ""
  422. rows = ch_query(
  423. f"SELECT {select} FROM {CH_DB}.energia WHERE {where} {group} ORDER BY t",
  424. params,
  425. )
  426. series: dict[str, dict] = {}
  427. for row in rows:
  428. s2 = series.setdefault(
  429. row["device"], {"t": [], "power": [], "voltage": [], "current": [], "energy": []}
  430. )
  431. s2["t"].append(int(row["t"]))
  432. s2["power"].append(row["power"])
  433. s2["voltage"].append(row["voltage"])
  434. s2["current"].append(row["current"])
  435. s2["energy"].append(row["energy"])
  436. return {"from": from_s, "to": to_s, "agg": agg, "series": series}
  437. @app.get("/api/uus67_latest", dependencies=[Depends(require_auth)])
  438. async def uus67_latest() -> dict:
  439. energy = ch_query(
  440. f"SELECT device, argMax(power, ts) AS power, argMax(voltage, ts) AS voltage, "
  441. f"argMax(current, ts) AS current, argMax(energy, ts) AS energy, "
  442. f"toUInt64(toUnixTimestamp(max(ts))) * 1000 AS t "
  443. f"FROM {CH_DB}.energia GROUP BY device ORDER BY device"
  444. )
  445. motion = ch_query(
  446. f"SELECT device, argMax(occupancy, ts) AS occupancy, "
  447. f"argMax(illumination, ts) AS illumination, "
  448. f"toUInt64(toUnixTimestamp(max(ts))) * 1000 AS t "
  449. f"FROM {CH_DB}.liikumine GROUP BY device ORDER BY device"
  450. )
  451. return {
  452. "energy": [
  453. {
  454. "device": r["device"],
  455. "power": r.get("power"),
  456. "voltage": r.get("voltage"),
  457. "current": r.get("current"),
  458. "energy": r.get("energy"),
  459. "t": int(r["t"]) if r.get("t") is not None else None,
  460. }
  461. for r in energy
  462. ],
  463. "motion": [
  464. {
  465. "device": r["device"],
  466. "occupancy": bool(r.get("occupancy")),
  467. "illumination": r.get("illumination"),
  468. "t": int(r["t"]) if r.get("t") is not None else None,
  469. }
  470. for r in motion
  471. ],
  472. }
  473. @app.get("/api/latest")
  474. async def latest(request: Request) -> dict:
  475. readings = ch_query(
  476. f"SELECT device, "
  477. f"argMax(temperature, hour) AS temperature, "
  478. f"argMax(humidity, hour) AS humidity, "
  479. f"toString(max(hour)) AS ts_str, "
  480. f"toUInt64(toUnixTimestamp(max(hour))) * 1000 AS t "
  481. f"FROM {CH_DB}.temperatuur GROUP BY device ORDER BY device"
  482. )
  483. faults = ch_query(
  484. f"SELECT device, tyyp, argMax(detail, ts) AS detail_now, "
  485. f"toString(max(ts)) AS ts_str, argMax(olek, ts) AS olek_now "
  486. f"FROM {CH_DB}.vead GROUP BY device, tyyp HAVING olek_now = 'aktiveeritud'"
  487. ) if is_authed(request) else []
  488. outside_rows = ch_query(
  489. f"SELECT toString(max(hour)) AS ts_str, "
  490. f"argMax(temperature, hour) AS temperature, "
  491. f"argMax(humidity, hour) AS humidity, "
  492. f"argMax(wind, hour) AS wind, "
  493. f"argMax(rain, hour) AS rain, "
  494. f"toUInt64(toUnixTimestamp(max(hour))) * 1000 AS t "
  495. f"FROM {CH_DB}.valisilm FINAL"
  496. )
  497. outside: dict = {
  498. "temperature": None,
  499. "humidity": None,
  500. "wind": None,
  501. "rain": None,
  502. "hour": None,
  503. "t": None,
  504. }
  505. if outside_rows:
  506. r = outside_rows[0]
  507. outside = {
  508. "temperature": r.get("temperature"),
  509. "humidity": r.get("humidity"),
  510. "wind": r.get("wind"),
  511. "rain": r.get("rain"),
  512. "hour": r.get("ts_str"),
  513. "t": int(r["t"]) if r.get("t") is not None else None,
  514. }
  515. return {
  516. "readings": [
  517. {
  518. "device": r["device"],
  519. "temperature": r["temperature"],
  520. "humidity": r["humidity"],
  521. "hour": r["ts_str"],
  522. "t": int(r["t"]) if r.get("t") is not None else None,
  523. }
  524. for r in readings
  525. ],
  526. "open_faults": [
  527. {"device": r["device"], "tyyp": r["tyyp"], "detail": r["detail_now"], "ts": r["ts_str"]}
  528. for r in faults
  529. ],
  530. "outside": outside,
  531. }
  532. # --- Transport: bussid (peatus.ee / Digitransit GraphQL) ---
  533. def fetch_bus_departures() -> dict:
  534. """Tänased/tulevased bussiväljumised Suuremõisa küla peatustest."""
  535. fields = (
  536. "name stoptimesWithoutPatterns(numberOfDepartures: 40) { "
  537. "serviceDay scheduledDeparture realtimeDeparture realtime headsign "
  538. "trip { route { shortName longName } } }"
  539. )
  540. aliases = " ".join(
  541. f's{i}: stop(id: "{sid}") {{ {fields} }}' for i, sid in enumerate(BUS_STOPS)
  542. )
  543. try:
  544. resp = requests.post(
  545. BUS_GRAPHQL,
  546. json={"query": "{ " + aliases + " }"},
  547. headers={"User-Agent": UA},
  548. timeout=18,
  549. )
  550. except requests.RequestException as exc:
  551. raise HTTPException(status_code=502, detail=f"Bussi API ühendus: {exc}") from exc
  552. if resp.status_code != 200:
  553. raise HTTPException(status_code=502, detail=f"Bussi API: HTTP {resp.status_code}")
  554. try:
  555. payload = resp.json()
  556. except ValueError as exc:
  557. raise HTTPException(status_code=502, detail="Bussi API: vigane JSON") from exc
  558. if payload.get("errors"):
  559. msg = payload["errors"][0].get("message", "viga")
  560. raise HTTPException(status_code=502, detail=f"Bussi API: {msg}")
  561. departures = []
  562. for stop in (payload.get("data") or {}).values():
  563. if not stop or not stop.get("stoptimesWithoutPatterns"):
  564. continue
  565. stop_name = stop.get("name") or "Suuremõisa"
  566. for st in stop["stoptimesWithoutPatterns"]:
  567. service_day = st.get("serviceDay")
  568. if service_day is None:
  569. continue
  570. sched_s = st.get("scheduledDeparture") or 0
  571. real_s = st.get("realtimeDeparture")
  572. dep_s = real_s if st.get("realtime") and real_s is not None else sched_s
  573. try:
  574. dep_dt = datetime.fromtimestamp(int(service_day) + int(dep_s), TZ)
  575. except (OverflowError, OSError, TypeError, ValueError):
  576. continue
  577. route = (st.get("trip") or {}).get("route") or {}
  578. delay = None
  579. if st.get("realtime") and real_s is not None:
  580. delay = round((int(real_s) - int(sched_s)) / 60)
  581. departures.append({
  582. "t": int(dep_dt.timestamp() * 1000),
  583. "time": dep_dt.strftime("%H:%M"),
  584. "date": dep_dt.strftime("%Y-%m-%d"),
  585. "stop": stop_name,
  586. "route": route.get("shortName") or "",
  587. "headsign": st.get("headsign") or "",
  588. "realtime": bool(st.get("realtime")),
  589. "delay_min": delay,
  590. })
  591. departures.sort(key=lambda d: d["t"])
  592. now = datetime.now(TZ)
  593. return {
  594. "stops": BUS_STOPS,
  595. "today": now.strftime("%Y-%m-%d"),
  596. "departures": departures,
  597. "updated": now.strftime("%Y-%m-%d %H:%M"),
  598. }
  599. @app.get("/api/buss")
  600. async def buss(limit: int = Query(12, ge=1, le=40)) -> dict:
  601. """Järgmised bussiväljumised Suuremõisa külast (avalik)."""
  602. data = cached("buss", TRANSPORT_TTL, fetch_bus_departures)
  603. return {**data, "departures": data["departures"][:limit]}
  604. # --- Transport: praamid (praamid.ee) ---
  605. def _praam_events(day: str, direction: str) -> list[dict]:
  606. try:
  607. resp = requests.get(
  608. PRAAM_API,
  609. params={"direction": direction, "departure-date": day, "time-shift": "0"},
  610. headers={"User-Agent": UA},
  611. timeout=18,
  612. )
  613. except requests.RequestException:
  614. return []
  615. if resp.status_code != 200:
  616. return []
  617. try:
  618. items = (resp.json() or {}).get("items") or []
  619. except ValueError:
  620. return []
  621. out = []
  622. for e in items:
  623. try:
  624. dep_dt = datetime.strptime((e.get("dtstart") or "")[:19], "%Y-%m-%dT%H:%M:%S").replace(tzinfo=TZ)
  625. except ValueError:
  626. continue
  627. ship = e.get("ship")
  628. ship_code = ship.get("code") if isinstance(ship, dict) else ship
  629. status = e.get("status") or "?"
  630. out.append({
  631. "uid": e.get("uid"),
  632. "direction": direction,
  633. "direction_name": PRAAM_DIRECTIONS.get(direction, direction),
  634. "date": dep_dt.strftime("%Y-%m-%d"),
  635. "time": dep_dt.strftime("%H:%M"),
  636. "t": int(dep_dt.timestamp() * 1000),
  637. "ship": SHIP_NAMES.get(ship_code, ship_code or "?"),
  638. "ship_code": ship_code,
  639. "status": status,
  640. "status_et": PRAAM_STATUS.get(status, status),
  641. "disrupted": status != "CONFIRMED",
  642. })
  643. return out
  644. def fetch_weather_warnings() -> list[dict]:
  645. """RII hoiatusleht -> Väinamere-liiklusega seotud aktiivsed hoiatused (parim pingutus)."""
  646. try:
  647. resp = requests.get(RII_HOIATUS_URL, headers={"User-Agent": UA}, timeout=18)
  648. except requests.RequestException:
  649. return []
  650. if resp.status_code != 200:
  651. return []
  652. text = html.unescape(resp.text)
  653. text = re.sub(r"<script.*?</script>", " ", text, flags=re.S)
  654. text = re.sub(r"<[^>]+>", " ", text)
  655. text = re.sub(r"\s+", " ", text)
  656. found, seen = [], set()
  657. for area in SEA_AREAS:
  658. i = 0
  659. while True:
  660. i = text.find(area, i)
  661. if i < 0:
  662. break
  663. seg = text[max(0, i - 100): i + 700]
  664. if any(w in seg.lower() for w in WIND_TYPES):
  665. m = re.search(r"Kehtib[^.]{0,200}", seg)
  666. valid = m.group(0).strip() if m else ""
  667. key = (area, valid or seg[:80])
  668. if key not in seen:
  669. seen.add(key)
  670. found.append({"area": area, "valid": valid, "text": seg.strip()[:300]})
  671. i += len(area)
  672. return found
  673. def fetch_praam(days: int) -> dict:
  674. now = datetime.now(TZ)
  675. day_list = [(now + timedelta(days=n)).strftime("%Y-%m-%d") for n in range(days)]
  676. departures = []
  677. for day in day_list:
  678. for direction in ("HR", "RH"):
  679. departures.extend(_praam_events(day, direction))
  680. departures.sort(key=lambda d: (d["date"], d["time"]))
  681. return {
  682. "date": day_list[0],
  683. "today": now.strftime("%Y-%m-%d"),
  684. "departures": departures,
  685. "warnings": fetch_weather_warnings(),
  686. "disrupted": sum(1 for d in departures if d["disrupted"]),
  687. "updated": now.strftime("%Y-%m-%d %H:%M"),
  688. }
  689. @app.get("/api/praam")
  690. async def praam(days: int = Query(1, ge=1, le=3)) -> dict:
  691. """Heltermaa–Rohuküla praamiväljumised + ilmahoiatuse kontekst (avalik)."""
  692. return cached(f"praam:{days}", TRANSPORT_TTL, lambda: fetch_praam(days))
  693. LOGIN_HTML = """<!doctype html>
  694. <html lang="et"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
  695. <title>Nutikodu — login</title>
  696. <style>
  697. body{font-family:system-ui,sans-serif;background:#0e1116;color:#e6edf3;display:flex;min-height:100vh;align-items:center;justify-content:center;margin:0}
  698. form{background:#161b22;padding:28px 24px;border-radius:12px;border:1px solid #30363d;min-width:280px}
  699. h1{font-size:18px;margin:0 0 16px}
  700. input{width:100%;padding:10px;border-radius:8px;border:1px solid #30363d;background:#0d1117;color:#e6edf3;box-sizing:border-box;font-size:16px}
  701. button{margin-top:14px;width:100%;padding:10px;border-radius:8px;border:0;background:#1f6feb;color:#fff;font-size:16px;cursor:pointer}
  702. .err{color:#f85149;font-size:14px;margin-top:10px;min-height:18px}
  703. a{color:#58a6ff;font-size:13px;text-decoration:none}
  704. </style></head><body>
  705. <form method="post" action="/login">
  706. <h1>🔒 Nutikodu — sisselogimine</h1>
  707. <input type="password" name="password" placeholder="Parool" autofocus autocomplete="current-password">
  708. <button type="submit">Logi sisse</button>
  709. <div class="err">{{ERR}}</div>
  710. <div style="margin-top:14px"><a href="/">← Temperatuuri leht (avalik)</a></div>
  711. </form>
  712. </body></html>"""
  713. @app.get("/login")
  714. async def login_page():
  715. return HTMLResponse(LOGIN_HTML.replace("{{ERR}}", ""))
  716. @app.post("/login")
  717. async def login_submit(request: Request, password: str = Form("")):
  718. ip = client_ip(request)
  719. ra = retry_after_seconds(ip)
  720. if ra:
  721. return HTMLResponse(
  722. LOGIN_HTML.replace("{{ERR}}", f"Liiga palju katseid. Proovi {ra} sekundi pärast."),
  723. status_code=429,
  724. )
  725. if WEB_PASSWORD and secrets.compare_digest(password, WEB_PASSWORD):
  726. reset_login_failures(ip)
  727. request.session["auth"] = True
  728. return RedirectResponse("/?auth=1", status_code=303)
  729. register_login_failure(ip)
  730. return HTMLResponse(LOGIN_HTML.replace("{{ERR}}", "Vale parool"), status_code=401)
  731. @app.get("/logout")
  732. async def logout(request: Request):
  733. request.session.clear()
  734. return RedirectResponse("/", status_code=303)
  735. @app.get("/api/me")
  736. async def me(request: Request) -> dict:
  737. return {"authed": is_authed(request)}
  738. def _ha_headers() -> dict:
  739. return {"Authorization": f"Bearer {HA_TOKEN}", "Content-Type": "application/json"}
  740. def ha_get_away() -> bool | None:
  741. if not HA_TOKEN:
  742. return None
  743. try:
  744. r = requests.get(f"{HA_URL}/api/states/{AWAY_ENTITY}", headers=_ha_headers(), timeout=8)
  745. if r.status_code == 200:
  746. return r.json().get("state") == "on"
  747. except requests.RequestException:
  748. pass
  749. return None
  750. def ha_set_away(on: bool) -> None:
  751. service = "turn_on" if on else "turn_off"
  752. r = requests.post(
  753. f"{HA_URL}/api/services/input_boolean/{service}",
  754. headers=_ha_headers(),
  755. json={"entity_id": AWAY_ENTITY},
  756. timeout=8,
  757. )
  758. r.raise_for_status()
  759. @app.get("/api/away", dependencies=[Depends(require_auth)])
  760. async def away_state() -> dict:
  761. """Kohalolu ("kodust eemal") olek."""
  762. state = ha_get_away()
  763. return {"away": state, "available": state is not None}
  764. @app.post("/api/away", dependencies=[Depends(require_auth)])
  765. async def away_set(payload: dict = Body(...)) -> dict:
  766. """Lülita "kodust eemal" sisse/välja (keha {"on": true|false})."""
  767. on = bool(payload.get("on"))
  768. try:
  769. ha_set_away(on)
  770. except requests.RequestException as exc:
  771. raise HTTPException(status_code=502, detail=f"Home Assistant viga: {exc}") from exc
  772. return {"away": on, "available": True}
  773. @app.get("/health")
  774. async def health() -> dict:
  775. try:
  776. ch_query("SELECT 1")
  777. ch_ok = True
  778. except HTTPException:
  779. ch_ok = False
  780. return {"status": "ok", "clickhouse": ch_ok}
  781. @app.get("/")
  782. async def index() -> FileResponse:
  783. return FileResponse(STATIC_DIR / "index.html", media_type="text/html")
  784. app.mount("/", StaticFiles(directory=STATIC_DIR, html=True), name="static")
  785. if __name__ == "__main__":
  786. import uvicorn
  787. uvicorn.run("app:app", host="0.0.0.0", port=8095, reload=True)