-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
114 lines (79 loc) · 3.26 KB
/
functions.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
from buttons import *
from constants import *
from functions import *
from time import gmtime, strftime, time
import requests
def convert(seconds):
return strftime("%H:%M:%S", gmtime(seconds))
def parse_update(obj):
chat_id = 0
data = ''
field = None
if 'message' in obj:
chat_id = obj['message']['chat']['id']
data = obj['message']['text']
field = 'message'
if 'callback_query' in obj:
chat_id = obj['callback_query']['message']['chat']['id']
data = obj['callback_query']['data']
field = 'callback_query'
print('-----')
print('\nRaw_Object : ',obj)
print('\nChat_ID : ', chat_id)
print('\nRaw_Data : ', data)
print('-----')
return chat_id, data, field
def send_message(chat_id, text):
url = f'https://api.telegram.org/bot{TOKEN}/sendMessage'
payload = {
'chat_id': chat_id,
'text': text
}
result = requests.post(url,json=payload)
return result
def update_timer_database(chat_id, txt):
with open(TIMER_FILE_NAME, mode = 'r', encoding = 'utf-8') as f:
timer_data = f.read().splitlines()
with open(DURATION_FILE_NAME, mode = 'r', encoding = 'utf-8') as f:
dur_data = f.read().splitlines()
washer_dur = dur_data[0]
dryer_dur = dur_data[1]
idx = CODE_TO_IDX_DICT[txt]
dur = washer_dur if idx < 3 else dryer_dur
timer_data[idx] = str(int(time()) + int(dur) * 60)
with open(TIMER_FILE_NAME, mode = 'w', encoding = 'utf-8') as f:
f.writelines(str(line) + '\n' for line in timer_data)
send_message(chat_id, f"{dur} minutes timer started for {MACHINE_NAME[idx]}")
def cancel_timer(chat_id, txt):
with open(TIMER_FILE_NAME, mode = 'r', encoding = 'utf-8') as f:
timer_data = f.read().splitlines()
txt = txt.split('_', 1)[1]
idx = CODE_TO_IDX_DICT[txt]
timer_data[idx] = str(int(time()))
with open(TIMER_FILE_NAME, mode = 'w', encoding = 'utf-8') as f:
f.writelines(str(line) + '\n' for line in timer_data)
send_message(chat_id, f"Timer has been reset for {MACHINE_NAME[idx]}.")
def update_dur_database(chat_id, txt):
with open(DURATION_FILE_NAME, mode = 'r', encoding = 'utf-8') as f:
dur_data = f.read().splitlines()
dur = int(txt)
dur_data[1] = dur
with open(DURATION_FILE_NAME, mode = 'w', encoding = 'utf-8') as f:
f.writelines(str(line) + '\n' for line in dur_data)
send_message(chat_id, f"Dryer default timer duration set to {dur} minutes.")
def get_duration():
with open(DURATION_FILE_NAME, mode = 'r', encoding = 'utf-8') as f:
dur_data = f.read().splitlines()
return dur_data
def reset_duration(chat_id):
with open(DURATION_FILE_NAME, mode = 'w', encoding = 'utf-8') as f:
f.writelines(str(line) + '\n' for line in DEFAULT_DUR_DATA)
send_message(chat_id, TXT_RESET)
def send_status(chat_id):
with open(TIMER_FILE_NAME, mode = 'r', encoding = 'utf-8') as f:
data = f.read().splitlines()
result = TXT_STATUS
for idx, item in enumerate(data):
seconds_left = max(0, (int(item) - int(time())))
result = result + f'{MACHINE_NAME[idx]}: {convert(seconds_left)}\n'
send_message(chat_id, result)