Fixed some location and iperf issues, moved normalize function to helper_functions

This commit is contained in:
madrigal 2025-09-19 10:06:54 -04:00
parent ff16bd3a20
commit d4a33c6fcd
2 changed files with 20 additions and 37 deletions

View File

@ -1,5 +1,4 @@
import os
import pprint
import re
import subprocess
import threading
@ -20,7 +19,7 @@ from at_commands import (
get_modem_sinr,
set_configs,
)
from helper_functions import calculate_distance, parse_lat_lon, save_data_to_json
from helper_functions import calculate_distance, normalize, parse_lat_lon, save_data_to_json
from sierra_commands import get_modem_nr_info, get_modem_status
# Globals
@ -73,7 +72,8 @@ def read_gps_data(
gps_data = {}
break
except serial.SerialException as e:
print(f"Serial communication error: {e}")
# print(f"Serial communication error: {e}")
pass
except Exception as e:
print(f"Error: {e}")
@ -110,7 +110,7 @@ def get_current_location(dictionary={}):
if gps_data.get("altitude"):
dictionary["altitude"] = gps_data["altitude"]
if type(base_location) == dict and 'latitude' in base_location:
if base_location and "latitude" in base_location:
dictionary["baseLatitude"] = base_location["latitude"]
dictionary["baseLongitude"] = base_location["longitude"]
if base_location.get("altitude"):
@ -168,8 +168,11 @@ def collect_iperf(
for command in commands:
try:
try:
gps_data = read_gps_data()
start_distance = calculate_distance(base_location, gps_data)
location = read_gps_data()
if base_location:
start_distance = calculate_distance(base_location, location)
else:
start_distance = None
except:
print("Could not collect location")
start_distance = None
@ -185,23 +188,11 @@ def collect_iperf(
print(f"Error running iperf3: {result.stderr}")
return result.stderr
try:
gps_data = read_gps_data()
end_distance = calculate_distance(base_location, gps_data)
except:
end_distance = None
matches = re.findall(
r"\[\s*\d+\]\s+\d+\.\d+\-\d+\.\d+\s+sec\s+[\d.]+\s+\w+Bytes\s+([\d.]+)\s+(Kbits/sec|Mbits/sec)",
output,
)
def normalize(value: float, unit: str) -> float:
"""Convert all rates to Mbits/sec."""
if unit.lower().startswith("kbit"):
return value / 1000.0
return value
if len(matches) >= 1:
# Normalize the last entries
last_value, last_unit = matches[-1]
@ -223,7 +214,7 @@ def collect_iperf(
bitrates = re.findall(r"(\d+\.\d+) Mbits/sec", output)
sender_bitrate = float(bitrates[-2])
receiver_bitrate = float(bitrates[-1])
if "-R" in command:
test_type = "downlink"
else:
@ -234,16 +225,15 @@ def collect_iperf(
"sender_bitrate": sender_bitrate,
"receiver_bitrate": receiver_bitrate,
"start_distance": start_distance,
"end_distance": end_distance,
"location": location,
"type": test_type,
}
print(f"\n{test_type} IPERF complete")
print(f"\n{server_ip} {test_type} IPERF complete")
print(f"IPERF sender bitrate: {sender_bitrate}")
print(f"IPERF receiver bitrate: {receiver_bitrate}")
save_data_to_json(data=data, filename=filename)
time.sleep(0.5)
except Exception as e:
print(f"iPerf Error: {e}")
@ -309,7 +299,7 @@ def collect_sierra_data(filename):
# Main function
def main():
global running
address = '10.46.0.1'
address = "10.46.0.1"
foldername = "data/boat_relay_sept_18"
os.makedirs(foldername, exist_ok=True)
filename = foldername + "/test_" + str(int(time.time())) + ".json"
@ -338,20 +328,6 @@ def main():
elif command == "x":
running = False
break
elif command == "m":
base_location_data = set_base_location()
start_time = time.time()
data = get_current_location()
data["timestamp"] = time.time()
end_time = time.time()
print("Collected Data:")
pprint.pprint(data)
print(" ")
save_data_to_json(data=data, filename=filename)
print(f"Run time: {end_time - start_time}")
elif command not in ["l", "b", "s", "i", "x"]:
print("Invalid command. Type 'l', 'b', 's', 'i', or 'x'.")

View File

@ -110,6 +110,13 @@ def rssi_to_dbm(rssi):
return -113 + 2 * rssi
def normalize(value: float, unit: str) -> float:
"""Convert all rates to Mbits/sec."""
if unit.lower().startswith("kbit"):
return value / 1000.0
return value
# Save data to JSON file
def save_data_to_json(data, filename):
try: