12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- # -*- coding: utf-8 -*-
- from enum import Enum
- import arrow
- from app.api.errors.iot import MissingIOTDataError
- from app.schemas.equipment import BaseEquipment
- from app.utils.date import get_time_str, TIME_FMT
- class SwitchSet(str, Enum):
- on = "on"
- off = "off"
- hold = "hold"
- class Switch:
- def __init__(self, equipment: BaseEquipment):
- super(Switch, self).__init__()
- self._equip = equipment
- self._now_time = arrow.get(get_time_str(), TIME_FMT).time().strftime("%H%M%S")
- async def build_next_action(self, is_workday: bool) -> SwitchSet:
- try:
- if self._equip.in_cloud_status:
- if is_workday:
- if self._equip.on_time <= self._equip.off_time:
- if self._equip.on_time <= self._now_time <= self._equip.off_time:
- switch_flag = True
- else:
- switch_flag = False
- else:
- if self._equip.off_time <= self._now_time <= self._equip.on_time:
- switch_flag = False
- else:
- switch_flag = True
- if switch_flag and not self._equip.running_status:
- action = SwitchSet.on
- elif not switch_flag and self._equip.running_status:
- action = SwitchSet.off
- else:
- action = SwitchSet.hold
- else:
- action = SwitchSet.hold
- else:
- action = SwitchSet.off
- except TypeError:
- raise MissingIOTDataError
- return action
|