range-testing/relay.py

182 lines
6.1 KiB
Python
Raw Normal View History

M
2025-09-22 10:54:39 -04:00
import os
import threading
import time
from communication import ping_basestation
from functions.at_commands import (
M
2025-09-22 10:54:39 -04:00
get_modem_cops,
get_modem_cpol,
get_modem_creg,
get_modem_csq,
get_modem_qnwcfg,
get_modem_qnwinfo,
get_modem_qspn,
get_modem_rsrp,
get_modem_rsrq,
get_modem_sinr,
set_configs,
)
from functions.helper_functions import save_data_to_json
from functions.location import get_current_location, set_base_location
from iperf.iperf_functions import collect_iperf
class Relay:
def __init__(self):
self.running = False
self.base_location = None
set_modem = (
input(
"Do you want limit the modem to NR5G without roaming? (Not recommended if PLMN is not 001) (y/n )"
)
.strip()
.lower()
)
while set_modem not in ("y", "yes", "n", "no"):
set_modem = input("Your response must be y or n ").strip().lower()
if set_modem in ("y", "yes"):
print("Setting configs...")
set_configs()
self.base_address = (
input("Enter the base station (ground) ip address (Default is 10.46.0.1): ")
.strip()
.lower()
or "10.46.0.1"
)
self.relay_address = (
input("Enter the relay station ip address (Default is 10.45.0.1): ")
.strip()
.lower()
or "10.45.0.1"
)
self.folder_name = (
input("Enter the log folder name (Default is boat_relay_oct_9): ")
.strip()
.lower()
or "boat_relay_oct_9"
)
self.interval = int(
input(
"Enter the time interval (s) in between pings/modem data collection (Default is 5): "
)
.strip()
.lower()
or 5
)
self.iperf_duration = int(
input("Enter the iperf test duration (s) (Default is 5): ").strip().lower()
or 5
M
2025-09-22 10:54:39 -04:00
)
foldername = f"data/{self.folder_name}"
self.filename = foldername + "/test_" + str(int(time.time())) + ".json"
os.makedirs(foldername, exist_ok=True)
def set_location(self):
self.base_location = set_base_location()
save_data_to_json(data=self.base_location, filename=self.filename)
def calculate_ping(
self, dictionary: dict, base_address: str, relay_address: str, name: str
) -> dict:
try:
base_ping = float(dictionary[f"{base_address}_ping_time"])
relay_ping = float(dictionary[f"{relay_address}_ping_time"])
calculated_ping = base_ping - relay_ping
dictionary[f"{name}_c_ping_time"] = calculated_ping
except Exception as e:
dictionary[f"{name}_c_ping error"] = f"{e}"
return dictionary
# Run iPerf test from End to Ground, then End to Relay
def relay_iperf(self) -> None:
collect_iperf(
filename=self.filename,
is_client=True,
server_ip=self.base_address,
stations="eg",
duration=self.iperf_duration,
timeout=(self.iperf_duration + 5),
base_location=self.base_location,
M
2025-09-22 10:54:39 -04:00
)
collect_iperf(
filename=self.filename,
is_client=True,
server_ip=self.relay_address,
stations="er",
duration=self.iperf_duration,
timeout=(self.iperf_duration + 5),
base_location=self.base_location,
M
2025-09-22 10:54:39 -04:00
)
# Collect and send data continuously
def collect_data(self) -> None:
while self.running:
data = {}
data = get_current_location(dictionary=data)
data = get_modem_cops(dictionary=data)
data = get_modem_creg(dictionary=data)
data = get_modem_csq(dictionary=data)
data = get_modem_rsrp(dictionary=data)
data = get_modem_rsrq(dictionary=data)
data = get_modem_sinr(dictionary=data)
data = get_modem_cpol(dictionary=data)
data = get_modem_qnwcfg(dictionary=data)
data = get_modem_qnwinfo(dictionary=data)
data = get_modem_qspn(dictionary=data)
data = ping_basestation(
dictionary=data, name="eg", address=self.base_address
) # edge to ground
data = ping_basestation(
dictionary=data, name="er", address=self.relay_address
) # edge to relay
data = self.calculate_ping(
dictionary=data,
base_address="eg",
relay_address="er",
name="rg",
) # relay to ground
data["timestamp"] = time.time()
# Send to server
print(f"\nPing Time: {data.get('ping_time')}")
print(
f"RSRP: PRX: {data.get('RSRP PRX')} DRX: {data.get('RSRP DRX')} "
f"RX2: {data.get('RSRP RX2')} RX3: {data.get('RSRP RX3')}"
)
print(f"Service: {data.get('network information')}")
save_data_to_json(data=data, filename=self.filename)
time.sleep(self.interval)
M
2025-09-22 10:54:39 -04:00
if __name__ == "__main__":
relay = Relay()
M
2025-09-22 10:54:39 -04:00
print(
"Type 'l' to set basestation location, 'b' to begin, "
"'s' to stop, 'i' to run an iperf test, or 'x' to exit:"
)
while True:
command = input("> ").strip().lower()
if command == "b" and not relay.running:
M
2025-09-22 10:54:39 -04:00
print("Starting data collection...")
relay.running = True
threading.Thread(target=relay.collect_data).start()
M
2025-09-22 10:54:39 -04:00
elif command == "l":
relay.set_location()
M
2025-09-22 10:54:39 -04:00
elif command == "i":
threading.Thread(target=relay.relay_iperf).start()
elif command == "s" and relay.running:
M
2025-09-22 10:54:39 -04:00
print("Stopping data collection...")
relay.running = False
M
2025-09-22 10:54:39 -04:00
elif command == "x":
relay.running = False
M
2025-09-22 10:54:39 -04:00
break
elif command not in ["l", "b", "s", "i", "x"]:
print("Invalid command. Type 'l', 'b', 's', 'i', or 'x'.")