From 17757f7b1fc3f9ed38942a4f67db36f54db5aa93 Mon Sep 17 00:00:00 2001 From: Pierrick C Date: Sun, 19 Aug 2018 23:39:58 +0200 Subject: [PATCH] Add file rotation : data.csv => hourly => daily --- circuitpython/code/main.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/circuitpython/code/main.py b/circuitpython/code/main.py index a8f598f..71181b7 100644 --- a/circuitpython/code/main.py +++ b/circuitpython/code/main.py @@ -245,6 +245,33 @@ def check_data_dir(): # Turn onboard led on to indicate read-only error 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): """Compare internal RTC and date-time from GPS (if enable and fixed) and set @@ -347,6 +374,8 @@ data = Data() #Init timer last_update = last_sent_packet = last_written_data = time.monotonic() +last_time = clock.datetime + while True: if gps_enable: @@ -369,6 +398,7 @@ while True: if backup_data and current - last_written_data >= write_interval : last_written_data = current + rotate_files() #First check if files need to rotate print("Backup data...") data.write_on_flash()