# -*- coding: utf-8 -*-

from fastapi_utils.tasks import repeat_every
from httpx import AsyncClient

from app.controllers.equipment.ahu.basic import get_freq_controlled
from app.controllers.equipment.ahu.switch import ahu_switch_control
from app.controllers.equipment.fcu.on_ratio import start_control_group_mode
from app.controllers.equipment.pau.switch import pau_switch_control
from app.controllers.equipment.ventilation_fan.switch import ventilation_fan_switch_control
from app.services.platform import DataPlatformService


@repeat_every(seconds=60 * 5)
async def regulate_ahu_freq():
    _PROJECT_ID = 'Pj1101050030'
    _AHU_LIST = [
        'Eq1101050030b6b2f1db3d6944afa71e213e0d45d565',
        'Eq1101050030846e0a94670842109f7c8d8db0d44cf5'
    ]

    for ahu in _AHU_LIST:
        await get_freq_controlled(_PROJECT_ID, ahu)


@repeat_every(seconds=60 * 15)
async def regulate_ahu_switch():
    _PROJECT_ID = 'Pj1101020002'
    async with AsyncClient() as client:
        platform = DataPlatformService(client, _PROJECT_ID)
        ahu_list = await platform.get_items_by_category('ATAH')

    for ahu in ahu_list:
        await ahu_switch_control(_PROJECT_ID, ahu)


@repeat_every(seconds=60 * 15)
async def regulate_pau_switch():
    _PROJECT_ID = 'Pj1101020002'
    async with AsyncClient() as client:
        platform = DataPlatformService(client, _PROJECT_ID)
        pau_list = await platform.get_items_by_category('ATFU')

    for pau in pau_list:
        await pau_switch_control(_PROJECT_ID, pau)


@repeat_every(seconds=60 * 15)
async def regulate_ventilation_fan_switch():
    _PROJECT_ID = 'Pj1101020002'
    async with AsyncClient() as client:
        platform = DataPlatformService(client, _PROJECT_ID)
        eq_list = await platform.get_items_by_category('VTSF')

    for eq in eq_list:
        await ventilation_fan_switch_control(_PROJECT_ID, eq)


@repeat_every(seconds=60 * 15)
async def run_control_group():
    await start_control_group_mode('Eq11010802593142c55b64c5492c92560d280885a2f7', 30.2)
    await start_control_group_mode('Eq11010802598449efe230f444cca826e840dbf67f41', 30.1)