2024-bsc-sebastian-lenzlinger/archive/device_metadata_utils.py
2024-06-28 23:49:16 +02:00

52 lines
1.7 KiB
Python

import json
from datetime import datetime
from pathlib import Path
from iottb.definitions import ReturnCodes
def update_firmware_version(version: str, file_path: Path):
assert file_path.is_file()
with file_path.open('r') as file:
metadata = json.load(file)
metadata['device_firmware_version'] = version
metadata['date_updated'] = datetime.now().strftime('%d-%m-%YT%H:%M:%S').lower()
with file_path.open('w') as file:
json.dump(metadata, file)
return ReturnCodes.SUCCESS
def add_capture_file_reference(capture_file_reference: str, file_path: Path):
assert file_path.is_file()
with file_path.open('r') as file:
metadata = json.load(file)
metadata['capture_files'] = capture_file_reference
metadata['date_updated'] = datetime.now().strftime('%d-%m-%YT%H:%M:%S').lower()
with file_path.open('w') as file:
json.dump(metadata, file)
return ReturnCodes.SUCCESS
def update_device_serial_number(device_id: str, file_path: Path):
assert file_path.is_file()
with file_path.open('r') as file:
metadata = json.load(file)
metadata['device_id'] = device_id
metadata['date_updated'] = datetime.now().strftime('%d-%m-%YT%H:%M:%S').lower()
with file_path.open('w') as file:
json.dump(metadata, file)
return ReturnCodes.SUCCESS
def update_device_type(device_type: str, file_path: Path):
assert file_path.is_file()
with file_path.open('r') as file:
metadata = json.load(file)
metadata['device_type'] = device_type
metadata['date_updated'] = datetime.now().strftime('%d-%m-%YT%H:%M:%S').lower()
with file_path.open('w') as file:
json.dump(metadata, file)
return ReturnCodes.SUCCESS