Always pause/resume speech detection on backend level

This commit is contained in:
Fabio Manganiello 2020-01-19 16:32:41 +01:00
parent 6c797b0ad9
commit f38121d176
4 changed files with 17 additions and 58 deletions

View file

@ -1,6 +1,7 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from platypush.plugins import Plugin from platypush.context import get_backend
from platypush.plugins import Plugin, action
class AssistantPlugin(ABC, Plugin): class AssistantPlugin(ABC, Plugin):
@ -22,26 +23,32 @@ class AssistantPlugin(ABC, Plugin):
""" """
raise NotImplementedError raise NotImplementedError
@abstractmethod def _get_assistant(self):
def pause_detection(self, *args, **kwargs): return get_backend('assistant.snowboy')
@action
def pause_detection(self):
""" """
Put the assistant on pause. No new conversation events will be triggered. Put the assistant on pause. No new conversation events will be triggered.
""" """
raise NotImplementedError assistant = self._get_assistant()
assistant.pause_detection()
@abstractmethod @action
def resume_detection(self, *args, **kwargs): def resume_detection(self):
""" """
Resume the assistant hotword detection from a paused state. Resume the assistant hotword detection from a paused state.
""" """
raise NotImplementedError assistant = self._get_assistant()
assistant.resume_detection()
@abstractmethod @action
def is_detecting(self) -> bool: def is_detecting(self) -> bool:
""" """
:return: True if the asistant is detecting, False otherwise. :return: True if the asistant is detecting, False otherwise.
""" """
raise NotImplementedError assistant = self._get_assistant()
return assistant.is_detecting()
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et:

View file

@ -3,7 +3,6 @@
""" """
import os import os
import threading
from platypush.context import get_bus from platypush.context import get_bus
from platypush.plugins import action from platypush.plugins import action
@ -71,7 +70,6 @@ class AssistantEchoPlugin(AssistantPlugin):
self.audio = Audio(device_name=audio_device) self.audio = Audio(device_name=audio_device)
self.alexa = Alexa(avs_config_file, audio_player=audio_player) self.alexa = Alexa(avs_config_file, audio_player=audio_player)
self._ready = False self._ready = False
self._detection_paused = threading.Event()
self.alexa.state_listener.on_ready = self._on_ready() self.alexa.state_listener.on_ready = self._on_ready()
self.alexa.state_listener.on_listening = self._on_listening() self.alexa.state_listener.on_listening = self._on_listening()
@ -128,17 +126,5 @@ class AssistantEchoPlugin(AssistantPlugin):
self.audio.stop() self.audio.stop()
self._on_finished()() self._on_finished()()
@action
def pause_detection(self):
self._detection_paused.set()
@action
def resume_detection(self):
self._detection_paused.clear()
@action
def is_detecting(self) -> bool:
return not self._detection_paused.is_set()
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et:

View file

@ -17,8 +17,7 @@ class AssistantGooglePlugin(AssistantPlugin):
def __init__(self, **kwargs): def __init__(self, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
@staticmethod def _get_assistant(self):
def _get_assistant():
return get_backend('assistant.google') return get_backend('assistant.google')
@action @action
@ -37,20 +36,5 @@ class AssistantGooglePlugin(AssistantPlugin):
assistant = self._get_assistant() assistant = self._get_assistant()
assistant.stop_conversation() assistant.stop_conversation()
@action
def pause_detection(self):
assistant = self._get_assistant()
assistant.pause_detection()
@action
def resume_detection(self):
assistant = self._get_assistant()
assistant.resume_detection()
@action
def is_detecting(self) -> bool:
assistant = self._get_assistant()
return assistant.is_detecting()
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et:

View file

@ -4,7 +4,6 @@
import json import json
import os import os
import threading
from platypush.context import get_bus from platypush.context import get_bus
from platypush.message.event.assistant import ConversationStartEvent, \ from platypush.message.event.assistant import ConversationStartEvent, \
@ -86,7 +85,6 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin):
self.play_response = play_response self.play_response = play_response
self.assistant = None self.assistant = None
self.interactions = [] self.interactions = []
self._detection_paused = threading.Event()
with open(self.device_config) as f: with open(self.device_config) as f:
device = json.load(f) device = json.load(f)
@ -221,10 +219,6 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin):
from platypush.plugins.assistant.google.lib import SampleAssistant from platypush.plugins.assistant.google.lib import SampleAssistant
if not self.is_detecting():
self.logger.info('Conversation start event received but detection is currently paused')
return
if not language: if not language:
language = self.language language = self.language
@ -279,17 +273,5 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin):
device_model_id=self.device_model_id, device_model_id=self.device_model_id,
on=on)) on=on))
@action
def pause_detection(self):
self._detection_paused.set()
@action
def resume_detection(self):
self._detection_paused.clear()
@action
def is_detecting(self) -> bool:
return not self._detection_paused.is_set()
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et: