basic.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. from operator import attrgetter
  2. import numpy as np
  3. from app.api.errors.iot import MissingIOTDataError
  4. from app.controllers.equipment.ashp.basic import ASHPGroupController
  5. from app.models.domain.devices import FeedWaterPumpRequest
  6. from app.schemas.equipment import FeedWaterPump, ASHP
  7. from app.schemas.season import Season
  8. class FeedWaterPumpController:
  9. def __init__(self, pump_list: list[FeedWaterPump]):
  10. self._pump_list = pump_list
  11. def get_switch_set(self, ashp_list: list[ASHP]):
  12. ashp_on_count = 0
  13. ashp_off_count = 0
  14. for item in ashp_list:
  15. if item.equip_switch_set:
  16. ashp_on_count += 1
  17. if not item.running_status:
  18. ashp_off_count += 1
  19. ashp_num = len(ashp_list)
  20. pump_num = len(self._pump_list)
  21. if ashp_off_count == ashp_num:
  22. self.turn_off_all()
  23. else:
  24. if pump_num == 2:
  25. if ashp_on_count < ashp_num / 2:
  26. pump_need_num = 1
  27. else:
  28. pump_need_num = 2
  29. elif pump_num == 3:
  30. if ashp_on_count <= ashp_num // 3:
  31. pump_need_num = 1
  32. elif ashp_on_count > 2 * ashp_num // 3:
  33. pump_need_num = 3
  34. else:
  35. pump_need_num = 2
  36. else:
  37. pump_need_num = 1
  38. self._pump_list = sorted(self._pump_list, key=attrgetter("acc_run_time"))
  39. for device in self._pump_list:
  40. if not device.running_status:
  41. device.equip_switch_set = True
  42. pump_need_num -= 1
  43. if pump_need_num == 0:
  44. break
  45. def turn_off_all(self):
  46. for device in self._pump_list:
  47. device.equip_switch_set = False
  48. def is_init(self):
  49. on_flag = False
  50. for device in self._pump_list:
  51. if device.running_status:
  52. on_flag = True
  53. init_flag = False
  54. if not on_flag:
  55. for device in self._pump_list:
  56. if device.equip_switch_set:
  57. init_flag = True
  58. return init_flag
  59. def get_freq_set(self, ashp_list: list[ASHP], season: Season, init: float = 30.0):
  60. if self.is_init():
  61. self.set_freq(0, init)
  62. else:
  63. in_temp_avg = np.mean([device.in_temp for device in ashp_list])
  64. out_temp_avg = np.mean([device.out_temp for device in ashp_list])
  65. if season == Season.cooling:
  66. temp_diff = in_temp_avg - out_temp_avg
  67. else:
  68. temp_diff = out_temp_avg - in_temp_avg
  69. if temp_diff > 6.0:
  70. diff = 5.0
  71. elif temp_diff < 4.0:
  72. diff = -5.0
  73. else:
  74. diff = 0.0
  75. self.set_freq(diff, 0)
  76. def set_freq(self, diff: float, base: float):
  77. for device in self._pump_list:
  78. if device.equip_switch_set:
  79. if base == 0:
  80. device.work_freq_set = device.work_freq_set + diff
  81. else:
  82. device.work_freq_set = base + diff
  83. def get_result(self):
  84. return self._pump_list
  85. class FeedWaterPumpBRController(FeedWaterPumpController):
  86. def __init__(self, pump_list: list[FeedWaterPump]):
  87. super().__init__(pump_list)
  88. def get_switch_set(self, ashp_list: list[ASHP]):
  89. ashp_on_count = 0
  90. ashp_off_count = 0
  91. for item in ashp_list:
  92. if item.equip_switch_set:
  93. ashp_on_count += 1
  94. if not item.running_status:
  95. ashp_off_count += 1
  96. ashp_num = len(ashp_list)
  97. pump_num = len(self._pump_list)
  98. if ashp_off_count == ashp_num:
  99. self.turn_off_all()
  100. else:
  101. if pump_num == 2:
  102. pump_need_num = 2
  103. elif pump_num == 3:
  104. if ashp_on_count <= ashp_num // 3:
  105. pump_need_num = 2
  106. elif ashp_on_count > 2 * ashp_num // 3:
  107. pump_need_num = 3
  108. else:
  109. pump_need_num = 2
  110. else:
  111. pump_need_num = 2
  112. self._pump_list = sorted(self._pump_list, key=attrgetter("acc_run_time"))
  113. for device in self._pump_list:
  114. if not device.running_status:
  115. device.equip_switch_set = True
  116. pump_need_num -= 1
  117. if pump_need_num == 0:
  118. break
  119. async def build_feed_water_pump_instructions(params: FeedWaterPumpRequest):
  120. ashp_controller = ASHPGroupController(params.ashp_list, params.outdoor_temp, params.season)
  121. try:
  122. ashp_controller.run()
  123. except (TypeError, IndexError):
  124. raise MissingIOTDataError
  125. ashp_list, _, _ = ashp_controller.get_results()
  126. pump_controller = FeedWaterPumpController(params.pump_list)
  127. pump_controller.get_freq_set(params.ashp_list, params.season)
  128. pump_controller.get_switch_set(params.ashp_list)
  129. return pump_controller.get_result()
  130. async def build_feed_water_pump_br_instructions(params: FeedWaterPumpRequest):
  131. ashp_controller = ASHPGroupController(params.ashp_list, params.outdoor_temp, params.season)
  132. try:
  133. ashp_controller.run()
  134. except (TypeError, IndexError):
  135. raise MissingIOTDataError
  136. ashp_list, _, _ = ashp_controller.get_results()
  137. pump_controller = FeedWaterPumpController(params.pump_list)
  138. pump_controller.get_freq_set(params.ashp_list, params.season, 40.0)
  139. pump_controller.get_switch_set(params.ashp_list)
  140. return pump_controller.get_result()