Fix file rotation.
Now it's a method of Data classes
This commit is contained in:
parent
bb2a2f2665
commit
af9d40ceb6
@ -60,6 +60,7 @@ class Data:
|
||||
self._last_update = 0
|
||||
self._last_backup = 0
|
||||
self._last_send = 0
|
||||
self._last_file_rotation = time.localtime()
|
||||
|
||||
# Check if data directories exists if we need to use them
|
||||
if self.write_interval >= 0:
|
||||
@ -139,8 +140,11 @@ class Data:
|
||||
self.write_interval))
|
||||
if current - self._last_backup >= self.write_interval:
|
||||
self._last_backup = current
|
||||
#First check if a rotation is needed
|
||||
self._file_rotation()
|
||||
|
||||
file_name = "{}.csv".format(self.name)
|
||||
file_path = "data/"
|
||||
file_path = "data"
|
||||
try:
|
||||
#Check if the file exists. If not, creates it with CSV header
|
||||
if file_name not in os.listdir(file_path):
|
||||
@ -179,6 +183,42 @@ class Data:
|
||||
print(self.json)
|
||||
#print("\n")
|
||||
|
||||
def _file_rotation(self):
|
||||
"""Check if files need to rotate
|
||||
=> every new hour
|
||||
=> every new day
|
||||
"""
|
||||
current_time = time.localtime()
|
||||
if self.debug:
|
||||
print(self._last_file_rotation[3])
|
||||
print(current_time[3])
|
||||
|
||||
# If the hour changed : copy current csv to hourly directory
|
||||
if current_time[3] != self._last_file_rotation[3]:
|
||||
print("Time to move hourly data !")
|
||||
os.rename("data/{}.csv".format(self.name),
|
||||
"data/hourly/{}_{:02}.csv".format(
|
||||
self.name,
|
||||
self._last_file_rotation[3]))
|
||||
|
||||
# If the day changed : copy content of hourly to daily directories
|
||||
if current_time[2] != self._last_file_rotation[2]:
|
||||
print("Time to move daily data !")
|
||||
# Create new dir for the date of "yesterday"
|
||||
newdir = "{}{:02}{:02}".format(*self._last_file_rotation[0:3])
|
||||
if newdir not in os.listdir("data/daily/"):
|
||||
os.mkdir("data/daily/" + newdir)
|
||||
# Move each "hourly file" to the new directory
|
||||
for file in os.listdir('data/hourly'):
|
||||
# Move only file which name is beginning with self.name
|
||||
if file.find(self.name) == 0:
|
||||
print("Move {} to {}".format(file, newdir))
|
||||
os.rename("data/hourly/{}".format(file), "{}/{}".format(newdir, file))
|
||||
|
||||
#Finally : remember last rotation check
|
||||
self._last_file_rotation = current_time
|
||||
|
||||
return
|
||||
|
||||
class SysData(Data):
|
||||
"""Subclass for Feather board data"""
|
||||
@ -375,36 +415,6 @@ class GPSData(Data):
|
||||
# Functions #
|
||||
#############
|
||||
|
||||
def rotate_files(last_time, debug=False):
|
||||
"""Check if files need to rotate
|
||||
=> every new hour
|
||||
=> every new day
|
||||
"""
|
||||
current_time = time.localtime()
|
||||
if debug:
|
||||
print(last_time[3])
|
||||
print(current_time[3])
|
||||
|
||||
# If the hour changed : copy current data.csv to hourly directory
|
||||
if current_time[3] != last_time[3]:
|
||||
print("Time to move hourly data !")
|
||||
os.rename("data/data.csv", "data/hourly/{:02}.csv".format(last_time[3]))
|
||||
|
||||
# If the day changed : copy content of hourly to daily directories
|
||||
if current_time[2] != last_time[2]:
|
||||
print("Time to move daily data !")
|
||||
# Create new dir for the date of yesterday
|
||||
newdir = "data/daily/{}{:02}{:02}".format(*last_time[0:3])
|
||||
os.mkdir(newdir)
|
||||
# Move each "hourly file" to the new directory
|
||||
for file in os.listdir('data/hourly'):
|
||||
print("Move {} to {}".format(file, newdir))
|
||||
os.rename("data/hourly/{}".format(file), "{}/{}".format(newdir, file))
|
||||
|
||||
# Finally update last_time, each time
|
||||
return current_time
|
||||
|
||||
|
||||
def set_clock_from_localtime(clock, treshold=2.0):
|
||||
"""
|
||||
Compare internal RTC and date-time from time.localtime() and set
|
||||
|
@ -97,7 +97,6 @@ data = [gps_data, sys_data, bme_data]
|
||||
|
||||
# Init timers
|
||||
last_update = last_sent_packet = last_written_data = time.monotonic()
|
||||
last_rotation = time.localtime()
|
||||
|
||||
#############
|
||||
# Main loop #
|
||||
@ -110,9 +109,6 @@ while True:
|
||||
for src in data:
|
||||
src.update(current_time, verbose=PRINT_DATA)
|
||||
|
||||
# First check if files need to rotate
|
||||
last_rotation = cameteo.rotate_files(last_rotation, debug=True)
|
||||
|
||||
for src in data:
|
||||
src.write_on_flash(current_time)
|
||||
src.send_json(current_time, uart=rpi_uart, verbose=PRINT_DATA)
|
||||
|
Loading…
Reference in New Issue
Block a user