AlertManager: reset duration when re-adding an event (#33478)

* fix alert duration when re-adding an alert before it's ended (personality)

* clean up

* messy test

* clean up test

* Revert "clean up test"

This reverts commit e7c0f80cb3.

* better name

* debug

* Revert "debug"

This reverts commit da8d561445.

* Reapply "clean up test"

This reverts commit a7dba540f7.

* update refs
pull/33559/head
Shane Smiskol 7 months ago committed by GitHub
parent 91e4978984
commit cf50d4ae19
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      selfdrive/selfdrived/alertmanager.py
  2. 21
      selfdrive/selfdrived/tests/test_alertmanager.py
  3. 2
      selfdrive/test/process_replay/ref_commit

@ -27,10 +27,13 @@ class AlertEntry:
alert: Alert | None = None alert: Alert | None = None
start_frame: int = -1 start_frame: int = -1
end_frame: int = -1 end_frame: int = -1
added_frame: int = -1
def active(self, frame: int) -> bool: def active(self, frame: int) -> bool:
return frame <= self.end_frame return frame <= self.end_frame
def just_added(self, frame: int) -> bool:
return self.active(frame) and frame == (self.added_frame + 1)
class AlertManager: class AlertManager:
def __init__(self): def __init__(self):
@ -41,10 +44,11 @@ class AlertManager:
for alert in alerts: for alert in alerts:
entry = self.alerts[alert.alert_type] entry = self.alerts[alert.alert_type]
entry.alert = alert entry.alert = alert
if not entry.active(frame): if not entry.just_added(frame):
entry.start_frame = frame entry.start_frame = frame
min_end_frame = entry.start_frame + alert.duration min_end_frame = entry.start_frame + alert.duration
entry.end_frame = max(frame + 1, min_end_frame) entry.end_frame = max(frame + 1, min_end_frame)
entry.added_frame = frame
def process_alerts(self, frame: int, clear_event_types: set): def process_alerts(self, frame: int, clear_event_types: set):
ae = AlertEntry() ae = AlertEntry()

@ -32,8 +32,27 @@ class TestAlertManager:
for frame in range(duration+10): for frame in range(duration+10):
if frame < add_duration: if frame < add_duration:
AM.add_many(frame, [alert, ]) AM.add_many(frame, [alert, ])
AM.process_alerts(frame, {}) AM.process_alerts(frame, set())
shown = AM.current_alert != EmptyAlert shown = AM.current_alert != EmptyAlert
should_show = frame <= show_duration should_show = frame <= show_duration
assert shown == should_show, f"{frame=} {add_duration=} {duration=}" assert shown == should_show, f"{frame=} {add_duration=} {duration=}"
# check one case:
# - if alert is re-added to AM before it ends the duration is extended
if duration > 1:
AM = AlertManager()
show_duration = duration * 2
for frame in range(duration * 2 + 10):
if frame == 0:
AM.add_many(frame, [alert, ])
if frame == duration:
# add alert one frame before it ends
assert AM.current_alert == alert
AM.add_many(frame, [alert, ])
AM.process_alerts(frame, set())
shown = AM.current_alert != EmptyAlert
should_show = frame <= show_duration
assert shown == should_show, f"{frame=} {duration=}"

@ -1 +1 @@
bfbdd4706abcf5757790526d99d0000644017b1e 36bb37715542677db4a42464128ca10968f2c589
Loading…
Cancel
Save