store difference of vehicle data to file

This commit is contained in:
2024-10-18 19:06:32 +02:00
parent 65849ccfe3
commit b1b9a7d385
3 changed files with 90 additions and 13 deletions
+29 -12
View File
@@ -7,8 +7,9 @@ from wci import wci
from credentials import user, password
import json
from folder_mgr.folder_mgr import get_date, date_has_changed, to_date_str, to_time_str, FolderMgr, create_timestamp
from deep_diff import deep_diff, deepcopy
INTERVAL = 5*60
INTERVAL = 1*60
domains = [
"access",
@@ -52,39 +53,55 @@ def main():
# Init collecting records
f_mgr = FolderMgr()
record_id = 0
record_list = []
_date_last = {}
initial_timestamp = create_timestamp().replace(':', '-').replace('.', '-')
record_list = None
_date_last = None
initial_timestamp = None
vehicle_data_last = None
# Collecting loop
while True:
start = time.time()
_date = get_date()
timestamp = create_timestamp()
# Reset on start up or day change
if date_has_changed(_date_last, _date):
_date_last = _date
record_id = 0
record_list = []
initial_timestamp = create_timestamp().replace(':', '-').replace('.', '-')
vehicle_data_last = {}
try:
data = w.get(endpoint)
record_list.append({'info': {'record_id': record_id, 'timestamp': timestamp}, 'data': data})
print(f"{timestamp}: Retrieve vehicle data")
# Create data difference
vehicle_data = w.get(endpoint)
vehicle_data_diff = deep_diff(vehicle_data, vehicle_data_last)
vehicle_data_last = vehicle_data
# Put vehicle data difference onto record list
record_list.append({'info': {'record_id': record_id, 'timestamp': timestamp}, 'data': vehicle_data_diff})
# Store record list
print(f"{timestamp}: Store record #{record_id:04d}")
folder = f_mgr.update(f"results/{vin}", _date)
filename = f"vehicle_{vin}_{initial_timestamp}.json"
_path = os.path.join(folder, filename)
with open(_path, "w") as fp:
json.dump(record_list, fp, indent=4)
record_id += 1
except Exception as e:
print(f"Exception: {e}")
_date_last = _date
record_id += 1
# wait until next loop
stop = time.time()
diff = int(stop - start)
time.sleep(max(0, INTERVAL - diff))
diff = stop - start
time_to_wait = INTERVAL - diff
if time_to_wait < 0:
time_to_wait = 0
time.sleep(time_to_wait)
if __name__ == '__main__':