switch.py 3.4 KB

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