text stringlengths 1 93.6k |
|---|
}
|
return context_alerts
|
def start_alert(self, token):
|
""" Called as a thread when the alarm is started.
|
:param token: token for active alarm
|
"""
|
print("Alarm started!")
|
# This function is called by the scheduler
|
# Be sure to delete the alert dictionary when done
|
self.alerts[token]['is_active'] = True
|
# Send alert started to alexa
|
stream_id = self.alexa_device.alexa.send_event_alert_name('AlertStarted', token)
|
self.alexa_device.alexa.get_and_process_response(stream_id)
|
# If foreground
|
if True:
|
# Play in foreground for 30 seconds, unless stopped
|
self.audio.play_wav('files/alarm.wav',
|
timeout=30, stop_event=self.alerts[token]['stop_event'], repeat=True)
|
# Send status to alexa
|
stream_id = self.alexa_device.alexa.send_event_alert_name('AlertEnteredForeground', token)
|
self.alexa_device.alexa.get_and_process_response(stream_id)
|
else:
|
# Play quietly in background (or not at all
|
# Send status to alexa
|
stream_id = self.alexa_device.alexa.send_event_alert_name('AlertEnteredBackground', token)
|
self.alexa_device.alexa.get_and_process_response(stream_id)
|
# If alert still exists (would exist if alarm is not cancelled)
|
if token in self.alerts:
|
self.delete_alert(token)
|
class AlexaDevice:
|
""" This object is the AlexaDevice. It uses the AlexaCommunication and AlexaAudio object. The goal is to provide a
|
highly abstract yet simple interface for Amazon's Alexa Voice Service (AVS).
|
"""
|
def __init__(self, alexa_config):
|
""" Initialize the AlexaDevice using the config dictionary. The config dictionary must containing the
|
Client_ID, Client_Secret, and refresh_token.
|
:param alexa_config: config dictionary specific to the device
|
"""
|
self.alexa_audio_instance = alexa_audio.AlexaAudio()
|
self.alarm_manager = AlarmManager(self.alexa_audio_instance)
|
self.config = alexa_config
|
self.alexa = None
|
self.device_stop_event = threading.Event()
|
self.device_thread = threading.Thread(target=self.device_thread_function)
|
self.device_thread.start()
|
def device_thread_function(self):
|
""" The main thread that waits until the the device is closed. It contains the AlexaConnection object and
|
starts any necessary threads for user input.
|
Eventually this function will incorporate any device specific functionality.
|
"""
|
# TODO make sure this function can be called again, not during instantiation
|
# Start connection and save
|
self.alexa = alexa_communication.AlexaConnection(self.config, context_handle=self.get_context,
|
process_response_handle=self.process_response)
|
self.alarm_manager.set_alexa_device(self)
|
# Connection loop
|
while not self.device_stop_event.is_set():
|
# Do any device related things here
|
time.sleep(0.1)
|
pass
|
# When complete (stop event is same as user_input_thread
|
# Close the alexa connection and set stop event
|
self.alexa.close()
|
self.device_stop_event.set()
|
print("Closing Thread")
|
# TODO If anything went wrong, and stop event is not set, start new thread automatically
|
def user_initiate_audio(self):
|
raw_audio = self.alexa_audio_instance.get_audio()
|
if raw_audio is None:
|
return
|
# TODO make it so the response can be interrupted by user if desired (maybe start a thread)
|
stream_id = self.alexa.start_recognize_event(raw_audio)
|
self.alexa.get_and_process_response(stream_id)
|
def get_context(self):
|
""" Returns the current context of the AlexaDevice.
|
See https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/reference/context for more
|
information.
|
:return: context dictionary
|
"""
|
# TODO eventually make this dynamic and actually reflect the device's state
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.