nico преди 7 години
родител
ревизия
46a5cafa74
променени са 2 файла, в които са добавени 44 реда и са изтрити 37 реда
  1. 6 5
      Tests/test_init.py
  2. 38 32
      kalliope/__init__.py

+ 6 - 5
Tests/test_init.py

@@ -43,11 +43,12 @@ class TestInit(unittest.TestCase):
     def test_main(self):
         # test start kalliope
         sys.argv = ['kalliope.py', 'start']
-        with mock.patch('kalliope.core.SignalLauncher.SignalLauncher.launch_signal_class_by_name') \
-                as mock_signal_launcher:
-            mock_signal_launcher.return_value = None
-            main()
-            mock_signal_launcher.assert_called()
+        with mock.patch('kalliope.start_rest_api') as mock_rest_api:
+            with mock.patch('kalliope.start_kalliope') as mock_start_kalliope:
+                mock_rest_api.return_value = None
+                main()
+                mock_rest_api.assert_called()
+                mock_start_kalliope.assert_called()
 
         # test start gui
         sys.argv = ['kalliope.py', 'gui']

+ 38 - 32
kalliope/__init__.py

@@ -157,38 +157,7 @@ def main():
         if (parser.run_synapse is None) and (parser.run_order is None):
             # start rest api
             start_rest_api(settings, brain)
-
-            # start kalliope
-            Utils.print_success("Starting Kalliope")
-            Utils.print_info("Press Ctrl+C for stopping")
-            # 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
-            try:
-                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)
-
-            except (KeyboardInterrupt, SystemExit):
-                # we need to switch GPIO pin to default status if we are using a Rpi
-                if settings.rpi_settings:
-                    Utils.print_info("GPIO cleaned")
-                    logger.debug("Clean GPIO")
-                    import RPi.GPIO as GPIO
-                    GPIO.cleanup()
+            start_kalliope(settings, brain)
 
     if parser.action == "gui":
         try:
@@ -265,3 +234,40 @@ def start_rest_api(settings, brain):
                              allowed_cors_origin=settings.rest_api.allowed_cors_origin)
         flask_api.daemon = True
         flask_api.start()
+
+
+def start_kalliope(settings, brain):
+    """
+    Start all signals declared in the brain
+    """
+    # start kalliope
+    Utils.print_success("Starting Kalliope")
+    Utils.print_info("Press Ctrl+C for stopping")
+    # 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
+    try:
+        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)
+
+    except (KeyboardInterrupt, SystemExit):
+        # we need to switch GPIO pin to default status if we are using a Rpi
+        if settings.rpi_settings:
+            Utils.print_info("GPIO cleaned")
+            logger.debug("Clean GPIO")
+            import RPi.GPIO as GPIO
+            GPIO.cleanup()