supply_air_temperature_set.py 3.0 KB

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