transfer.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # -*- coding: utf-8 -*-
  2. from enum import Enum
  3. from typing import Dict, List
  4. import arrow
  5. import numpy as np
  6. import pandas as pd
  7. from httpx import AsyncClient, URL
  8. from app.core.config import TransferSettings
  9. from app.services.service import Service
  10. from app.utils.date import get_time_str, TIME_FMT
  11. transfer_settings = TransferSettings()
  12. class Season(str, Enum):
  13. cooling = 'Cooling'
  14. heating = 'Warm'
  15. transition = 'Transition'
  16. class SpaceInfoService(Service):
  17. def __init__(
  18. self,
  19. client: AsyncClient,
  20. project_id: str,
  21. space_id: str,
  22. settings: TransferSettings = transfer_settings
  23. ) -> None:
  24. super(SpaceInfoService, self).__init__(client)
  25. self._project_id = project_id
  26. self._space_id = space_id
  27. self._base_url = URL(settings.transfer_host)
  28. self._now_time = get_time_str()
  29. def _common_parameters(self) -> Dict:
  30. return {'projectId': self._project_id, 'spaceId': self._space_id}
  31. async def get_season(self) -> Season:
  32. url = self._base_url.join('duoduo-service/transfer/environment/getSeasonType')
  33. params = {
  34. 'projectId': self._project_id,
  35. 'date': self._now_time,
  36. }
  37. raw_info = await self._get(url, params)
  38. return Season(raw_info.get('data'))
  39. async def is_customized(self) -> bool:
  40. url = self._base_url.join('duoduo-service/custom-service/custom/timetarget')
  41. time_str = arrow.get(arrow.get(self._now_time, TIME_FMT).shift(minutes=15).timestamp
  42. // 900 * 900).strftime('%Y%m%d%H%M%S')
  43. params = {
  44. 'projectId': self._project_id,
  45. 'objectId': self._space_id,
  46. 'timepoint': time_str,
  47. }
  48. raw_info = await self._get(url, params)
  49. flag = False
  50. if raw_info.get('data'):
  51. flag = True
  52. return flag
  53. async def is_temporary(self) -> bool:
  54. url = self._base_url.join('duoduo-service/transfer/environment/temp/target')
  55. params = self._common_parameters()
  56. params.update({'time': self._now_time})
  57. raw_info = await self._get(url, params)
  58. flag = False
  59. if raw_info.get('flag') == 1:
  60. flag = True
  61. return flag
  62. async def get_feedback(self, wechat_time: str) -> Dict:
  63. url = self._base_url.join('duoduo-service/transfer/environment/feedbackCount')
  64. params = self._common_parameters()
  65. params.update({'time': wechat_time})
  66. raw_info = await self._get(url, params)
  67. meaning_dict = {
  68. 'Id1': 'a little cold',
  69. 'Id2': 'so cold',
  70. 'Id3': 'a little hot',
  71. 'Id4': 'so hot',
  72. 'Id5': 'noisy or blowy',
  73. 'Id6': 'so stuffy',
  74. 'Id7': 'more sunshine',
  75. 'Id8': 'less sunshine',
  76. 'Id9': 'send a repairman',
  77. 'Id10': 'switch off',
  78. 'Id11': 'nice',
  79. 'Id12': 'switch on',
  80. }
  81. feedback_dic = {meaning_dict.get(k): v for k, v in raw_info.items() if k != 'result'}
  82. return feedback_dic
  83. async def get_custom_target(self) -> pd.DataFrame:
  84. url = self._base_url.join('duoduo-service/transfer/environment/target')
  85. params = self._common_parameters()
  86. params.update({'date': arrow.get(self._now_time, TIME_FMT).date().strftime('%Y%m%d')})
  87. raw_info = await self._get(url, params)
  88. custom_target_df = pd.DataFrame(raw_info.get('data'))
  89. custom_target_df.set_index('time', inplace=True)
  90. return custom_target_df
  91. async def get_current_temperature_target(self) -> float:
  92. current_targets = await self.get_custom_target()
  93. temp = arrow.get(self._now_time, TIME_FMT).shift(minutes=15).timestamp // (15 * 60) * (15 * 60)
  94. next_quarter_minutes = arrow.get(temp).time().strftime('%H%M%S')
  95. current_lower_target = current_targets['temperatureMin'].loc[next_quarter_minutes]
  96. current_upper_target = current_targets['temperatureMax'].loc[next_quarter_minutes]
  97. return (current_lower_target + current_upper_target) / 2
  98. async def env_database_set(self, form: str, value: float) -> None:
  99. url = self._base_url.join('duoduo-service/transfer/environment/hispoint/set')
  100. params = self._common_parameters()
  101. time_str = arrow.get(arrow.get(self._now_time, TIME_FMT).timestamp // 900 * 900).strftime('%Y%m%d%H%M%S')
  102. params.update({'time': time_str, 'type': form, 'value': value})
  103. await self._get(url, params)
  104. async def env_database_get(self) -> Dict[str, pd.DataFrame]:
  105. url = self._base_url.join('duoduo-service/transfer/environment/hispoint/get')
  106. params = self._common_parameters()
  107. params.update({'date': arrow.get(self._now_time, TIME_FMT).date().strftime('%Y%m%d')})
  108. raw_info = await self._get(url, params)
  109. result = {}
  110. if raw_info.get('result') == 'success':
  111. for k, v in raw_info.items():
  112. if k != 'result':
  113. if len(v) > 0:
  114. temp = {}
  115. data = np.array(v)
  116. temp.update({'timestamp': data[:, 0]})
  117. temp.update({'value': data[:, 1].astype(np.float)})
  118. result.update({k: pd.DataFrame(temp)})
  119. else:
  120. result.update({k: pd.DataFrame()})
  121. return result
  122. async def set_custom_target(self, form: str, target_value: Dict[str, List[float]], flag: str = '1') -> None:
  123. url = self._base_url.join('duoduo-service/transfer/environment/target/setting')
  124. params = {
  125. 'projectId': self._project_id,
  126. 'spaceId': self._space_id,
  127. 'timepoint': self._now_time,
  128. 'type': form,
  129. 'flag': flag
  130. }
  131. await self._post(url, params=params, payload=target_value)
  132. async def set_temporary_custom(self) -> None:
  133. url = self._base_url.join('duoduo-service/transfer/environment/setServiceFlag')
  134. params = self._common_parameters()
  135. params.update({'time': self._now_time})
  136. await self._get(url, params)
  137. async def get_equipment(self) -> List[dict]:
  138. url = self._base_url.join('duoduo-service/object-service/object/equipment/findForServe')
  139. params = self._common_parameters()
  140. raw_info = await self._post(url, params)
  141. result = []
  142. for eq in raw_info.get('data'):
  143. result.append({'id': eq.get('id'), 'category': eq.get('equipmentCategory')})
  144. return result
  145. class EquipmentInfoService(Service):
  146. def __init__(self, client: AsyncClient, project_id: str, settings: TransferSettings = transfer_settings):
  147. super(EquipmentInfoService, self).__init__(client)
  148. self._project_id = project_id
  149. self._base_url = URL(settings.transfer_host)
  150. self._now_time = get_time_str()
  151. async def get_space_by_equipment(self, equipment_id: str) -> List[dict]:
  152. url = self._base_url.join('duoduo-service/object-service/object/space/findForServe')
  153. params = {
  154. 'projectId': self._project_id,
  155. 'objectId': equipment_id
  156. }
  157. raw_info = await self._post(url, params)
  158. result = []
  159. for sp in raw_info.get('data'):
  160. result.append({'id': sp.get('id')})
  161. return result