From bb3a39acb915b82279972f290e3cd61c3e82576a Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 12 Jul 2019 18:11:14 +0200 Subject: [PATCH] Better synchronization mechanism between push-to-talk and platypush when an event is matched and no assistant playback should follow up --- platypush/message/event/assistant/__init__.py | 10 ++++------ .../plugins/assistant/google/lib/__init__.py | 6 ++++-- .../plugins/assistant/google/pushtotalk.py | 19 ++++++++++++------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/platypush/message/event/assistant/__init__.py b/platypush/message/event/assistant/__init__.py index 7facd40ec4..79ab7cf006 100644 --- a/platypush/message/event/assistant/__init__.py +++ b/platypush/message/event/assistant/__init__.py @@ -22,7 +22,7 @@ class AssistantEvent(Event): self._assistant = get_plugin('assistant.google.pushtotalk') if not self._assistant: - logger.warning('google.assistant not configured/initialized') + logger.warning('Assistant plugin/backend not configured/initialized') self._assistant = None class ConversationStartEvent(AssistantEvent): @@ -85,13 +85,13 @@ class SpeechRecognizedEvent(AssistantEvent): Event triggered when a speech is recognized """ - def __init__(self, phrase, *args, assistant=None, **kwargs): + def __init__(self, phrase, *args, **kwargs): """ :param phrase: Recognized user phrase :type phrase: str """ - super().__init__(*args, phrase=phrase, assistant=assistant, **kwargs) + super().__init__(*args, phrase=phrase, **kwargs) self.recognized_phrase = phrase.strip().lower() if not self._assistant and assistant: @@ -106,9 +106,7 @@ class SpeechRecognizedEvent(AssistantEvent): result = super().matches_condition(condition) if result.is_match and self._assistant and 'phrase' in condition.args: - if hasattr(self._assistant, 'play_response'): - self._assistant.play_response = False - elif hasattr(self._assistant, 'stop_conversation'): + if self._assistant: self._assistant.stop_conversation() return result diff --git a/platypush/plugins/assistant/google/lib/__init__.py b/platypush/plugins/assistant/google/lib/__init__.py index 0222594451..6c31bb1550 100644 --- a/platypush/plugins/assistant/google/lib/__init__.py +++ b/platypush/plugins/assistant/google/lib/__init__.py @@ -115,6 +115,7 @@ class SampleAssistant(object): if self.on_conversation_start: self.on_conversation_start() + self.play_response = True logging.info('Recording audio request.') def iter_log_assist_requests(): @@ -132,7 +133,6 @@ class SampleAssistant(object): logging.info('End of audio request detected.') logging.info('Stopping recording.') self.conversation_stream.stop_recording() - self.play_response = True if self.detected_speech and self.on_speech_recognized: self.on_speech_recognized(self.detected_speech) @@ -151,8 +151,10 @@ class SampleAssistant(object): self.conversation_stream.start_playback() logging.info('Playing assistant response.') - if self.play_response: + if self.play_response and self.conversation_stream.playing: self.conversation_stream.write(resp.audio_out.audio_data) + elif self.conversation_stream.playing: + self.conversation_stream.stop_playback() if resp.dialog_state_out.conversation_state: conversation_state = resp.dialog_state_out.conversation_state logging.debug('Updating conversation state.') diff --git a/platypush/plugins/assistant/google/pushtotalk.py b/platypush/plugins/assistant/google/pushtotalk.py index 702b2574be..ffe8eeccda 100644 --- a/platypush/plugins/assistant/google/pushtotalk.py +++ b/platypush/plugins/assistant/google/pushtotalk.py @@ -146,21 +146,21 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin): def on_conversation_start(self): """ Conversation start handler """ def handler(): - get_bus().post(ConversationStartEvent(assistant=self.assistant)) + get_bus().post(ConversationStartEvent()) return handler def on_conversation_end(self): """ Conversation end handler """ def handler(with_follow_on_turn): - get_bus().post(ConversationEndEvent(with_follow_on_turn=with_follow_on_turn, assistant=self.assistant)) + get_bus().post(ConversationEndEvent(with_follow_on_turn=with_follow_on_turn)) return handler def on_speech_recognized(self): """ Speech recognized handler """ def handler(phrase): - get_bus().post(SpeechRecognizedEvent(phrase=phrase, assistant=self.assistant)) + get_bus().post(SpeechRecognizedEvent(phrase=phrase)) self.interactions.append({'request': phrase}) return handler @@ -168,14 +168,14 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin): def on_volume_changed(self): """ Volume changed event """ def handler(volume): - get_bus().post(VolumeChangedEvent(volume=volume, assistant=self.assistant)) + get_bus().post(VolumeChangedEvent(volume=volume)) return handler def on_response(self): """ Response handler """ def handler(response): - get_bus().post(ResponseEvent(response_text=response, assistant=self.assistant)) + get_bus().post(ResponseEvent(response_text=response)) if not self.interactions: self.interactions.append({'response': response}) @@ -243,8 +243,13 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin): def stop_conversation(self): """ Stop a conversation """ if self.assistant: - self.conversation_stream.stop_playback() - get_bus().post(ConversationEndEvent(assistant=self.assistant)) + self.assistant.play_response = False + + if self.conversation_stream: + self.conversation_stream.stop_playback() + self.conversation_stream.stop_recording() + + get_bus().post(ConversationEndEvent()) # vim:sw=4:ts=4:et: