-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
131 lines (104 loc) · 3.86 KB
/
main.py
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
import time
import configparser
from itertools import groupby
import paho.mqtt.client as mqtt
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackQueryHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
keys = {'погода': '1', 'теплицы': '2', 'приборы': '3'}
def maketree(group, items, path):
def sep(s):
return s.split('/', 1)
head = [i for i in items if len(sep(i)) == 2]
tail = [i for i in items if len(sep(i)) == 1]
if len(tail) == 1:
return group, tail[0]
gv = groupby(sorted(head), lambda i: sep(i)[0])
return group, dict([(i, path) for i in tail] + [maketree(g, [sep(i)[1] for i in v], '') for g, v in gv])
# Telegram process
def get_keyb():
return [[InlineKeyboardButton('Погода', callback_data='1'),
InlineKeyboardButton('Теплицы', callback_data='2')]]
def get_data_text(text, alldata):
try:
key = keys[text.lower()]
except:
return 'err'
return get_data(key, alldata)
def get_data(key, alldata):
tree = dict([maketree('tree', list(k + '/' + v for k, v in alldata.items()), '')])['tree']
if key == '1':
try:
rez = tree['air']['outdoor']['1']['temp'] + ' °C\n' \
+ tree['air']['outdoor']['1']['humidity'] + ' %\n' \
+ tree['air']['outdoor']['1']['pressure'] + ' mmHg\n' \
+ tree['air']['outdoor']['1']['upd']
return rez
except:
return 'Нет данных'
elif key == '2':
rez = ''
try:
for n, f in sorted(tree['greenhouse'].items(), key=lambda item: int(item[0])):
rez += '№ %s: %s \n' % (n, f['temp'])
rez += tree['greenhouse']['1']['upd']
return rez
except:
return 'Нет данных'
elif key == '3':
return '40'
else:
return 'Error'
def text(update, context):
keyboard = get_keyb()
reply_markup = InlineKeyboardMarkup(keyboard)
rez = get_data_text(update.message.text, alldata)
if rez == 'err':
update.message.reply_text('>', reply_markup=reply_markup)
else:
update.message.reply_text(rez)
def button(update, context):
query = update.callback_query
query.answer()
keyboard = get_keyb()
reply_markup = InlineKeyboardMarkup(keyboard)
rez = get_data(query.data, alldata)
query.edit_message_text(text=rez, reply_markup=reply_markup)
def error(update, context):
print('Update caused error ', time.strftime('%Y-%m-%d %H:%M:%S'), update, context.error)
# MQTT process
def on_connect(client, userdata, flags, rc):
for topic in TOPICS.split(','):
client.subscribe(topic)
def on_message(client, userdata, msg):
alldata.update({str(msg.topic): str(msg.payload)[2:-1]})
def readmqtt():
client = mqtt.Client()
client.username_pw_set(username=NAME, password=PASS)
client.on_connect = on_connect
client.on_message = on_message
client.connect(IP, PORT, 60)
client.loop_forever()
if __name__ == '__main__':
config = configparser.ConfigParser()
config.read('./settings.ini')
TOKEN = config['TELEGRAM']['token']
NAME = config['MQTT']['username']
PASS = config['MQTT']['password']
IP = config['MQTT']['ip']
PORT = int(config['MQTT']['port'])
TOPICS = config['MQTT']['topics']
alldata = {}
while 1:
try:
updater = Updater(TOKEN, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", text))
dp.add_handler(CallbackQueryHandler(button))
dp.add_handler(MessageHandler(Filters.text, text))
dp.add_error_handler(error)
updater.start_polling()
readmqtt()
updater.idle()
except:
print('Connection error')
time.sleep(5)