Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions TUSS4470_shield_002/web/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ async def lifespan(app: FastAPI):
async def update_settings(new_settings: Settings):
settings = Settings.model_validate(
{
**app.state.settings.model_dump(exclude_none=True, exclude_unset=True, exclude_defaults=True),
**new_settings.model_dump(exclude_none=True, exclude_unset=True, exclude_defaults=True),
**app.state.settings.model_dump(exclude_none=True, exclude_unset=True),
**new_settings.model_dump(exclude_none=True, exclude_unset=True),
}
)

Expand Down
35 changes: 33 additions & 2 deletions TUSS4470_shield_002/web/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,39 @@ def __init__(

def update_settings(self, new_settings):
log.info("EchoReader updating settings...")
self.settings = new_settings
self._restart_event.set() # Signal restart
restart_fields = {
"connection_type",
"udp_port",
"serial_port",
"baud_rate",
"num_samples",
}

field_names = new_settings.model_fields.keys()
new_values = {key: getattr(new_settings, key) for key in field_names}

restart_needed = False
if self.settings is None:
restart_needed = True
changed_fields = restart_fields
else:
changed_fields = {
key for key, value in new_values.items() if getattr(self.settings, key) != value
}
restart_needed = bool(restart_fields & changed_fields)

if self.settings is None:
self.settings = new_settings
else:
for key, value in new_values.items():
setattr(self.settings, key, value)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just overwrite the settings object if you are going to overwrite all of it's values?


if restart_needed:
log.info(
"Restart requested due to changes in: %s",
", ".join(sorted(restart_fields & changed_fields)),
)
self._restart_event.set() # Signal restart
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess what you are trying to do here is just make sure we only update the connection if the relevant settings change?

Could it be simplified like this:

restart_needed = (
    self.settings is None or \
    any([
        getattr(self.settings, k) != getattr(new_settings, k)
        for k in restart_fields
    ])
)

self.settings = new_settings
if restart_needed:
    self._restart_event.set()


def __enter__(self):
self._task = asyncio.create_task(self.run_forever())
Expand Down