79 lines
2.3 KiB
Python
79 lines
2.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Client Irrigazione Smart — irri.loogle.it."""
|
|
|
|
import os
|
|
from typing import Any, Optional
|
|
|
|
from .session_client import SessionApiClient
|
|
|
|
MCP_USERS = ("daniele", "lucia", "davide", "luca")
|
|
MCP_TO_SERVICE_USER = {
|
|
"daniele": "admin",
|
|
"lucia": "lucia",
|
|
"davide": "dado",
|
|
"luca": "luca",
|
|
}
|
|
|
|
PUBLIC_URL = os.environ.get("IRRIGAZIONE_URL", "https://irri.loogle.it").rstrip("/")
|
|
API_URL = os.environ.get("IRRIGAZIONE_API_URL", PUBLIC_URL).rstrip("/")
|
|
|
|
_client: Optional[SessionApiClient] = None
|
|
|
|
|
|
def _service_username(mcp_username: str) -> str:
|
|
return MCP_TO_SERVICE_USER.get(mcp_username.lower(), mcp_username.lower())
|
|
|
|
|
|
def _client_instance() -> SessionApiClient:
|
|
global _client
|
|
if _client is None:
|
|
verify = os.environ.get("IRRIGAZIONE_VERIFY_SSL", "true").strip().lower() not in (
|
|
"0", "false", "no", "off",
|
|
)
|
|
client = SessionApiClient(
|
|
service="IRRIGAZIONE",
|
|
base_url=API_URL,
|
|
verify_ssl=verify,
|
|
)
|
|
client.map_username = _service_username # type: ignore[attr-defined]
|
|
_client = client
|
|
return _client
|
|
|
|
|
|
def is_configured(username: Optional[str] = None) -> bool:
|
|
user = (username or "daniele").lower()
|
|
if os.environ.get(f"IRRIGAZIONE_PASSWORD_{user.upper()}", "").strip():
|
|
return True
|
|
if os.environ.get("IRRIGAZIONE_PASSWORD", "").strip():
|
|
return True
|
|
return user in MCP_USERS
|
|
|
|
|
|
def get_status(username: str) -> dict:
|
|
return _client_instance().get("/api/status", username=username)
|
|
|
|
|
|
def get_zones(username: str) -> Any:
|
|
return _client_instance().get("/api/zones", username=username)
|
|
|
|
|
|
def get_history(username: str, limit: int = 30) -> Any:
|
|
data = _client_instance().get("/api/history", username=username)
|
|
if isinstance(data, list):
|
|
return data[:limit]
|
|
if isinstance(data, dict) and "items" in data:
|
|
items = data["items"]
|
|
return items[:limit] if isinstance(items, list) else data
|
|
return data
|
|
|
|
|
|
def get_events(username: str, limit: int = 50) -> Any:
|
|
data = _client_instance().get("/api/events", username=username)
|
|
if isinstance(data, list):
|
|
return data[:limit]
|
|
return data
|
|
|
|
|
|
def get_lavori_summary(username: str) -> Any:
|
|
return _client_instance().get("/api/lavori/summary", username=username)
|