fcu.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # -*- coding: utf-8 -*-
  2. import numpy as np
  3. from httpx import AsyncClient
  4. from loguru import logger
  5. from app.controllers.equipment.controller import EquipmentController
  6. from app.models.domain.equipment import AirValveSpeed
  7. from app.models.domain.equipment import FCU
  8. from app.models.domain.equipment import Space
  9. from app.services.platform import DataPlatformService
  10. from app.services.transfer import SpaceInfoService, EquipmentInfoService
  11. class FCUController(EquipmentController):
  12. def __init__(self, equipment: FCU):
  13. super(FCUController, self).__init__()
  14. self._equipment = equipment
  15. async def get_temperature_target(self) -> float:
  16. target = self._equipment.space.temperature_target
  17. if not np.isnan(target) and target > 0:
  18. setting_target = target
  19. else:
  20. setting_target = 25.0
  21. self._equipment.setting_temperature = setting_target
  22. return setting_target
  23. async def get_air_valve_speed(self):
  24. temperature_target = await self.get_temperature_target()
  25. if temperature_target and temperature_target > 0:
  26. self._equipment.air_valve_speed = AirValveSpeed.auto
  27. else:
  28. self._equipment.air_valve_speed = AirValveSpeed.off
  29. async def run(self):
  30. await self.get_temperature_target()
  31. await self.get_air_valve_speed()
  32. self._equipment.running_status = True
  33. def get_results(self):
  34. return self._equipment
  35. @logger.catch()
  36. async def get_fcu_control_result(project_id: str, equipment_id: str) -> FCU:
  37. async with AsyncClient() as client:
  38. duo_duo = EquipmentInfoService(client, project_id)
  39. platform = DataPlatformService(client, project_id)
  40. spaces = await duo_duo.get_space_by_equipment(equipment_id)
  41. for sp in spaces:
  42. transfer = SpaceInfoService(client, project_id, sp.get('id'))
  43. current_target = await transfer.get_current_temperature_target()
  44. realtime_temperature = await platform.get_realtime_temperature(sp.get('id'))
  45. temp_space_params = {
  46. 'id': sp.get('id'),
  47. 'temperature_target': current_target,
  48. 'realtime_temperature': realtime_temperature
  49. }
  50. space = Space(**temp_space_params)
  51. break
  52. fcu_temp_params = {
  53. 'id': equipment_id,
  54. 'space': space
  55. }
  56. fcu = FCU(**fcu_temp_params)
  57. fcu_controller = FCUController(fcu)
  58. await fcu_controller.run()
  59. regulated_fcu = fcu_controller.get_results()
  60. return regulated_fcu