Skip to content

Commit

Permalink
refactor<tel=>veh> move the robot config from tel to veh
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Ahelsamahy committed Oct 21, 2024
1 parent b07a2c8 commit 866b7e2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
38 changes: 0 additions & 38 deletions jetson_runtime/teleop/teleop/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
47 changes: 44 additions & 3 deletions jetson_runtime/vehicles/rover/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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]

Expand All @@ -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()
Expand Down Expand Up @@ -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()):
Expand Down

0 comments on commit 866b7e2

Please sign in to comment.