thermal_mode.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from typing import Dict, List
  2. from httpx import AsyncClient
  3. from loguru import logger
  4. from app.models.domain.devices import ThermalMode
  5. from app.schemas.equipment import VAVBox
  6. from app.services.platform import DataPlatformService, InfoCode
  7. from app.services.transfer import Duoduo, Season
  8. def count_vav_box_weight(
  9. realtime: float,
  10. target: float,
  11. upper_limit_flow: float,
  12. lower_limit_flow: float,
  13. current_flow_set: float,
  14. valve_opening: float,
  15. season: Season
  16. ) -> float:
  17. diff = realtime - target
  18. flag = False
  19. if current_flow_set < lower_limit_flow * 1.1:
  20. if season == Season.cooling:
  21. if diff < 0:
  22. flag = True
  23. if season == Season.heating:
  24. if diff > 0:
  25. flag = True
  26. elif current_flow_set > upper_limit_flow * 0.9:
  27. if valve_opening > 90.0:
  28. if season == Season.cooling:
  29. if diff > 0:
  30. flag = True
  31. if season == Season.heating:
  32. if diff < 0:
  33. flag = True
  34. if flag:
  35. if abs(diff) < 1:
  36. weight = 0.0
  37. else:
  38. weight = diff
  39. weight = max(-4.0, min(4.0, weight))
  40. else:
  41. weight = 0
  42. return weight
  43. class ACATAHThermalModeController:
  44. """
  45. Decide whether to use cooling or heating mode according to space condition controlled by VAV Box.
  46. Writen by WuXu
  47. """
  48. def __init__(self, vav_boxes_list: List[VAVBox], season: Season):
  49. super(ACATAHThermalModeController, self).__init__()
  50. self.vav_boxes_list = vav_boxes_list
  51. self.season = season
  52. def build(self) -> str:
  53. weight = 0.0
  54. for box in self.vav_boxes_list:
  55. weight += count_vav_box_weight(
  56. box.virtual_realtime_temperature,
  57. box.virtual_target_temperature,
  58. box.supply_air_flow_upper_limit,
  59. box.supply_air_flow_lower_limit,
  60. box.supply_air_flow_set,
  61. box.valve_opening,
  62. self.season
  63. )
  64. if weight > 0:
  65. mode = 'cooling'
  66. elif weight < 0:
  67. mode = 'heating'
  68. else:
  69. mode = 'hold'
  70. return mode
  71. async def fetch_status_params(project_id: str, device_id: str) -> Dict:
  72. async with AsyncClient() as client:
  73. platform = DataPlatformService(client, project_id)
  74. duoduo = Duoduo(client, project_id)
  75. season = duoduo.get_season()
  76. relations = await platform.query_relations(from_id=device_id, graph_id='GtControlEquipNetwork001')
  77. vav_id_list = [item.get('to_id') for item in relations]
  78. vav_boxes_list = []
  79. for vav_id in vav_id_list:
  80. virtual_realtime_temperature = await duoduo.query_device_virtual_data(
  81. vav_id,
  82. 'VirtualRealtimeTemperature'
  83. )
  84. virtual_temperature_target = await duoduo.query_device_virtual_data(vav_id, 'TargetTemperatureSet')
  85. lower_limit_flow, upper_limit_flow = await platform.get_air_flow_limit(vav_id)
  86. current_flow_set = await platform.get_realtime_data(InfoCode.supply_air_flow_set, vav_id)
  87. valve_opening = await platform.get_realtime_data(InfoCode.valve_opening, vav_id)
  88. vav_params = {
  89. 'id': vav_id,
  90. 'virtual_realtime_temperature': virtual_realtime_temperature,
  91. 'virtual_target_temperature': virtual_temperature_target,
  92. 'supply_air_flow_lower_limit': lower_limit_flow,
  93. 'supply_air_flow_upper_limit': upper_limit_flow,
  94. 'supply_air_flow_set': current_flow_set,
  95. 'valve_opening': valve_opening
  96. }
  97. vav = VAVBox(**vav_params)
  98. vav_boxes_list.append(vav)
  99. return {'vav_boxes_list': vav_boxes_list, 'season': season}
  100. @logger.catch()
  101. async def get_thermal_mode(project_id: str, device_id: str) -> ThermalMode:
  102. prams = await fetch_status_params(project_id, device_id)
  103. controller = ACATAHThermalModeController(prams.get('vav_boxes_list'), prams.get('season'))
  104. mode = controller.build()
  105. return ThermalMode(mode)