Add file rotation : data.csv => hourly => daily

This commit is contained in:
Pierrick C 2018-08-19 23:39:58 +02:00
parent c62ad455ba
commit 17757f7b1f

View File

@ -245,6 +245,33 @@ def check_data_dir():
# Turn onboard led on to indicate read-only error # Turn onboard led on to indicate read-only error
led13.value = True led13.value = True
def rotate_files():
"""Check if files need to rotate
=> every new hour
=> every new day
"""
global last_time
current_time = clock.datetime
#If the hour changed : copy current data.csv to hourly directory
if current_time[3] != last_time[3] and backup_data:
print("Time to move hourly data !")
os.rename("data/data.csv", "data/hourly/{}.csv".format(last_time[3]))
#If the day changed : copy content of hourly to daily directories
if current_time[2] != last_time[2] and backup_data:
print("Time to move daily data !")
#Create new dir for the date of yesterday
newdir = "data/daily/{}{}{}.csv".format(last_time[0:2])
os.mkdir(newdir)
#Move each "hourly file" to the new directory
for file in os.listdir('data/hourly'):
print("Move {} to {}".format(newdir, file))
os.rename("data/hourly/{}".format(file), "{}/{}".format(newdir, file))
#Finally update last_time, each time
last_time = current_time
def set_clock_from_GPS(treshold=5.0): def set_clock_from_GPS(treshold=5.0):
"""Compare internal RTC and date-time from GPS (if enable and fixed) and set """Compare internal RTC and date-time from GPS (if enable and fixed) and set
@ -347,6 +374,8 @@ data = Data()
#Init timer #Init timer
last_update = last_sent_packet = last_written_data = time.monotonic() last_update = last_sent_packet = last_written_data = time.monotonic()
last_time = clock.datetime
while True: while True:
if gps_enable: if gps_enable:
@ -369,6 +398,7 @@ while True:
if backup_data and current - last_written_data >= write_interval : if backup_data and current - last_written_data >= write_interval :
last_written_data = current last_written_data = current
rotate_files() #First check if files need to rotate
print("Backup data...") print("Backup data...")
data.write_on_flash() data.write_on_flash()