From b679847b852303a08dfac5f6b68589618ff95825 Mon Sep 17 00:00:00 2001 From: CrafterTA Date: Sun, 7 Dec 2025 21:13:29 +0700 Subject: [PATCH] fix(sumo): num clients 2 and license error in sumocfg --- fix_xml_files.py | 62 +++++++ src/backend/Dockerfile | 2 +- src/backend/app/sumo_rl/agents/iot_agent.py | 7 +- .../app/sumo_rl/sumo_files/Nga4ThuDuc/.tmp | 1 + .../sumo_files/Nga4ThuDuc/Nga4ThuDuc.add.xml | 3 +- .../sumo_files/Nga4ThuDuc/Nga4ThuDuc.net.xml | 3 +- .../sumo_files/Nga4ThuDuc/Nga4ThuDuc.sumocfg | 12 +- .../sumo_rl/sumo_files/Nga4ThuDuc/e1_0.xml | 10 +- .../sumo_rl/sumo_files/Nga4ThuDuc/e1_1.xml | 10 +- .../sumo_rl/sumo_files/Nga4ThuDuc/e1_2.xml | 10 +- .../sumo_rl/sumo_files/Nga4ThuDuc/e1_3.xml | 10 +- .../sumo_rl/sumo_files/Nga4ThuDuc/e1_4.xml | 10 +- .../sumo_rl/sumo_files/Nga4ThuDuc/e1_5.xml | 10 +- .../sumo_rl/sumo_files/Nga4ThuDuc/e1i_0.xml | 10 +- .../sumo_rl/sumo_files/Nga4ThuDuc/e1i_1.xml | 10 +- .../sumo_rl/sumo_files/Nga4ThuDuc/e1i_2.xml | 165 +----------------- .../sumo_rl/sumo_files/Nga4ThuDuc/e1i_3.xml | 10 +- .../sumo_rl/sumo_files/Nga4ThuDuc/e1i_4.xml | 10 +- .../sumo_rl/sumo_files/Nga4ThuDuc/e2_0.xml | 10 +- .../sumo_rl/sumo_files/Nga4ThuDuc/e2_2.xml | 10 +- .../sumo_files/Nga4ThuDuc/routes.rou.xml | 3 +- .../sumo_files/Nga4ThuDuc/trips.trips.xml | 3 +- 22 files changed, 103 insertions(+), 278 deletions(-) create mode 100644 fix_xml_files.py create mode 100644 src/backend/app/sumo_rl/sumo_files/Nga4ThuDuc/.tmp diff --git a/fix_xml_files.py b/fix_xml_files.py new file mode 100644 index 0000000..321d124 --- /dev/null +++ b/fix_xml_files.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 +"""Fix XML files to have XML declaration at the beginning""" + +import os +import re +from pathlib import Path + +def fix_xml_file(filepath): + """Fix a single XML file""" + with open(filepath, 'r', encoding='utf-8') as f: + content = f.read() + + # Check if file starts with XML declaration + if content.strip().startswith('' + match = re.search(xml_decl_pattern, content) + + if not match: + print(f"✗ {filepath.name} - No XML declaration found") + return False + + # Extract XML declaration + xml_decl = match.group(0) + + # Remove the XML declaration from its current position + content_without_decl = content.replace(xml_decl, '', 1) + + # Remove leading whitespace/newlines but keep copyright comments + content_without_decl = content_without_decl.lstrip('\n') + + # Put XML declaration at the beginning + new_content = xml_decl + '\n' + content_without_decl + + # Write back + with open(filepath, 'w', encoding='utf-8') as f: + f.write(new_content) + + print(f"✓ {filepath.name} - Fixed") + return True + +def main(): + # Path to Nga4ThuDuc directory + nga4_dir = Path('/home/thaianh/Study/GreenWave/src/backend/app/sumo_rl/sumo_files/Nga4ThuDuc') + + print("Fixing XML files in Nga4ThuDuc directory...\n") + + fixed_count = 0 + + # Fix all .xml and .sumocfg files + for pattern in ['*.xml', '*.sumocfg']: + for filepath in nga4_dir.glob(pattern): + if fix_xml_file(filepath): + fixed_count += 1 + + print(f"\nTotal files fixed: {fixed_count}") + +if __name__ == '__main__': + main() diff --git a/src/backend/Dockerfile b/src/backend/Dockerfile index e87ce39..f2691ee 100644 --- a/src/backend/Dockerfile +++ b/src/backend/Dockerfile @@ -8,7 +8,7 @@ FROM python:3.9-slim WORKDIR /app COPY requirements.txt . -RUN pip install --no-cache-dir -r requirements.txt +RUN pip install --no-cache-dir --timeout 600 -r requirements.txt COPY . . diff --git a/src/backend/app/sumo_rl/agents/iot_agent.py b/src/backend/app/sumo_rl/agents/iot_agent.py index bd94313..5faff9e 100644 --- a/src/backend/app/sumo_rl/agents/iot_agent.py +++ b/src/backend/app/sumo_rl/agents/iot_agent.py @@ -266,9 +266,10 @@ def get_status(self) -> Dict[str, Any]: config_file = os.path.join("/app/sumo_files", config_rel_path) # Explicitly construct arguments for subprocess - # num-clients=2 (REMOVED): Caused blocking/connection closed errors. - # We use explicit setOrder() in code to manage synchronization instead. - base_cmd = ["-c", config_file, "--remote-port", "8813"] + # num-clients=2: Allow multiple TraCI connections (IoT Agent + Backend) + # Order 1: Backend (Passive/Observer) + # Order 2: IoT Agent (Active/Driver) + base_cmd = ["-c", config_file, "--remote-port", "8813", "--num-clients", "2"] # Try GUI first if requested and DISPLAY is set success = False diff --git a/src/backend/app/sumo_rl/sumo_files/Nga4ThuDuc/.tmp b/src/backend/app/sumo_rl/sumo_files/Nga4ThuDuc/.tmp new file mode 100644 index 0000000..12bbf74 --- /dev/null +++ b/src/backend/app/sumo_rl/sumo_files/Nga4ThuDuc/.tmp @@ -0,0 +1 @@ + diff --git a/src/backend/app/sumo_rl/sumo_files/Nga4ThuDuc/Nga4ThuDuc.add.xml b/src/backend/app/sumo_rl/sumo_files/Nga4ThuDuc/Nga4ThuDuc.add.xml index ca8099c..26f5476 100644 --- a/src/backend/app/sumo_rl/sumo_files/Nga4ThuDuc/Nga4ThuDuc.add.xml +++ b/src/backend/app/sumo_rl/sumo_files/Nga4ThuDuc/Nga4ThuDuc.add.xml @@ -1,3 +1,4 @@ + - + diff --git a/src/backend/app/sumo_rl/sumo_files/Nga4ThuDuc/Nga4ThuDuc.net.xml b/src/backend/app/sumo_rl/sumo_files/Nga4ThuDuc/Nga4ThuDuc.net.xml index 91fe266..3999171 100644 --- a/src/backend/app/sumo_rl/sumo_files/Nga4ThuDuc/Nga4ThuDuc.net.xml +++ b/src/backend/app/sumo_rl/sumo_files/Nga4ThuDuc/Nga4ThuDuc.net.xml @@ -1,3 +1,4 @@ + - + - - - - + diff --git a/src/backend/app/sumo_rl/sumo_files/Nga4ThuDuc/e1_0.xml b/src/backend/app/sumo_rl/sumo_files/Nga4ThuDuc/e1_0.xml index d9da0b4..5c148a4 100644 --- a/src/backend/app/sumo_rl/sumo_files/Nga4ThuDuc/e1_0.xml +++ b/src/backend/app/sumo_rl/sumo_files/Nga4ThuDuc/e1_0.xml @@ -1,13 +1,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - +