-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.py
185 lines (144 loc) · 5.54 KB
/
commands.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
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 python
# -*- coding: utf-8 -*-
import datetime
import time
from telegram.ext import ConversationHandler
import switchbot_py3
import heatbot
import verifier
from configurations import Configuration
LOG = list()
LOG_SIZE = 15
AUTOMATIC_OFF_THREAD_INTERVALS = 5 * 60 # 5 Minutes
AUTOMATIC_OFF_THREAD_SLEEP = 5 # 5 Seconds
def add_to_log(update, action):
global LOG
if update is None:
name = "System"
else:
user = update.message.from_user
user_id = str(user["id"])
name = Configuration.Allowed[user_id]
current_time = datetime.datetime.now().strftime("%d.%m %H:%M")
LOG.append((name, current_time, action))
if len(LOG) >= LOG_SIZE:
LOG.pop(0)
heatbot.logger.info(f"{name} used action '{action}'.")
def get_log(is_master=False):
global LOG
log_output = "Logs:\n"
if len(LOG) == 0:
return log_output + " None."
for name, current_time, action in LOG:
if is_master:
log_output += "%10s : " % (name,)
log_output += f"{current_time} - {action}.\n"
return log_output[:-1]
def get_status():
last_change = datetime.datetime.fromtimestamp(Configuration.LastChange)
last_change_duration = datetime.datetime.now() - last_change
hours = last_change_duration.seconds // 3600 + last_change_duration.days * 24
minutes = (last_change_duration.seconds // 60) % 60
duration_string = ""
if hours > 1:
duration_string += f"{hours} hours and "
elif hours == 1:
duration_string += f"{hours} hour and "
duration_string += f"{minutes} minutes"
if Configuration.CurrentStatus == "ON":
status_message = f"The heat has been ON for {duration_string}."
elif Configuration.CurrentStatus == "OFF":
status_message = f"The heat has been OFF for {duration_string}."
else:
status_message = f"Current Status: UNKNOWN.\nLast status change: {duration_string} ago."
return status_message
@verifier.verify_id
def status(update, context):
update.message.reply_text(get_status())
add_to_log(update, "status")
return ConversationHandler.END
@verifier.verify_id
def log(update, context):
user = update.message.from_user
user_id = str(user["id"])
update.message.reply_text(get_log(user_id == Configuration.MasterID))
add_to_log(update, "log")
return ConversationHandler.END
def turn_on():
switchbot_driver = switchbot_py3.Driver(Configuration.BluetoothAddress,
Configuration.BluetoothInterface)
return_code = switchbot_driver.run_command("on")
if return_code != [b'\x13']:
return False
Configuration.CurrentStatus = "ON"
Configuration.LastChange = datetime.datetime.now().timestamp()
Configuration.save_configuration()
return True
def turn_off():
switchbot_driver = switchbot_py3.Driver(Configuration.BluetoothAddress,
Configuration.BluetoothInterface)
return_code = switchbot_driver.run_command("off")
if return_code != [b'\x13']:
return False
Configuration.CurrentStatus = "OFF"
Configuration.LastChange = datetime.datetime.now().timestamp()
Configuration.save_configuration()
return True
@verifier.verify_id
def on(update, context):
if Configuration.CurrentStatus == "ON":
return status(update, context)
if turn_on():
update.message.reply_text("Turned Heatbot ON 💡")
add_to_log(update, "on")
else:
update.message.reply_text("Failed to turn on...")
return ConversationHandler.END
@verifier.verify_id
def off(update, context):
if Configuration.CurrentStatus == "OFF":
return status(update, context)
if turn_off():
update.message.reply_text("Turned Heat OFF 🍗")
add_to_log(update, "off")
else:
update.message.reply_text("Failed to turn off...")
return ConversationHandler.END
@verifier.verify_id
def force_on(update, context):
if Configuration.CurrentStatus == "ON":
update.message.reply_text("HeatBot is already ON 💡. Turning it ON anyways...")
if turn_on():
update.message.reply_text("Turned Heat ON 💡")
add_to_log(update, "force on")
else:
update.message.reply_text("Failed to turn on...")
return ConversationHandler.END
@verifier.verify_id
def force_off(update, context):
if Configuration.CurrentStatus == "OFF":
update.message.reply_text("HeatBot is already OFF 🍗. Turning it OFF anyways...")
if turn_off():
update.message.reply_text("Turned Heatbot OFF.")
add_to_log(update, "force off")
else:
update.message.reply_text("Failed to turn off...")
return ConversationHandler.END
def async_automatic_off(should_stop_event):
while not should_stop_event.is_set():
for _ in range(AUTOMATIC_OFF_THREAD_INTERVALS // AUTOMATIC_OFF_THREAD_SLEEP):
time.sleep(AUTOMATIC_OFF_THREAD_SLEEP)
if should_stop_event.is_set():
return
if Configuration.CurrentStatus != "ON":
continue
if Configuration.AutomaticOffInMinutes is None:
continue
now = datetime.datetime.now()
last_change = datetime.datetime.fromtimestamp(Configuration.LastChange)
minutes_since_last_change = (now - last_change).total_seconds() / 60
if minutes_since_last_change >= Configuration.AutomaticOffInMinutes:
if turn_off():
add_to_log(None, "automatic off")
else:
heatbot.logger.warning("Automatic off failed")