MainController.py 4.0 KB

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