From 866b7e2999f729d538719bd0bff83ff03093a5b6 Mon Sep 17 00:00:00 2001 From: Ahmed Mahfouz Date: Mon, 21 Oct 2024 09:05:12 +0200 Subject: [PATCH] refactorveh> move the robot config from tel to veh When I was working on this feature previously, VEH had python 2.7. Later along the dev process, I made a new docker image with a higher python image. As VEH is dealing with the configuration file, it makes sense to have it also doing the robot configuration file and populate it --- jetson_runtime/teleop/teleop/app.py | 38 ---------------------- jetson_runtime/vehicles/rover/app.py | 47 ++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 41 deletions(-) diff --git a/jetson_runtime/teleop/teleop/app.py b/jetson_runtime/teleop/teleop/app.py index c67126e0..1ef8403e 100644 --- a/jetson_runtime/teleop/teleop/app.py +++ b/jetson_runtime/teleop/teleop/app.py @@ -88,44 +88,6 @@ def __check_configuration_files(self): if self._robot_config_file is None or self._user_config_file is None: logger.info("Warning: Not all config files were found") - def populate_robot_config(self): - """Add mac address, SSID, and IP for current robot in robot_config file""" - # It is done here because vehicle service doesn't support any communication with the router (low py version 2.7) - config = configparser.ConfigParser() - config.read(self._robot_config_file) - - # Check how many segments are present - segments = [s for s in config.sections() if s.startswith("segment_")] - if len(segments) > 1: - logger.info("Part of robot. Nothing to do.") - return - - if len(segments) == 1: - segment = segments[0] - - # Fetch new values - new_values = { - "mac.address": self._router.fetch_router_mac(), - "wifi.name": self._router.fetch_ssid(), - "ip.number": self._nano.get_ip_address(), - } - - updated = False - for key, new_value in new_values.items(): - # Get current value - current_value = config.get(segment, key, fallback="") - - if new_value != current_value: - config[segment][key] = new_value - logger.info(f"Changed value of {key} with {new_value}") - updated = True - - # Write the updated configuration back to the file if there were updates - if updated: - with open(self._robot_config_file, "w") as configfile: - config.write(configfile) - else: - logger.info("No changes made to the robot configuration.") def _config(self): parser = SafeConfigParser() diff --git a/jetson_runtime/vehicles/rover/app.py b/jetson_runtime/vehicles/rover/app.py index 0b7a4d2f..409f2cdb 100644 --- a/jetson_runtime/vehicles/rover/app.py +++ b/jetson_runtime/vehicles/rover/app.py @@ -12,6 +12,7 @@ from BYODR_utils.common.location import GeoTracker from BYODR_utils.common.option import hash_dict, parse_option from BYODR_utils.JETSON_specific.utilities import Nano +from BYODR_utils.common.ssh import Router from configparser import ConfigParser as SafeConfigParser from core import ConfigurableImageGstSource, GpsPollerThreadSNMP, PTZCamera @@ -207,6 +208,9 @@ def step(self, c_pilot, c_teleop): class ConfigFiles: def __init__(self, config_dir): self._config_dir = config_dir + self._user_config_file = None + self._robot_config_file = None + self._router = Router() self.__set_parsers() def __set_parsers(self): @@ -253,9 +257,7 @@ def _verify_and_add_missing_keys(self, ini_file, template_file): config.write(configfile) def change_segment_config(self): - """Change the ips in the config file the segment is using them. - It will count on the ip of the nano""" - # Get the local IP address's third octet + """Change the IPs in the segment config file""" ip_address = Nano.get_ip_address() third_octet_new = ip_address.split(".")[2] @@ -264,6 +266,7 @@ def change_segment_config(self): # Regular expression to match IP addresses ip_regex = re.compile(r"(\d+\.\d+\.)(\d+)(\.\d+)") + # TODO: it doesn't need to go through all the config files. it should do it only with the segment config file for file in _candidates: with open(file, "r") as f: content = f.readlines() @@ -294,6 +297,44 @@ def change_segment_config(self): if changes_made_in_file: logger.info("Updated {file} with new ip address") + # def populate_robot_config(self): + # """Add mac address, SSID, and IP for current robot in robot_config file""" + # config = configparser.ConfigParser() + # config.read(self._robot_config_file) + + # # Check how many segments are present + # segments = [s for s in config.sections() if s.startswith("segment_")] + # if len(segments) > 1: + # logger.info("Part of robot. Nothing to do.") + # return + + # if len(segments) == 1: + # segment = segments[0] + + # # Fetch new values + # new_values = { + # "mac.address": self._router.fetch_router_mac(), + # "wifi.name": self._router.fetch_ssid(), + # "ip.number": Nano.get_ip_address(), + # } + + # updated = False + # for key, new_value in new_values.items(): + # # Get current value + # current_value = config.get(segment, key, fallback="") + + # if new_value != current_value: + # config[segment][key] = new_value + # logger.info(f"Changed value of {key} with {new_value}") + # updated = True + + # # Write the updated configuration back to the file if there were updates + # if updated: + # with open(self._robot_config_file, "w") as configfile: + # config.write(configfile) + # else: + # logger.info("No changes made to the robot configuration.") + class RoverApplication(Application): def __init__(self, handler=None, config_dir=os.getcwd()):