switch.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # -*- coding: utf-8 -*-
  2. from typing import List
  3. import numpy as np
  4. from loguru import logger
  5. from app.controllers.equipment.switch import Switch, SwitchSet
  6. from app.models.domain.devices import ACATFUSwitchSetRequest, ACATFUCO2SwitchSetRequest
  7. from app.schemas.equipment import PAU
  8. class PAUSwitch(Switch):
  9. def __init__(self, equipment: PAU):
  10. super(PAUSwitch, self).__init__(equipment)
  11. def break_time_action(self, begin: str, end: str, is_workday: bool) -> SwitchSet:
  12. if self._equip.in_cloud_status:
  13. if is_workday:
  14. if begin and end:
  15. switch_flag = True
  16. if begin <= end:
  17. if begin <= self._now_time <= end:
  18. switch_flag = False
  19. else:
  20. if not end <= self._now_time <= begin:
  21. switch_flag = False
  22. if not switch_flag:
  23. action = SwitchSet.off
  24. else:
  25. action = SwitchSet.hold
  26. else:
  27. action = SwitchSet.hold
  28. else:
  29. action = SwitchSet.hold
  30. else:
  31. action = SwitchSet.hold
  32. return action
  33. class PAUSwitchWithCO2(PAUSwitch):
  34. def __int__(self, equipment: PAU):
  35. super(PAUSwitchWithCO2, self).__int__(equipment)
  36. @staticmethod
  37. def co2_logic(co2_list: List[float]) -> SwitchSet:
  38. co2_list = np.array(co2_list)
  39. action = SwitchSet.hold
  40. if co2_list.size > 0:
  41. if co2_list.mean() >= 800.0 or co2_list.max() > 1000.0:
  42. action = SwitchSet.on
  43. if co2_list.mean() <= 650.0 and co2_list.max() < 1000.0:
  44. action = SwitchSet.off
  45. return action
  46. async def run(self, is_workday: bool, break_start_time: str, break_end_time: str,
  47. co2_list: List[float]) -> SwitchSet:
  48. action = await self.build_next_action(is_workday)
  49. break_action = self.break_time_action(break_start_time, break_end_time, is_workday)
  50. if break_action == SwitchSet.off:
  51. action = SwitchSet.off
  52. co2_action = self.co2_logic(co2_list)
  53. if co2_action == SwitchSet.off:
  54. action = SwitchSet.off
  55. elif co2_action == SwitchSet.on:
  56. if action == SwitchSet.off:
  57. action = SwitchSet.off
  58. if action == SwitchSet.hold and not self._equip:
  59. action = SwitchSet.hold
  60. return action
  61. @logger.catch()
  62. async def build_acatfu_switch_set(params: ACATFUSwitchSetRequest) -> SwitchSet:
  63. pau = PAU(
  64. running_status=params.running_status,
  65. in_cloud_status=params.in_cloud_status,
  66. on_time=params.on_time,
  67. off_time=params.off_time,
  68. )
  69. pau_switch_controller = PAUSwitch(pau)
  70. action = await pau_switch_controller.build_next_action(params.is_workday)
  71. break_action = pau_switch_controller.break_time_action(
  72. params.break_start_time, params.break_end_time, params.is_workday
  73. )
  74. if break_action == "off":
  75. action = SwitchSet.off
  76. return action
  77. @logger.catch
  78. async def build_co2_acatfu_switch(params: ACATFUCO2SwitchSetRequest) -> SwitchSet:
  79. pau = PAU(**params.dict())
  80. controller = PAUSwitchWithCO2(pau)
  81. action = await controller.run(params.is_workday, params.break_start_time, params.break_end_time, params.co2_list)
  82. return action