summaryrefslogtreecommitdiff
path: root/src/sensors_bot.py
blob: ea3dc9e72db31f0028e9e9eb870372044c38d254 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python3
import json
import socket
import logging
import re
import gc

from io import BytesIO
from typing import Optional
from functools import partial

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.ticker as mticker

from telegram import ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardButton
from telegram.ext import MessageHandler, CallbackQueryHandler

from home.config import config
from home.bot import Wrapper, Context, text_filter
from home.util import chunks, MySimpleSocketClient
from home.api import WebAPIClient
from home.api.types import (
    BotType,
    TemperatureSensorLocation
)

bot: Optional[Wrapper] = None
plt.rcParams['font.size'] = 7
logger = logging.getLogger(__name__)
plot_hours = [3, 6, 12, 24]


def read_sensor(sensor: str, ctx: 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': <b>{temp} °C</b>\n'
    text += ctx.lang('humidity') + f': <b>{humidity}%</b>'

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


def callback_handler(ctx: 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()
    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.updater.bot.send_photo(ctx.user_id, 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


class SensorsBot(Wrapper):
    def __init__(self):
        super().__init__()

        self.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 ч."
        )

        self.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"
        )

        for k, v in config['sensors'].items():
            self.lang.set({k: v['label_ru']}, 'ru')
            self.lang.set({k: v['label_en']}, 'en')
            self.add_handler(MessageHandler(text_filter(self.lang.all(k)), self.wrap(partial(read_sensor, k))))

        self.add_handler(CallbackQueryHandler(self.wrap(callback_handler)))

    def markup(self, ctx: Optional[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__':
    config.load('sensors_bot')

    bot = SensorsBot()
    if 'api' in config:
        bot.enable_logging(BotType.SENSORS)
    bot.run()