-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSim_upload_v3.2.py
346 lines (319 loc) · 12.2 KB
/
Sim_upload_v3.2.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
334
335
336
337
338
339
340
341
342
343
344
345
346
#cloud upload script under construction
import pytz
import tzlocal
import datetime
import time
import os
import sys
import shutil
import socket
import subprocess
from time import sleep
"""
TO BE DONE import from the main file "Start_timelapse.py" a variable that define witch variables will be imported
from specified preset
from Start_timelapse import Preset #Preset = Picam-preset_1
from Preset import Start_day, Stop_day, Sync_to_cloud, Save_to_cloud, Stop_time, Start_time, sync, upload, restart_cloud
"""
######### - uncomment for manually run - #################
#from picam_preset_1 import start_day, stop_day, Sync_to_cloud, Save_to_cloud, stop_time, start_time, sync, upload, restart_cloud
Start_day = 0 #only import from preset
Stop_day = 6 #only import from preset
Sync_to_cloud = False #only import from preset
Save_to_cloud = True #only import from preset
Start_time = "8:00" #only import from preset
Stop_time = "18:00" #only import from preset
############# - Custom time - only for 2. option ########
#sch_time() #auto
Time_upload = "18:00" #manually
Time_upload_str = (sum(x * int(t) for x, t in zip([3600, 60, 1], Time_upload.split(":"))))
#############################################################
x = datetime.datetime.utcnow() #UTC time
Local_timezone = tzlocal.get_localzone() #Time Zone
Moment_utc = x.replace(tzinfo=pytz.utc).astimezone(Local_timezone) #UTC Time Zone
Moment =(Moment_utc.strftime("%X")) #time now in hh:mm:ss
Start_str = (sum(x * int(t) for x, t in zip([3600, 60, 1], Start_time.split(":")))) #string time start now in seconds
Stop_str = (sum(x * int(t) for x, t in zip([3600, 60, 1], Stop_time.split(":")))) #string time stop now in seconds
Time_now_str = (sum(x * int(t) for x, t in zip([3600, 60, 1], Moment.split(":")))) #string time now in seconds
Day_now = Moment_utc.strftime("%w") #day of the week 0-6 in the time zone
cloud_path = "/home/pi/Desktop/Timelapse/gdrive" #location of gdrive
destination = "uploaded/" #moved uploaded files
checkfile = "logfile.txt" #log file from rclone check
local = "Local/"
#--------------------------------- Day -------------------------------------------------
def today(): #F Return today day int
return Day_now
print("today is today")#---------------------------
#-------------------------------- Clock ------------------------------------------------
def clock(): #F Return time in second string
return Time_now_str
print("now is current time ")#---------------------------
#-------------------------------scheduler ---------------------------------------------
def sch_start_time(): #Operating_T True/ False
global Operating_T, Time_now_str, Start_time_str, Stop_time_str
if Start_time <= Stop_time:
clock()
if Time_now_str >= Start_time_str and Time_now_str < Stop_time_str:
Operating_T = True
else:
Operating_T = False
elif Start_time > Stop_time:
clock()
if not Time_now_str > Start_time_str or Time_now_str < Stop_time_str:
Operating_T = True
else:
Operating_T = False
return Operating_T
def sch_start_day(): #Operating_D True/ False
global Operating_D, Time_now_str, Start_day, Stop_day
if Start_day <= Stop_day:
today()
if Day_now >= Start_day and Day_now <= Stop_day:
Operating_D = True
else:
Operating_D = False
elif Start_day > Stop_day:
today()
if not Day_now >= Start_day or Day_now <= Stop_day:
if Day_now > Start_day:
Operating_D = True
else:
Operating_D = False
return Operating_D
#---------------------------- Ping google --------------------------------------------
def raspberry_online(): #V Raspberry_online True/ False
print(" check if is online ")
global Raspberry_online, Cloud_online
try:
socket.create_connection(('Google.com', 80)) # #################################### google or default server gateway
except OSError:
Raspberry_online = False
return Raspberry_online
Cloud_online == False
return Cloud_online
print("Offline")#---------------------------
else:
Raspberry_online = True
return Raspberry_online
print("Online")#---------------------------
finally:
pass
#------------------------- check cloud online ------------------------------------------ #####
def cloud_check(): #V Cloud_online True/ False
print(" check if the gdrive is mounted ")
global Cloud_online
try:
os.path.ismount(cloud_path)
except:
Cloud_online = False
return Cloud_online
print("Not mounted")#---------------------------
else:
Cloud_online = True
return Cloud_online
print("Mounted")#---------------------------
finally:
pass
"""
----------------------------------- Late upload ---------------------------------------
ther is a problem, if a time lapse ends in 23:50 and the next day is a non working day
the upload will have only 10 minutes to run and it will not be completed, the upload
will coninue next working day again after 23:50, but will have two days of files to
upload. To overcome this probelm the next stsment will ignore the Shedule_day to make
it work one more day this is for fail safe safety
"""
##ovaj dio nije gotov upload_check() ####################################################################################
late_upload() #upload_check()
upload_check()
sync_to_cloud() #upload_check()
save_to_cloud() #upload_check()
restart_cloud() #odvojiti procese
################################
def late_upload(): #V Operating_D True/False Stop_str > 82800
global Cloud_online
print("Not a working day, if fail will continue next working day")
if Error_upload == True and Cloud_online == True and New_day == False and Raspberry_online == True:
print("Uploading in a non working day...")
upload()
upload_check()
raspberry_online()
elif Error_upload == False:
print("Succes upload, new upload schedule next scheduled day!")
New_day == True
elif Cloud_online == False or Raspberry_online == False:
print("Cheking internet and restarting cloud")
restart_cloud()
cloud_check()
raspberry_online()
sleep(120)
pass
#-------------------------- check uploaded data -----------------------------------------
def upload_check():
print(" check if all files are uploaded ")
global Error_upload
os.system("rclone check 'Local' 'gdrive' --one-way -vv -P --combined logfile.txt")
destination = "uploaded/"
checkfile = "logfile.txt"
search = "=" # move from the folder successfuly uplouded files
list_of_files = []
lines = []
folders = []
uniq_folder_list = []
shutil_l = []
shutil_f = []
for line in open(checkfile, "r"):
if search in line:
list_of_files = line.split("/")[1]
lines.append(list_of_files.rstrip())
list_of_folders = line.split(" ")[1].split("/")[0]
folders.append(list_of_folders.rstrip())
[uniq_folder_list.append(n) for n in folders if n not in uniq_folder_list]
for new_folder in uniq_folder_list:
if not os.path.exists(destination + new_folder):
os.makedirs(destination + new_folder)
for l, f in zip(lines, folders):
l1 = (local + f + "/" + l)
f1 = (destination + f)
shutil_l.append(l1.rstrip())
shutil_f.append(f1.rstrip())
for src, dest in zip(shutil_l, shutil_f):
shutil.move(src,dest)
os.system("rclone check 'Local' 'gdrive' --one-way -vv -P --combined logfile.txt")
with open(checkfile, 'r') as read_obj:
one_char = read_obj.read(1)
if not one_char:
Error_upload = False
return Error_upload
print("all files are online")
else:
Error_upload = True
return Error_upload
print("Not uploaded ")
#----------------------- upload after taking picture ------------------------------------
def sync_to_cloud(): #Cloud_online == True ## New_day == False ## Raspberry_online == True
if Operating_T == True and Operating_D == True and Cloud_online == True and New_day == False and Raspberry_online == True: #working time is not over
sch_time()
sch_day()
try:
sync() #no --checksum, faster upload, if is done before stop time just wait 2 min and resart
except:
print("Disconnected!!")
pass
upload_check()
raspberry_online()
cloud_check()
sleep(1)
if Operating_T == False and New_day == False and Raspberry_online == True: #working time is over no new picture will be taken
if Cloud_online == True and New_day == False and Raspberry_online == True:
upload_check()
reupload()
pass
pass
elif Cloud_online == False and Raspberry_online == True:
restart_cloud()
sleep(5)
pass
##----------------------- upload after timelapse stopped ----------------------------------- #####
def save_to_cloud(): # Upload to cloud after working day is over and stops before time lapse starts
upload_check()
if Operating_T == True and Operating_D == True and Cloud_online == True and New_day == False and Raspberry_online == True:
sch_time()
sch_day()
try:
upload() ##########################In case of lost connection break
except:
print("Disconnected!!")
pass
upload_check()
raspberry_online()
reupload()
pass
##----------------------- upload after timelapse stopped ----------------------------------- #####
def reupload(): #reupload after checking if data on cloud is not completed
if Error_upload == True and Cloud_online == True and New_day == False and Raspberry_online == True:
print("Upload all and repeat!")
seelp(2)
try:
upload() ##########################In case of lost connection break
except:
print("Disconnected!!")
pass
upload_check()
raspberry_online()
cloud_check()
elif Error_upload == False and New_day == False and Raspberry_online == True:
print("Succes upload, new upload schedule tomorrow!")
New_day == True
elif Cloud_online == False and Raspberry_online == True:
restart_drive()
upload_check()
cloud_check()
sleep(120)
pass
################# will be defined in picam_reset_1 ######################
def sync():
print("rclone Sync function")#-------------------------------------
os.system("rclone --ignore-existing sync 'Local' gdrive -P -vv --dry-run") # -P -vv -dry-run
sleep(300)
def upload():
print("rclone Upload function")#-------------------------------------
os.system("rclone --checksum --ignore-existing sync 'Local' gdrive -P -vv --dry-run") # -P -vv -dry-run
def restart_cloud(): ## subprocces and check for other procces and kill it
try:
Raspberry_online == True
except:
print("Not online")
else:
#os.system("fusermount -u gdrive")
sleep(1)
#os.system("rclone mount rpi-gdrive: gdrive")
sleep(1)
cloud_check()
finally:
pass
# ############################ - Main loop - ##########################
Error_upload = True
New_day = True
Operating_D = False
Operating_T = False
Sync_to_cloud = False
Save_to_cloud = False
Check = True
while True:
# restart_cloud()
raspberry_online()
sch_day()
New_day = False
if Operating_D == True and New_day == False and Raspberry_online == True:
sch_day()
sch_time()
if Sync_to_cloud == True and Save_to_cloud == False and New_day == False and Raspberry_online == True:
sync_to_cloud() #syncronise every 2 min with the cloud
elif Sync_to_cloud == False and Save_to_cloud == True and New_day == False and Raspberry_online == True:
save_to_cloud() #upload after working our all files until starting time
elif Raspberry_online == False:
raspberry_online() # raspberry not online
sleep(300)
elif Sync_to_cloud == True and Save_to_cloud == True:
print("Chose Sync or Save")
break
elif Sync_to_cloud == False and Save_to_cloud == False:
print("Sync or Save not working")
break
else:
print("done for today")
Check = True
Return Check
elif Operating_D == False and Error_upload == True and Stop_str > 82800: #23:00 in seconds
late_upload()
sch_day()
sch_time()
Check = True
Return Check
elif Operating_D == False and Check == True:
print("Upload will continue next scheduled day")
sch_day()
Check = False
Return Check
sleep(600)
pass