|
@@ -1,5 +1,4 @@
|
|
|
import asyncio
|
|
|
-import time
|
|
|
from typing import Tuple
|
|
|
|
|
|
import arrow
|
|
@@ -68,6 +67,8 @@ class OnRatioController:
|
|
|
else:
|
|
|
try:
|
|
|
ratio = (0.5 * (self.return_air - self.target) + delta_off) / (delta_off - delta_on)
|
|
|
+ if ratio > 1:
|
|
|
+ ratio = 1.0
|
|
|
except ZeroDivisionError:
|
|
|
ratio = 0.0
|
|
|
|
|
@@ -119,62 +120,65 @@ async def fetch_params(device_id: str, period_time: int, last_ratio: float) -> T
|
|
|
|
|
|
|
|
|
@logger.catch()
|
|
|
-async def start_on_ratio_mode(device_id):
|
|
|
- _TARGET = 26.0
|
|
|
+async def start_on_ratio_mode(device_id: str, target: float, period_time: int):
|
|
|
_DAILY_ON_TIME = '060000'
|
|
|
_DAILY_OFF_TIME = '220000'
|
|
|
|
|
|
- period_time = 10 * 60
|
|
|
period_num = 0
|
|
|
last_on_ratio = 1.0
|
|
|
life_count = 0
|
|
|
- while life_count < 10:
|
|
|
- time_str = get_time_str()
|
|
|
- if _DAILY_ON_TIME <= arrow.get(time_str, TIME_FMT).time().strftime('%H%M%S') < _DAILY_OFF_TIME:
|
|
|
- return_air, delta_all, delta_on, delta_off = await fetch_params(device_id, period_time, last_on_ratio)
|
|
|
- controller = OnRatioController(_TARGET, return_air, period_num)
|
|
|
- mode = controller.select_mode()
|
|
|
- speed = controller.select_speed(delta_all)
|
|
|
- switch = False if speed == 'off' else True
|
|
|
- water_valve = controller.select_water_valve()
|
|
|
-
|
|
|
- if mode == 'on_ratio':
|
|
|
- on_ratio = controller.calculate_on_ratio(delta_on, delta_off)
|
|
|
- on_range = round_half_up(period_time * on_ratio)
|
|
|
- off_range = period_time - on_range
|
|
|
-
|
|
|
- await send_instructions(device_id, switch, speed, water_valve)
|
|
|
- await asyncio.sleep(on_range)
|
|
|
- await send_instructions(device_id, switch, speed, False)
|
|
|
- await asyncio.sleep(off_range)
|
|
|
-
|
|
|
- period_num += 1
|
|
|
- last_on_ratio = on_ratio
|
|
|
+ while life_count < 2000:
|
|
|
+ try:
|
|
|
+ time_str = get_time_str()
|
|
|
+ if _DAILY_ON_TIME <= arrow.get(time_str, TIME_FMT).time().strftime('%H%M%S') < _DAILY_OFF_TIME:
|
|
|
+ return_air, delta_all, delta_on, delta_off = await fetch_params(device_id, period_time, last_on_ratio)
|
|
|
+ controller = OnRatioController(target, return_air, period_num)
|
|
|
+ mode = controller.select_mode()
|
|
|
+ speed = controller.select_speed(delta_all)
|
|
|
+ switch = False if speed == 'off' else True
|
|
|
+ water_valve = controller.select_water_valve()
|
|
|
+
|
|
|
+ if mode == 'on_ratio':
|
|
|
+ on_ratio = controller.calculate_on_ratio(delta_on, delta_off)
|
|
|
+ on_range = round_half_up(period_time * on_ratio)
|
|
|
+ off_range = period_time - on_range
|
|
|
+ logger.debug(f'on time: {on_range}, off time: {off_range}, on ratio: {on_ratio}')
|
|
|
+
|
|
|
+ await send_instructions(device_id, switch, speed, water_valve)
|
|
|
+ await asyncio.sleep(on_range)
|
|
|
+ await send_instructions(device_id, switch, speed, False)
|
|
|
+ await asyncio.sleep(off_range)
|
|
|
+
|
|
|
+ period_num += 1
|
|
|
+ last_on_ratio = on_ratio
|
|
|
+ else:
|
|
|
+ await send_instructions(device_id, switch, speed, water_valve)
|
|
|
+ await asyncio.sleep(period_time)
|
|
|
+
|
|
|
+ period_num = 0
|
|
|
+ last_on_ratio = 1.0
|
|
|
+
|
|
|
+ life_count += 1
|
|
|
+
|
|
|
+ logger.info(f'{life_count}: {device_id} - {mode}')
|
|
|
else:
|
|
|
- await send_instructions(device_id, switch, speed, water_valve)
|
|
|
- time.sleep(period_time)
|
|
|
-
|
|
|
+ await send_instructions(device_id, False, 'off', False)
|
|
|
period_num = 0
|
|
|
- last_on_ratio = 1.0
|
|
|
-
|
|
|
- life_count += 1
|
|
|
-
|
|
|
- logger.info(f'{life_count}: {device_id} - {mode}')
|
|
|
- else:
|
|
|
- await send_instructions(device_id, False, 'off', False)
|
|
|
- period_num = 0
|
|
|
- last_on_ratio = 0.0
|
|
|
+ last_on_ratio = 0.0
|
|
|
+ await asyncio.sleep(period_time)
|
|
|
+ except (KeyError, IndexError, TypeError):
|
|
|
+ continue
|
|
|
|
|
|
|
|
|
@logger.catch()
|
|
|
-async def start_control_group_mode(device_id: str):
|
|
|
+async def start_control_group_mode(device_id: str, target: float):
|
|
|
async with AsyncClient() as client:
|
|
|
platform = DataPlatformService(client, 'Pj1101080259')
|
|
|
|
|
|
return_air = await platform.get_realtime_data(InfoCode.return_air_temperature, device_id)
|
|
|
space_params = {
|
|
|
'id': device_id,
|
|
|
- 'temperature_target': 26.0,
|
|
|
+ 'temperature_target': target,
|
|
|
'realtime_temperature': return_air
|
|
|
}
|
|
|
space = Space(**space_params)
|