Format python tool.

This commit is contained in:
Victor Chang
2024-06-02 22:03:47 -07:00
parent d108b4f8d1
commit cbe6e974fc

View File

@@ -4,11 +4,12 @@ from collections import defaultdict
import re import re
from datetime import datetime from datetime import datetime
def parse_sniffed_packet(csv_data): def parse_sniffed_packet(csv_data):
# Regular expression pattern for the timestamp # Regular expression pattern for the timestamp
pattern = r'("\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}",)' pattern = r'("\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}",)'
# Split the data using the pattern, keeping the delimiters # Split the data using the pattern, keeping the delimiters
entries = re.split(pattern, csv_data)[1:] # first element is always empty entries = re.split(pattern, csv_data)[1:] # first element is always empty
parsed_data = {} parsed_data = {}
for i in range(0, len(entries), 2): for i in range(0, len(entries), 2):
@@ -16,7 +17,7 @@ def parse_sniffed_packet(csv_data):
value = entries[i + 1] value = entries[i + 1]
# Parse the timestamp string into a datetime object # Parse the timestamp string into a datetime object
timestamp = datetime.strptime(timestamp_str, '%Y-%m-%d %H:%M:%S') timestamp = datetime.strptime(timestamp_str, "%Y-%m-%d %H:%M:%S")
if timestamp in parsed_data: if timestamp in parsed_data:
# Concatenate value if timestamp already exists # Concatenate value if timestamp already exists
@@ -27,6 +28,7 @@ def parse_sniffed_packet(csv_data):
return parsed_data return parsed_data
def parse_emporia(csv_data): def parse_emporia(csv_data):
parsed_data = {} parsed_data = {}
last_value = None last_value = None
@@ -46,7 +48,7 @@ def parse_emporia(csv_data):
# Parse the timestamp string into a datetime object # Parse the timestamp string into a datetime object
try: try:
timestamp = datetime.strptime(timestamp_str, '%m/%d/%Y %H:%M:%S') timestamp = datetime.strptime(timestamp_str, "%m/%d/%Y %H:%M:%S")
except ValueError: except ValueError:
continue continue
@@ -56,6 +58,7 @@ def parse_emporia(csv_data):
return parsed_data return parsed_data
def find_nearest_before(from_emporia, sniffed): def find_nearest_before(from_emporia, sniffed):
result = [] result = []
for keyA, valueA in from_emporia.items(): for keyA, valueA in from_emporia.items():
@@ -75,6 +78,7 @@ def find_nearest_before(from_emporia, sniffed):
result.append((nearest_time, valueA, nearest_value)) result.append((nearest_time, valueA, nearest_value))
return result return result
def find_nearest_after(sniffed, from_emporia): def find_nearest_after(sniffed, from_emporia):
result = [] result = []
for keyA, valueA in sniffed.items(): for keyA, valueA in sniffed.items():
@@ -93,16 +97,18 @@ def find_nearest_after(sniffed, from_emporia):
if nearest_time: if nearest_time:
result.append((keyA, valueA, nearest_value)) result.append((keyA, valueA, nearest_value))
else: else:
print('have time', keyA, 'that doesnt have a nearest value') print("have time", keyA, "that doesnt have a nearest value")
return result return result
def swap_endianness(hex_string): def swap_endianness(hex_string):
# Split the string into bytes (two characters each) # Split the string into bytes (two characters each)
bytes_list = [hex_string[i:i+2] for i in range(0, len(hex_string), 2)] bytes_list = [hex_string[i : i + 2] for i in range(0, len(hex_string), 2)]
# Reverse the list of bytes and join them back into a string # Reverse the list of bytes and join them back into a string
reversed_hex = ''.join(reversed(bytes_list)) reversed_hex = "".join(reversed(bytes_list))
return reversed_hex return reversed_hex
def hex_to_decimal(hex_string): def hex_to_decimal(hex_string):
try: try:
# First, swap the endianness # First, swap the endianness
@@ -112,14 +118,20 @@ def hex_to_decimal(hex_string):
except ValueError: except ValueError:
return "Invalid hexadecimal number" return "Invalid hexadecimal number"
def read_file(file_path): def read_file(file_path):
with open(file_path, 'r') as file: with open(file_path, "r") as file:
return file.read() return file.read()
def main(): def main():
parser = argparse.ArgumentParser(description="Merge the data.") parser = argparse.ArgumentParser(description="Merge the data.")
parser.add_argument("--sniffed_data", required=True, help="Filepath to the sniffed UART traffic.") parser.add_argument(
parser.add_argument("--emporia_csv", required=True, help="Filepath to the Emporia 1SEC CSV.") "--sniffed_data", required=True, help="Filepath to the sniffed UART traffic."
)
parser.add_argument(
"--emporia_csv", required=True, help="Filepath to the Emporia 1SEC CSV."
)
args = parser.parse_args() args = parser.parse_args()
@@ -136,7 +148,8 @@ def main():
hex3_to_watts[hex3].append((watts, packet)) hex3_to_watts[hex3].append((watts, packet))
for hex3, watts in hex3_to_watts.items(): for hex3, watts in hex3_to_watts.items():
print('Hex3:', hex3, 'Calculated W:', hex_to_decimal(hex3), 'Pairing:', watts) print("Hex3:", hex3, "Calculated W:", hex_to_decimal(hex3), "Pairing:", watts)
if __name__ == "__main__": if __name__ == "__main__":
main() main()