diff --git a/communication.py b/communication.py index 7d804af..f435703 100644 --- a/communication.py +++ b/communication.py @@ -129,7 +129,7 @@ def get_current_location(dictionary={}): # Ping base station def ping_basestation( - address="10.46.0.1", dictionary={} + address="10.45.0.1", dictionary={} ): # raspberry pi address 192.168.0.29, host1 is 0.30 try: result = subprocess.run( @@ -157,13 +157,13 @@ def collect_iperf( duration=10, is_client=True, ): - if not is_client: - commands = [["iperf3", "-s"]] - else: + if is_client: commands = [ ["iperf3", "-c", server_ip, "-t", str(duration)], ["iperf3", "-c", server_ip, "-t", str(duration), "-R"], ] + else: + commands = [["iperf3", "-s"]] for command in commands: try: @@ -191,27 +191,33 @@ def collect_iperf( except: end_distance = None - # Look for final sender and receiver bitrates (usually in summary lines) matches = re.findall( - r"\[\s*\d+\]\s+\d+\.\d+\-\d+\.\d+\s+sec\s+[\d.]+\s+\w+Bytes\s+([\d.]+)\s+Mbits/sec\s+(\S+)?", + r"\[\s*\d+\]\s+\d+\.\d+\-\d+\.\d+\s+sec\s+[\d.]+\s+\w+Bytes\s+([\d.]+)\s+(Kbits/sec|Mbits/sec)", output, ) - if len(matches) >= 1: - # Take the last throughput entry - last_entry = matches[-1] - sender_bitrate = float(last_entry[0]) + def normalize(value: float, unit: str) -> float: + """Convert all rates to Mbits/sec.""" + if unit.lower().startswith("kbit"): + return value / 1000.0 + return value - # Sometimes iperf omits receiver or sender lines depending on direction - # Try to find both separately: + if len(matches) >= 1: + # Normalize the last entries + last_value, last_unit = matches[-1] + sender_bitrate = normalize(float(last_value), last_unit) + + # Try to differentiate sender vs receiver receiver_match = re.search(r"receiver", output, re.IGNORECASE) sender_match = re.search(r"sender", output, re.IGNORECASE) if receiver_match and sender_match and len(matches) >= 2: - receiver_bitrate = float(matches[-1][0]) - sender_bitrate = float(matches[-2][0]) + recv_value, recv_unit = matches[-1] + send_value, send_unit = matches[-2] + receiver_bitrate = normalize(float(recv_value), recv_unit) + sender_bitrate = normalize(float(send_value), send_unit) else: - receiver_bitrate = sender_bitrate # fallback: assume same + receiver_bitrate = sender_bitrate else: bitrates = re.findall(r"(\d+\.\d+) Mbits/sec", output) @@ -227,10 +233,6 @@ def collect_iperf( "iperf_full": output, "sender_bitrate": sender_bitrate, "receiver_bitrate": receiver_bitrate, - "note": ( - "avgs are calculated with the assumption " - "that all value are in Mbits/sec" - ), "start_distance": start_distance, "end_distance": end_distance, "type": test_type,