MainController.py 3.9 KB

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