From 0bb4adb10af5b719c89ca8fa50c637b620831e68 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Tue, 16 Nov 2021 02:59:27 +0300 Subject: report requests to solarmon api --- .gitignore | 3 +++ main.py | 15 +++++++++++++++ requirements.txt | 2 ++ 3 files changed, 20 insertions(+) create mode 100644 .gitignore create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cd762ce --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__ +/.idea +/venv \ No newline at end of file diff --git a/main.py b/main.py index 2d041e9..caf055f 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 import logging +import solarmon_api +from typing import Optional from argparse import ArgumentParser import socket from telegram import ( @@ -18,6 +20,9 @@ from telegram.ext import ( from telegram.error import TimedOut +solarmon: Optional[solarmon_api.Client] = None + + class RelayClient: def __init__(self, port=8307, host='127.0.0.1'): self._host = host @@ -103,6 +108,8 @@ def msg_status(update: Update, context: CallbackContext) -> None: status = 'Включен ✅' if response == 'on' else 'Выключен ❌' reply(update, status) + + solarmon.log_bot_request(solarmon_api.BotType.PUMP, update.message.chat_id, 'Статус') except Exception as e: handle_exc(update, e) @@ -114,6 +121,8 @@ def msg_on(update: Update, context: CallbackContext) -> None: relay.on() reply(update, 'Готово') + + solarmon.log_bot_request(solarmon_api.BotType.PUMP, update.message.chat_id, 'Включить') except Exception as e: handle_exc(update, e) @@ -125,6 +134,8 @@ def msg_off(update: Update, context: CallbackContext) -> None: relay.off() reply(update, 'Готово') + + solarmon.log_bot_request(solarmon_api.BotType.PUMP, update.message.chat_id, 'Выключить') except Exception as e: handle_exc(update, e) @@ -142,6 +153,7 @@ if __name__ == '__main__': help='ID of users allowed to use the bot') parser.add_argument('--gpio-relayd-host', default='127.0.0.1', type=str) parser.add_argument('--gpio-relayd-port', default=8307, type=int) + parser.add_argument('--solarmon-api-token', type=str, required=True) args = parser.parse_args() whitelist = list(map(lambda x: int(x), args.users_whitelist)) @@ -164,6 +176,9 @@ if __name__ == '__main__': dispatcher.add_handler(MessageHandler(Filters.all & user_filter, msg_all)) + # create api client instance + solarmon = solarmon_api.Client(args.solarmon_api_token, timeout=3) + solarmon.enable_async() # start the bot updater.start_polling() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..91e02d0 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +python-telegram-bot~=13.1 +git+ssh://git-hidden@ch1p.io/home/git-hidden/solarmon_api.git \ No newline at end of file -- cgit v1.2.3