work: добавил автоматический режим
This commit is contained in:
+113
-19
@@ -7,6 +7,9 @@ from fastapi.responses import JSONResponse
|
||||
|
||||
from app.my_dataclasses import (
|
||||
TION_MAC,
|
||||
QINGPING_MAC,
|
||||
QINGPING_MQTT_HOST,
|
||||
QINGPING_MQTT_PORT,
|
||||
MIN_FAN_SPEED,
|
||||
MAX_FAN_SPEED,
|
||||
MIN_TARGET_TEMP,
|
||||
@@ -14,6 +17,13 @@ from app.my_dataclasses import (
|
||||
AIR_MODE_OUTSIDE,
|
||||
AIR_MODE_RECIRCULATION,
|
||||
SCHEDULE_FILE,
|
||||
AUTO_CONFIG_FILE,
|
||||
)
|
||||
|
||||
from app.auto import (
|
||||
AutoController,
|
||||
Co2SpeedPolicy,
|
||||
load_auto_config,
|
||||
)
|
||||
|
||||
from app.tion import (
|
||||
@@ -27,7 +37,7 @@ from schedule import (
|
||||
load_schedule,
|
||||
)
|
||||
|
||||
|
||||
from app.qingping.service import QingpingService
|
||||
|
||||
def configure_logging() -> None:
|
||||
noisy_loggers = (
|
||||
@@ -57,9 +67,19 @@ service = TionService(
|
||||
controller,
|
||||
poll_interval=5,
|
||||
)
|
||||
|
||||
qingping_service = QingpingService(
|
||||
host=QINGPING_MQTT_HOST,
|
||||
port=QINGPING_MQTT_PORT,
|
||||
mac=QINGPING_MAC,
|
||||
)
|
||||
|
||||
schedule_service: ScheduleService | None = None
|
||||
schedule_load_error: str | None = None
|
||||
|
||||
auto_controller: AutoController | None = None
|
||||
auto_load_error: str | None = None
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
# Application lifecycle
|
||||
# ----------------------------------------------------------------------
|
||||
@@ -68,8 +88,11 @@ schedule_load_error: str | None = None
|
||||
async def lifespan(app: FastAPI):
|
||||
global schedule_service
|
||||
global schedule_load_error
|
||||
global auto_controller
|
||||
global auto_load_error
|
||||
|
||||
await service.start()
|
||||
await qingping_service.start()
|
||||
|
||||
try:
|
||||
try:
|
||||
@@ -84,6 +107,50 @@ async def lifespan(app: FastAPI):
|
||||
await schedule_service.start()
|
||||
schedule_load_error = None
|
||||
|
||||
try:
|
||||
auto_config = load_auto_config(
|
||||
AUTO_CONFIG_FILE
|
||||
)
|
||||
|
||||
auto_policy = Co2SpeedPolicy(
|
||||
base_speed=(
|
||||
auto_config.co2.base_speed
|
||||
),
|
||||
thresholds=(
|
||||
auto_config.co2.thresholds
|
||||
),
|
||||
hysteresis=(
|
||||
auto_config.co2.hysteresis
|
||||
),
|
||||
)
|
||||
|
||||
auto_interval = (
|
||||
auto_config.check_interval
|
||||
)
|
||||
|
||||
auto_load_error = None
|
||||
|
||||
except Exception as exc:
|
||||
auto_policy = None
|
||||
|
||||
# Безопасный встроенный интервал нужен,
|
||||
# потому что auto.yaml сейчас недоступен.
|
||||
auto_interval = 5.0
|
||||
|
||||
auto_load_error = (
|
||||
f"{type(exc).__name__}: {exc}"
|
||||
)
|
||||
|
||||
auto_controller = AutoController(
|
||||
schedule_service=schedule_service,
|
||||
qingping_service=qingping_service,
|
||||
tion_service=service,
|
||||
policy=auto_policy,
|
||||
interval=auto_interval,
|
||||
)
|
||||
|
||||
await auto_controller.start()
|
||||
|
||||
except Exception as exc:
|
||||
schedule_service = None
|
||||
schedule_load_error = f"{type(exc).__name__}: {exc}"
|
||||
@@ -93,6 +160,7 @@ async def lifespan(app: FastAPI):
|
||||
if schedule_service is not None:
|
||||
await schedule_service.stop()
|
||||
|
||||
await qingping_service.stop()
|
||||
await service.stop()
|
||||
|
||||
|
||||
@@ -152,6 +220,8 @@ def get_status() -> dict:
|
||||
else None
|
||||
),
|
||||
|
||||
"qingping": qingping_service.status(),
|
||||
|
||||
"schedule": get_schedule_status(),
|
||||
}
|
||||
|
||||
@@ -349,6 +419,30 @@ def get_schedule_status() -> dict:
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def get_auto_status() -> dict:
|
||||
|
||||
if auto_controller is None:
|
||||
return {
|
||||
"available": False,
|
||||
"state": "unavailable",
|
||||
"reason": None,
|
||||
"target_speed": None,
|
||||
"auto_speed": None,
|
||||
"last_error": None,
|
||||
"config_error": (
|
||||
auto_load_error
|
||||
),
|
||||
}
|
||||
|
||||
return {
|
||||
"available": True,
|
||||
**auto_controller.status(),
|
||||
"config_error": (
|
||||
auto_load_error
|
||||
),
|
||||
}
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
# Status
|
||||
# ----------------------------------------------------------------------
|
||||
@@ -401,24 +495,6 @@ async def increase_speed():
|
||||
),
|
||||
)
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
# Fan speed
|
||||
# ----------------------------------------------------------------------
|
||||
@app.post("/api/tion/speed/{speed}")
|
||||
async def set_speed(
|
||||
speed: Annotated[
|
||||
int,
|
||||
Path(
|
||||
ge=MIN_FAN_SPEED,
|
||||
le=MAX_FAN_SPEED,
|
||||
),
|
||||
] ):
|
||||
|
||||
return await execute_command(
|
||||
lambda tion: tion.set_speed(speed),
|
||||
ScheduledSettings(speed=speed),
|
||||
)
|
||||
|
||||
|
||||
@app.post("/api/tion/speed/decrease")
|
||||
async def decrease_speed():
|
||||
@@ -440,6 +516,24 @@ async def decrease_speed():
|
||||
)
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
# Fan speed
|
||||
# ----------------------------------------------------------------------
|
||||
@app.post("/api/tion/speed/{speed}")
|
||||
async def set_speed(
|
||||
speed: Annotated[
|
||||
int,
|
||||
Path(
|
||||
ge=MIN_FAN_SPEED,
|
||||
le=MAX_FAN_SPEED,
|
||||
),
|
||||
] ):
|
||||
|
||||
return await execute_command(
|
||||
lambda tion: tion.set_speed(speed),
|
||||
ScheduledSettings(speed=speed),
|
||||
)
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
# Heater
|
||||
# ----------------------------------------------------------------------
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
from .config import (
|
||||
AutoConfig,
|
||||
Co2Config,
|
||||
load_auto_config,
|
||||
)
|
||||
|
||||
from .co2_policy import (
|
||||
Co2SpeedPolicy,
|
||||
)
|
||||
|
||||
from .controller import (
|
||||
AutoController,
|
||||
)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"AutoConfig",
|
||||
"Co2Config",
|
||||
"load_auto_config",
|
||||
"Co2SpeedPolicy",
|
||||
"AutoController",
|
||||
]
|
||||
@@ -0,0 +1,129 @@
|
||||
class Co2SpeedPolicy:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
base_speed: int,
|
||||
thresholds: tuple[
|
||||
tuple[int, int],
|
||||
...
|
||||
],
|
||||
hysteresis: int,
|
||||
):
|
||||
if hysteresis < 0:
|
||||
raise ValueError(
|
||||
"hysteresis must be >= 0"
|
||||
)
|
||||
|
||||
if not thresholds:
|
||||
raise ValueError(
|
||||
"thresholds cannot be empty"
|
||||
)
|
||||
|
||||
previous_ppm = None
|
||||
|
||||
speeds = [base_speed]
|
||||
|
||||
for ppm, speed in thresholds:
|
||||
|
||||
if previous_ppm is not None:
|
||||
if ppm <= previous_ppm:
|
||||
raise ValueError(
|
||||
"CO2 thresholds must "
|
||||
"be strictly increasing"
|
||||
)
|
||||
|
||||
if speed in speeds:
|
||||
raise ValueError(
|
||||
"AUTO speeds must be unique"
|
||||
)
|
||||
|
||||
speeds.append(speed)
|
||||
previous_ppm = ppm
|
||||
|
||||
self._base_speed = base_speed
|
||||
self._thresholds = thresholds
|
||||
self._hysteresis = hysteresis
|
||||
self._speeds = tuple(speeds)
|
||||
|
||||
|
||||
def select_speed(self, co2: int, current_speed: int | None) -> int:
|
||||
|
||||
if co2 < 0:
|
||||
raise ValueError(
|
||||
"CO2 cannot be negative"
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Первое решение AUTO.
|
||||
#
|
||||
# Гистерезис пока применять не к чему:
|
||||
# предыдущей AUTO-скорости ещё нет.
|
||||
# --------------------------------------------------
|
||||
|
||||
if (
|
||||
current_speed is None
|
||||
or current_speed not in self._speeds
|
||||
):
|
||||
return self._select_initial_speed(co2)
|
||||
|
||||
index = self._speeds.index(current_speed)
|
||||
|
||||
# --------------------------------------------------
|
||||
# CO2 растёт.
|
||||
#
|
||||
# Проверяем пороги перехода вверх.
|
||||
# За один вызов можем перепрыгнуть
|
||||
# сразу несколько скоростей.
|
||||
# --------------------------------------------------
|
||||
|
||||
while index < len(
|
||||
self._thresholds
|
||||
):
|
||||
|
||||
ppm, _ = self._thresholds[index]
|
||||
|
||||
if co2 < ppm:
|
||||
break
|
||||
|
||||
index += 1
|
||||
|
||||
# --------------------------------------------------
|
||||
# CO2 падает.
|
||||
#
|
||||
# Для перехода вниз используем:
|
||||
#
|
||||
# threshold - hysteresis
|
||||
#
|
||||
# Поэтому скорость не будет прыгать
|
||||
# туда-сюда около одного порога.
|
||||
# --------------------------------------------------
|
||||
|
||||
while index > 0:
|
||||
|
||||
ppm, _ = self._thresholds[index - 1]
|
||||
|
||||
down_threshold = ppm - self._hysteresis
|
||||
|
||||
if co2 > down_threshold:
|
||||
break
|
||||
|
||||
index -= 1
|
||||
|
||||
return self._speeds[index]
|
||||
|
||||
|
||||
def _select_initial_speed(self, co2: int) -> int:
|
||||
|
||||
speed = self._base_speed
|
||||
|
||||
for ppm, candidate_speed in (
|
||||
self._thresholds
|
||||
):
|
||||
|
||||
if co2 < ppm:
|
||||
break
|
||||
|
||||
speed = candidate_speed
|
||||
|
||||
return speed
|
||||
@@ -0,0 +1,310 @@
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import yaml
|
||||
|
||||
from app.my_dataclasses import (
|
||||
MIN_FAN_SPEED,
|
||||
MAX_FAN_SPEED,
|
||||
)
|
||||
|
||||
|
||||
@dataclass(
|
||||
frozen=True,
|
||||
slots=True,
|
||||
)
|
||||
class Co2Config:
|
||||
base_speed: int
|
||||
hysteresis: int
|
||||
|
||||
thresholds: tuple[
|
||||
tuple[int, int],
|
||||
...
|
||||
]
|
||||
|
||||
|
||||
@dataclass(
|
||||
frozen=True,
|
||||
slots=True,
|
||||
)
|
||||
class AutoConfig:
|
||||
version: int
|
||||
check_interval: float
|
||||
co2: Co2Config
|
||||
|
||||
|
||||
def _require_dict(name: str, value: Any) -> dict:
|
||||
|
||||
if not isinstance(value, dict):
|
||||
raise ValueError(
|
||||
f"{name} must be an object"
|
||||
)
|
||||
|
||||
return value
|
||||
|
||||
|
||||
def _parse_speed(name: str, value: Any) -> int:
|
||||
|
||||
if type(value) is not int:
|
||||
raise ValueError(
|
||||
f"{name} must be an integer"
|
||||
)
|
||||
|
||||
if not (
|
||||
MIN_FAN_SPEED
|
||||
<= value
|
||||
<= MAX_FAN_SPEED
|
||||
):
|
||||
raise ValueError(
|
||||
f"{name} must be between "
|
||||
f"{MIN_FAN_SPEED} and "
|
||||
f"{MAX_FAN_SPEED}"
|
||||
)
|
||||
|
||||
return value
|
||||
|
||||
|
||||
def _parse_thresholds(value: Any) -> tuple[
|
||||
tuple[int, int],
|
||||
...
|
||||
]:
|
||||
|
||||
if not isinstance(value, list):
|
||||
raise ValueError(
|
||||
"co2.thresholds must be a list"
|
||||
)
|
||||
|
||||
if not value:
|
||||
raise ValueError(
|
||||
"co2.thresholds cannot be empty"
|
||||
)
|
||||
|
||||
result = []
|
||||
|
||||
previous_ppm = None
|
||||
previous_speed = None
|
||||
|
||||
for index, item in enumerate(value):
|
||||
|
||||
item = _require_dict(
|
||||
f"co2.thresholds[{index}]",
|
||||
item,
|
||||
)
|
||||
|
||||
unknown = set(item) - {
|
||||
"ppm",
|
||||
"speed",
|
||||
}
|
||||
|
||||
if unknown:
|
||||
raise ValueError(
|
||||
f"Unknown fields in "
|
||||
f"co2.thresholds[{index}]: "
|
||||
f"{sorted(unknown)}"
|
||||
)
|
||||
|
||||
if "ppm" not in item:
|
||||
raise ValueError(
|
||||
f"co2.thresholds[{index}].ppm "
|
||||
f"is required"
|
||||
)
|
||||
|
||||
if "speed" not in item:
|
||||
raise ValueError(
|
||||
f"co2.thresholds[{index}].speed "
|
||||
f"is required"
|
||||
)
|
||||
|
||||
ppm = item["ppm"]
|
||||
|
||||
if type(ppm) is not int:
|
||||
raise ValueError(
|
||||
f"co2.thresholds[{index}].ppm "
|
||||
f"must be an integer"
|
||||
)
|
||||
|
||||
if ppm <= 0:
|
||||
raise ValueError(
|
||||
f"co2.thresholds[{index}].ppm "
|
||||
f"must be > 0"
|
||||
)
|
||||
|
||||
speed = _parse_speed(
|
||||
(
|
||||
f"co2.thresholds"
|
||||
f"[{index}].speed"
|
||||
),
|
||||
item["speed"],
|
||||
)
|
||||
|
||||
if (
|
||||
previous_ppm is not None
|
||||
and ppm <= previous_ppm
|
||||
):
|
||||
raise ValueError(
|
||||
"CO2 thresholds must be "
|
||||
"strictly increasing"
|
||||
)
|
||||
|
||||
if (
|
||||
previous_speed is not None
|
||||
and speed <= previous_speed
|
||||
):
|
||||
raise ValueError(
|
||||
"AUTO speeds must be "
|
||||
"strictly increasing"
|
||||
)
|
||||
|
||||
result.append(
|
||||
(
|
||||
ppm,
|
||||
speed,
|
||||
)
|
||||
)
|
||||
|
||||
previous_ppm = ppm
|
||||
previous_speed = speed
|
||||
|
||||
return tuple(result)
|
||||
|
||||
|
||||
def _parse_co2(value: Any) -> Co2Config:
|
||||
|
||||
data = _require_dict(
|
||||
"co2",
|
||||
value,
|
||||
)
|
||||
|
||||
unknown = set(data) - {
|
||||
"base_speed",
|
||||
"hysteresis",
|
||||
"thresholds",
|
||||
}
|
||||
|
||||
if unknown:
|
||||
raise ValueError(
|
||||
f"Unknown CO2 config fields: "
|
||||
f"{sorted(unknown)}"
|
||||
)
|
||||
|
||||
if "base_speed" not in data:
|
||||
raise ValueError(
|
||||
"co2.base_speed is required"
|
||||
)
|
||||
|
||||
if "hysteresis" not in data:
|
||||
raise ValueError(
|
||||
"co2.hysteresis is required"
|
||||
)
|
||||
|
||||
if "thresholds" not in data:
|
||||
raise ValueError(
|
||||
"co2.thresholds is required"
|
||||
)
|
||||
|
||||
base_speed = _parse_speed(
|
||||
"co2.base_speed",
|
||||
data["base_speed"],
|
||||
)
|
||||
|
||||
hysteresis = data["hysteresis"]
|
||||
|
||||
if type(hysteresis) is not int:
|
||||
raise ValueError(
|
||||
"co2.hysteresis must be "
|
||||
"an integer"
|
||||
)
|
||||
|
||||
if hysteresis < 0:
|
||||
raise ValueError(
|
||||
"co2.hysteresis must be >= 0"
|
||||
)
|
||||
|
||||
thresholds = _parse_thresholds(
|
||||
data["thresholds"]
|
||||
)
|
||||
|
||||
first_speed = thresholds[0][1]
|
||||
|
||||
if first_speed <= base_speed:
|
||||
raise ValueError(
|
||||
"First threshold speed must be "
|
||||
"greater than base_speed"
|
||||
)
|
||||
|
||||
return Co2Config(
|
||||
base_speed=base_speed,
|
||||
hysteresis=hysteresis,
|
||||
thresholds=thresholds,
|
||||
)
|
||||
|
||||
|
||||
def load_auto_config(path: str | Path) -> AutoConfig:
|
||||
|
||||
path = Path(path)
|
||||
|
||||
with path.open(
|
||||
"r",
|
||||
encoding="utf-8",
|
||||
) as file:
|
||||
raw = yaml.safe_load(file)
|
||||
|
||||
data = _require_dict(
|
||||
"AUTO config",
|
||||
raw,
|
||||
)
|
||||
|
||||
unknown = set(data) - {
|
||||
"version",
|
||||
"check_interval",
|
||||
"co2",
|
||||
}
|
||||
|
||||
if unknown:
|
||||
raise ValueError(
|
||||
f"Unknown AUTO config fields: "
|
||||
f"{sorted(unknown)}"
|
||||
)
|
||||
|
||||
version = data.get("version")
|
||||
|
||||
if version != 1:
|
||||
raise ValueError(
|
||||
f"Unsupported AUTO config "
|
||||
f"version: {version!r}"
|
||||
)
|
||||
|
||||
check_interval = data.get(
|
||||
"check_interval"
|
||||
)
|
||||
|
||||
if (
|
||||
type(check_interval) not in {
|
||||
int,
|
||||
float,
|
||||
}
|
||||
):
|
||||
raise ValueError(
|
||||
"check_interval must be a number"
|
||||
)
|
||||
|
||||
if check_interval <= 0:
|
||||
raise ValueError(
|
||||
"check_interval must be > 0"
|
||||
)
|
||||
|
||||
if "co2" not in data:
|
||||
raise ValueError(
|
||||
"co2 config is required"
|
||||
)
|
||||
|
||||
co2 = _parse_co2(
|
||||
data["co2"]
|
||||
)
|
||||
|
||||
return AutoConfig(
|
||||
version=version,
|
||||
check_interval=float(check_interval),
|
||||
co2=co2,
|
||||
)
|
||||
@@ -0,0 +1,208 @@
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
from app.auto.co2_policy import Co2SpeedPolicy
|
||||
|
||||
class AutoController:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
schedule_service,
|
||||
qingping_service,
|
||||
tion_service,
|
||||
policy: Co2SpeedPolicy | None = None,
|
||||
interval: float = 5.0,
|
||||
):
|
||||
self._schedule = schedule_service
|
||||
self._qingping = qingping_service
|
||||
self._tion = tion_service
|
||||
self._policy = policy
|
||||
|
||||
self._interval = interval
|
||||
|
||||
self._task: asyncio.Task | None = None
|
||||
|
||||
self._state = "inactive"
|
||||
self._reason: str | None = None
|
||||
|
||||
self._target_speed: int | None = None
|
||||
self._last_error: str | None = None
|
||||
|
||||
self._auto_speed: int | None = None
|
||||
|
||||
|
||||
async def start(self) -> None:
|
||||
|
||||
if self._task is not None:
|
||||
return
|
||||
|
||||
self._task = asyncio.create_task( self._loop() )
|
||||
|
||||
|
||||
async def stop(self) -> None:
|
||||
|
||||
if self._task is None:
|
||||
return
|
||||
|
||||
self._task.cancel()
|
||||
|
||||
try:
|
||||
await self._task
|
||||
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
finally:
|
||||
self._task = None
|
||||
|
||||
|
||||
async def _loop(self) -> None:
|
||||
|
||||
while True:
|
||||
|
||||
try:
|
||||
await self._process()
|
||||
|
||||
except asyncio.CancelledError:
|
||||
raise
|
||||
|
||||
except Exception as exc:
|
||||
self._last_error = str(exc)
|
||||
|
||||
await asyncio.sleep( self._interval )
|
||||
|
||||
|
||||
async def _process(self) -> None:
|
||||
|
||||
now = datetime.now()
|
||||
|
||||
resolution = self._schedule.resolve(now)
|
||||
|
||||
# ----------------------------------------------
|
||||
# Активен ручной override.
|
||||
#
|
||||
# Пока пользователь вручную управляет Tion,
|
||||
# AUTO вообще не вмешивается.
|
||||
# ----------------------------------------------
|
||||
if self._schedule.override_active:
|
||||
self._state = "suspended"
|
||||
self._reason = "manual_override"
|
||||
self._target_speed = None
|
||||
self._last_error = None
|
||||
self._auto_speed = None
|
||||
|
||||
return
|
||||
|
||||
# ----------------------------------------------
|
||||
# AUTO сейчас не активен
|
||||
# ----------------------------------------------
|
||||
|
||||
if (
|
||||
not resolution.enabled
|
||||
or not resolution.auto_active
|
||||
):
|
||||
self._state = "inactive"
|
||||
self._reason = None
|
||||
self._target_speed = None
|
||||
self._last_error = None
|
||||
self._auto_speed = None
|
||||
|
||||
return
|
||||
|
||||
fallback_speed = resolution.auto_fallback_speed
|
||||
|
||||
if fallback_speed is None:
|
||||
raise RuntimeError(
|
||||
"AUTO is active but "
|
||||
"fallback speed is missing"
|
||||
)
|
||||
# ----------------------------------------------
|
||||
# CO2 policy недоступна.
|
||||
#
|
||||
# Например, auto.yaml не загрузился.
|
||||
# AUTO работает в аварийном fallback-only режиме.
|
||||
# ----------------------------------------------
|
||||
if self._policy is None:
|
||||
self._state = "fallback"
|
||||
self._reason = "auto_policy_unavailable"
|
||||
self._auto_speed = None
|
||||
|
||||
await self._set_speed(
|
||||
fallback_speed
|
||||
)
|
||||
|
||||
self._last_error = None
|
||||
|
||||
return
|
||||
|
||||
qingping_state = self._qingping.state
|
||||
|
||||
# ----------------------------------------------
|
||||
# Qingping исправен
|
||||
# ----------------------------------------------
|
||||
|
||||
if (
|
||||
self._qingping.online
|
||||
and qingping_state.co2 is not None
|
||||
):
|
||||
target_speed = (
|
||||
self._policy.select_speed(
|
||||
co2=qingping_state.co2,
|
||||
current_speed=self._auto_speed,
|
||||
)
|
||||
)
|
||||
|
||||
self._state = "active"
|
||||
self._reason = None
|
||||
|
||||
await self._set_speed(
|
||||
target_speed
|
||||
)
|
||||
|
||||
self._auto_speed = target_speed
|
||||
self._last_error = None
|
||||
|
||||
return
|
||||
|
||||
# ----------------------------------------------
|
||||
# AUTO не может работать.
|
||||
# Переходим на fallback.
|
||||
# ----------------------------------------------
|
||||
|
||||
if not self._qingping.online:
|
||||
reason = "qingping_offline"
|
||||
|
||||
else:
|
||||
reason = "co2_missing"
|
||||
|
||||
self._state = "fallback"
|
||||
self._reason = reason
|
||||
|
||||
self._auto_speed = None
|
||||
|
||||
await self._set_speed(fallback_speed)
|
||||
|
||||
self._last_error = None
|
||||
|
||||
|
||||
async def _set_speed(self, speed: int) -> None:
|
||||
|
||||
if self._target_speed == speed:
|
||||
return
|
||||
|
||||
await self._tion.execute(
|
||||
lambda controller:
|
||||
controller.set_speed(speed)
|
||||
)
|
||||
|
||||
self._target_speed = speed
|
||||
|
||||
|
||||
def status(self) -> dict:
|
||||
return {
|
||||
"state": self._state,
|
||||
"reason": self._reason,
|
||||
"target_speed": self._target_speed,
|
||||
"auto_speed": self._auto_speed,
|
||||
"last_error": self._last_error,
|
||||
}
|
||||
|
||||
@@ -9,6 +9,11 @@ from typing import Any, Mapping
|
||||
# MAC Bluetooth бризера
|
||||
TION_MAC = "d1:74:9b:eb:ee:a6"
|
||||
|
||||
QINGPING_MAC = "CCB5D131BA93"
|
||||
QINGPING_MQTT_HOST = "192.168.7.3"
|
||||
QINGPING_MQTT_PORT = 1883
|
||||
|
||||
|
||||
MIN_FAN_SPEED = 1
|
||||
MAX_FAN_SPEED = 6
|
||||
|
||||
@@ -31,5 +36,11 @@ SCHEDULE_FILE = (
|
||||
/ "schedule.yaml"
|
||||
)
|
||||
|
||||
AUTO_CONFIG_FILE = (
|
||||
PROJECT_ROOT
|
||||
/ "config"
|
||||
/ "auto.yaml"
|
||||
)
|
||||
|
||||
#Классы
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
# from dataclasses import asdict, dataclass
|
||||
#
|
||||
#
|
||||
# @dataclass(frozen=True, slots=True)
|
||||
# class QingpingState:
|
||||
# temperature: float
|
||||
# humidity: float
|
||||
# co2: int
|
||||
# pm25: int
|
||||
# pm10: int
|
||||
# rssi: int | None = None
|
||||
#
|
||||
# def to_dict(self) -> dict:
|
||||
# return asdict(self)
|
||||
#
|
||||
#
|
||||
from dataclasses import asdict, dataclass
|
||||
from datetime import datetime
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class QingpingState:
|
||||
temperature: float | None = None
|
||||
humidity: float | None = None
|
||||
co2: int | None = None
|
||||
pm25: int | None = None
|
||||
pm10: int | None = None
|
||||
battery: int | None = None
|
||||
|
||||
sample_timestamp: int | None = None
|
||||
sample_received_at: datetime | None = None
|
||||
last_message_at: datetime | None = None
|
||||
|
||||
wifi_rssi: int | None = None
|
||||
firmware: str | None = None
|
||||
mqtt_connected: bool = False
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
result = asdict(self)
|
||||
|
||||
for key in ("sample_received_at", "last_message_at"):
|
||||
if result[key] is not None:
|
||||
result[key] = result[key].isoformat()
|
||||
|
||||
return result
|
||||
@@ -0,0 +1,88 @@
|
||||
from app.qingping.models import QingpingState
|
||||
|
||||
|
||||
def parse_cgdn1(data: bytes, rssi: int | None = None) -> QingpingState | None:
|
||||
|
||||
# Восемь первых байт — заголовок CGDN1.
|
||||
if len(data) < 8:
|
||||
return None
|
||||
|
||||
temperature = None
|
||||
humidity = None
|
||||
pm25 = None
|
||||
pm10 = None
|
||||
co2 = None
|
||||
|
||||
pos = 8
|
||||
|
||||
while pos + 2 <= len(data):
|
||||
field_type = data[pos]
|
||||
length = data[pos + 1]
|
||||
|
||||
pos += 2
|
||||
|
||||
if pos + length > len(data):
|
||||
return None
|
||||
|
||||
value = data[pos:pos + length]
|
||||
pos += length
|
||||
|
||||
# Temperature + Humidity
|
||||
if field_type == 0x01 and length == 4:
|
||||
temperature = (
|
||||
int.from_bytes(
|
||||
value[0:2],
|
||||
byteorder="little",
|
||||
signed=True,
|
||||
)
|
||||
/ 10
|
||||
)
|
||||
|
||||
humidity = (
|
||||
int.from_bytes(
|
||||
value[2:4],
|
||||
byteorder="little",
|
||||
signed=False,
|
||||
)
|
||||
/ 10
|
||||
)
|
||||
|
||||
# PM2.5 + PM10
|
||||
elif field_type == 0x12 and length == 4:
|
||||
pm25 = int.from_bytes(
|
||||
value[0:2],
|
||||
byteorder="little",
|
||||
)
|
||||
|
||||
pm10 = int.from_bytes(
|
||||
value[2:4],
|
||||
byteorder="little",
|
||||
)
|
||||
|
||||
# CO2
|
||||
elif field_type == 0x13 and length == 2:
|
||||
co2 = int.from_bytes(
|
||||
value,
|
||||
byteorder="little",
|
||||
)
|
||||
|
||||
if any(
|
||||
value is None
|
||||
for value in (
|
||||
temperature,
|
||||
humidity,
|
||||
pm25,
|
||||
pm10,
|
||||
co2,
|
||||
)
|
||||
):
|
||||
return None
|
||||
|
||||
return QingpingState(
|
||||
temperature=temperature,
|
||||
humidity=humidity,
|
||||
co2=co2,
|
||||
pm25=pm25,
|
||||
pm10=pm10,
|
||||
rssi=rssi,
|
||||
)
|
||||
@@ -0,0 +1,610 @@
|
||||
# from datetime import datetime, timedelta, timezone
|
||||
#
|
||||
# from bleak import BleakScanner
|
||||
# from bleak.backends.device import BLEDevice
|
||||
# from bleak.backends.scanner import AdvertisementData
|
||||
#
|
||||
# from app.qingping.models import QingpingState
|
||||
# from app.qingping.parser import parse_cgdn1
|
||||
#
|
||||
#
|
||||
# QINGPING_SERVICE_UUID = "0000fdcd-0000-1000-8000-00805f9b34fb"
|
||||
#
|
||||
#
|
||||
# class QingpingService:
|
||||
# def __init__(self, mac: str, stale_after: float = 30.0):
|
||||
# self._mac = mac.upper()
|
||||
# self._stale_after = stale_after
|
||||
#
|
||||
# self._scanner: BleakScanner | None = None
|
||||
# self._state: QingpingState | None = None
|
||||
#
|
||||
# self._last_seen: datetime | None = None
|
||||
# self._last_error: str | None = None
|
||||
#
|
||||
# self._running = False
|
||||
#
|
||||
# @property
|
||||
# def state(self) -> QingpingState | None:
|
||||
# return self._state
|
||||
#
|
||||
# @property
|
||||
# def running(self) -> bool:
|
||||
# return self._running
|
||||
#
|
||||
# @property
|
||||
# def last_seen(self) -> datetime | None:
|
||||
# return self._last_seen
|
||||
#
|
||||
# @property
|
||||
# def last_error(self) -> str | None:
|
||||
# return self._last_error
|
||||
#
|
||||
# @property
|
||||
# def online(self) -> bool:
|
||||
# if not self._running:
|
||||
# return False
|
||||
#
|
||||
# if self._last_seen is None:
|
||||
# return False
|
||||
#
|
||||
# age = datetime.now(timezone.utc) - self._last_seen
|
||||
#
|
||||
# return age <= timedelta(
|
||||
# seconds=self._stale_after
|
||||
# )
|
||||
#
|
||||
# async def start(self):
|
||||
# if self._running:
|
||||
# return
|
||||
#
|
||||
# try:
|
||||
# self._scanner = BleakScanner(
|
||||
# self._on_advertisement,
|
||||
# # service_uuids=[
|
||||
# # QINGPING_SERVICE_UUID,
|
||||
# # ],
|
||||
# )
|
||||
#
|
||||
# await self._scanner.start()
|
||||
#
|
||||
# self._running = True
|
||||
# self._last_error = None
|
||||
#
|
||||
# except Exception as exc:
|
||||
# self._scanner = None
|
||||
# self._running = False
|
||||
# self._last_error = str(exc)
|
||||
#
|
||||
# raise
|
||||
#
|
||||
# async def stop(self):
|
||||
# scanner = self._scanner
|
||||
#
|
||||
# self._scanner = None
|
||||
# self._running = False
|
||||
#
|
||||
# if scanner is not None:
|
||||
# await scanner.stop()
|
||||
#
|
||||
# # def _on_advertisement(
|
||||
# # self,
|
||||
# # device: BLEDevice,
|
||||
# # advertisement: AdvertisementData,
|
||||
# # ):
|
||||
# # if device.address.upper() != self._mac:
|
||||
# # return
|
||||
# #
|
||||
# # data = advertisement.service_data.get(
|
||||
# # QINGPING_SERVICE_UUID
|
||||
# # )
|
||||
# #
|
||||
# # if not data:
|
||||
# # return
|
||||
# #
|
||||
# # try:
|
||||
# # state = parse_cgdn1(
|
||||
# # data,
|
||||
# # rssi=getattr(
|
||||
# # advertisement,
|
||||
# # "rssi",
|
||||
# # None,
|
||||
# # ),
|
||||
# # )
|
||||
# #
|
||||
# # if state is None:
|
||||
# # return
|
||||
# #
|
||||
# # self._state = state
|
||||
# #
|
||||
# # self._last_seen = datetime.now(
|
||||
# # timezone.utc
|
||||
# # )
|
||||
# #
|
||||
# # self._last_error = None
|
||||
# #
|
||||
# # except Exception as exc:
|
||||
# # self._last_error = str(exc)
|
||||
#
|
||||
# def _on_advertisement(
|
||||
# self,
|
||||
# device: BLEDevice,
|
||||
# advertisement: AdvertisementData,
|
||||
# ):
|
||||
# data = advertisement.service_data.get(
|
||||
# QINGPING_SERVICE_UUID
|
||||
# )
|
||||
#
|
||||
# if not data:
|
||||
# return
|
||||
#
|
||||
# print(
|
||||
# "QINGPING:",
|
||||
# device.address,
|
||||
# device.name,
|
||||
# data.hex(" "),
|
||||
# )
|
||||
#
|
||||
# if device.address.upper() != self._mac:
|
||||
# print(
|
||||
# "MAC mismatch:",
|
||||
# device.address,
|
||||
# "!=",
|
||||
# self._mac,
|
||||
# )
|
||||
# return
|
||||
#
|
||||
# try:
|
||||
# state = parse_cgdn1(
|
||||
# data,
|
||||
# rssi=getattr(
|
||||
# advertisement,
|
||||
# "rssi",
|
||||
# None,
|
||||
# ),
|
||||
# )
|
||||
#
|
||||
# print("PARSED:", state)
|
||||
#
|
||||
# if state is None:
|
||||
# return
|
||||
#
|
||||
# self._state = state
|
||||
# self._last_seen = datetime.now(
|
||||
# timezone.utc
|
||||
# )
|
||||
# self._last_error = None
|
||||
#
|
||||
# except Exception as exc:
|
||||
# self._last_error = str(exc)
|
||||
# print("Qingping parse error:", exc)
|
||||
#
|
||||
# async def __aenter__(self):
|
||||
# await self.start()
|
||||
# return self
|
||||
#
|
||||
# async def __aexit__(
|
||||
# self,
|
||||
# exc_type,
|
||||
# exc_val,
|
||||
# exc_tb,
|
||||
# ):
|
||||
# await self.stop()
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
from dataclasses import replace
|
||||
from datetime import datetime, timezone
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
from .models import QingpingState
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class QingpingService:
|
||||
def __init__(
|
||||
self,
|
||||
host: str,
|
||||
port: int = 1883,
|
||||
mac: str = "CCB5D131BA93",
|
||||
):
|
||||
self._host = host
|
||||
self._port = port
|
||||
self._mac = mac
|
||||
|
||||
self._up_topic = f"qingping/{mac}/up"
|
||||
self._down_topic = f"qingping/{mac}/down"
|
||||
|
||||
self._state = QingpingState()
|
||||
self._lock = threading.Lock()
|
||||
|
||||
self._client: mqtt.Client | None = None
|
||||
self._watchdog_task: asyncio.Task | None = None
|
||||
|
||||
self._last_sample_monotonic: float | None = None
|
||||
self._connected_since: float | None = None
|
||||
|
||||
self._heartbeat_seen = False
|
||||
self._recovery_sent = False
|
||||
|
||||
self._last_device_timestamp: int | None = None
|
||||
self._reboot_detected = False
|
||||
|
||||
@property
|
||||
def state(self) -> QingpingState:
|
||||
with self._lock:
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def online(self) -> bool:
|
||||
state = self.state
|
||||
|
||||
if not state.mqtt_connected:
|
||||
return False
|
||||
|
||||
if self._last_sample_monotonic is None:
|
||||
return False
|
||||
|
||||
return time.monotonic() - self._last_sample_monotonic < 60
|
||||
|
||||
async def start(self) -> None:
|
||||
client = mqtt.Client(
|
||||
callback_api_version=mqtt.CallbackAPIVersion.VERSION2,
|
||||
client_id="tioncontroller-qingping",
|
||||
)
|
||||
|
||||
client.on_connect = self._on_connect
|
||||
client.on_disconnect = self._on_disconnect
|
||||
client.on_message = self._on_message
|
||||
|
||||
client.reconnect_delay_set(
|
||||
min_delay=1,
|
||||
max_delay=30,
|
||||
)
|
||||
|
||||
self._client = client
|
||||
|
||||
client.connect_async(
|
||||
self._host,
|
||||
self._port,
|
||||
keepalive=60,
|
||||
)
|
||||
|
||||
client.loop_start()
|
||||
|
||||
self._watchdog_task = asyncio.create_task(
|
||||
self._watchdog_loop()
|
||||
)
|
||||
|
||||
async def stop(self) -> None:
|
||||
if self._watchdog_task is not None:
|
||||
self._watchdog_task.cancel()
|
||||
|
||||
try:
|
||||
await self._watchdog_task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
self._watchdog_task = None
|
||||
|
||||
if self._client is not None:
|
||||
self._client.disconnect()
|
||||
self._client.loop_stop()
|
||||
self._client = None
|
||||
|
||||
def status(self) -> dict:
|
||||
result = self.state.to_dict()
|
||||
result["online"] = self.online
|
||||
return result
|
||||
|
||||
def _on_connect(
|
||||
self,
|
||||
client,
|
||||
userdata,
|
||||
flags,
|
||||
reason_code,
|
||||
properties,
|
||||
):
|
||||
if reason_code.is_failure:
|
||||
logger.warning(
|
||||
"Qingping MQTT connection failed: %s",
|
||||
reason_code,
|
||||
)
|
||||
return
|
||||
|
||||
client.subscribe(self._up_topic)
|
||||
|
||||
with self._lock:
|
||||
self._state = replace(
|
||||
self._state,
|
||||
mqtt_connected=True,
|
||||
)
|
||||
|
||||
self._connected_since = time.monotonic()
|
||||
self._heartbeat_seen = False
|
||||
self._recovery_sent = False
|
||||
|
||||
logger.info(
|
||||
"Qingping MQTT connected"
|
||||
)
|
||||
|
||||
def _on_disconnect(
|
||||
self,
|
||||
client,
|
||||
userdata,
|
||||
disconnect_flags,
|
||||
reason_code,
|
||||
properties,
|
||||
):
|
||||
with self._lock:
|
||||
self._state = replace(
|
||||
self._state,
|
||||
mqtt_connected=False,
|
||||
)
|
||||
|
||||
logger.warning(
|
||||
"Qingping MQTT disconnected: %s",
|
||||
reason_code,
|
||||
)
|
||||
|
||||
def _on_message(
|
||||
self,
|
||||
client,
|
||||
userdata,
|
||||
message,
|
||||
):
|
||||
try:
|
||||
payload = json.loads(
|
||||
message.payload.decode("utf-8")
|
||||
)
|
||||
except (UnicodeDecodeError, json.JSONDecodeError):
|
||||
logger.warning(
|
||||
"Invalid Qingping MQTT message"
|
||||
)
|
||||
return
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
|
||||
with self._lock:
|
||||
self._state = replace(
|
||||
self._state,
|
||||
last_message_at=now,
|
||||
)
|
||||
|
||||
message_type = str(payload.get("type"))
|
||||
|
||||
if message_type == "13":
|
||||
self._handle_heartbeat(payload)
|
||||
return
|
||||
|
||||
if message_type in {"12", "17"}:
|
||||
self._handle_sensor_data(
|
||||
payload,
|
||||
now,
|
||||
)
|
||||
|
||||
def _handle_heartbeat(
|
||||
self,
|
||||
payload: dict,
|
||||
) -> None:
|
||||
self._heartbeat_seen = True
|
||||
|
||||
device_timestamp = payload.get("timestamp")
|
||||
|
||||
if isinstance(device_timestamp, int):
|
||||
previous = self._last_device_timestamp
|
||||
|
||||
if (
|
||||
device_timestamp < 300
|
||||
and (
|
||||
previous is None
|
||||
or previous > 300
|
||||
)
|
||||
):
|
||||
self._reboot_detected = True
|
||||
self._recovery_sent = False
|
||||
|
||||
logger.info(
|
||||
"Qingping reboot detected"
|
||||
)
|
||||
|
||||
self._last_device_timestamp = device_timestamp
|
||||
|
||||
wifi_rssi = None
|
||||
|
||||
wifi_info = payload.get("wifi_info")
|
||||
|
||||
if isinstance(wifi_info, str):
|
||||
parts = wifi_info.split(",")
|
||||
|
||||
if len(parts) >= 2:
|
||||
try:
|
||||
wifi_rssi = int(parts[1])
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
with self._lock:
|
||||
self._state = replace(
|
||||
self._state,
|
||||
wifi_rssi=wifi_rssi,
|
||||
firmware=payload.get("sw_version"),
|
||||
)
|
||||
|
||||
def _handle_sensor_data(
|
||||
self,
|
||||
payload: dict,
|
||||
received_at: datetime,
|
||||
) -> None:
|
||||
sensor_data = payload.get("sensorData")
|
||||
|
||||
if not isinstance(sensor_data, list):
|
||||
return
|
||||
|
||||
if not sensor_data:
|
||||
return
|
||||
|
||||
sample = max(
|
||||
sensor_data,
|
||||
key=self._sample_timestamp,
|
||||
)
|
||||
|
||||
sample_timestamp = self._sample_timestamp(
|
||||
sample
|
||||
)
|
||||
|
||||
if sample_timestamp <= 0:
|
||||
return
|
||||
|
||||
current_timestamp = (
|
||||
self.state.sample_timestamp
|
||||
)
|
||||
|
||||
# CGDN1 после запуска может несколько раз
|
||||
# присылать одну и ту же историческую точку.
|
||||
if (
|
||||
current_timestamp is not None
|
||||
and sample_timestamp <= current_timestamp
|
||||
):
|
||||
return
|
||||
|
||||
temperature = self._value(
|
||||
sample,
|
||||
"temperature",
|
||||
)
|
||||
humidity = self._value(
|
||||
sample,
|
||||
"humidity",
|
||||
)
|
||||
co2 = self._value(
|
||||
sample,
|
||||
"co2",
|
||||
)
|
||||
pm25 = self._value(
|
||||
sample,
|
||||
"pm25",
|
||||
)
|
||||
pm10 = self._value(
|
||||
sample,
|
||||
"pm10",
|
||||
)
|
||||
battery = self._value(
|
||||
sample,
|
||||
"battery",
|
||||
)
|
||||
|
||||
with self._lock:
|
||||
self._state = replace(
|
||||
self._state,
|
||||
temperature=temperature,
|
||||
humidity=humidity,
|
||||
co2=co2,
|
||||
pm25=pm25,
|
||||
pm10=pm10,
|
||||
battery=battery,
|
||||
sample_timestamp=sample_timestamp,
|
||||
sample_received_at=received_at,
|
||||
)
|
||||
|
||||
self._last_sample_monotonic = (
|
||||
time.monotonic()
|
||||
)
|
||||
|
||||
self._reboot_detected = False
|
||||
|
||||
@staticmethod
|
||||
def _sample_timestamp(
|
||||
sample: dict,
|
||||
) -> int:
|
||||
timestamp = sample.get("timestamp")
|
||||
|
||||
if isinstance(timestamp, dict):
|
||||
timestamp = timestamp.get("value")
|
||||
|
||||
try:
|
||||
return int(timestamp)
|
||||
except (TypeError, ValueError):
|
||||
return 0
|
||||
|
||||
@staticmethod
|
||||
def _value(
|
||||
sample: dict,
|
||||
key: str,
|
||||
):
|
||||
value = sample.get(key)
|
||||
|
||||
if isinstance(value, dict):
|
||||
return value.get("value")
|
||||
|
||||
return value
|
||||
|
||||
async def _watchdog_loop(self) -> None:
|
||||
while True:
|
||||
await asyncio.sleep(5)
|
||||
|
||||
if self._client is None:
|
||||
continue
|
||||
|
||||
state = self.state
|
||||
|
||||
if not state.mqtt_connected:
|
||||
continue
|
||||
|
||||
if self._recovery_sent:
|
||||
continue
|
||||
|
||||
# Явно увидели reboot CGDN1.
|
||||
if self._reboot_detected:
|
||||
self._send_recovery()
|
||||
continue
|
||||
|
||||
# Или сервис подключился, heartbeat есть,
|
||||
# но свежих sensorData так и не появилось.
|
||||
if (
|
||||
self._connected_since is not None
|
||||
and self._heartbeat_seen
|
||||
and self._last_sample_monotonic is None
|
||||
and time.monotonic()
|
||||
- self._connected_since
|
||||
> 30
|
||||
):
|
||||
self._send_recovery()
|
||||
|
||||
def _send_recovery(self) -> None:
|
||||
if self._client is None:
|
||||
return
|
||||
|
||||
payload = {
|
||||
"type": "17",
|
||||
"timestamp": int(time.time()),
|
||||
"setting": {
|
||||
"report_interval": 15,
|
||||
"collect_interval": 15,
|
||||
"need_ack": 0,
|
||||
},
|
||||
}
|
||||
|
||||
result = self._client.publish(
|
||||
self._down_topic,
|
||||
json.dumps(
|
||||
payload,
|
||||
separators=(",", ":"),
|
||||
),
|
||||
)
|
||||
|
||||
if result.rc == mqtt.MQTT_ERR_SUCCESS:
|
||||
self._recovery_sent = True
|
||||
|
||||
logger.warning(
|
||||
"Qingping recovery command sent"
|
||||
)
|
||||
else:
|
||||
logger.warning(
|
||||
"Failed to send Qingping recovery: %s",
|
||||
result.rc,
|
||||
)
|
||||
Reference in New Issue
Block a user