neurotransmitter.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import logging
  2. from core.NeuronModule import NeuronModule, MissingParameterException
  3. logging.basicConfig()
  4. logger = logging.getLogger("kalliope")
  5. class Neurotransmitter(NeuronModule):
  6. def __init__(self, **kwargs):
  7. super(Neurotransmitter, self).__init__(**kwargs)
  8. # get links
  9. self.links = kwargs.get('links', None)
  10. self.default = kwargs.get('default', None)
  11. # do some check
  12. if self._links_content_ok():
  13. # the brain seems fine, we call the stt to get an audio
  14. self.get_audio_from_stt(callback=self.callback)
  15. def callback(self, audio):
  16. """
  17. The callback used by the STT module to get the linked synapse
  18. :param audio: the audio to play by STT
  19. """
  20. logger.debug("Neurotransmitter, receiver audio from STT: %s" % audio)
  21. # print self.links
  22. # set a bool to know if we have found a valid answer
  23. found = False
  24. for el in self.links:
  25. if audio in el["answers"]:
  26. found = True
  27. self.run_synapse_ny_name(el["synapse"])
  28. # we don't need to check to rest of answer
  29. break
  30. if not found:
  31. # the answer do not correspond to any answer. We run the default synapse
  32. self.run_synapse_ny_name(self.default)
  33. def _links_content_ok(self):
  34. """
  35. Check if received links are ok to perform operations
  36. :return: true if links are ok, raise an exception otherwise
  37. .. raises:: MissingParameterException
  38. """
  39. if self.links is None:
  40. raise MissingParameterException("links parameter required and must contain at least one link")
  41. if self.default is None:
  42. raise MissingParameterException("default parameter is required and must contain a valid synapse name")
  43. for el in self.links:
  44. if "synapse" not in el:
  45. raise MissingParameterException("Links must contain a synapse name: %s" % el)
  46. if "answers" not in el:
  47. raise MissingParameterException("Links must contain answers: %s" % el)
  48. return True