-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
333 lines (301 loc) · 12.4 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
from datetime import datetime
import re
from pygame import mixer
import os, sys
import glob
import pyrebase
import json
import requests
import calendar
import threading
serial_num = "test"
pathOfConfig = "Resources/config.json"
all_alarm_times = []
all_alarm_days = []
all_alarm_ids = []
all_alarm_everyday = []
all_alarm_days_selected = []
all_alarm_musics = []
all_alarm_snooze = []
all_alarm_plays = []
all_alarm_setters = []
downloaded_music_files = []
def firebase_handler(response):
global firebase, uid, data
data = firebase.database().child("alarms_users").child(uid).get().val()
json_file = open("Resources/firebase_database_data.json", "w")
json_file.truncate(0)
json_file.write(json.dumps(data, indent=4))
json_file.close()
try:
refill_the_data_to_variables()
except:
print("still no data")
def refill_the_data_to_variables():
all_alarm_days.clear()
all_alarm_times.clear()
all_alarm_plays.clear()
all_alarm_snooze.clear()
all_alarm_setters.clear()
all_alarm_everyday.clear()
all_alarm_ids.clear()
all_alarm_days_selected.clear()
all_alarm_musics.clear()
json_file = open("Resources/firebase_database_data.json", "r")
temp__data = json_file.read()
temp_data = json.loads(temp__data)
json_file.close()
for i in temp_data:
hours, minutes = "", ""
play = False
for j in temp_data[i]:
if j == "setter":
setter = temp_data[i][j]
all_alarm_setters.append(setter)
if j == "hours":
hours = temp_data[i][j]
if j == "minutes":
minutes = temp_data[i][j]
if j == "id":
id = temp_data[i][j]
all_alarm_ids.append(id)
if j == "music":
download_music = temp_data[i][j]
all_alarm_musics.append(download_music)
if j == "play":
play = temp_data[i][j]
all_alarm_plays.append(play)
if j == "snooze":
snooze = temp_data[i][j]
all_alarm_snooze.append(snooze)
if j == "everytime":
everytime = temp_data[i][j]
all_alarm_everyday.append(everytime)
if j == "days_selected":
days_selected = temp_data[i][j]
all_alarm_days_selected.append(days_selected)
print("*************************")
if len(str(hours)) < 2:
hours = "0" + str(hours)
#print(hours)
else:
hours = str(hours)
if len(str(minutes)) < 2:
minutes = "0" + str(minutes)
#print(minutes)
else:
minutes = str(minutes)
all_alarm_times.append(str(hours) + ":" + str(minutes))
thread = threading.Thread(target=download_all_musics, args=("Resources/Downloads/Music", all_alarm_musics))
thread.start()
def refill_the_data_to_variables_from_storage():
all_alarm_days.clear()
all_alarm_times.clear()
all_alarm_plays.clear()
all_alarm_snooze.clear()
all_alarm_setters.clear()
all_alarm_everyday.clear()
all_alarm_ids.clear()
all_alarm_days_selected.clear()
all_alarm_musics.clear()
json_file = open("Resources/firebase_database_data.json", "r")
temp__data = json_file.read()
temp_data = json.loads(temp__data)
json_file.close()
for i in temp_data:
hours, minutes = "", ""
play = False
for j in temp_data[i]:
if j == "setter":
setter = temp_data[i][j]
all_alarm_setters.append(setter)
if j == "hours":
hours = temp_data[i][j]
if j == "minutes":
minutes = temp_data[i][j]
if j == "id":
id = temp_data[i][j]
all_alarm_ids.append(id)
if j == "music":
download_music = temp_data[i][j]
all_alarm_musics.append(download_music)
if j == "play":
play = temp_data[i][j]
all_alarm_plays.append(play)
if j == "snooze":
snooze = temp_data[i][j]
all_alarm_snooze.append(snooze)
if j == "everytime":
everytime = temp_data[i][j]
all_alarm_everyday.append(everytime)
if j == "days_selected":
days_selected = temp_data[i][j]
all_alarm_days_selected.append(days_selected)
print("*************************")
if len(str(hours)) < 2:
hours = "0"+str(hours)
#print(hours)
else:
hours = str(hours)
if len(str(minutes)) < 2:
minutes = "0"+str(minutes)
#print(minutes)
else:
minutes = str(minutes)
all_alarm_times.append(hours + ":" + minutes)
def load_all_songs():
downloaded_music_files.clear()
with open("Resources/Downloads/downloaded_music_paths.txt", "r") as file:
downloaded_music_files.append(file.readline().rstrip("\n"))
print(downloaded_music_files)
def delete_old_songs():
delete_files = glob.glob('Resources/Downloads/Music/*')
for file_to_be_deleted in delete_files:
os.remove(file_to_be_deleted)
print("All the old songs are deleted...")
def download_all_musics(path, all_alarms_url_path):
paths = open("Resources/Downloads/downloaded_music_paths.txt", "w+")
downloaded_music_files.clear()
paths.truncate(0)
for i in all_alarms_url_path:
music_uri = requests.get(i)
temp_index = all_alarm_musics.index(i)
temp_name = all_alarm_ids[temp_index]
temp_music_name = re.sub('[^A-Za-z0-9]+', "", temp_name)
temp_path = r"{0}/{1}".format(path, temp_music_name)
with open(temp_path + ".mp3", 'wb+') as music_file:
music_file.write(music_uri.content)
paths.write(temp_path + ".mp3" + "\n")
downloaded_music_files.append(temp_path + ".mp3")
print("adding/updating a music file")
print("ALL MUSIC FILES ARE DOWNLOADED")
print(downloaded_music_files)
paths.close()
def first_time_init(firebase):
uid = firebase.database().child("iot_alarm").child(serial_num).child(serial_num)
uid = uid.get().val()
if uid is not None:
stream = firebase.database().child("alarms_users").child(uid).stream(firebase_handler)
data_file = open("Resources/firebase_database_data.json", "r")
temp_data = data_file.read()
data_file.close()
return uid, stream
else:
print("OOPS! :< There is no account paired with this IoT Device")
data_file = open("Resources/firebase_database_data.json", "r")
temp_data = data_file.read()
data_file.close()
return None, None
def Thread_to_display_time_on_LCD():
now = datetime.now()
day_num = datetime.today().weekday()
day = str(calendar.day_name[day_num])
time = str(now.strftime("%H:%M:%S"))
date = str(now.strftime("%d/%m/%Y"))
# Here you write te code to display time
def mainLoopWhenNotConnectedToInternet():
load_all_songs()
try:
refill_the_data_to_variables_from_storage()
except:
print("There is no data found in the account to play alarm")
playing = False
while not connected_to_internet:
now = datetime.now()
day_num = datetime.today().weekday()
day = str(calendar.day_name[day_num])
time = str(now.strftime("%H:%M"))
print(time)
#print("Downloaded music files:" + str(downloaded_music_files))
#print(all_alarm_times)
if time in all_alarm_times:
index = all_alarm_times.index(time)
if day in all_alarm_days_selected[index]:
if all_alarm_plays[index]:
print("Alarm time now")
if not playing:
mixer.music.load(downloaded_music_files[index])
mixer.music.play(-1)
playing = True
if all_alarm_everyday[index] == False:
pass
# you cannot perform under following operations as, you are not reachable to the internet
#databases = firebase.database().child("alarms_users").child(uid).child(all_alarm_ids[index])
#databases.update({"play": False})
#################################################################################################
# THE MAIN PROGRAM STARTS HERE #
#################################################################################################
if __name__ == "__main__":
mixer.init()
file = open(pathOfConfig, )
config = json.load(file)
file.close()
firebase = pyrebase.initialize_app(config)
connected_to_internet = False
try:
check_internet = requests.get("https://www.google.com/", timeout=2)
print("IoT Alarm is connected to internet :)")
connected_to_internet = True
except:
print("NO INTERNET! :< . DEVICE READS PREVIOUS DATA AND ALARMS ACCORDING TO IT")
while True:
if connected_to_internet:
os.system("sudo timedatectl set-ntp True")
print("updating the time is done")
uid, stream = first_time_init(firebase)
if uid is not None and stream is not None:
print("STARTed IN MODE")
load_all_songs()
try:
refill_the_data_to_variables_from_storage()
except:
print("There is no data found in the account to play alarm")
playing = False
while connected_to_internet:
now = datetime.now()
day_num = datetime.today().weekday()
day = str(calendar.day_name[day_num])
time = str(now.strftime("%H:%M"))
#print(time)
#print("Downloaded music files:" + str(downloaded_music_files))
#print(all_alarm_times)
if time in all_alarm_times:
index = all_alarm_times.index(time)
if day in all_alarm_days_selected[index]:
if all_alarm_plays[index]:
print("Alarm time now")
if not playing:
mixer.music.load(downloaded_music_files[index])
mixer.music.play(-1)
playing = True
if all_alarm_everyday[index]==False:
databases = firebase.database().child("alarms_users").child(uid).child(all_alarm_ids[index])
databases.update({"play":False})
# I am planning to do some kind of interrupts stuffs here
else:
print("OOPS, PLEASE PAIR YOUR DEVICE WITH THE ACCOUNT, RESTART THE DEVICE AND TRY AGAIN")
keep_in_while_loop_until_restart = True
while keep_in_while_loop_until_restart:
pass # NO PROGRAMME CAN BE WRITTEN HERE
else:
inner_condition = False
try:
refill_the_data_to_variables_from_storage()
print("RETRIEVED PREVIOUSLY SAVED DATA")
inner_condition = True
except:
print("OH YOU MIGHT SEEM TO BE A NEW USER :> YOU DONT HAVE ANY ALARMS IN YOUR ACCOUNT :)")
print("Get me synced to internet!. Once you get internet")
# Write a program in try except block to detect inteernet, if intreernt iss pressent then direclty kill the process and relaunch it. this can be done only in linucx environement
while not inner_condition:
try:
test = requests.get("https://github.com")
print("finally, connected to internet network")
os.system("chmod +x restart_script.sh")
os.system("./restart_script.sh") # this will restart the main script ok!!!
except:
pass
while not connected_to_internet:
pass
# This shall be taken care in linux environment
mainLoopWhenNotConnectedToInternet()