switch.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # -*- coding: utf-8 -*-
  2. from typing import Tuple
  3. from httpx import AsyncClient
  4. from loguru import logger
  5. from app.controllers.equipment.switch import Switch, fetch_data, send_switch_command
  6. from app.schemas.equipment import AHU
  7. from app.services.platform import DataPlatformService
  8. class AHUSwitch(Switch):
  9. def __init__(self, equipment: AHU):
  10. super(AHUSwitch, self).__init__(equipment)
  11. def break_time_action(self, begin: str, end: str, day_type: str) -> str:
  12. if self._equip.in_cloud_status:
  13. if day_type == 'WeekDay':
  14. switch_flag = True
  15. if begin <= end:
  16. if begin <= self._now_time <= end:
  17. switch_flag = False
  18. else:
  19. if not end <= self._now_time <= begin:
  20. switch_flag = False
  21. if not switch_flag and self._equip.running_status:
  22. action = 'off'
  23. else:
  24. action = 'hold'
  25. else:
  26. action = 'hold'
  27. else:
  28. action = 'hold'
  29. return action
  30. async def fetch_break_time(project_id: str, device_id: str) -> Tuple[str, str]:
  31. async with AsyncClient() as client:
  32. platform = DataPlatformService(client, project_id)
  33. begin = await platform.get_static_info('ctm-BeginBreakTime', device_id)
  34. end = await platform.get_static_info('ctm-EndBreakTime', device_id)
  35. return begin, end
  36. @logger.catch()
  37. async def ahu_switch_control(project_id: str, equipment_id: str) -> None:
  38. equip_params, day_type = await fetch_data(project_id, equipment_id)
  39. begin, end = await fetch_break_time(project_id, equipment_id)
  40. switch_controller = AHUSwitch(AHU(**equip_params))
  41. action = await switch_controller.build_next_action(day_type.get('day_type'))
  42. break_action = switch_controller.break_time_action(begin, end, day_type.get('day_type'))
  43. if break_action == 'off':
  44. action = 'off'
  45. logger.debug(f'AHU-{equipment_id}: {action}')
  46. await send_switch_command(project_id, equipment_id, action)