supply_air_temperature_set.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from typing import Tuple
  2. from httpx import AsyncClient
  3. from loguru import logger
  4. from app.services.platform import DataPlatformService, InfoCode
  5. from app.services.transfer import Duoduo
  6. class ACATFUSupplyAirTemperatureController:
  7. """
  8. Supply air temperature setting logic version 1 by Wenyan.
  9. """
  10. def __init__(self, current_supply_air_temp: float, hot_rate: float, cold_rate: float):
  11. self.current_air_temp = current_supply_air_temp
  12. self.hot_rate = hot_rate
  13. self.cold_rate = cold_rate
  14. def get_next_set(self, is_just_booted: bool) -> float:
  15. try:
  16. cold_hot_ratio = self.cold_rate / self.hot_rate
  17. except ZeroDivisionError:
  18. cold_hot_ratio = 99
  19. if is_just_booted:
  20. if cold_hot_ratio < 0.5:
  21. next_temperature_set = 8.0
  22. elif cold_hot_ratio < 0.9:
  23. next_temperature_set = 11.0
  24. elif cold_hot_ratio < 1.1:
  25. next_temperature_set = 14.0
  26. elif cold_hot_ratio < 1.5:
  27. next_temperature_set = 17.0
  28. elif cold_hot_ratio >= 1.5:
  29. next_temperature_set = 20.0
  30. else: # If cold hot ratio is nan.
  31. next_temperature_set = 20.0
  32. else:
  33. if cold_hot_ratio < 0.5:
  34. diff = -3
  35. elif cold_hot_ratio < 0.9:
  36. diff = -2
  37. elif cold_hot_ratio < 1.1:
  38. diff = 0
  39. elif cold_hot_ratio < 1.5:
  40. diff = 2
  41. elif cold_hot_ratio >= 1.5:
  42. diff = 3
  43. else: # If cold hot ratio is nan.
  44. diff = 0
  45. next_temperature_set = self.current_air_temp + diff
  46. next_temperature_set = max(8.0, min(20.0, next_temperature_set))
  47. return next_temperature_set
  48. async def fetch_params(project_id: str, device_id: str) -> Tuple[float, float, float, bool]:
  49. async with AsyncClient() as client:
  50. platform = DataPlatformService(client, project_id)
  51. duoduo = Duoduo(client, project_id)
  52. current_supply_air_temperature = await platform.get_realtime_supply_air_temperature(device_id)
  53. running_status_duration = await platform.get_duration(InfoCode.running_status, device_id, 15 * 60)
  54. hot_rate, cold_rate = await duoduo.query_fill_rate_by_device(device_id)
  55. is_just_booted = False
  56. if running_status_duration[-1]['value'] == 1.0:
  57. for item in running_status_duration[::-1]:
  58. if item['value'] == 0.0:
  59. is_just_booted = True
  60. break
  61. return current_supply_air_temperature, hot_rate, cold_rate, is_just_booted
  62. @logger.catch()
  63. async def get_next_acatfu_supply_air_temperature_set(project_id: str, device_id: str) -> float:
  64. current_supply_air_temperature, hot_rate, cold_rate, is_just_booted = await fetch_params(project_id, device_id)
  65. controller = ACATFUSupplyAirTemperatureController(
  66. current_supply_air_temperature,
  67. hot_rate,
  68. cold_rate
  69. )
  70. next_temperature_set = controller.get_next_set(is_just_booted)
  71. logger.debug(next_temperature_set)
  72. return next_temperature_set