-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdid_I_lock.py
181 lines (148 loc) · 5.89 KB
/
did_I_lock.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from gpiozero import LED, Button
import time
import emoji
from time import gmtime, strftime
from telegram.ext import Updater, CommandHandler, Filters, MessageHandler, CallbackQueryHandler
from telegram import ParseMode, InlineKeyboardButton, InlineKeyboardMarkup
from secrets import TELEGRAM_BOT_TOKEN, ALLOWED_USERS_LIST
import sheet
GreenLed = LED(22)
RedLed = LED(23)
button = Button(17)
global curr_state
curr_state = "unlocked"
prev_state = None
counter = 0
last_locked_time = None
# Check for new messages via API
production_bot = TELEGRAM_BOT_TOKEN
updater = Updater(production_bot, use_context=True)
# allows registering handler (command, text, video)
dispatcher = updater.dispatcher
global wks
wks=sheet.open_sheet("https://docs.google.com/spreadsheets/d/1DLf9p4pRmMCpJnwdYXg8BVLiSfSp3eqiOaER4jeFOYs")
# create a command callback function
def check_status(update, context):
check_button = [[InlineKeyboardButton("Check", callback_data='Check')]]
reply_markup1 = InlineKeyboardMarkup(check_button)
if curr_state == "locked":
string_to_translate1 = ":white_check_mark: Door was last locked on {} and it is now {} :key:".format(
last_locked_time, curr_state)
translation1 = emoji.emojize(string_to_translate1, use_aliases=True, delimiters=(':', ':'))
context.bot.send_message(chat_id=update.message.chat_id, text=translation1, reply_markup=reply_markup1)
else:
string_to_translate2 = " :x: Door is unlocked :unlocked: :bangbang:".format(last_locked_time, curr_state)
translation2 = emoji.emojize(string_to_translate2, use_aliases=True, delimiters=(':', ':'))
context.bot.send_message(chat_id=update.message.chat_id, text=translation2, reply_markup=reply_markup1)
# currently not working
def locked_for_minute():
check_handler = CommandHandler("Check", check_status)
dispatcher.add_handler(check_handler)
def blink_test(update=None, context=None):
string_to_translate3 = " :wink: Blink blink! :wink:".format(last_locked_time, curr_state)
translation3 = emoji.emojize(string_to_translate3, use_aliases=True, delimiters=(':', ':'))
context.bot.send_message(chat_id=update.message.chat_id, text=translation3)
for t in range(3):
GreenLed.on()
RedLed.off()
time.sleep(0.1)
GreenLed.off()
RedLed.on()
time.sleep(0.1)
GreenLed.on()
RedLed.off()
time.sleep(0.1)
GreenLed.off()
RedLed.on()
time.sleep(0.1)
GreenLed.on()
RedLed.off()
time.sleep(0.2)
GreenLed.off()
RedLed.on()
time.sleep(0.2)
GreenLed.off()
RedLed.off()
time.sleep(0.2)
GreenLed.on()
RedLed.on()
time.sleep(0.2)
GreenLed.off()
RedLed.off()
time.sleep(0.2)
GreenLed.on()
RedLed.on()
time.sleep(0.2)
print("blink blink!")
def show_log(update=None, context=None):
values_list=sheet.get_last_N_rows(wks,5)
print(values_list)
context.bot.send_message(chat_id=update.message.chat_id, text="**Last entries are:**")
for row in range (0,5):
if len(values_list[row])!=0:
context.bot.send_message(chat_id=update.message.chat_id, text="\t\t{}\t\t{}\t\t{}".format(values_list[row][0],values_list[row][1],values_list[row][2]))
# prints the last 5 lines of the log
def log_data(state_to_log):
last_locked_date = strftime("%d.%m.%y", time.localtime())
last_locked_time = strftime("%H:%M", time.localtime())
wks.append_row([str(last_locked_date),
str(last_locked_time),
str(state_to_log)])
next_row = sheet.next_available_row(wks)
def C_B_button(bot, update):
query = update.callback_query
print(query)
bot.edit_message_text(chat_id=query.message.chat.id, text=query.message.text,
message_id=query.message.message_id)
global button_handler
global check_handler
log_data("System start")
print("starting...")
button_handler = CallbackQueryHandler(C_B_button)
dispatcher.add_handler(button_handler)
check_handler = CommandHandler("Check", check_status)
dispatcher.add_handler(CommandHandler("Check", check_status, Filters.user(username=ALLOWED_USERS_LIST)))
blink_handler = CommandHandler("blink", blink_test)
dispatcher.add_handler(CommandHandler("blink", blink_test, Filters.user(username=ALLOWED_USERS_LIST)))
dispatcher.add_handler(blink_handler)
showlog_handler = CommandHandler("log", show_log)
dispatcher.add_handler(CommandHandler("log", show_log, Filters.user(username=ALLOWED_USERS_LIST)))
dispatcher.add_handler(showlog_handler)
#blink_test()
while True:
# restricting access to authorized users only.
# echo_handler = MessageHandler(Filters.text,check_status)
# dispatcher.add_handler(echo_handler)
# artificial delay to reduce noises
time.sleep(1)
if button.is_pressed:
# print(curr_state)
GreenLed.on()
RedLed.off()
# means change of state
#if prev_state == "unlocked":
if counter == 0:
last_locked_time = strftime("%H:%M \t %d.%m.%y", time.localtime())
log_data("locked")
prev_state = curr_state
curr_state = "locked"
counter = counter + 1
# counter as a helper for control
if counter % 60==0:
print(curr_state)
RedLed.on()
#counter = 0
else:
# means change of state
#if prev_state == "locked":
if counter !=0:
log_data("unlocked")
prev_state = curr_state
curr_state = "unlocked"
# print(curr_state)
GreenLed.off()
RedLed.on()
counter = 0
updater.start_polling()