555 lines
13 KiB
Python
555 lines
13 KiB
Python
import asyncio
|
|
import os
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
from app.auto.config import load_auto_config
|
|
from app.auto.controller import AutoController
|
|
from app.auto.elica_assist import ElicaAutoAssistState
|
|
|
|
from app.elica import (
|
|
ElicaConfig,
|
|
ElicaController,
|
|
ElicaService,
|
|
)
|
|
|
|
from app.my_dataclasses import (
|
|
ELICA_MQTT_HOST,
|
|
ELICA_MQTT_PORT,
|
|
)
|
|
|
|
|
|
AUTO_CONFIG = "config/auto.yaml"
|
|
|
|
ELICA_READY_TIMEOUT = 15.0
|
|
MANUAL_CHANGE_TIMEOUT = 60.0
|
|
|
|
|
|
class TestElicaService(ElicaService):
|
|
"""
|
|
Отдельный MQTT client_id для live-теста.
|
|
"""
|
|
|
|
async def start(self) -> None:
|
|
if self._running:
|
|
return
|
|
|
|
client_id = (
|
|
f"climatcontroller-auto-elica-manual-test-"
|
|
f"{os.getpid()}"
|
|
)
|
|
|
|
print(
|
|
"Test MQTT client_id:",
|
|
client_id,
|
|
)
|
|
|
|
client = mqtt.Client(
|
|
callback_api_version=(
|
|
mqtt.CallbackAPIVersion.VERSION2
|
|
),
|
|
client_id=client_id,
|
|
)
|
|
|
|
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
|
|
self._running = True
|
|
|
|
client.connect_async(
|
|
self._config.mqtt_host,
|
|
self._config.mqtt_port,
|
|
keepalive=60,
|
|
)
|
|
|
|
client.loop_start()
|
|
|
|
|
|
async def wait_for_elica(
|
|
service: ElicaService,
|
|
) -> None:
|
|
loop = asyncio.get_running_loop()
|
|
|
|
deadline = (
|
|
loop.time()
|
|
+ ELICA_READY_TIMEOUT
|
|
)
|
|
|
|
while True:
|
|
state = service.state
|
|
|
|
if (
|
|
state.mqtt_connected
|
|
and state.device_online
|
|
and service.feedback_ready
|
|
):
|
|
return
|
|
|
|
if loop.time() >= deadline:
|
|
raise RuntimeError(
|
|
"Elica did not become ready.\n"
|
|
f"mqtt_connected="
|
|
f"{state.mqtt_connected}\n"
|
|
f"device_online="
|
|
f"{state.device_online}\n"
|
|
f"feedback_ready="
|
|
f"{service.feedback_ready}\n"
|
|
f"missing_feedback_inputs="
|
|
f"{service.missing_feedback_inputs}"
|
|
)
|
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
|
|
async def wait_for_manual_change(
|
|
service: ElicaService,
|
|
*,
|
|
power: bool,
|
|
speed: int,
|
|
timer: bool,
|
|
):
|
|
"""
|
|
Ждём реального изменения Elica пользователем.
|
|
|
|
Потеря MQTT/feedback изменением
|
|
не считается.
|
|
"""
|
|
|
|
loop = asyncio.get_running_loop()
|
|
|
|
deadline = (
|
|
loop.time()
|
|
+ MANUAL_CHANGE_TIMEOUT
|
|
)
|
|
|
|
while True:
|
|
state = service.state
|
|
|
|
ready = (
|
|
state.mqtt_connected
|
|
and state.device_online
|
|
and service.feedback_ready
|
|
)
|
|
|
|
if ready:
|
|
logical = service.logical_state
|
|
|
|
if (
|
|
logical.power != power
|
|
or logical.speed != speed
|
|
or logical.timer != timer
|
|
):
|
|
return logical
|
|
|
|
if loop.time() >= deadline:
|
|
raise RuntimeError(
|
|
"No manual Elica change detected "
|
|
f"within {MANUAL_CHANGE_TIMEOUT} seconds"
|
|
)
|
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
|
|
async def main() -> None:
|
|
service = TestElicaService(
|
|
ElicaConfig(
|
|
mqtt_host=ELICA_MQTT_HOST,
|
|
mqtt_port=ELICA_MQTT_PORT,
|
|
)
|
|
)
|
|
|
|
elica_controller = ElicaController(
|
|
service
|
|
)
|
|
|
|
print(
|
|
"Starting Elica service..."
|
|
)
|
|
|
|
await service.start()
|
|
|
|
try:
|
|
print(
|
|
f"Connecting to MQTT "
|
|
f"{ELICA_MQTT_HOST}:"
|
|
f"{ELICA_MQTT_PORT}..."
|
|
)
|
|
|
|
await wait_for_elica(
|
|
service
|
|
)
|
|
|
|
print(
|
|
"Elica connected and "
|
|
"feedback ready"
|
|
)
|
|
|
|
initial = elica_controller.state
|
|
|
|
print()
|
|
print(
|
|
"Initial Elica:",
|
|
{
|
|
"power": initial.power,
|
|
"speed": initial.speed,
|
|
"timer": initial.timer,
|
|
},
|
|
)
|
|
|
|
if initial.power:
|
|
raise RuntimeError(
|
|
"Elica must be OFF before this test. "
|
|
"Turn it off manually and run again."
|
|
)
|
|
|
|
# --------------------------------------------------
|
|
# Создаём настоящий Auto Assist.
|
|
# --------------------------------------------------
|
|
|
|
config = load_auto_config(
|
|
AUTO_CONFIG
|
|
)
|
|
|
|
assist = ElicaAutoAssistState(
|
|
config.elica_assist
|
|
)
|
|
|
|
# --------------------------------------------------
|
|
# Настоящий AutoController.
|
|
#
|
|
# Основной цикл мы не запускаем.
|
|
# Вызываем только _update_elica_assist().
|
|
# --------------------------------------------------
|
|
|
|
auto_controller = AutoController(
|
|
schedule_service=object(),
|
|
qingping_service=object(),
|
|
tion_service=object(),
|
|
elica_controller=elica_controller,
|
|
policy=None,
|
|
temperature_policy=None,
|
|
elica_assist=assist,
|
|
interval=1.0,
|
|
)
|
|
|
|
# ==================================================
|
|
# ЭТАП 1
|
|
#
|
|
# Tion 4
|
|
# -> AutoController включает Elica 1
|
|
# -> OWNED
|
|
# ==================================================
|
|
|
|
print()
|
|
print(
|
|
"STEP 1:"
|
|
)
|
|
|
|
print(
|
|
"Tion AUTO speed 4 "
|
|
"-> Auto Assist should acquire Elica"
|
|
)
|
|
|
|
await auto_controller._update_elica_assist(
|
|
4
|
|
)
|
|
|
|
owned = elica_controller.state
|
|
|
|
print()
|
|
print(
|
|
"Elica:",
|
|
{
|
|
"power": owned.power,
|
|
"speed": owned.speed,
|
|
"timer": owned.timer,
|
|
},
|
|
)
|
|
|
|
print(
|
|
"Assist:",
|
|
assist.status(),
|
|
)
|
|
|
|
if (
|
|
not owned.power
|
|
or owned.speed != 1
|
|
):
|
|
raise RuntimeError(
|
|
"AutoController did not acquire "
|
|
"Elica at speed 1"
|
|
)
|
|
|
|
if assist.mode.value != "owned":
|
|
raise RuntimeError(
|
|
"Assist did not enter OWNED"
|
|
)
|
|
|
|
print()
|
|
print(
|
|
"ACQUIRE PASSED"
|
|
)
|
|
|
|
# ==================================================
|
|
# ЭТАП 2
|
|
#
|
|
# Теперь пользователь физически меняет Elica.
|
|
#
|
|
# Например:
|
|
#
|
|
# speed 1 -> 2
|
|
#
|
|
# AutoController пока ничего не делает.
|
|
# Просто ждём реальный MQTT feedback.
|
|
# ==================================================
|
|
|
|
print()
|
|
print(
|
|
"STEP 2:"
|
|
)
|
|
|
|
print(
|
|
"Now MANUALLY change Elica."
|
|
)
|
|
|
|
print(
|
|
"For example change speed 1 -> 2."
|
|
)
|
|
|
|
print(
|
|
"The test is waiting..."
|
|
)
|
|
|
|
changed = await wait_for_manual_change(
|
|
service,
|
|
power=owned.power,
|
|
speed=owned.speed,
|
|
timer=owned.timer,
|
|
)
|
|
|
|
print()
|
|
print(
|
|
"Manual Elica state:",
|
|
{
|
|
"power": changed.power,
|
|
"speed": changed.speed,
|
|
"timer": changed.timer,
|
|
},
|
|
)
|
|
|
|
# ==================================================
|
|
# ЭТАП 3
|
|
#
|
|
# Tion всё ещё 4.
|
|
#
|
|
# Следующий проход AutoController должен увидеть:
|
|
#
|
|
# expected_speed = 1
|
|
# actual speed = 2
|
|
#
|
|
# -> manual_intervention
|
|
# -> SUPPRESSED
|
|
#
|
|
# И НИКАКИХ команд Elica.
|
|
# ==================================================
|
|
|
|
print()
|
|
print(
|
|
"STEP 3:"
|
|
)
|
|
|
|
print(
|
|
"Tion is still speed 4."
|
|
)
|
|
|
|
print(
|
|
"AutoController should detect "
|
|
"manual intervention..."
|
|
)
|
|
|
|
await auto_controller._update_elica_assist(
|
|
4
|
|
)
|
|
|
|
after_takeover = elica_controller.state
|
|
|
|
print()
|
|
print(
|
|
"Elica after detection:",
|
|
{
|
|
"power": after_takeover.power,
|
|
"speed": after_takeover.speed,
|
|
"timer": after_takeover.timer,
|
|
},
|
|
)
|
|
|
|
print(
|
|
"Assist:",
|
|
assist.status(),
|
|
)
|
|
|
|
if assist.mode.value != "suppressed":
|
|
raise RuntimeError(
|
|
"Assist did not enter SUPPRESSED"
|
|
)
|
|
|
|
if (
|
|
assist.status()["reason"]
|
|
!= "manual_intervention"
|
|
):
|
|
raise RuntimeError(
|
|
"Expected reason manual_intervention"
|
|
)
|
|
|
|
# Elica должна остаться ровно такой,
|
|
# какой её сделал пользователь.
|
|
if (
|
|
after_takeover.power
|
|
!= changed.power
|
|
or after_takeover.speed
|
|
!= changed.speed
|
|
or after_takeover.timer
|
|
!= changed.timer
|
|
):
|
|
raise RuntimeError(
|
|
"AutoController changed Elica "
|
|
"after manual intervention"
|
|
)
|
|
|
|
print()
|
|
print(
|
|
"MANUAL TAKEOVER PASSED"
|
|
)
|
|
|
|
# ==================================================
|
|
# ЭТАП 4
|
|
#
|
|
# Теперь Tion падает ниже порога:
|
|
#
|
|
# 4 -> 3
|
|
#
|
|
# Текущий автоматический цикл заканчивается.
|
|
#
|
|
# Но Elica уже пользовательская,
|
|
# поэтому выключать её ЗАПРЕЩЕНО.
|
|
#
|
|
# SUPPRESSED -> IDLE
|
|
# Elica остаётся включена.
|
|
# ==================================================
|
|
|
|
print()
|
|
print(
|
|
"STEP 4:"
|
|
)
|
|
|
|
print(
|
|
"Tion AUTO speed drops to 3."
|
|
)
|
|
|
|
print(
|
|
"Elica must NOT be switched off."
|
|
)
|
|
|
|
await auto_controller._update_elica_assist(
|
|
3
|
|
)
|
|
|
|
final_elica = elica_controller.state
|
|
|
|
print()
|
|
print(
|
|
"Final Elica:",
|
|
{
|
|
"power": final_elica.power,
|
|
"speed": final_elica.speed,
|
|
"timer": final_elica.timer,
|
|
},
|
|
)
|
|
|
|
print(
|
|
"Final Assist:",
|
|
assist.status(),
|
|
)
|
|
|
|
# --------------------------------------------------
|
|
# Проверяем, что пользовательское состояние
|
|
# вообще не изменилось.
|
|
# --------------------------------------------------
|
|
|
|
if (
|
|
final_elica.power
|
|
!= changed.power
|
|
or final_elica.speed
|
|
!= changed.speed
|
|
or final_elica.timer
|
|
!= changed.timer
|
|
):
|
|
raise RuntimeError(
|
|
"AutoController modified user's "
|
|
"Elica when Tion dropped to speed 3"
|
|
)
|
|
|
|
# Новый цикл должен быть готов начаться
|
|
# в будущем.
|
|
if assist.mode.value != "idle":
|
|
raise RuntimeError(
|
|
"Assist did not return to IDLE "
|
|
"after the automatic cycle ended"
|
|
)
|
|
|
|
print()
|
|
print(
|
|
"RELEASE AFTER TAKEOVER PASSED"
|
|
)
|
|
|
|
print()
|
|
print(
|
|
"TEST PASSED"
|
|
)
|
|
|
|
print()
|
|
print(
|
|
"Verified sequence:"
|
|
)
|
|
|
|
print(
|
|
"Tion 4"
|
|
" -> Auto Elica 1"
|
|
" -> OWNED"
|
|
)
|
|
|
|
print(
|
|
"User changes Elica"
|
|
" -> SUPPRESSED"
|
|
)
|
|
|
|
print(
|
|
"Tion 3"
|
|
" -> Elica untouched"
|
|
" -> IDLE"
|
|
)
|
|
|
|
print()
|
|
print(
|
|
"Turn Elica off manually "
|
|
"after the test."
|
|
)
|
|
|
|
finally:
|
|
print()
|
|
print(
|
|
"Stopping Elica service..."
|
|
)
|
|
|
|
await service.stop()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main()) |