platform.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # -*- coding: utf-8 -*-
  2. from enum import Enum
  3. from typing import Dict
  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 PlatformSettings
  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. platform_settings = PlatformSettings()
  13. class InfoCode(str, Enum):
  14. temperature = 'Tdb'
  15. co2 = 'CO2'
  16. hcho = 'HCHO'
  17. pm2d5 = 'PM2d5'
  18. humidity = 'RH'
  19. supply_air_flow = 'SupplyAirFlow'
  20. supply_air_temperature = 'SupplyAirTemp'
  21. fan_speed = 'FanGear'
  22. class DataPlatformService(Service):
  23. def __init__(
  24. self,
  25. client: AsyncClient,
  26. project_id: str,
  27. settings: PlatformSettings = platform_settings
  28. ):
  29. super(DataPlatformService, self).__init__(client)
  30. self._project_id = project_id
  31. self._base_url = URL(settings.platform_host)
  32. self._now_time = get_time_str()
  33. self._secret = settings.platform_secret
  34. def _common_parameters(self) -> Dict:
  35. return {'projectId': self._project_id, 'secret': self._secret}
  36. async def get_realtime_data(self, code: InfoCode, object_id: str) -> float:
  37. url = self._base_url.join('data-platform-3/hisdata/query_by_obj')
  38. params = self._common_parameters()
  39. start_time = get_time_str(60 * 60, flag='ago')
  40. payload = {
  41. 'criteria': {
  42. 'id': object_id,
  43. 'code': code.value,
  44. 'receivetime': {
  45. '$gte': start_time,
  46. '$lte': self._now_time,
  47. }
  48. }
  49. }
  50. raw_info = await self._post(url, params, payload)
  51. value = np.NAN
  52. if raw_info.get('Content'):
  53. latest_data = raw_info.get('Content')[-1].get('data')
  54. latest_time = raw_info.get('Content')[-1].get('receivetime')
  55. if arrow.get(latest_time, TIME_FMT).shift(minutes=15) < arrow.get(self._now_time, TIME_FMT):
  56. logger.info(f'delayed data - {self._space_id}: ({latest_time}, {latest_data})')
  57. value = round_half_up(latest_data, 2)
  58. return value
  59. async def get_realtime_temperature(self, space_id: str) -> float:
  60. return await self.get_realtime_data(InfoCode.temperature, space_id)
  61. async def get_realtime_co2(self, space_id: str) -> float:
  62. return await self.get_realtime_data(InfoCode.co2, space_id)
  63. async def get_realtime_hcho(self, space_id: str) -> float:
  64. return await self.get_realtime_data(InfoCode.hcho, space_id)
  65. async def get_realtime_pm2d5(self, space_id: str) -> float:
  66. return await self.get_realtime_data(InfoCode.pm2d5, space_id)
  67. async def get_realtime_humidity(self, space_id: str) -> float:
  68. return await self.get_realtime_data(InfoCode.humidity, space_id)
  69. async def get_realtime_supply_air_flow(self, equipment_id: str) -> float:
  70. return await self.get_realtime_data(InfoCode.supply_air_flow, equipment_id)
  71. async def get_realtime_supply_air_temperature(self, equipment_id: str) -> float:
  72. return await self.get_realtime_data(InfoCode.supply_air_temperature, equipment_id)
  73. async def get_fan_speed(self, equipment_id: str) -> float:
  74. return await self.get_realtime_data(InfoCode.fan_speed, equipment_id)