From f29e139cbb7e4a4d539cba6e894ef4a6acd312d6 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Wed, 31 May 2023 09:22:00 +0300 Subject: WIP: big refactoring --- src/sensors_bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/sensors_bot.py') diff --git a/src/sensors_bot.py b/src/sensors_bot.py index dc081b0..152dd24 100755 --- a/src/sensors_bot.py +++ b/src/sensors_bot.py @@ -23,7 +23,7 @@ from home.api.types import ( TemperatureSensorLocation ) -config.load('sensors_bot') +config.load_app('sensors_bot') bot.initialize() bot.lang.ru( -- cgit v1.2.3 From 3790c2205396cf860738f297e6ddc49cd2b2a03f Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sat, 10 Jun 2023 22:29:24 +0300 Subject: new config: port openwrt_logger and webapiclient --- src/sensors_bot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/sensors_bot.py') diff --git a/src/sensors_bot.py b/src/sensors_bot.py index 152dd24..441c212 100755 --- a/src/sensors_bot.py +++ b/src/sensors_bot.py @@ -17,7 +17,7 @@ from telegram import ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardBu from home.config import config from home.telegram import bot from home.util import chunks, MySimpleSocketClient -from home.api import WebAPIClient +from home.api import WebApiClient from home.api.types import ( BotType, TemperatureSensorLocation @@ -111,7 +111,7 @@ def callback_handler(ctx: bot.Context) -> None: sensor = TemperatureSensorLocation[match.group(1).upper()] hours = int(match.group(2)) - api = WebAPIClient(timeout=20) + api = WebApiClient(timeout=20) data = api.get_sensors_data(sensor, hours) title = ctx.lang(sensor.name.lower()) + ' (' + ctx.lang('n_hrs', hours) + ')' -- cgit v1.2.3 From b0bf43e6a272d42a55158e657bd937cb82fc3d8d Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sat, 10 Jun 2023 23:02:34 +0300 Subject: move files, rename home package to homekit --- src/sensors_bot.py | 181 ----------------------------------------------------- 1 file changed, 181 deletions(-) delete mode 100755 src/sensors_bot.py (limited to 'src/sensors_bot.py') diff --git a/src/sensors_bot.py b/src/sensors_bot.py deleted file mode 100755 index 441c212..0000000 --- a/src/sensors_bot.py +++ /dev/null @@ -1,181 +0,0 @@ -#!/usr/bin/env python3 -import json -import socket -import logging -import re -import gc - -from io import BytesIO -from typing import Optional - -import matplotlib.pyplot as plt -import matplotlib.dates as mdates -import matplotlib.ticker as mticker - -from telegram import ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardButton - -from home.config import config -from home.telegram import bot -from home.util import chunks, MySimpleSocketClient -from home.api import WebApiClient -from home.api.types import ( - BotType, - TemperatureSensorLocation -) - -config.load_app('sensors_bot') -bot.initialize() - -bot.lang.ru( - start_message="Выберите датчик на клавиатуре", - unknown_command="Неизвестная команда", - temperature="Температура", - humidity="Влажность", - plot_3h="График за 3 часа", - plot_6h="График за 6 часов", - plot_12h="График за 12 часов", - plot_24h="График за 24 часа", - unexpected_callback_data="Ошибка: неверные данные", - loading="Загрузка...", - n_hrs="график за %d ч." -) -bot.lang.en( - start_message="Select the sensor on the keyboard", - unknown_command="Unknown command", - temperature="Temperature", - humidity="Relative humidity", - plot_3h="Graph for 3 hours", - plot_6h="Graph for 6 hours", - plot_12h="Graph for 12 hours", - plot_24h="Graph for 24 hours", - unexpected_callback_data="Unexpected callback data", - loading="Loading...", - n_hrs="graph for %d hours" -) - -plt.rcParams['font.size'] = 7 -logger = logging.getLogger(__name__) -plot_hours = [3, 6, 12, 24] - - -_sensor_names = [] -for k, v in config['sensors'].items(): - _sensor_names.append(k) - bot.lang.set({k: v['label_ru']}, 'ru') - bot.lang.set({k: v['label_en']}, 'en') - - -@bot.handler(messages=_sensor_names, argument='message_key') -def read_sensor(sensor: str, ctx: bot.Context) -> None: - host = config['sensors'][sensor]['ip'] - port = config['sensors'][sensor]['port'] - - try: - client = MySimpleSocketClient(host, port) - client.write('read') - data = json.loads(client.read()) - except (socket.timeout, socket.error) as error: - return ctx.reply_exc(error) - - temp = round(data['temp'], 2) - humidity = round(data['humidity'], 2) - - text = ctx.lang('temperature') + f': {temp} °C\n' - text += ctx.lang('humidity') + f': {humidity}%' - - buttons = list(map( - lambda h: InlineKeyboardButton(ctx.lang(f'plot_{h}h'), callback_data=f'plot/{sensor}/{h}'), - plot_hours - )) - ctx.reply(text, markup=InlineKeyboardMarkup(chunks(buttons, 2))) - - -@bot.callbackhandler(callback='*') -def callback_handler(ctx: bot.Context) -> None: - query = ctx.callback_query - - sensors_variants = '|'.join(config['sensors'].keys()) - hour_variants = '|'.join(list(map( - lambda n: str(n), - plot_hours - ))) - - match = re.match(rf'plot/({sensors_variants})/({hour_variants})', query.data) - if not match: - query.answer(ctx.lang('unexpected_callback_data')) - return - - query.answer(ctx.lang('loading')) - - # retrieve data - sensor = TemperatureSensorLocation[match.group(1).upper()] - hours = int(match.group(2)) - - api = WebApiClient(timeout=20) - data = api.get_sensors_data(sensor, hours) - - title = ctx.lang(sensor.name.lower()) + ' (' + ctx.lang('n_hrs', hours) + ')' - plot = draw_plot(data, title, - ctx.lang('temperature'), - ctx.lang('humidity')) - bot.send_photo(ctx.user_id, photo=plot) - - gc.collect() - - -def draw_plot(data, - title: str, - label_temp: str, - label_hum: str) -> BytesIO: - tempval = [] - humval = [] - dates = [] - for date, temp, humidity in data: - dates.append(date) - tempval.append(temp) - humval.append(humidity) - - fig, axs = plt.subplots(2, 1) - df = mdates.DateFormatter('%H:%M') - - axs[0].set_title(label_temp) - axs[0].plot(dates, tempval) - axs[0].xaxis.set_major_formatter(df) - axs[0].yaxis.set_major_formatter(mticker.FormatStrFormatter('%2.2f °C')) - - fig.suptitle(title, fontsize=10) - - axs[1].set_title(label_hum) - axs[1].plot(dates, humval) - axs[1].xaxis.set_major_formatter(df) - axs[1].yaxis.set_major_formatter(mticker.FormatStrFormatter('%2.1f %%')) - - fig.autofmt_xdate() - - # should be called after all axes have been added - fig.tight_layout() - - buf = BytesIO() - fig.savefig(buf, format='png', dpi=160) - buf.seek(0) - - plt.clf() - plt.close('all') - - return buf - - -@bot.defaultreplymarkup -def markup(ctx: Optional[bot.Context]) -> Optional[ReplyKeyboardMarkup]: - buttons = [] - for k in config['sensors'].keys(): - buttons.append(ctx.lang(k)) - buttons = chunks(buttons, 2) - return ReplyKeyboardMarkup(buttons, one_time_keyboard=False) - - -if __name__ == '__main__': - if 'api' in config: - bot.enable_logging(BotType.SENSORS) - - bot.run() -- cgit v1.2.3