switch.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # -*- coding: utf-8 -*-
  2. from enum import Enum
  3. import arrow
  4. from app.api.errors.iot import MissingIOTDataError
  5. from app.schemas.equipment import BaseEquipment
  6. from app.utils.date import get_time_str, TIME_FMT
  7. class SwitchSet(str, Enum):
  8. on = "on"
  9. off = "off"
  10. hold = "hold"
  11. class Switch:
  12. def __init__(self, equipment: BaseEquipment):
  13. super(Switch, self).__init__()
  14. self._equip = equipment
  15. self._now_time = arrow.get(get_time_str(), TIME_FMT).time().strftime("%H%M%S")
  16. async def build_next_action(self, is_workday: bool) -> SwitchSet:
  17. try:
  18. if self._equip.in_cloud_status:
  19. if is_workday:
  20. if self._equip.on_time <= self._equip.off_time:
  21. if self._equip.on_time <= self._now_time <= self._equip.off_time:
  22. switch_flag = True
  23. else:
  24. switch_flag = False
  25. else:
  26. if self._equip.off_time <= self._now_time <= self._equip.on_time:
  27. switch_flag = False
  28. else:
  29. switch_flag = True
  30. if switch_flag and not self._equip.running_status:
  31. action = SwitchSet.on
  32. elif not switch_flag and self._equip.running_status:
  33. action = SwitchSet.off
  34. else:
  35. action = SwitchSet.hold
  36. else:
  37. action = SwitchSet.hold
  38. else:
  39. action = SwitchSet.off
  40. except TypeError:
  41. raise MissingIOTDataError
  42. return action