Backup automatico script del 2026-07-19 07:00
This commit is contained in:
@@ -192,6 +192,15 @@ def telegram_send_html(message_html: str, chat_ids: Optional[List[str]] = None)
|
||||
message_html: Messaggio HTML da inviare
|
||||
chat_ids: Lista di chat IDs (default: TELEGRAM_CHAT_IDS)
|
||||
"""
|
||||
try:
|
||||
from telegram_gate import telegram_alerts_enabled
|
||||
except ImportError:
|
||||
telegram_alerts_enabled = lambda: True # type: ignore
|
||||
|
||||
if not telegram_alerts_enabled():
|
||||
LOGGER.info("Telegram sospeso: skip arome_snow")
|
||||
return False
|
||||
|
||||
token = load_bot_token()
|
||||
if not token:
|
||||
LOGGER.warning("Telegram token missing: message not sent.")
|
||||
@@ -252,6 +261,15 @@ def telegram_send_photo(photo_path: str, caption: str, chat_ids: Optional[List[s
|
||||
Returns:
|
||||
True se inviata con successo, False altrimenti
|
||||
"""
|
||||
try:
|
||||
from telegram_gate import telegram_alerts_enabled
|
||||
except ImportError:
|
||||
telegram_alerts_enabled = lambda: True # type: ignore
|
||||
|
||||
if not telegram_alerts_enabled():
|
||||
LOGGER.info("Telegram sospeso: skip arome_snow photo")
|
||||
return False
|
||||
|
||||
token = load_bot_token()
|
||||
if not token:
|
||||
LOGGER.warning("Telegram token missing: photo not sent.")
|
||||
|
||||
@@ -1619,6 +1619,13 @@ def format_route_ice_report(df: pd.DataFrame, city1: str, city2: str) -> str:
|
||||
return msg
|
||||
|
||||
def send_telegram_broadcast(token, message, debug_mode=False):
|
||||
try:
|
||||
from telegram_gate import telegram_alerts_enabled
|
||||
except ImportError:
|
||||
telegram_alerts_enabled = lambda: True # type: ignore
|
||||
|
||||
if not telegram_alerts_enabled():
|
||||
return
|
||||
base_url = f"https://api.telegram.org/bot{token}/sendMessage"
|
||||
recipients = [ADMIN_CHAT_ID] if debug_mode else TELEGRAM_CHAT_IDS
|
||||
|
||||
@@ -1793,6 +1800,13 @@ def generate_route_ice_map(df: pd.DataFrame, city1: str, city2: str, output_path
|
||||
|
||||
def send_telegram_photo(token, photo_path, caption, debug_mode=False):
|
||||
"""Invia foto via Telegram API."""
|
||||
try:
|
||||
from telegram_gate import telegram_alerts_enabled
|
||||
except ImportError:
|
||||
telegram_alerts_enabled = lambda: True # type: ignore
|
||||
|
||||
if not telegram_alerts_enabled():
|
||||
return False
|
||||
if not os.path.exists(photo_path):
|
||||
return False
|
||||
|
||||
|
||||
@@ -176,6 +176,18 @@ def telegram_send_html(message_html: str, chat_ids: Optional[List[str]] = None)
|
||||
if message_html:
|
||||
message_html = re.sub(r"<\s*br\s*/?\s*>", "\n", message_html, flags=re.IGNORECASE)
|
||||
|
||||
try:
|
||||
from telegram_gate import mirror_alert_to_web, telegram_alerts_enabled
|
||||
except ImportError:
|
||||
telegram_alerts_enabled = lambda: True # type: ignore
|
||||
mirror_alert_to_web = lambda *a, **k: False # type: ignore
|
||||
|
||||
if not telegram_alerts_enabled():
|
||||
LOGGER.info("Telegram sospeso: skip civil_protection")
|
||||
if message_html:
|
||||
mirror_alert_to_web(message_html, "civil_protection", "warning", is_html=True)
|
||||
return False
|
||||
|
||||
token = load_bot_token()
|
||||
if not token:
|
||||
LOGGER.warning("Token Telegram assente. Nessun invio effettuato.")
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# 0 = sospende Telegram per script meteo/speedtest (WebApp invariata dove configurata)
|
||||
LOOGLE_TELEGRAM_ALERTS=0
|
||||
@@ -105,6 +105,15 @@ def send_telegram_message(message: str, chat_ids: Optional[List[str]] = None) ->
|
||||
if not message:
|
||||
return
|
||||
|
||||
try:
|
||||
from telegram_gate import telegram_alerts_enabled
|
||||
except ImportError:
|
||||
telegram_alerts_enabled = lambda: True # type: ignore
|
||||
|
||||
if not telegram_alerts_enabled():
|
||||
LOGGER.info("Telegram sospeso: report speedtest non inviato")
|
||||
return
|
||||
|
||||
bot_token = load_bot_token()
|
||||
if not bot_token:
|
||||
LOGGER.error("Token Telegram mancante (env/file). Messaggio NON inviato.")
|
||||
|
||||
@@ -154,6 +154,15 @@ def telegram_send_html(message_html: str, chat_ids: Optional[List[str]] = None)
|
||||
message_html: Messaggio HTML da inviare
|
||||
chat_ids: Lista di chat IDs (default: TELEGRAM_CHAT_IDS)
|
||||
"""
|
||||
try:
|
||||
from telegram_gate import telegram_alerts_enabled
|
||||
except ImportError:
|
||||
telegram_alerts_enabled = lambda: True # type: ignore
|
||||
|
||||
if not telegram_alerts_enabled():
|
||||
LOGGER.info("Telegram sospeso: skip freeze_alert")
|
||||
return False
|
||||
|
||||
token = load_bot_token()
|
||||
if not token:
|
||||
LOGGER.warning("Telegram token missing: message not sent.")
|
||||
|
||||
@@ -63,6 +63,14 @@ def load_bot_token() -> str:
|
||||
|
||||
def telegram_send_markdown(message_md: str, chat_ids: Optional[List[str]] = None) -> bool:
|
||||
"""Invia messaggio Markdown a Telegram. Returns True se almeno un invio è riuscito."""
|
||||
try:
|
||||
from telegram_gate import telegram_alerts_enabled
|
||||
if not telegram_alerts_enabled():
|
||||
logger.info("Telegram sospeso (LOOGLE_TELEGRAM_ALERTS=0): meteo.py non invia.")
|
||||
return False
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
token = load_bot_token()
|
||||
if not token:
|
||||
logger.warning("Telegram token missing: message not sent.")
|
||||
@@ -168,7 +176,28 @@ def get_icon_set(prec, snow, code, is_day, cloud, vis, temp, rain, gust, cape, c
|
||||
return "❓", "-"
|
||||
|
||||
def get_coordinates(city_name: str):
|
||||
params = {"name": city_name, "count": 1, "language": "it", "format": "json"}
|
||||
q = (city_name or "").strip()
|
||||
if not q:
|
||||
return None
|
||||
try:
|
||||
from loogle_core.geo_parse import resolve_place_query
|
||||
except ImportError:
|
||||
try:
|
||||
from geo_parse import resolve_place_query
|
||||
except ImportError:
|
||||
resolve_place_query = None
|
||||
if resolve_place_query:
|
||||
place = resolve_place_query(q)
|
||||
if place:
|
||||
return (
|
||||
place["lat"],
|
||||
place["lon"],
|
||||
place.get("name") or f"{place['lat']:.5f}, {place['lon']:.5f}",
|
||||
place.get("country_code") or "IT",
|
||||
place.get("timezone"),
|
||||
)
|
||||
return None
|
||||
params = {"name": q, "count": 1, "language": "it", "format": "json"}
|
||||
try:
|
||||
r = open_meteo_get(GEOCODING_URL, params=params, headers=HTTP_HEADERS, timeout=(5, 15))
|
||||
data = r.json()
|
||||
@@ -657,6 +686,8 @@ def generate_weather_report(lat, lon, location_name, debug_mode=False, cc="IT",
|
||||
if as_json:
|
||||
return {
|
||||
"location": location_name,
|
||||
"lat": lat,
|
||||
"lon": lon,
|
||||
"model": model_name,
|
||||
"timezone": tz_to_use,
|
||||
"blocks": json_blocks,
|
||||
|
||||
@@ -4,14 +4,7 @@ import subprocess
|
||||
import re
|
||||
import os
|
||||
import json
|
||||
import time
|
||||
import urllib.request
|
||||
import urllib.parse
|
||||
from typing import List, Optional
|
||||
|
||||
# --- CONFIGURAZIONE ---
|
||||
BOT_TOKEN="8155587974:AAF9OekvBpixtk8ZH6KoIc0L8edbhdXt7A4"
|
||||
TELEGRAM_CHAT_IDS = ["64463169"]
|
||||
import sys
|
||||
|
||||
# BERSAGLIO (Cloudflare è solitamente il più stabile per i ping)
|
||||
TARGET_HOST = "1.1.1.1"
|
||||
@@ -33,72 +26,48 @@ def log_line(message: str) -> None:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def send_telegram(msg, chat_ids: Optional[List[str]] = None):
|
||||
"""Invia alert su Telegram e/o WebApp via alert_dispatcher."""
|
||||
try:
|
||||
import sys
|
||||
sys.path.insert(0, "/home/daniely/docker/shared")
|
||||
from loogle_core.alert_dispatcher import dispatch_alert
|
||||
plain = msg.replace("*", "").replace("_", "").replace("`", "")
|
||||
dispatch_alert(
|
||||
"Degrado qualità linea",
|
||||
plain,
|
||||
category="net_quality",
|
||||
severity="warning",
|
||||
telegram_text=msg,
|
||||
)
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
if not BOT_TOKEN or "INSERISCI" in BOT_TOKEN:
|
||||
return
|
||||
if chat_ids is None:
|
||||
chat_ids = TELEGRAM_CHAT_IDS
|
||||
url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage"
|
||||
for chat_id in chat_ids:
|
||||
try:
|
||||
payload = {"chat_id": chat_id, "text": msg, "parse_mode": "Markdown"}
|
||||
data = urllib.parse.urlencode(payload).encode('utf-8')
|
||||
req = urllib.request.Request(url, data=data)
|
||||
with urllib.request.urlopen(req) as r: pass
|
||||
time.sleep(0.2)
|
||||
except: pass
|
||||
|
||||
def send_alert(title: str, body: str, severity: str = "warning") -> None:
|
||||
"""Invia alert alla WebApp Loogle Casa."""
|
||||
sys.path.insert(0, "/home/daniely/docker/shared")
|
||||
from loogle_core.alert_dispatcher import dispatch_alert
|
||||
|
||||
dispatch_alert(title, body, category="net_quality", severity=severity)
|
||||
|
||||
|
||||
def load_state():
|
||||
if os.path.exists(STATE_FILE):
|
||||
try:
|
||||
with open(STATE_FILE, 'r') as f: return json.load(f)
|
||||
except: pass
|
||||
with open(STATE_FILE, "r") as f:
|
||||
return json.load(f)
|
||||
except Exception:
|
||||
pass
|
||||
return {"alert_active": False}
|
||||
|
||||
|
||||
def save_state(active):
|
||||
try:
|
||||
with open(STATE_FILE, 'w') as f: json.dump({"alert_active": active}, f)
|
||||
except: pass
|
||||
with open(STATE_FILE, "w") as f:
|
||||
json.dump({"alert_active": active}, f)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def measure_quality(chat_ids: Optional[List[str]] = None):
|
||||
|
||||
def measure_quality(dry_run: bool = False):
|
||||
print("--- Avvio Test Qualità Linea ---")
|
||||
log_line("INFO Avvio Test Qualità Linea")
|
||||
|
||||
# Esegue 50 ping rapidi (0.2s intervallo)
|
||||
# -q: quiet (solo riepilogo finale)
|
||||
# -c 50: conta 50 pacchetti
|
||||
# -i 0.2: intervallo rapido
|
||||
# -w 15: timeout massimo 15 secondi
|
||||
|
||||
cmd = f"ping -c 50 -i 0.2 -q -w 15 {TARGET_HOST}"
|
||||
|
||||
|
||||
try:
|
||||
output = subprocess.check_output(cmd, shell=True).decode('utf-8')
|
||||
output = subprocess.check_output(cmd, shell=True).decode("utf-8")
|
||||
except subprocess.CalledProcessError as e:
|
||||
# Se ping fallisce completamente (es. internet down), catturiamo l'output comunque se c'è
|
||||
output = e.output.decode('utf-8') if e.output else ""
|
||||
output = e.output.decode("utf-8") if e.output else ""
|
||||
if not output:
|
||||
print("Errore critico: Nessuna connessione.")
|
||||
return
|
||||
|
||||
# Parsing Packet Loss
|
||||
# Cerca pattern: "X% packet loss"
|
||||
loss_match = re.search(r'([0-9]+(?:[\\.,][0-9]+)?)% packet loss', output)
|
||||
loss_match = re.search(r"([0-9]+(?:[\\.,][0-9]+)?)% packet loss", output)
|
||||
if loss_match:
|
||||
loss_raw = loss_match.group(1).replace(",", ".")
|
||||
try:
|
||||
@@ -107,18 +76,16 @@ def measure_quality(chat_ids: Optional[List[str]] = None):
|
||||
loss = 100.0
|
||||
else:
|
||||
loss = 100.0
|
||||
# Clamp to avoid parsing artifacts (e.g., "0.96078%" -> 0.96078, not 96078).
|
||||
if loss < 0:
|
||||
loss = 0.0
|
||||
if loss > 100:
|
||||
loss = 100.0
|
||||
|
||||
# Parsing Jitter (mdev)
|
||||
# Output tipico: rtt min/avg/max/mdev = 10.1/12.5/40.2/5.1 ms
|
||||
# mdev è la 4a cifra
|
||||
jitter = 0.0
|
||||
rtt_match = re.search(r'rtt min/avg/max/mdev = ([\d\.]+)/([\d\.]+)/([\d\.]+)/([\d\.]+)', output)
|
||||
|
||||
rtt_match = re.search(
|
||||
r"rtt min/avg/max/mdev = ([\d\.]+)/([\d\.]+)/([\d\.]+)/([\d\.]+)", output
|
||||
)
|
||||
|
||||
if rtt_match:
|
||||
avg_ping = float(rtt_match.group(2))
|
||||
jitter = float(rtt_match.group(4))
|
||||
@@ -129,49 +96,51 @@ def measure_quality(chat_ids: Optional[List[str]] = None):
|
||||
print(result_line)
|
||||
log_line(f"INFO {result_line}")
|
||||
|
||||
# --- LOGICA ALLARME ---
|
||||
# Ping senza risposte (es. iptables DROP icmp locale) → misura non affidabile.
|
||||
if loss >= 99.0 and avg_ping == 0.0 and jitter == 0.0:
|
||||
print("Misura non affidabile (ping senza risposte, possibile blocco ICMP locale).")
|
||||
log_line("WARN Misura non affidabile: ping senza risposte")
|
||||
return
|
||||
|
||||
state = load_state()
|
||||
was_active = state.get("alert_active", False)
|
||||
|
||||
is_bad = (loss >= LIMIT_LOSS) or (jitter >= LIMIT_JITTER)
|
||||
|
||||
|
||||
if is_bad:
|
||||
if not was_active:
|
||||
# NUOVO ALLARME
|
||||
msg = f"📉 **DEGRADO QUALITÀ LINEA**\n\n"
|
||||
body_parts = ["Degrado qualità linea rilevato."]
|
||||
if loss >= LIMIT_LOSS:
|
||||
msg += f"🔴 **Packet Loss:** `{loss:.2f}%` (Soglia {LIMIT_LOSS}%)\n"
|
||||
body_parts.append(f"Packet loss: {loss:.2f}% (soglia {LIMIT_LOSS}%)")
|
||||
if jitter >= LIMIT_JITTER:
|
||||
msg += f"⚠️ **Jitter (Instabilità):** `{jitter}ms` (Soglia {LIMIT_JITTER}ms)\n"
|
||||
|
||||
msg += f"\n_Ping Medio: {avg_ping}ms_"
|
||||
send_telegram(msg, chat_ids=chat_ids)
|
||||
body_parts.append(f"Jitter: {jitter}ms (soglia {LIMIT_JITTER}ms)")
|
||||
body_parts.append(f"Ping medio: {avg_ping}ms")
|
||||
body = "\n".join(body_parts)
|
||||
if not dry_run:
|
||||
send_alert("Degrado qualità linea", body, severity="warning")
|
||||
save_state(True)
|
||||
print("Allarme inviato.")
|
||||
print("Allarme inviato." if not dry_run else "Allarme (dry-run).")
|
||||
log_line("WARN Allarme inviato")
|
||||
else:
|
||||
print("Qualità ancora scarsa (già notificato).")
|
||||
log_line("WARN Qualità ancora scarsa (già notificato)")
|
||||
|
||||
elif was_active and not is_bad:
|
||||
# RECOVERY
|
||||
msg = f"✅ **QUALITÀ LINEA RIPRISTINATA**\n\n"
|
||||
msg += f"I parametri sono rientrati nella norma.\n"
|
||||
msg += f"Ping: `{avg_ping}ms` | Jitter: `{jitter}ms` | Loss: `{loss:.2f}%`"
|
||||
send_telegram(msg, chat_ids=chat_ids)
|
||||
body = (
|
||||
f"Qualità linea ripristinata.\n"
|
||||
f"Ping: {avg_ping}ms | Jitter: {jitter}ms | Loss: {loss:.2f}%"
|
||||
)
|
||||
if not dry_run:
|
||||
send_alert("Qualità linea ripristinata", body, severity="info")
|
||||
save_state(False)
|
||||
print("Recovery inviata.")
|
||||
print("Recovery inviata." if not dry_run else "Recovery (dry-run).")
|
||||
log_line("INFO Recovery inviata")
|
||||
else:
|
||||
print("Linea OK.")
|
||||
log_line("INFO Linea OK")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description="Network quality monitor")
|
||||
parser.add_argument("--debug", action="store_true", help="Invia messaggi solo all'admin (chat ID: %s)" % TELEGRAM_CHAT_IDS[0])
|
||||
parser.add_argument("--dry-run", action="store_true", help="Esegui senza inviare notifiche")
|
||||
args = parser.parse_args()
|
||||
|
||||
# In modalità debug, invia solo al primo chat ID (admin)
|
||||
chat_ids = [TELEGRAM_CHAT_IDS[0]] if args.debug else None
|
||||
|
||||
measure_quality(chat_ids=chat_ids)
|
||||
measure_quality(dry_run=args.dry_run)
|
||||
|
||||
@@ -128,6 +128,17 @@ def telegram_send_markdown(message: str, chat_ids: Optional[List[str]] = None) -
|
||||
if not message:
|
||||
return False
|
||||
|
||||
try:
|
||||
from telegram_gate import mirror_alert_to_web, telegram_alerts_enabled
|
||||
except ImportError:
|
||||
telegram_alerts_enabled = lambda: True # type: ignore
|
||||
mirror_alert_to_web = lambda *a, **k: False # type: ignore
|
||||
|
||||
if not telegram_alerts_enabled():
|
||||
LOGGER.info("Telegram sospeso: skip nowcast_120m")
|
||||
mirror_alert_to_web(message, "nowcast_120m", "warning", is_html=False)
|
||||
return False
|
||||
|
||||
token = load_bot_token()
|
||||
if not token:
|
||||
LOGGER.error("Token Telegram mancante. Messaggio NON inviato.")
|
||||
|
||||
@@ -113,16 +113,34 @@ def get_bot_token():
|
||||
def get_coordinates(query):
|
||||
if not query or query.lower() == "casa":
|
||||
return DEFAULT_LAT, DEFAULT_LON, DEFAULT_NAME, "SM"
|
||||
q = query.strip()
|
||||
try:
|
||||
from loogle_core.geo_parse import resolve_place_query
|
||||
except ImportError:
|
||||
try:
|
||||
from geo_parse import resolve_place_query
|
||||
except ImportError:
|
||||
resolve_place_query = None
|
||||
if resolve_place_query:
|
||||
place = resolve_place_query(q)
|
||||
if place:
|
||||
return (
|
||||
place["lat"],
|
||||
place["lon"],
|
||||
place.get("name") or f"{place['lat']:.5f}, {place['lon']:.5f}",
|
||||
place.get("country_code") or "IT",
|
||||
)
|
||||
return None, None, None, None
|
||||
url = "https://geocoding-api.open-meteo.com/v1/search"
|
||||
try:
|
||||
resp = open_meteo_get(url, params={"name": query, "count": 1, "language": "it", "format": "json"}, timeout=(5, 10))
|
||||
resp = open_meteo_get(url, params={"name": q, "count": 1, "language": "it", "format": "json"}, timeout=(5, 10))
|
||||
res = resp.json().get("results", [])
|
||||
if res:
|
||||
res = res[0]
|
||||
cc = res.get("country_code", "IT").upper()
|
||||
name = f"{res.get('name')} ({cc})"
|
||||
return res['latitude'], res['longitude'], name, cc
|
||||
except:
|
||||
except Exception:
|
||||
pass
|
||||
return None, None, None, None
|
||||
|
||||
@@ -1141,7 +1159,7 @@ def _apply_unified_precip(hourly: Dict, daily: Dict, casa: bool) -> Tuple[Dict,
|
||||
return hourly, daily
|
||||
|
||||
|
||||
def format_weather_context_report(models_data, location_name, country_code, as_json=False):
|
||||
def format_weather_context_report(models_data, location_name, country_code, as_json=False, lat=None, lon=None):
|
||||
"""Genera report contestuale intelligente con ensemble multi-modello"""
|
||||
# Combina modelli a breve e lungo termine
|
||||
merged_data = merge_multi_model_forecast(models_data, forecast_days=10)
|
||||
@@ -1761,6 +1779,8 @@ def format_weather_context_report(models_data, location_name, country_code, as_j
|
||||
return {
|
||||
"location": location_name,
|
||||
"country_code": country_code,
|
||||
"lat": lat,
|
||||
"lon": lon,
|
||||
"models": models_used,
|
||||
"trend_html": trend_explanation if trend else "",
|
||||
"weather_changes": weather_changes,
|
||||
@@ -1781,6 +1801,13 @@ def format_weather_context_report(models_data, location_name, country_code, as_j
|
||||
return "\n".join(msg_parts)
|
||||
|
||||
def send_telegram(text, chat_id, token, debug_mode=False):
|
||||
try:
|
||||
from telegram_gate import telegram_alerts_enabled
|
||||
except ImportError:
|
||||
telegram_alerts_enabled = lambda: True # type: ignore
|
||||
|
||||
if not telegram_alerts_enabled():
|
||||
return False
|
||||
if not token:
|
||||
return False
|
||||
url = f"https://api.telegram.org/bot{token}/sendMessage"
|
||||
@@ -1858,11 +1885,11 @@ def main():
|
||||
|
||||
# Genera report
|
||||
if args.json:
|
||||
payload = format_weather_context_report(models_data, name, cc, as_json=True)
|
||||
payload = format_weather_context_report(models_data, name, cc, as_json=True, lat=lat, lon=lon)
|
||||
print(json.dumps(payload, ensure_ascii=False))
|
||||
return
|
||||
|
||||
report = format_weather_context_report(models_data, name, cc)
|
||||
report = format_weather_context_report(models_data, name, cc, lat=lat, lon=lon)
|
||||
|
||||
if debug_mode:
|
||||
report = f"🛠 <b>[DEBUG MODE]</b> 🛠\n\n{report}"
|
||||
|
||||
@@ -388,21 +388,37 @@ def calculate_route_points(lat1: float, lon1: float, lat2: float, lon2: float,
|
||||
|
||||
|
||||
def get_coordinates_from_city(city_name: str) -> Optional[Tuple[float, float, str]]:
|
||||
"""Ottiene coordinate da nome città usando Open-Meteo Geocoding API."""
|
||||
# Gestione caso speciale "Casa"
|
||||
"""Ottiene coordinate da città, POI, coordinate Google Maps o Plus Code."""
|
||||
if not city_name or city_name.lower() == "casa":
|
||||
# Coordinate fisse per Casa (San Marino)
|
||||
return (43.9356, 12.4296, "Casa")
|
||||
|
||||
|
||||
q = city_name.strip()
|
||||
try:
|
||||
from loogle_core.geo_parse import resolve_place_query
|
||||
except ImportError:
|
||||
try:
|
||||
from geo_parse import resolve_place_query
|
||||
except ImportError:
|
||||
resolve_place_query = None
|
||||
if resolve_place_query:
|
||||
place = resolve_place_query(q)
|
||||
if place:
|
||||
return (
|
||||
place["lat"],
|
||||
place["lon"],
|
||||
place.get("name") or f"{place['lat']:.5f}, {place['lon']:.5f}",
|
||||
)
|
||||
return None
|
||||
|
||||
url = "https://geocoding-api.open-meteo.com/v1/search"
|
||||
params = {"name": city_name, "count": 1, "language": "it"}
|
||||
params = {"name": q, "count": 1, "language": "it"}
|
||||
try:
|
||||
resp = open_meteo_get(url, params=params, timeout=(5, 10))
|
||||
if resp.status_code == 200:
|
||||
data = resp.json()
|
||||
if data.get("results"):
|
||||
result = data["results"][0]
|
||||
return (result["latitude"], result["longitude"], result.get("name", city_name))
|
||||
return (result["latitude"], result["longitude"], result.get("name", q))
|
||||
except Exception as e:
|
||||
LOGGER.warning(f"Errore geocoding per {city_name}: {e}")
|
||||
return None
|
||||
|
||||
@@ -101,16 +101,56 @@ CONVECTIVE_MIN_PRECIP_H = 0.5 # mm/h
|
||||
CONVECTIVE_MIN_PRECIP_3H = 2.0 # mm/3h
|
||||
|
||||
# ----------------- SOGLIE DI SIGNIFICATIVITÀ (per NOTIFICA temporali) -----------------
|
||||
# Si notifica solo se c'è almeno un evento "significativo": fulminazioni forti,
|
||||
# bomba d'acqua o downburst. Soglie alte per filtrare i temporali marginali estivi.
|
||||
SIGNIFICANT_CAPE_LIGHTNING = 1200.0 # J/kg - fulminazioni rilevanti (instabilità moderata: scelta orientata alla sicurezza)
|
||||
# La fulminazione "forte" deve corrispondere a una cella temporalesca prevista sul posto:
|
||||
# oltre a CAPE+LPI richiede precipitazione convettiva consistente (non semplice pioggerella).
|
||||
# Base (fascia "near"): si notifica solo se c'è almeno un evento "significativo".
|
||||
SIGNIFICANT_CAPE_LIGHTNING = 1200.0 # J/kg - fulminazioni rilevanti
|
||||
SIGNIFICANT_LIGHTNING_PRECIP_H = 2.0 # mm/h
|
||||
SIGNIFICANT_LIGHTNING_PRECIP_3H = 5.0 # mm/3h
|
||||
SIGNIFICANT_PRECIP_H = 30.0 # mm/h - bomba d'acqua (nubifragio forte)
|
||||
SIGNIFICANT_PRECIP_3H = 50.0 # mm/3h - nubifragio forte su 3 ore
|
||||
SIGNIFICANT_STORM_SCORE = 55.0 # Storm Severity Score minimo per significativo
|
||||
SIGNIFICANT_PRECIP_H = 30.0 # mm/h - bomba d'acqua
|
||||
SIGNIFICANT_PRECIP_3H = 50.0 # mm/3h
|
||||
SIGNIFICANT_STORM_SCORE = 55.0
|
||||
|
||||
# ----------------- SOGLIE PER ANTICIPO (lead-time) -----------------
|
||||
# Concetto: avvisa 24h prima solo se è un "finimondo"; eventi moderati bastano
|
||||
# poche ore prima (le previsioni lontane sono spesso fuori scala e poi si smussano).
|
||||
# near: < 6h → soglie base (sensibili)
|
||||
# mid: 6–12h → soglie elevate
|
||||
# far: ≥ 12h → soglie estreme
|
||||
LEAD_NEAR_H = 6.0
|
||||
LEAD_MID_H = 12.0
|
||||
|
||||
LEAD_STORM = {
|
||||
"near": {
|
||||
"cape": SIGNIFICANT_CAPE_LIGHTNING,
|
||||
"lightning_precip_h": SIGNIFICANT_LIGHTNING_PRECIP_H,
|
||||
"lightning_precip_3h": SIGNIFICANT_LIGHTNING_PRECIP_3H,
|
||||
"precip_h": SIGNIFICANT_PRECIP_H,
|
||||
"precip_3h": SIGNIFICANT_PRECIP_3H,
|
||||
"score": SIGNIFICANT_STORM_SCORE,
|
||||
"downburst_gusts": WIND_GUST_DOWNBURST_THRESHOLD,
|
||||
},
|
||||
"mid": {
|
||||
"cape": 1800.0,
|
||||
"lightning_precip_h": 5.0,
|
||||
"lightning_precip_3h": 12.0,
|
||||
"precip_h": 40.0,
|
||||
"precip_3h": 65.0,
|
||||
"score": 70.0,
|
||||
"downburst_gusts": 75.0,
|
||||
},
|
||||
"far": {
|
||||
"cape": 2500.0,
|
||||
"lightning_precip_h": 10.0,
|
||||
"lightning_precip_3h": 25.0,
|
||||
"precip_h": 55.0,
|
||||
"precip_3h": 80.0,
|
||||
"score": 85.0,
|
||||
"downburst_gusts": 90.0,
|
||||
},
|
||||
}
|
||||
# Vento: livello minimo notificato per fascia (1=giallo, 2=arancione, 3=rosso)
|
||||
LEAD_WIND_MIN_LEVEL = {"near": 1, "mid": 2, "far": 3}
|
||||
# Pioggia: mm/3h minimi per fascia
|
||||
LEAD_RAIN_3H = {"near": RAIN_3H_LIMIT, "mid": 40.0, "far": 55.0}
|
||||
|
||||
# ----------------- FILES -----------------
|
||||
STATE_FILE = "/home/daniely/docker/telegram-bot/weather_state.json"
|
||||
@@ -232,6 +272,18 @@ def telegram_send_html(message_html: str, chat_ids: Optional[List[str]] = None)
|
||||
message_html: Messaggio HTML da inviare
|
||||
chat_ids: Lista di chat IDs (default: TELEGRAM_CHAT_IDS)
|
||||
"""
|
||||
try:
|
||||
from telegram_gate import mirror_alert_to_web, telegram_alerts_enabled
|
||||
except ImportError:
|
||||
telegram_alerts_enabled = lambda: True # type: ignore
|
||||
mirror_alert_to_web = lambda *a, **k: False # type: ignore
|
||||
|
||||
if not telegram_alerts_enabled():
|
||||
LOGGER.info("Telegram sospeso: skip severe_weather")
|
||||
if message_html:
|
||||
mirror_alert_to_web(message_html, "severe_weather", "warning", is_html=True)
|
||||
return False
|
||||
|
||||
token = load_bot_token()
|
||||
if not token:
|
||||
LOGGER.warning("Telegram token missing: message not sent.")
|
||||
@@ -1159,9 +1211,32 @@ def format_convective_alert(storm_events: List[Dict], times: List[str], start_id
|
||||
# =============================================================================
|
||||
# SIGNIFICATIVITÀ E ANTI-SPAM (temporali severi)
|
||||
# =============================================================================
|
||||
def storm_event_significance(event: Dict) -> List[str]:
|
||||
"""Ritorna i motivi per cui un evento convettivo è 'significativo' (degno di
|
||||
notifica). Lista vuota = evento marginale, da NON notificare."""
|
||||
def lead_tier(lead_hours: float) -> str:
|
||||
"""Fascia di anticipo: near (<6h), mid (6–12h), far (≥12h)."""
|
||||
if lead_hours < LEAD_NEAR_H:
|
||||
return "near"
|
||||
if lead_hours < LEAD_MID_H:
|
||||
return "mid"
|
||||
return "far"
|
||||
|
||||
|
||||
def lead_hours_from_start_str(start_str: str, now: datetime.datetime) -> float:
|
||||
"""Calcola ore di anticipo da stringa 'dd/mm HH:MM' (o vuota → 0)."""
|
||||
if not start_str:
|
||||
return 0.0
|
||||
try:
|
||||
# Formato ddmmyy_hhmm: "dd/mm HH:MM" — anno corrente / prossimo
|
||||
dt = datetime.datetime.strptime(start_str.strip(), "%d/%m %H:%M")
|
||||
dt = dt.replace(year=now.year, tzinfo=TZINFO)
|
||||
if dt < now - datetime.timedelta(days=1):
|
||||
dt = dt.replace(year=now.year + 1)
|
||||
return max(0.0, (dt - now).total_seconds() / 3600.0)
|
||||
except Exception:
|
||||
return 0.0
|
||||
|
||||
|
||||
def storm_event_significance(event: Dict, lead_hours: float = 0.0) -> List[str]:
|
||||
"""Ritorna i motivi di significatività; soglie scalate per anticipo (lead_hours)."""
|
||||
reasons: List[str] = []
|
||||
threats = event.get("threats", []) or []
|
||||
cape = float(event.get("cape", 0.0) or 0.0)
|
||||
@@ -1169,39 +1244,59 @@ def storm_event_significance(event: Dict) -> List[str]:
|
||||
precip = float(event.get("precip", 0.0) or 0.0)
|
||||
precip_3h = float(event.get("precip_3h", 0.0) or 0.0)
|
||||
score = float(event.get("score", 0.0) or 0.0)
|
||||
gusts = float(event.get("gusts", 0.0) or 0.0)
|
||||
|
||||
lightning_precip = (precip >= SIGNIFICANT_LIGHTNING_PRECIP_H or
|
||||
precip_3h >= SIGNIFICANT_LIGHTNING_PRECIP_3H)
|
||||
if ("Fulminazioni" in threats and cape >= SIGNIFICANT_CAPE_LIGHTNING
|
||||
thr = LEAD_STORM[lead_tier(lead_hours)]
|
||||
|
||||
lightning_precip = (precip >= thr["lightning_precip_h"] or
|
||||
precip_3h >= thr["lightning_precip_3h"])
|
||||
if ("Fulminazioni" in threats and cape >= thr["cape"]
|
||||
and lpi > 0 and lightning_precip):
|
||||
reasons.append("Fulminazioni forti")
|
||||
if precip >= SIGNIFICANT_PRECIP_H or precip_3h >= SIGNIFICANT_PRECIP_3H:
|
||||
if precip >= thr["precip_h"] or precip_3h >= thr["precip_3h"]:
|
||||
reasons.append("Bomba d'acqua")
|
||||
if "Downburst/Temporale violento" in threats:
|
||||
if ("Downburst/Temporale violento" in threats
|
||||
and gusts >= thr["downburst_gusts"]
|
||||
and cape >= thr["cape"]):
|
||||
reasons.append("Downburst")
|
||||
if score >= SIGNIFICANT_STORM_SCORE and not reasons:
|
||||
if score >= thr["score"] and not reasons:
|
||||
reasons.append("Temporale severo")
|
||||
return reasons
|
||||
|
||||
|
||||
def filter_significant_storms(storm_events: List[Dict], now: datetime.datetime) -> List[Dict]:
|
||||
"""Mantiene solo gli eventi significativi, con cluster minimo e anti-glitch."""
|
||||
"""Mantiene solo eventi significativi per la loro fascia di anticipo + cluster."""
|
||||
out: List[Dict] = []
|
||||
for ev in storm_events or []:
|
||||
reasons = storm_event_significance(ev)
|
||||
if not reasons:
|
||||
continue
|
||||
try:
|
||||
lead = (parse_time_to_local(ev["timestamp"]) - now).total_seconds() / 3600.0
|
||||
except Exception:
|
||||
lead = 0.0
|
||||
if lead < 0:
|
||||
lead = 0.0
|
||||
reasons = storm_event_significance(ev, lead_hours=lead)
|
||||
if not reasons:
|
||||
continue
|
||||
e2 = dict(ev)
|
||||
e2["lead_hours"] = lead
|
||||
e2["lead_tier"] = lead_tier(lead)
|
||||
e2["significance"] = reasons
|
||||
out.append(e2)
|
||||
return filter_storm_cluster(out)
|
||||
|
||||
|
||||
def wind_meets_lead_threshold(level: int, lead_hours: float) -> bool:
|
||||
"""Vento notificato solo se il livello raggiunge il minimo della fascia di anticipo."""
|
||||
if level <= 0:
|
||||
return False
|
||||
return level >= LEAD_WIND_MIN_LEVEL[lead_tier(lead_hours)]
|
||||
|
||||
|
||||
def rain_meets_lead_threshold(rain_max_3h: float, lead_hours: float) -> bool:
|
||||
"""Pioggia notificata solo se mm/3h raggiungono la soglia della fascia di anticipo."""
|
||||
return rain_max_3h >= LEAD_RAIN_3H[lead_tier(lead_hours)]
|
||||
|
||||
|
||||
def _today_key(now: datetime.datetime) -> str:
|
||||
return now.strftime("%Y-%m-%d")
|
||||
|
||||
@@ -1634,21 +1729,37 @@ def analyze(chat_ids: Optional[List[str]] = None, debug_mode: bool = False, lat:
|
||||
state["convective_storm_active"] = False
|
||||
state["last_storm_score"] = 0.0
|
||||
|
||||
# 2) Vento persistente
|
||||
if wind_level_curr > 0:
|
||||
# 2) Vento persistente — soglia livello scalata per anticipo
|
||||
wind_lead = lead_hours_from_start_str(wind_start, now) if wind_level_curr > 0 else 0.0
|
||||
wind_notify = wind_meets_lead_threshold(wind_level_curr, wind_lead)
|
||||
if wind_notify:
|
||||
wind_msg = wind_message(wind_level_curr, wind_peak, wind_start, wind_run_len)
|
||||
tier = lead_tier(wind_lead)
|
||||
if tier != "near":
|
||||
wind_msg += f"\n🔭 <i>Anticipo ~{wind_lead:.0f}h (fascia {tier}: soglia elevata)</i>"
|
||||
if "wind" in comparisons:
|
||||
comp = comparisons["wind"]
|
||||
wind_msg += f"\n⚠️ <b>Discordanza modelli</b>: AROME {comp['arome']:.0f} km/h | ICON {comp['icon']:.0f} km/h (scostamento {comp['diff_pct']:.0f}%)"
|
||||
alerts.append(wind_msg)
|
||||
state["wind_level"] = wind_level_curr
|
||||
state["last_wind_peak"] = float(wind_peak)
|
||||
elif wind_level_curr > 0:
|
||||
LOGGER.info(
|
||||
"Vento livello %s soppresso (anticipo ~%.0fh, fascia %s richiede livello ≥%s)",
|
||||
wind_level_curr, wind_lead, lead_tier(wind_lead),
|
||||
LEAD_WIND_MIN_LEVEL[lead_tier(wind_lead)],
|
||||
)
|
||||
state["wind_level"] = 0
|
||||
state["last_wind_peak"] = 0.0
|
||||
else:
|
||||
state["wind_level"] = 0
|
||||
state["last_wind_peak"] = 0.0
|
||||
|
||||
# 3) Pioggia persistente
|
||||
if rain_persist >= PERSIST_HOURS and rain_max_3h >= RAIN_3H_LIMIT:
|
||||
# 3) Pioggia persistente — mm/3h scalati per anticipo
|
||||
rain_lead = lead_hours_from_start_str(rain_start, now) if rain_persist >= PERSIST_HOURS else 0.0
|
||||
rain_base_ok = rain_persist >= PERSIST_HOURS and rain_max_3h >= RAIN_3H_LIMIT
|
||||
rain_notify = rain_base_ok and rain_meets_lead_threshold(rain_max_3h, rain_lead)
|
||||
if rain_notify:
|
||||
rain_analysis = None
|
||||
if rain_start:
|
||||
rain_start_idx = -1
|
||||
@@ -1670,18 +1781,31 @@ def analyze(chat_ids: Optional[List[str]] = None, debug_mode: bool = False, lat:
|
||||
threshold_mm_h=8.0,
|
||||
)
|
||||
rain_msg = rain_message(rain_max_3h, rain_start, rain_persist, rain_analysis=rain_analysis)
|
||||
tier = lead_tier(rain_lead)
|
||||
if tier != "near":
|
||||
rain_msg += (
|
||||
f"\n🔭 <i>Anticipo ~{rain_lead:.0f}h (fascia {tier}: "
|
||||
f"soglia {LEAD_RAIN_3H[tier]:.0f} mm/3h)</i>"
|
||||
)
|
||||
if "rain" in comparisons:
|
||||
comp = comparisons["rain"]
|
||||
rain_msg += f"\n⚠️ <b>Discordanza modelli</b>: AROME {comp['arome']:.1f} mm | ICON {comp['icon']:.1f} mm (scostamento {comp['diff_pct']:.0f}%)"
|
||||
alerts.append(rain_msg)
|
||||
state["last_rain_3h"] = float(rain_max_3h)
|
||||
elif rain_base_ok:
|
||||
LOGGER.info(
|
||||
"Pioggia %.1f mm/3h soppressa (anticipo ~%.0fh, fascia %s richiede ≥%.0f mm/3h)",
|
||||
rain_max_3h, rain_lead, lead_tier(rain_lead),
|
||||
LEAD_RAIN_3H[lead_tier(rain_lead)],
|
||||
)
|
||||
state["last_rain_3h"] = 0.0
|
||||
else:
|
||||
state["last_rain_3h"] = 0.0
|
||||
|
||||
is_alarm_now = (
|
||||
(len(sig_storms) > 0)
|
||||
or (wind_level_curr > 0)
|
||||
or (rain_persist >= PERSIST_HOURS and rain_max_3h >= RAIN_3H_LIMIT)
|
||||
or wind_notify
|
||||
or rain_notify
|
||||
)
|
||||
|
||||
should_notify = bool(alerts)
|
||||
@@ -1692,7 +1816,12 @@ def analyze(chat_ids: Optional[List[str]] = None, debug_mode: bool = False, lat:
|
||||
should_notify = True
|
||||
debug_message_only = True
|
||||
|
||||
alert_signature = build_alert_signature(alerts, wind_level_curr, rain_max_3h, sig_storms)
|
||||
alert_signature = build_alert_signature(
|
||||
alerts,
|
||||
wind_level_curr if wind_notify else 0,
|
||||
rain_max_3h if rain_notify else 0.0,
|
||||
sig_storms,
|
||||
)
|
||||
|
||||
# --- Gate anti-spam globale (max 1/giorno + cooldown, escalation se peggioramento) ---
|
||||
if should_notify and alerts and not debug_message_only:
|
||||
@@ -1741,10 +1870,10 @@ def analyze(chat_ids: Optional[List[str]] = None, debug_mode: bool = False, lat:
|
||||
alert_types = []
|
||||
if sig_storms and len(sig_storms) > 0:
|
||||
alert_types.append("TEMPORALI SEVERI")
|
||||
if wind_level_curr > 0:
|
||||
if wind_notify:
|
||||
wind_labels = {3: "TEMPESTA", 2: "VENTO MOLTO FORTE", 1: "VENTO FORTE"}
|
||||
alert_types.append(wind_labels.get(wind_level_curr, "VENTO FORTE"))
|
||||
if rain_persist >= PERSIST_HOURS and rain_max_3h >= RAIN_3H_LIMIT:
|
||||
if rain_notify:
|
||||
alert_types.append("PIOGGIA INTENSA")
|
||||
|
||||
state["alert_active"] = True
|
||||
|
||||
@@ -200,6 +200,15 @@ def ddmmyyhhmm(dt: datetime.datetime) -> str:
|
||||
# =============================================================================
|
||||
def telegram_send_html(message_html: str, chat_ids: Optional[List[str]] = None) -> bool:
|
||||
"""Never raises. Returns True if at least one chat_id succeeded."""
|
||||
try:
|
||||
from telegram_gate import telegram_alerts_enabled
|
||||
except ImportError:
|
||||
telegram_alerts_enabled = lambda: True # type: ignore
|
||||
|
||||
if not telegram_alerts_enabled():
|
||||
LOGGER.info("Telegram sospeso: skip severe_circondario")
|
||||
return False
|
||||
|
||||
token = load_bot_token()
|
||||
if not token:
|
||||
LOGGER.warning("Telegram token missing: message not sent.")
|
||||
|
||||
@@ -144,6 +144,15 @@ def telegram_send_html(message_html: str, chat_ids: Optional[List[str]] = None)
|
||||
message_html: Messaggio HTML da inviare
|
||||
chat_ids: Lista di chat IDs (default: TELEGRAM_CHAT_IDS)
|
||||
"""
|
||||
try:
|
||||
from telegram_gate import telegram_alerts_enabled
|
||||
except ImportError:
|
||||
telegram_alerts_enabled = lambda: True # type: ignore
|
||||
|
||||
if not telegram_alerts_enabled():
|
||||
LOGGER.info("Telegram sospeso: skip student_alert")
|
||||
return False
|
||||
|
||||
token = load_bot_token()
|
||||
if not token:
|
||||
LOGGER.warning("Telegram token missing: message not sent.")
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Abilitazione/sospensione notifiche Telegram per script cron meteo e report."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
LOGGER = logging.getLogger("telegram_gate")
|
||||
_CONFIG = Path(__file__).with_name("cron-alerts.env")
|
||||
|
||||
|
||||
def _load_config() -> None:
|
||||
if not _CONFIG.is_file():
|
||||
return
|
||||
try:
|
||||
for line in _CONFIG.read_text(encoding="utf-8").splitlines():
|
||||
line = line.strip()
|
||||
if not line or line.startswith("#") or "=" not in line:
|
||||
continue
|
||||
key, value = line.split("=", 1)
|
||||
key = key.strip()
|
||||
value = value.strip().strip('"').strip("'")
|
||||
if key and key not in os.environ:
|
||||
os.environ[key] = value
|
||||
except OSError as exc:
|
||||
LOGGER.debug("Config non caricata: %s", exc)
|
||||
|
||||
|
||||
_load_config()
|
||||
|
||||
|
||||
def telegram_alerts_enabled() -> bool:
|
||||
value = os.environ.get("LOOGLE_TELEGRAM_ALERTS", "1").strip().lower()
|
||||
return value not in ("0", "off", "false", "no", "disabled")
|
||||
|
||||
|
||||
def mirror_alert_to_web(
|
||||
message: str,
|
||||
category: str,
|
||||
severity: str = "warning",
|
||||
*,
|
||||
is_html: bool = False,
|
||||
) -> bool:
|
||||
if not message or not category:
|
||||
return False
|
||||
try:
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, "/home/daniely/docker/shared")
|
||||
from loogle_core.alert_dispatcher import mirror_to_web
|
||||
|
||||
return mirror_to_web(message, category, severity, is_html=is_html)
|
||||
except Exception as exc:
|
||||
LOGGER.debug("Mirror WebApp fallito (%s): %s", category, exc)
|
||||
return False
|
||||
Reference in New Issue
Block a user