Implement custom to_json function for CaptureMetadata
This commit is contained in:
parent
2681ee9a8e
commit
cb1ad33cae
@ -2,7 +2,7 @@ import json
|
|||||||
import uuid
|
import uuid
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional, Any
|
from typing import Optional, Any, OrderedDict
|
||||||
|
|
||||||
from iottb.definitions import ReturnCodes, CAPTURE_METADATA_FILE
|
from iottb.definitions import ReturnCodes, CAPTURE_METADATA_FILE
|
||||||
from iottb.models.device_metadata_model import DeviceMetadata
|
from iottb.models.device_metadata_model import DeviceMetadata
|
||||||
@ -67,5 +67,36 @@ class CaptureMetadata:
|
|||||||
json.dump(metadata, file)
|
json.dump(metadata, file)
|
||||||
return ReturnCodes.SUCCESS
|
return ReturnCodes.SUCCESS
|
||||||
|
|
||||||
def to_json(self, indent):
|
def to_json(self, indent=2):
|
||||||
pass
|
# TODO: Where to validate data?
|
||||||
|
logger.info(f"Converting CaptureMetadata to JSON")
|
||||||
|
data = {}
|
||||||
|
|
||||||
|
# List of fields from CaptureData class, if fields[key]==True, then it is a required field
|
||||||
|
fields = {
|
||||||
|
'capture_id': True, #
|
||||||
|
'device_id': True,
|
||||||
|
'capture_dir': True,
|
||||||
|
'capture_file': False,
|
||||||
|
'capture_date': False,
|
||||||
|
'start_time': True,
|
||||||
|
'stop_time': True,
|
||||||
|
'packet_count': False,
|
||||||
|
'pcap_filter': False,
|
||||||
|
'tcpdump_command': False,
|
||||||
|
'interface': False,
|
||||||
|
'device_ip_address': False,
|
||||||
|
'device_mac_address': False,
|
||||||
|
'app': False,
|
||||||
|
'app_version': False,
|
||||||
|
'firmware_version': False
|
||||||
|
}
|
||||||
|
|
||||||
|
for field, is_mandatory in fields.items():
|
||||||
|
value = getattr(self, field, None)
|
||||||
|
if value not in [None, ""] or is_mandatory:
|
||||||
|
if value in [None, ""] and is_mandatory:
|
||||||
|
raise ValueError(f"Field {field} is required and cannot be empty.")
|
||||||
|
data[field] = str(value) if not isinstance(value, str) else value
|
||||||
|
logger.debug(f"Capture metadata: {data}")
|
||||||
|
return json.dumps(data, indent=indent)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user