platform.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. import os
  3. from enum import Enum
  4. import arrow
  5. import numpy as np
  6. from httpx import AsyncClient, URL
  7. from loguru import logger
  8. from app.core.config import settings
  9. from app.services.service import Service
  10. from app.utils.date import get_time_str, TIME_FMT
  11. from app.utils.math import round_half_up
  12. class InfoCode(str, Enum):
  13. temperature = "Tdb"
  14. co2 = "CO2"
  15. pm2d5 = "PM2d5"
  16. humidity = "RH"
  17. supply_air_flow = "SupplyAirFlow"
  18. supply_air_flow_set = "SupplyAirFlowSet"
  19. supply_air_temperature = "SupplyAirTemp"
  20. supply_air_temperature_set = "SupplyAirTempSet"
  21. fan_speed = "FanGear"
  22. fan_speed_set = "FanGearSet"
  23. fan_freq = "FanFreq"
  24. fan_freq_set = "FanFreqSet"
  25. supply_static_press = "SupplyStaticPress"
  26. supply_static_press_set = "SupplyStaticPressSet"
  27. running_status = "RunStatus"
  28. cloud_status = "InCloudStatus"
  29. equip_switch_set = "EquipSwitchSet"
  30. return_air_temperature = "ReturnAirTemp"
  31. chill_water_valve_opening_set = "ChillWaterValveOpeningSet"
  32. hot_water_valve_opening_set = "HotWaterValveOpeningSet"
  33. water_valve_switch_set = "WaterValveSwitchSet"
  34. in_cloud_set = "InCloudSet"
  35. work_mode_set = "WorkModeSet"
  36. supply_temperature = "SupplyTemp"
  37. water_out_temperature = "WaterOutTemp"
  38. water_in_temperature = "WaterInTemp"
  39. valve_opening = "ValveOpening"
  40. class DataPlatformService(Service):
  41. def __init__(self, client: AsyncClient, project_id: str, server_settings=settings):
  42. super(DataPlatformService, self).__init__(client)
  43. self._project_id = project_id
  44. self._base_url = os.getenv("PLATFORM_HOST", server_settings.PLATFORM_HOST)
  45. self._now_time = get_time_str()
  46. self._secret = server_settings.PLATFORM_SECRET
  47. def _common_parameters(self) -> dict:
  48. return {"projectId": self._project_id, "secret": self._secret}
  49. async def get_realtime_data(self, code: InfoCode, object_id: str) -> float:
  50. url = URL(f"{self._base_url}/hisdata/query_by_obj")
  51. params = self._common_parameters()
  52. start_time = get_time_str(60 * 60, flag="ago")
  53. payload = {
  54. "criteria": {
  55. "id": object_id,
  56. "code": code.value,
  57. "receivetime": {
  58. "$gte": start_time,
  59. "$lte": self._now_time,
  60. },
  61. }
  62. }
  63. raw_info = await self._post(url, params, payload)
  64. try:
  65. latest_data = raw_info.get("Content")[-1].get("data")
  66. latest_time = raw_info.get("Content")[-1].get("receivetime")
  67. if arrow.get(latest_time, TIME_FMT).shift(minutes=15) < arrow.get(
  68. self._now_time, TIME_FMT
  69. ):
  70. logger.info(
  71. f"delayed data - {object_id}: ({latest_time}, {latest_data})"
  72. )
  73. value = round_half_up(latest_data, 2)
  74. except (IndexError, KeyError, TypeError):
  75. value = np.NAN
  76. return value
  77. async def get_realtime_temperature(self, space_id: str) -> float:
  78. return await self.get_realtime_data(InfoCode.temperature, space_id)