diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2022-11-06 20:09:49 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2022-11-06 20:09:49 +0300 |
commit | 2b70f151eed4853c62019efd8119c0d917abf8d2 (patch) | |
tree | 1c095aecbc258348ba6f084ed130cfb5c7e4661b /src | |
parent | ae2bfdcd68f312c47f9699917ccb13302f401ba7 (diff) |
api: inverter methods
Diffstat (limited to 'src')
-rwxr-xr-x | src/electricity_calc.py | 1 | ||||
-rw-r--r-- | src/home/api/web_api_client.py | 15 | ||||
-rw-r--r-- | src/home/database/inverter.py | 2 | ||||
-rw-r--r-- | src/home/database/inverter_time_formats.py | 2 | ||||
-rwxr-xr-x | src/web_api.py | 40 |
5 files changed, 56 insertions, 4 deletions
diff --git a/src/electricity_calc.py b/src/electricity_calc.py index 8ad2957..c3cb233 100755 --- a/src/electricity_calc.py +++ b/src/electricity_calc.py @@ -18,7 +18,6 @@ _is_verbose = False fmt_time = '%Y-%m-%d %H:%M:%S' fmt_date = '%Y-%m-%d' -tz = zoneinfo.ZoneInfo('Europe/Moscow') def method_usage() -> str: diff --git a/src/home/api/web_api_client.py b/src/home/api/web_api_client.py index 4a9f4bc..2884ddb 100644 --- a/src/home/api/web_api_client.py +++ b/src/home/api/web_api_client.py @@ -106,7 +106,20 @@ class WebAPIClient: return MediaNodeClient.record_list_from_serialized(files) return files - def _process_sound_sensor_hits_data(self, data: List[dict]) -> List[dict]: + def inverter_get_consumed_energy(self, s_from: str, s_to: str): + return self._post('inverter/get_consumed_energy/', { + 'from': s_from, + 'to': s_to + }) + + def inverter_get_grid_consumed_energy(self, s_from: str, s_to: str): + return self._post('inverter/get_grid_consumed_energy/', { + 'from': s_from, + 'to': s_to + }) + + @staticmethod + def _process_sound_sensor_hits_data(data: List[dict]) -> List[dict]: for item in data: item['time'] = datetime.fromtimestamp(item['time']) return data diff --git a/src/home/database/inverter.py b/src/home/database/inverter.py index 1e967c4..fc3f74f 100644 --- a/src/home/database/inverter.py +++ b/src/home/database/inverter.py @@ -1,5 +1,5 @@ from time import time -from datetime import datetime +from datetime import datetime, timedelta from typing import Optional from collections import namedtuple diff --git a/src/home/database/inverter_time_formats.py b/src/home/database/inverter_time_formats.py new file mode 100644 index 0000000..7c37d30 --- /dev/null +++ b/src/home/database/inverter_time_formats.py @@ -0,0 +1,2 @@ +FormatTime = '%Y-%m-%d %H:%M:%S' +FormatDate = '%Y-%m-%d' diff --git a/src/web_api.py b/src/web_api.py index 93d2539..fc6eb49 100755 --- a/src/web_api.py +++ b/src/web_api.py @@ -9,11 +9,22 @@ from aiohttp import web from home import http from home.util import parse_addr from home.config import config, is_development_mode -from home.database import BotsDatabase, SensorsDatabase +from home.database import BotsDatabase, SensorsDatabase, InverterDatabase +from home.database.inverter_time_formats import * from home.api.types import BotType, TemperatureSensorLocation, SoundSensorLocation from home.media import SoundRecordStorage +def strptime_auto(s: str) -> datetime: + e = None + for fmt in (FormatTime, FormatDate): + try: + return datetime.strptime(s, fmt) + except ValueError as _e: + e = _e + raise e + + class AuthError(Exception): def __init__(self, message: str): super().__init__() @@ -34,6 +45,9 @@ class WebAPIServer(http.HTTPServer): self.post('/log/bot-request/', self.POST_bot_request_log) self.post('/log/openwrt/', self.POST_openwrt_log) + self.get('/inverter/consumed_energy/', self.GET_consumed_energy) + self.get('/inverter/grid_consumed_energy/', self.GET_grid_consumed_energy) + self.get('/recordings/list/', self.GET_recordings_list) @staticmethod @@ -184,6 +198,30 @@ class WebAPIServer(http.HTTPServer): return self.ok(files) + @staticmethod + def _get_inverter_from_to(req: http.Request): + s_from = req.query['from'] + s_to = req.query['to'] + + dt_from = strptime_auto(s_from) + + if s_to == 'now': + dt_to = datetime.now() + else: + dt_to = strptime_auto(s_to) + + return dt_from, dt_to + + async def GET_consumed_energy(self, req: http.Request): + dt_from, dt_to = self._get_inverter_from_to(req) + wh = InverterDatabase().get_consumed_energy(dt_from, dt_to) + return self.ok(wh) + + async def GET_grid_consumed_energy(self, req: http.Request): + dt_from, dt_to = self._get_inverter_from_to(req) + wh = InverterDatabase().get_consumed_energy(dt_from, dt_to) + return self.ok(wh) + # start of the program # -------------------- |