Browse Source

add a function for querying past data from platform

chenhaiyang 4 years ago
parent
commit
6b23fe20e5

BIN
app/controllers/equipment/fcu/net_1_summer.mat


BIN
app/controllers/equipment/fcu/net_1_winter.mat


+ 38 - 2
app/services/platform.py

@@ -58,8 +58,6 @@ class DataPlatformService(Service):
         }
         raw_info = await self._post(url, params, payload)
 
-        # value = np.NAN
-        # if raw_info.get('Content'):
         try:
             latest_data = raw_info.get('Content')[-1].get('data')
             latest_time = raw_info.get('Content')[-1].get('receivetime')
@@ -71,9 +69,47 @@ class DataPlatformService(Service):
 
         return value
 
+    async def get_past_data(self, code: InfoCode, object_id: str, interval: int) -> float:
+        """
+        Query past data from data platform.
+        :param code: Info code
+        :param object_id:
+        :param interval: time interval(seconds) from now to past
+        :return: a past value
+        """
+        url = self._base_url.join('data-platform-3/hisdata/query_by_obj')
+        params = self._common_parameters()
+        start_time = get_time_str(60 * 60 + interval, flag='ago')
+        end_time = get_time_str(interval, flag='ago')
+        payload = {
+            'criteria': {
+                'id': object_id,
+                'code': code.value,
+                'receivetime': {
+                    '$gte': start_time,
+                    '$lte': end_time,
+                }
+            }
+        }
+        raw_info = await self._post(url, params, payload)
+
+        try:
+            latest_data = raw_info.get('Content')[-1].get('data')
+            latest_time = raw_info.get('Content')[-1].get('receivetime')
+            if arrow.get(latest_time, TIME_FMT).shift(minutes=15) < arrow.get(end_time, TIME_FMT):
+                logger.info(f'delayed data - {object_id}: ({latest_time}, {latest_data})')
+            value = round_half_up(latest_data, 2)
+        except KeyError:
+            value = np.NAN
+
+        return value
+
     async def get_realtime_temperature(self, space_id: str) -> float:
         return await self.get_realtime_data(InfoCode.temperature, space_id)
 
+    async def get_past_temperature(self, space_id: str, interval: int) -> float:
+        return await self.get_past_data(InfoCode.temperature, space_id, interval)
+
     async def get_realtime_co2(self, space_id: str) -> float:
         return await self.get_realtime_data(InfoCode.co2, space_id)