MainController.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import logging
  2. import os
  3. import random
  4. from core import Utils
  5. from core.ConfigurationManager import SettingLoader
  6. from core.ConfigurationManager.BrainLoader import BrainLoader
  7. from core.OrderAnalyser import OrderAnalyser
  8. from core.OrderListener import OrderListener
  9. from core.Players import Mplayer
  10. from core.TriggerLauncher import TriggerLauncher
  11. from flask import Flask
  12. from core.RestAPI.FlaskAPI import FlaskAPI
  13. from neurons import Say
  14. logging.basicConfig()
  15. logger = logging.getLogger("kalliope")
  16. class MainController:
  17. def __init__(self, brain=None):
  18. self.brain = brain
  19. # get global configuration
  20. self.settings = SettingLoader.get_settings()
  21. # run the api if the user want it
  22. if self.settings.rest_api.active:
  23. Utils.print_info("Starting REST API Listening port: %s" % self.settings.rest_api.port)
  24. app = Flask(__name__)
  25. flask_api = FlaskAPI(app, port=self.settings.rest_api.port, brain=self.brain)
  26. flask_api.start()
  27. # create an order listener object. This last will the trigger callback before starting
  28. self.order_listener = OrderListener(self.analyse_order)
  29. # Wait that the kalliope trigger is pronounced by the user
  30. self.trigger_instance = self._get_default_trigger()
  31. self.trigger_instance.start()
  32. Utils.print_info("Waiting for trigger detection")
  33. def callback(self):
  34. """
  35. # we have detected the hotword, we can now pause the kalliope Trigger for a while
  36. # The user can speak out loud his order during this time.
  37. :return:
  38. """
  39. # pause the snowboy process
  40. self.trigger_instance.pause()
  41. # start listening for an order
  42. self.order_listener.start()
  43. # if random wake answer sentence are present, we play this
  44. if self.settings.random_wake_up_answers is not None:
  45. Say(message=self.settings.random_wake_up_answers)
  46. else:
  47. random_sound_to_play = self._get_random_sound(self.settings.random_wake_up_sounds)
  48. Mplayer.play(random_sound_to_play)
  49. def analyse_order(self, order):
  50. """
  51. Receive an order, try to retreive it in the brain.yml to launch to attached plugins
  52. :return:
  53. """
  54. order_analyser = OrderAnalyser(order, main_controller=self, brain=self.brain)
  55. order_analyser.start()
  56. # restart the trigger when the order analyser has finish his job
  57. Utils.print_info("Waiting for trigger detection")
  58. self.trigger_instance.unpause()
  59. # create a new order listener that will wait for start
  60. self.order_listener = OrderListener(self.analyse_order)
  61. # restart the trigger to catch the hotword
  62. self.trigger_instance.start()
  63. def _get_default_trigger(self):
  64. """
  65. Return an instance of the default trigger
  66. :return:
  67. """
  68. for trigger in self.settings.triggers:
  69. if trigger.name == self.settings.default_trigger_name:
  70. return TriggerLauncher.get_trigger(trigger, callback=self.callback)
  71. @staticmethod
  72. def _get_random_sound(random_wake_up_sounds):
  73. """
  74. Return a path of a sound to play
  75. If the path is absolute, test if file exist
  76. If the path is relative, we check if the file exist in the sound folder
  77. :param random_wake_up_sounds:
  78. :return:
  79. """
  80. # take first randomly a path
  81. random_path = random.choice(random_wake_up_sounds)
  82. logger.debug("Selected sound: %s" % random_path)
  83. if os.path.isabs(random_path):
  84. logger.debug("Path of file %s is absolute" % random_path)
  85. return random_path
  86. else:
  87. logger.debug("Path of file %s is relative" % random_path)
  88. return "sounds" + os.sep + random_path