ソースを参照

create neurotransmitter class

nico 8 年 前
コミット
33f7bb382b

+ 13 - 12
brain.yml

@@ -1,14 +1,15 @@
 ---
 
-  - !include brains/ansible_playbook.yml
-  - !include brains/gmail_checker.yml
-  - !include brains/kill_switch.yml
-  - !include brains/openweathermap.yml
-  - !include brains/push_message.yml
-  - !include brains/say.yml
-  - !include brains/script.yml
-  - !include brains/shell.yml
-  - !include brains/systemdate.yml
-  - !include brains/tasker_autoremote.yml
-  - !include brains/wake_on_lan.yml
-  - !include brains/twitter.yml
+#  - !include brains/ansible_playbook.yml
+#  - !include brains/gmail_checker.yml
+#  - !include brains/kill_switch.yml
+#  - !include brains/openweathermap.yml
+#  - !include brains/push_message.yml
+#  - !include brains/say.yml
+#  - !include brains/script.yml
+#  - !include brains/shell.yml
+#  - !include brains/systemdate.yml
+#  - !include brains/tasker_autoremote.yml
+#  - !include brains/wake_on_lan.yml
+#  - !include brains/twitter.yml
+  - !include brains/neurotransmitter.yml

+ 3 - 2
brains/neurotransmitter.yml

@@ -1,9 +1,10 @@
 ---
   - name: "synapse1"
     neurons:
+#      - say:
+#          message: "aimez vous les frites?"
       - neurotransmitter:
-          question: "do you like fries?"
-          link:
+          links:
             - synapse: "synapse2"
               answers:
                 - "yes"

+ 17 - 13
core/NeuronModule.py

@@ -6,6 +6,7 @@ import random
 import sys
 from jinja2 import Template
 
+from core import OrderListener
 from core.Utils import Utils
 from core.ConfigurationManager import SettingLoader
 
@@ -91,7 +92,7 @@ class NeuronModule(object):
 
         if isinstance(message, list):
             logger.debug("message is list")
-            tts_message = self._get_message_from_list(message)
+            tts_message = random.choice(message)
 
         if isinstance(message, dict):
             logger.debug("message is dict")
@@ -99,7 +100,7 @@ class NeuronModule(object):
 
         if tts_message is not None:
             # get an instance of the target TTS
-            tts_instance = self._get_tts_instance(self.tts)
+            tts_instance = Utils.get_dynamic_class_instantiation("tts", self.tts.capitalize())
             tts_args = None
             for tts_object in self.settings.ttss:
                 if tts_object.name == self.tts:
@@ -114,16 +115,12 @@ class NeuronModule(object):
 
             tts_instance.say(words=tts_message, **(tts_args if tts_args is not None else {}))
 
-    @staticmethod
-    def _get_message_from_list(message_list):
+    def _get_message_from_dict(self, message_dict):
         """
-        Return an element from the list randomly
-        :param message_list:
+        Generate a message taht can be played by a TTS engine from a dict of variable and the jinja template
+        :param message_dict:
         :return:
         """
-        return random.choice(message_list)
-
-    def _get_message_from_dict(self, message_dict):
         returned_message = None
 
         if (self.say_template is not None and self.file_template is None) or \
@@ -161,13 +158,20 @@ class NeuronModule(object):
         with open(real_file_template_path, 'r') as content_file:
             return content_file.read()
 
-    @staticmethod
-    def _get_tts_instance(tts_name):
-        return Utils.get_dynamic_class_instantiation("tts", tts_name.capitalize())
-
     @staticmethod
     def _update_cache_var(new_override_cache, args_list):
         logger.debug("args for TTS plugin before update: %s" % str(args_list))
         args_list["cache"] = new_override_cache
         logger.debug("args for TTS plugin after update: %s" % str(args_list))
         return args_list
+
+    @staticmethod
+    def get_audio_from_stt(callback):
+        """
+        Call the default STT to get an audio sample and return it into the callback method
+        :param callback:
+        :return:
+        """
+        # call the order listenner
+        oa = OrderListener(callback=callback)
+        oa.start()

+ 1 - 0
neurons/__init__.py

@@ -11,3 +11,4 @@ from openweathermap import Openweathermap
 from tasker_autoremote import Tasker_autoremote
 from wake_on_lan import Wake_on_lan
 from twitter import Twitter
+from neurotransmitter import Neurotransmitter

+ 1 - 0
neurons/neurotransmitter/__init__.py

@@ -0,0 +1 @@
+from neurotransmitter import Neurotransmitter

+ 37 - 0
neurons/neurotransmitter/neurotransmitter.py

@@ -0,0 +1,37 @@
+import logging
+
+from core.NeuronModule import NeuronModule, MissingParameterException
+
+logging.basicConfig()
+logger = logging.getLogger("kalliope")
+
+
+class Neurotransmitter(NeuronModule):
+    def __init__(self, **kwargs):
+        super(Neurotransmitter, self).__init__(**kwargs)
+
+        # get links
+        self.links = kwargs.get('links', None)
+        # do some check
+        if self._links_content_ok():
+            # the brain seems fine, we call the stt to get an audio
+            self.get_audio_from_stt(callback=self.callback)
+
+    def callback(self, audio):
+        logger.debug("Neurotransmitter, receiver audio from STT: %s" % audio)
+        print audio
+
+    def _links_content_ok(self):
+        """
+        Check the content of the links parameter
+        :return:
+        """
+        if self.links is None:
+            raise MissingParameterException("links parameter required and must contain at least one link")
+        for el in self.links:
+            if "synapse" not in el:
+                raise MissingParameterException("Links must contain a synapse name: %s" % el)
+            if "answers" not in el:
+                raise MissingParameterException("Links must contain answers: %s" % el)
+
+        return True

+ 3 - 4
test.py

@@ -23,11 +23,10 @@ logger.setLevel(logging.DEBUG)
 
 SettingLoader.get_settings()
 
+order = "pose moi une question"
+oa = OrderAnalyser(order=order)
 
-# app = Flask(__name__)
-# flask_api = FlaskAPI(app)
-# flask_api.start()
-
+oa.start()