Files
dragonpilot/selfdrive/controls/lib/alertmanager.py

72 lines
2.6 KiB
Python
Raw Normal View History

2019-08-13 01:36:45 +00:00
from cereal import car, log
2019-06-28 21:11:30 +00:00
from common.realtime import DT_CTRL
2017-02-08 09:51:56 -08:00
from selfdrive.swaglog import cloudlog
2018-11-17 02:08:34 -08:00
from selfdrive.controls.lib.alerts import ALERTS
2017-07-28 01:24:39 -07:00
import copy
2016-12-12 17:47:46 -08:00
2017-09-30 03:07:27 -07:00
2019-06-06 04:38:45 +00:00
AlertSize = log.ControlsState.AlertSize
AlertStatus = log.ControlsState.AlertStatus
2019-08-13 01:36:45 +00:00
VisualAlert = car.CarControl.HUDControl.VisualAlert
AudibleAlert = car.CarControl.HUDControl.AudibleAlert
2016-12-12 17:47:46 -08:00
2019-10-09 18:43:53 +00:00
class AlertManager():
2017-09-30 03:07:27 -07:00
2016-12-12 17:47:46 -08:00
def __init__(self):
self.activealerts = []
2018-11-17 02:08:34 -08:00
self.alerts = {alert.alert_type: alert for alert in ALERTS}
2016-12-12 17:47:46 -08:00
def alertPresent(self):
return len(self.activealerts) > 0
2019-06-28 21:11:30 +00:00
def add(self, frame, alert_type, enabled=True, extra_text_1='', extra_text_2=''):
2017-02-08 09:51:56 -08:00
alert_type = str(alert_type)
2017-12-23 17:15:27 -08:00
added_alert = copy.copy(self.alerts[alert_type])
2018-09-25 00:13:41 -07:00
added_alert.alert_text_1 += extra_text_1
added_alert.alert_text_2 += extra_text_2
2019-06-28 21:11:30 +00:00
added_alert.start_time = frame * DT_CTRL
2016-12-12 17:47:46 -08:00
2017-07-28 01:24:39 -07:00
# if new alert is higher priority, log it
2018-11-17 02:08:34 -08:00
if not self.alertPresent() or added_alert.alert_priority > self.activealerts[0].alert_priority:
cloudlog.event('alert_add', alert_type=alert_type, enabled=enabled)
2017-02-08 09:51:56 -08:00
2017-12-23 17:15:27 -08:00
self.activealerts.append(added_alert)
2018-11-17 02:08:34 -08:00
2018-03-17 00:01:50 -07:00
# sort by priority first and then by start_time
self.activealerts.sort(key=lambda k: (k.alert_priority, k.start_time), reverse=True)
2016-12-12 17:47:46 -08:00
2019-06-28 21:11:30 +00:00
def process_alerts(self, frame):
cur_time = frame * DT_CTRL
2017-12-23 17:15:27 -08:00
# first get rid of all the expired alerts
2018-03-17 00:01:50 -07:00
self.activealerts = [a for a in self.activealerts if a.start_time +
2017-12-23 17:15:27 -08:00
max(a.duration_sound, a.duration_hud_alert, a.duration_text) > cur_time]
2018-11-17 02:08:34 -08:00
current_alert = self.activealerts[0] if self.alertPresent() else None
2017-09-30 03:07:27 -07:00
# start with assuming no alerts
2018-10-21 15:00:31 -07:00
self.alert_type = ""
2017-09-30 03:07:27 -07:00
self.alert_text_1 = ""
self.alert_text_2 = ""
2017-12-23 17:15:27 -08:00
self.alert_status = AlertStatus.normal
self.alert_size = AlertSize.none
2019-08-13 01:36:45 +00:00
self.visual_alert = VisualAlert.none
self.audible_alert = AudibleAlert.none
2018-07-12 18:52:06 -07:00
self.alert_rate = 0.
2016-12-12 17:47:46 -08:00
2018-11-17 02:08:34 -08:00
if current_alert:
self.alert_type = current_alert.alert_type
if current_alert.start_time + current_alert.duration_sound > cur_time:
self.audible_alert = current_alert.audible_alert
2016-12-12 17:47:46 -08:00
2018-11-17 02:08:34 -08:00
if current_alert.start_time + current_alert.duration_hud_alert > cur_time:
self.visual_alert = current_alert.visual_alert
2016-12-12 17:47:46 -08:00
2018-11-17 02:08:34 -08:00
if current_alert.start_time + current_alert.duration_text > cur_time:
self.alert_text_1 = current_alert.alert_text_1
self.alert_text_2 = current_alert.alert_text_2
self.alert_status = current_alert.alert_status
self.alert_size = current_alert.alert_size
self.alert_rate = current_alert.alert_rate