def on_message(self, client, userdata, msg): """Received message from MQTT broker.""" try: if not msg.topic.endswith("/audioFrame"): _LOGGER.debug("Received %s byte(s) on %s", len(msg.payload), msg.topic) # Check enable/disable messages if msg.topic == AsrToggleOn.topic(): json_payload = json.loads(msg.payload or "{}") if self._check_siteId(json_payload): self.enabled = True _LOGGER.debug("Enabled") elif msg.topic == AsrToggleOn.topic(): json_payload = json.loads(msg.payload or "{}") if self._check_siteId(json_payload): self.enabled = False _LOGGER.debug("Disabled") if not self.enabled: # Disabled return if AudioFrame.is_topic(msg.topic): # Check siteId if (not self.audioframe_topics) or ( msg.topic in self.audioframe_topics ): # Add to all active sessions if self.first_audio: _LOGGER.debug("Receiving audio") self.first_audio = False siteId = AudioFrame.get_siteId(msg.topic) for result in self.handle_audio_frame(msg.payload, siteId=siteId): self.publish(result) elif msg.topic == AsrStartListening.topic(): # hermes/asr/startListening json_payload = json.loads(msg.payload) if self._check_siteId(json_payload): for result in self.start_listening( AsrStartListening(**json_payload) ): self.publish(result) elif msg.topic == AsrStopListening.topic(): # hermes/asr/stopListening json_payload = json.loads(msg.payload) if self._check_siteId(json_payload): for result in self.stop_listening(AsrStopListening(**json_payload)): self.publish(result) except Exception: _LOGGER.exception("on_message")
def on_connect(self, client, userdata, flags, rc): """Connected to MQTT broker.""" try: topics = [ AsrToggleOn.topic(), AsrToggleOff.topic(), AsrStartListening.topic(), AsrStopListening.topic(), ] if self.audioframe_topics: # Specific siteIds topics.extend(self.audioframe_topics) else: # All siteIds topics.append(AudioFrame.topic(siteId="+")) for topic in topics: self.client.subscribe(topic) _LOGGER.debug("Subscribed to %s", topic) except Exception: _LOGGER.exception("on_connect")
def test_asr_stop_listening(): """Test AsrStopListening.""" assert AsrStopListening.topic() == "hermes/asr/stopListening"