report requests to solarmon api

This commit is contained in:
Evgeny Zinoviev 2021-11-16 02:59:27 +03:00
parent aced1ab769
commit 0bb4adb10a
3 changed files with 20 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
__pycache__
/.idea
/venv

15
main.py
View File

@ -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()

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
python-telegram-bot~=13.1
git+ssh://git-hidden@ch1p.io/home/git-hidden/solarmon_api.git