supply_air_temperature_set.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from typing import List
  2. import arrow
  3. from httpx import AsyncClient
  4. from loguru import logger
  5. from app.services.platform import DataPlatformService, InfoCode
  6. from app.services.transfer import Duoduo
  7. class ACATFUSupplyAirTemperatureController:
  8. """
  9. Supply air temperature setting logic version 1 by Wenyan.
  10. """
  11. def __init__(self, current_supply_air_temp: float, cold_space_count: float, hot_space_count: float):
  12. self.current_air_temp = current_supply_air_temp
  13. self.cold_space_count = cold_space_count
  14. self.hot_space_count = hot_space_count
  15. def get_next_set(self, is_just_booted: bool) -> float:
  16. try:
  17. cold_hot_ratio = self.cold_space_count / self.hot_space_count
  18. except ZeroDivisionError:
  19. cold_hot_ratio = 99
  20. if is_just_booted:
  21. if cold_hot_ratio < 0.5:
  22. next_temperature_set = 8.0
  23. elif cold_hot_ratio < 0.9:
  24. next_temperature_set = 11.0
  25. elif cold_hot_ratio < 1.1:
  26. next_temperature_set = 14.0
  27. elif cold_hot_ratio < 1.5:
  28. next_temperature_set = 17.0
  29. elif cold_hot_ratio >= 1.5:
  30. next_temperature_set = 20.0
  31. else: # If cold hot ratio is nan.
  32. next_temperature_set = 20.0
  33. else:
  34. if cold_hot_ratio < 0.5:
  35. diff = -3
  36. elif cold_hot_ratio < 0.9:
  37. diff = -2
  38. elif cold_hot_ratio < 1.1:
  39. diff = 0
  40. elif cold_hot_ratio < 1.5:
  41. diff = 2
  42. elif cold_hot_ratio >= 1.5:
  43. diff = 3
  44. else: # If cold hot ratio is nan.
  45. diff = 0
  46. next_temperature_set = self.current_air_temp + diff
  47. return next_temperature_set
  48. async def fetch_params(project_id: str, device_id: str):
  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. count = await duoduo.get_fill_count()
  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, count.get('coldNum'), count.get('hotNum'), 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, cold_count, hot_count, is_just_booted = await fetch_params(project_id, device_id)
  65. controller = ACATFUSupplyAirTemperatureController(
  66. current_supply_air_temperature,
  67. cold_count,
  68. hot_count
  69. )
  70. next_temperature_set = controller.get_next_set(is_just_booted)
  71. return next_temperature_set