summaryrefslogtreecommitdiff
path: root/main.py
blob: 46fdac3b8728b7ecc0a75d7487c606ff54d778a1 (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
186
import logging
import re
import datetime
import isv
import configstore

from time import sleep
from strings import lang as _
from telegram import (
    Update,
    ParseMode,
    KeyboardButton,
    ReplyKeyboardMarkup
)
from telegram.ext import (
    Updater,
    Filters,
    CommandHandler,
    MessageHandler,
    CallbackContext
)


#
# helpers
#

def get_markup() -> ReplyKeyboardMarkup:
    button = [
        [
            _('status'),
            _('generation')
        ],
        [
            _('gs'),
            _('ri'),
            _('errors')
        ]
    ]
    return ReplyKeyboardMarkup(button, one_time_keyboard=False)


def reply(update: Update, text: str) -> None:
    update.message.reply_text(text,
                              reply_markup=get_markup(),
                              parse_mode=ParseMode.HTML)


#
# command/message handlers
#

def start(update: Update, context: CallbackContext) -> None:
    reply(update, 'Select a command on the keyboard.')


def msg_status(update: Update, context: CallbackContext) -> None:
    try:
        gs = isv.general_status()

        # render response
        power_direction = gs['battery_power_direction'].lower()
        power_direction = re.sub(r'ge$', 'ging', power_direction)

        charging_rate = ''
        if power_direction == 'charging':
            charging_rate = ' @ %s %s' % tuple(gs['battery_charging_current'])
        elif power_direction == 'discharging':
            charging_rate = ' @ %s %s' % tuple(gs['battery_discharge_current'])

        html = '<b>Battery:</b> %s %s' % tuple(gs['battery_voltage'])
        html += ' (%s%s, ' % tuple(gs['battery_capacity'])
        html += '%s%s)' % (power_direction, charging_rate)

        html += '\n<b>Load:</b> %s %s' % tuple(gs['ac_output_active_power'])
        html += ' (%s%%)' % (gs['output_load_percent'][0])

        if gs['pv1_input_power'][0] > 0:
            html += '\n<b>Input power:</b> %s%s' % tuple(gs['pv1_input_power'])

        if gs['grid_voltage'][0] > 0 or gs['grid_freq'][0] > 0:
            html += '\n<b>Generator:</b> %s %s' % tuple(gs['grid_voltage'])
            html += ', %s %s' % tuple(gs['grid_freq'])

        # send response
        reply(update, html)
    except Exception as e:
        logging.exception(str(e))
        reply(update, 'exception: ' + str(e))


def msg_generation(update: Update, context: CallbackContext) -> None:
    try:
        today = datetime.date.today()
        yday = today - datetime.timedelta(days=1)
        yday2 = today - datetime.timedelta(days=2)

        gs = isv.general_status()
        sleep(0.1)

        gen_today = isv.day_generated(today.year, today.month, today.day)
        gen_yday = None
        gen_yday2 = None

        if yday.month == today.month:
            sleep(0.1)
            gen_yday = isv.day_generated(yday.year, yday.month, yday.day)

        if yday2.month == today.month:
            sleep(0.1)
            gen_yday2 = isv.day_generated(yday2.year, yday2.month, yday2.day)

        # render response
        html = '<b>Input power:</b> %s %s' % tuple(gs['pv1_input_power'])
        html += ' (%s %s)' % tuple(gs['pv1_input_voltage'])

        html += '\n<b>Today:</b> %s Wh' % (gen_today['wh'])

        if gen_yday is not None:
            html += '\n<b>Yesterday:</b> %s Wh' % (gen_yday['wh'])

        if gen_yday2 is not None:
            html += '\n<b>The day before yesterday:</b> %s Wh' % (gen_yday2['wh'])

        # send response
        reply(update, html)
    except Exception as e:
        logging.exception(str(e))
        reply(update, 'exception: ' + str(e))


def msg_gs(update: Update, context: CallbackContext) -> None:
    try:
        status = isv.general_status(as_table=True)
        reply(update, status)
    except Exception as e:
        logging.exception(str(e))
        reply(update, 'exception: ' + str(e))


def msg_ri(update: Update, context: CallbackContext) -> None:
    try:
        rated = isv.rated_information(as_table=True)
        reply(update, rated)
    except Exception as e:
        logging.exception(str(e))
        reply(update, 'exception: ' + str(e))


def msg_errors(update: Update, context: CallbackContext) -> None:
    try:
        faults = isv.faults(as_table=True)
        reply(update, faults)
    except Exception as e:
        logging.exception(str(e))
        reply(update, 'exception: ' + str(e))


def msg_all(update: Update, context: CallbackContext) -> None:
    reply(update, "Command not recognized. Please try again.")


if __name__ == '__main__':
    config = configstore.get_config()

    logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                        level=logging.INFO)

    updater = Updater(configstore.get_token(), request_kwargs={'read_timeout': 6, 'connect_timeout': 7})
    dispatcher = updater.dispatcher

    user_filter = Filters.user(configstore.get_admins())

    dispatcher.add_handler(CommandHandler('start', start))
    dispatcher.add_handler(MessageHandler(Filters.text(_('status')) & user_filter, msg_status))
    dispatcher.add_handler(MessageHandler(Filters.text(_('generation')) & user_filter, msg_generation))
    dispatcher.add_handler(MessageHandler(Filters.text(_('gs')) & user_filter, msg_gs))
    dispatcher.add_handler(MessageHandler(Filters.text(_('ri')) & user_filter, msg_ri))
    dispatcher.add_handler(MessageHandler(Filters.text(_('errors')) & user_filter, msg_errors))
    dispatcher.add_handler(MessageHandler(Filters.all & user_filter, msg_all))

    # start the bot
    updater.start_polling()

    # run the bot until the user presses Ctrl-C or the process receives SIGINT, SIGTERM or SIGABRT
    updater.idle()