def __init__(self, **kwargs): super(SignalModule, self).__init__(**kwargs) # get the child who called the class self.signal_name = self.__class__.__name__ Utils.print_info('Init Signal :' + self.signal_name) self.brain = BrainLoader().brain
def load_events(self): """ For each received synapse that have an event as signal, we add a new job scheduled to launch the synapse """ for synapse in self.list_synapses_with_event: for signal in synapse.signals: # We need to loop here again if the synapse has multiple event signals. # if the signal is an event we add it to the task list. if signal.name == "event": my_cron = CronTrigger( year=self.get_parameter_from_dict( "year", signal.parameters), month=self.get_parameter_from_dict( "month", signal.parameters), day=self.get_parameter_from_dict( "day", signal.parameters), week=self.get_parameter_from_dict( "week", signal.parameters), day_of_week=self.get_parameter_from_dict( "day_of_week", signal.parameters), hour=self.get_parameter_from_dict( "hour", signal.parameters), minute=self.get_parameter_from_dict( "minute", signal.parameters), second=self.get_parameter_from_dict( "second", signal.parameters), ) Utils.print_info( "Add synapse name \"%s\" to the scheduler: %s" % (synapse.name, my_cron)) self.scheduler.add_job(self.run_synapse_by_name, my_cron, args=[synapse.name])
def __init__(self, **kwargs): super(Event, self).__init__(**kwargs) Utils.print_info('[Event] Starting manager') self.scheduler = BackgroundScheduler() self.list_synapses_with_event = list( super(Event, self).get_list_synapse()) self.load_events()
def __init__(self, **kwargs): super(Mqtt_subscriber, self).__init__(**kwargs) Utils.print_info('[Mqtt_subscriber] Starting manager') # variables self.list_synapses_with_mqtt = list( super(Mqtt_subscriber, self).get_list_synapse()) self.broker_ip = None self.topic = None self.json_message = False
def run_synapse_by_name(synapse_name): """ This method will run the synapse """ Utils.print_info("[Event] triggered, running synapse: %s" % synapse_name) # get a brain brain_loader = BrainLoader() brain = brain_loader.brain SynapseLauncher.start_synapse_by_name(synapse_name, brain=brain)
def signal_handler(signal, frame): """ Press Ctrl+C to stop Intelora :param signal: signal handler :param frame: execution frame """ print("\n") Utils.print_info("Intelora stopped") sys.exit(0)
def start_rest_api(settings, brain): """ Start the Rest API if asked in the user settings """ # run the api if the user want it if settings.rest_api.active: Utils.print_info("Starting REST API Listening port: %s" % settings.rest_api.port) app = Flask(__name__) flask_api = FlaskAPI( app=app, port=settings.rest_api.port, brain=brain, allowed_cors_origin=settings.rest_api.allowed_cors_origin) flask_api.daemon = True flask_api.start()
def google_callback(self, recognizer, audio): """ called from the background thread """ try: captured_audio = recognizer.recognize_google( audio, key=self.key, language=self.language, show_all=self.show_all) Utils.print_success( "Google Speech Recognition thinks you said %s" % captured_audio) self._analyse_audio(audio_to_text=captured_audio) except sr.UnknownValueError: Utils.print_warning( "Google Speech Recognition could not understand audio") # callback anyway, we need to listen again for a new order self._analyse_audio(audio_to_text=None) except sr.RequestError as e: Utils.print_danger( "Could not request results from Google Speech Recognition service; {0}" .format(e)) # callback anyway, we need to listen again for a new order self._analyse_audio(audio_to_text=None) self.stop_listening()
def apiai_callback(self, recognizer, audio): """ called from the background thread :param recognizer: :param audio: :return: """ try: captured_audio = recognizer.recognize_api(audio, client_access_token=self.key, language=self.language, session_id=self.session_id, show_all=self.show_all) Utils.print_success("Apiai Speech Recognition thinks you said %s" % captured_audio) self._analyse_audio(captured_audio) except sr.UnknownValueError as e: Utils.print_warning("Apiai Speech Recognition could not understand audio; {0}".format(e)) # callback anyway, we need to listen again for a new order self._analyse_audio(audio_to_text=None) except sr.RequestError as e: Utils.print_danger("Could not request results from Apiai Speech Recognition service; {0}".format(e)) # callback anyway, we need to listen again for a new order self._analyse_audio(audio_to_text=None) # stop listening for an audio self.stop_listening()
def get_player(settings): """ Instantiate a Player :param settings: setting object :type settings: Settings :return: the Player instance :rtype: Player """ player_instance = None for player in settings.players: if player.name == settings.default_player_name: logger.debug( "PlayerLauncher: Start player %s with parameters: %s" % (player.name, player.parameters)) player_instance = Utils.get_dynamic_class_instantiation( package_name="players", module_name=player.name, parameters=player.parameters) break return player_instance
def get_trigger(settings, callback): """ Start a trigger module :param trigger: trigger object to instantiate :type trigger: Trigger :param callback: Callback function to call when the trigger catch the magic word :return: The instance of Trigger :rtype: Trigger """ trigger_instance = None for trigger in settings.triggers: if trigger.name == settings.default_trigger_name: # add the callback method to parameters trigger.parameters["callback"] = callback logger.debug( "TriggerLauncher: Start trigger %s with parameters: %s" % (trigger.name, trigger.parameters)) trigger_instance = Utils.get_dynamic_class_instantiation( package_name="trigger", module_name=trigger.name, parameters=trigger.parameters) break return trigger_instance
def wit_callback(self, recognizer, audio): try: captured_audio = recognizer.recognize_wit(audio, key=self.key, show_all=self.show_all) Utils.print_success( "Wit.ai Speech Recognition thinks you said %s" % captured_audio) self._analyse_audio(captured_audio) except sr.UnknownValueError: Utils.print_warning( "Wit.ai Speech Recognition could not understand audio") # callback anyway, we need to listen again for a new order self._analyse_audio(audio_to_text=None) except sr.RequestError as e: Utils.print_danger( "Could not request results from Wit.ai Speech Recognition service; {0}" .format(e)) # callback anyway, we need to listen again for a new order self._analyse_audio(audio_to_text=None) # stop listening for an audio self.stop_listening()
def main(): """Entry point of Intelora program.""" # parse argument. the script name is removed try: parser = parse_args(sys.argv[1:]) except SystemExit: sys.exit(1) # check if we want debug configure_logging(debug=parser.debug) logger.debug("intelora args: %s" % parser) # by default, no brain file is set. # Use the default one: brain.yml in the root path brain_file = None # check if user set a brain.yml file if parser.brain_file: brain_file = parser.brain_file # check the user provide a valid action if parser.action not in ACTION_LIST: Utils.print_warning("%s is not a recognised action\n" % parser.action) sys.exit(1) # install modules if parser.action == "install": if not parser.git_url: Utils.print_danger("You must specify the git url") sys.exit(1) else: parameters = {"git_url": parser.git_url} res_manager = ResourcesManager(**parameters) res_manager.install() return # uninstall modules if parser.action == "uninstall": if not parser.neuron_name \ and not parser.stt_name \ and not parser.tts_name \ and not parser.trigger_name\ and not parser.signal_name: Utils.print_danger("You must specify a module name with " "--neuron-name " "or --stt-name " "or --tts-name " "or --trigger-name " "or --signal-name") sys.exit(1) else: res_manager = ResourcesManager() res_manager.uninstall(neuron_name=parser.neuron_name, stt_name=parser.stt_name, tts_name=parser.tts_name, trigger_name=parser.trigger_name, signal_name=parser.signal_name) return # load the brain once brain_loader = BrainLoader(file_path=brain_file) brain = brain_loader.brain # load settings # get global configuration once settings_loader = SettingLoader() settings = settings_loader.settings if parser.action == "start": # user set a synapse to start if parser.run_synapse is not None: SynapseLauncher.start_synapse_by_name(parser.run_synapse, brain=brain) if parser.run_order is not None: SynapseLauncher.run_matching_synapse_from_order(parser.run_order, brain=brain, settings=settings, is_api_call=False) if (parser.run_synapse is None) and (parser.run_order is None): # if --muted if parser.muted: settings.start_options['muted'] = True # start rest api start_rest_api(settings, brain) start_intelora(settings, brain) if parser.action == "gui": try: ShellGui(brain=brain) except (KeyboardInterrupt, SystemExit): Utils.print_info("Intelora stopped") sys.exit(0)
def start_intelora(settings, brain): """ Start all signals declared in the brain """ # start intelora Utils.print_success("") Utils.print_success("Intelora is initializing the agent:") time.sleep(randint(1, 3)) Utils.print_success("Brain................[OK]") time.sleep(randint(1, 3)) Utils.print_success("Neurons..............[OK]") time.sleep(randint(1, 3)) Utils.print_success("Synapses.............[OK]") time.sleep(randint(1, 3)) Utils.print_success("Speech Recognizer....[OK]") time.sleep(randint(1, 3)) Utils.print_success("Order Manager........[OK]") time.sleep(randint(1, 3)) Utils.print_success("Speech Synthesizer...[OK]") time.sleep(randint(1, 3)) Utils.print_success("Triggers.............[OK]") time.sleep(randint(1, 3)) Utils.print_success("Variables............[OK]") time.sleep(randint(1, 3)) Utils.print_success("") Utils.print_success("Agent is now online!") Utils.print_success("") Utils.print_info("Press Ctrl+C to stop Intelora") # catch signal for killing on Ctrl+C pressed signal.signal(signal.SIGINT, signal_handler) # get a list of signal class to load from declared synapse in the brain # this list will contain string of signal class type. # For example, if the brain contains multiple time the signal type "order", the list will be ["order"] # If the brain contains some synapse with "order" and "event", the list will be ["order", "event"] list_signals_class_to_load = get_list_signal_class_to_load(brain) # start each class name for signal_class_name in list_signals_class_to_load: signal_instance = SignalLauncher.launch_signal_class_by_name( signal_name=signal_class_name, settings=settings) if signal_instance is not None: signal_instance.daemon = True signal_instance.start() while True: # keep main thread alive time.sleep(0.1)