text
stringlengths
1
93.6k
class AlarmManager:
""" This object manages all alarms and timers sent via the Alerts interface.
"""
def __init__(self, audio):
""" Initializes the AlarmManager object. Requires an AlexaAudio object to sound alarms. The
AlexaCommunication object must be specified in a separate function call.
:param audio: AlexaAudio object instance
"""
self.alexa_device = None
self.audio = audio
self.alerts = {}
def set_alexa_device(self, alexa_device):
""" Set's the current AlexaDevice object.
:param alexa_device: AlexaDevice object
"""
self.alexa_device = alexa_device
def set_alert(self, token, alert_type, scheduled_time):
""" Called when a new alarm is to be added (from SetAlert directive).
:param token: token for the alarm
:param alert_type: alert type from the API
:param scheduled_time: scheduled time (UTC time as ISO string)
:return: boolean indicating success or failure
"""
try:
s_time = helper.get_timestamp_from_iso(scheduled_time)
time_difference = s_time-time.time()
print(time_difference)
timer_thread = threading.Timer(time_difference, self.start_alert, args=(token,))
timer_thread.start()
stop_event = threading.Event()
self.alerts[token] = {
'type': alert_type,
'scheduled_time': scheduled_time,
'timer_thread': timer_thread,
'stop_event': stop_event,
'is_active': False
}
print("Alarm set successfully.")
except:
print("Error setting alarm")
return False
return True
def delete_alert(self, token):
""" Called when an alarm is to be deleted (from DeleteAlert directive).
:param token: token for the alarm
:param alert_type: alert type from the API
:param scheduled_time: scheduled time (UTC time as ISO string)
:return: boolean indicating success or failure
"""
try:
self.alerts[token]['timer_thread'].cancel()
if self.alerts[token]['is_active']:
print("Stopping alarm")
self.alerts[token]['stop_event'].set()
stream_id = self.alexa_device.alexa.send_event_alert_name('AlertStopped', token)
# TODO combine get_and_process_reponse with alexa_send_event
self.alexa_device.alexa.get_and_process_response(stream_id)
del self.alerts[token]
print("Alarm deleted")
return True
except:
traceback.print_exc()
print("Error deleting alarm")
return False
def get_alarm_context(self):
""" Get the alert context dictionary.
:return: dictionary containing alert context
"""
tokens = self.alerts.keys()
all_alerts = []
active_alerts = []
for token in tokens:
alert = {
'token': token,
'type': self.alerts[token]['type'],
'scheduledTime': self.alerts[token]['scheduled_time']
}
all_alerts.append(alert)
if self.alerts[token]['is_active']:
active_alerts.append(alert)
context_alerts = {
"header": {
"namespace": "Alerts",
"name": "AlertsState"
},
"payload": {
"allAlerts": all_alerts,
"activeAlerts": active_alerts
}