Example #1
0
    def init_process(self, **kwargs):
        self.spine.log.verbose("load: %s", self.name)

        try:
            import kervi.hal as hal
            hal._load()
            __import__(self.name, fromlist=[''])

        except ImportError:
            self.spine.log.exception("module not found:{0}", self.name)
        except:
            self.spine.log.exception("error load module:{0}", self.name)

        self.spine.send_command("startThreads", local_only=True)
        self.spine.trigger_event("moduleLoaded", self.name)
Example #2
0
    def init_process(self, **kwargs):
        print("load:", self.name)
        #import kervi.core_sensors.cpu_sensors
        try:
            import kervi.hal as hal
            hal._load()
            __import__(self.name, fromlist=[''])

        except ImportError:
            self.spine.log.exception("load module:{0}", self.name)
        #import kervi.utility.storage
        self.spine.send_command("startThreads", local_only=True)
        self.spine.trigger_event(
            "moduleLoaded",
            self.name,
        )
Example #3
0
def devices(hw_platform):
    """Detect devices that are found on this device."""
    import kervi.hal as hal
    platform = hal._load(hw_platform)
    print("platform", platform)
    devices = hal.detect_devices()
    _pretty_print(devices)
Example #4
0
    def __init__(self, user_config=None):

        print("Starting kervi (u)application")
        #import inspect
        import getopt

        config_files = []

        opts, args = getopt.getopt(sys.argv[1:], "c", [
            "config_file=", "as-service", "install-service",
            "uninstall-service", "start-service", "stop-service",
            "restart-service", "status-service", "detect-devices"
        ])
        for opt, arg in opts:
            if opt in ("-c", "--config_file"):
                if os.path.isfile(arg):
                    config_files += [arg]
                else:
                    print("Specified config file not found:", arg)
        #script_path = os.path.abspath(inspect.stack()[1][1])
        #script_name = os.path.basename(script_path)
        #script_name, script_ext = os.path.splitext(script_name)

        #config_files += [script_name +".config.json"]
        #config_files += ["kervi.config.json"]

        selected_config_file = None
        for config_file in config_files:
            if os.path.isfile(config_file):
                selected_config_file = config_file
                break

        from kervi.config import load
        self.config = load(config_file=selected_config_file,
                           config_user=user_config,
                           config_base=get_default_config())

        import kervi.hal as hal
        hal_driver = hal._load(self.config.platform.driver)
        if hal_driver:
            print("platform driver:", hal_driver)
Example #5
0
    def __init__(self, user_config=None, **kwargs):
        """ Settings is a dictionary with the following content
        """

        from kervi.config import load
        import inspect
        import sys
        import os

        script_path = os.path.basename(os.path.abspath(inspect.stack()[1][1]))
        script_name, script_ext = os.path.splitext(script_path)

        config_files = []

        import getopt
        opts, args = getopt.getopt(sys.argv[1:], "c", [
            "config_file=", "as-service", "install-service",
            "uninstall-service", "start-service", "stop-service",
            "restart-service", "status-service", "detect-devices"
        ])
        for opt, arg in opts:
            if opt in ("-c", "--config_file"):
                if os.path.isfile(arg):
                    config_files += [arg]
                else:
                    print("Specified config file not found:", arg)

        config_files += [script_name + ".config.json"]
        config_files += ["kervi.config.json"]

        selected_config_file = None
        for config_file in config_files:
            if os.path.isfile(config_file):
                selected_config_file = config_file
                break
        if selected_config_file:
            print("using config file:", selected_config_file)
        #else:
        #    print("no config file found, revert to defaults")

        self.config = load(config_file=selected_config_file,
                           config_user=user_config,
                           config_base=get_default_config())

        service_commands = []
        detect_devices = None
        self._as_service = False
        for opt, arg in opts:
            if opt in ("--as-service"):
                self._as_service = True

            if opt in ("--detect-devices"):
                detect_devices = True

            if opt in ("--install-service"):
                service_commands += ["install"]

            if opt in ("--uninstall-service"):
                service_commands += ["uninstall"]

            if opt in ("--start-service"):
                service_commands += ["start"]

            if opt in ("--stop-service"):
                service_commands = ["stop"]

            if opt in ("--restart-service"):
                service_commands = ["restart"]
            if opt in ("--service-status"):
                service_commands = ["status"]

        if service_commands:
            import kervi.hal as hal
            hal_driver = hal._load(self.config.platform.driver)
            if hal_driver:
                hal.service_commands(service_commands,
                                     self.config.application.name,
                                     self.config.application.id, script_path)
            exit()

        if detect_devices:
            import kervi.hal as hal
            hal_driver = hal._load(self.config.platform.driver)
            if hal_driver:
                devices = hal.detect_devices()
                print("devices:")
                _pretty_print(devices)
            exit()

        self._log_handler = KerviLogHandler(self.config)
        self._log_queue = self._log_handler._log_queue
        self._logger = KerviLog("module")

        self._logger.info("Starting kervi module, please wait")

        try:
            from kervi.version import VERSION
        except:
            VERSION = "0.0.0"

        self._logger.verbose("kervi version: %s", VERSION)

        self.started = False
        if self.config.module.app_connection_local and not self.config.network.ipc_root_address:
            self._logger.verbose("Locating kervi application with id {0}...",
                                 self.config.application.id)
            from kervi.utility.discovery import find_kervi_app
            address, port = find_kervi_app(self.config.application.id)
            if address:
                self.config.network.ipc_root_address = address
                self.config.network.ipc_root_port = port
            else:
                self._logger.error("Locate kervi application failed")
                exit()

        self._root_address = "tcp://" + self.config.network.ipc_root_address + ":" + str(
            self.config.network.ipc_root_port)

        from kervi.plugin.message_bus.bus_manager import BusManager
        self.bus_manager = BusManager()
        #self.config.network.ipc_root_port = nethelper.get_free_port([self.config.network.ipc_root_port])
        self._logger.verbose("ip: {0}", self.config.network.ip)
        if self.config.module.app_connection_local:
            self.bus_manager.load(
                "kervi-module", self.config.network.ipc_module_port,
                "tcp://" + self.config.network.ipc_root_address + ":" +
                str(self.config.network.ipc_root_port), self.config.network.ip)
        else:
            self.bus_manager.load("kervi-main",
                                  self.config.network.ipc_root_port, None,
                                  self.config.network.ipc_root_address)

        from kervi import spine
        self.spine = spine.Spine()

        self.spine.register_event_handler("processReady",
                                          self._process_ready,
                                          scope="app-" +
                                          self.config.application.id)
        self.spine = spine.Spine()

        self._module_processes = []
        self._process_info = []
        self._process_info_lock = threading.Lock()

        import kervi.hal as hal
        hal_driver = hal._load()
        if hal_driver:
            self._logger.verbose("Using HAL driver: %s", hal_driver)

        self._actions = _ModuleActions(self)
Example #6
0
    def __init__(self, user_config=None):
        """ Settings is a dictionary with the following content
        """
        global _console_log

        print("\033[92mStarting kervi application \033[0m")
        import inspect
        import getopt
        self.char_list = []
        self._websocket_event = threading.Event()
        self._in_stop = False
        self._discovery_thread = None
        config_files = []
        self._webserver_info = None
        opts, args = getopt.getopt(sys.argv[1:], "c", [
            "config_file=", "as-service", "install-service",
            "uninstall-service", "start-service", "stop-service",
            "restart-service", "status-service", "detect-devices"
        ])
        for opt, arg in opts:
            if opt in ("-c", "--config_file"):
                if os.path.isfile(arg):
                    config_files += [arg]
                else:
                    print("Specified config file not found:", arg)
        script_path = os.path.abspath(inspect.stack()[1][1])
        script_name = os.path.basename(script_path)
        script_name, script_ext = os.path.splitext(script_name)

        config_files += [script_name + ".config.json"]
        config_files += ["kervi.config.json"]

        selected_config_file = None
        for config_file in config_files:
            if os.path.isfile(config_file):
                selected_config_file = config_file
                break

        from kervi.config import load
        self.config = load(config_file=selected_config_file,
                           config_user=user_config,
                           config_base=get_default_config())

        service_commands = []
        detect_devices = None
        self._as_service = False
        for opt, arg in opts:
            if opt in ("--as-service"):
                self._as_service = True

            if opt in ("--detect-devices"):
                detect_devices = True

            if opt in ("--install-service"):
                service_commands += ["install"]

            if opt in ("--uninstall-service"):
                service_commands += ["uninstall"]

            if opt in ("--start-service"):
                service_commands += ["start"]

            if opt in ("--stop-service"):
                service_commands = ["stop"]

            if opt in ("--restart-service"):
                service_commands = ["restart"]
            if opt in ("--service-status"):
                service_commands = ["status"]

        if service_commands:
            import kervi.hal as hal
            hal_driver = hal._load(self.config.platform.driver)
            if hal_driver:
                hal.service_commands(service_commands,
                                     self.config.application.name,
                                     self.config.application.id, script_path)
            exit()

        if detect_devices:
            import kervi.hal as hal
            hal_driver = hal._load(self.config.platform.driver)
            if hal_driver:
                devices = hal.detect_devices()
                print("devices:")
                _pretty_print(devices)
            exit()
        self._log_handler = KerviLogHandler(self.config)
        self._log_queue = self._log_handler._log_queue
        self._logger = KerviLog("application")
        #self._validateSettings()
        self.started = False
        self._webserver_port = None

        try:
            from kervi.version import VERSION
        except:
            VERSION = "0.0.0"

        self._logger.verbose("kervi version: %s", VERSION)

        import kervi.hal as hal
        hal_driver = hal._load(self.config.platform.driver)
        if hal_driver:
            self._logger.verbose("platform driver: %s", hal_driver)
            self._logger.verbose("board: %s", hal.get_board_name())

        from kervi.plugin.message_bus.bus_manager import BusManager
        self.bus_manager = BusManager(self._log_queue)
        self.config.network.ipc_root_port = nethelper.get_free_port(
            [self.config.network.ipc_root_port])
        self.bus_manager.load("kervi-main", self.config.network.ipc_root_port,
                              None, self.config.network.ipc_root_address)

        from kervi import spine
        self.spine = spine.Spine()
        self.spine.register_query_handler("GetApplicationInfo",
                                          self._get_application_info)
        self.spine.register_query_handler("getProcessInfo",
                                          self.get_process_info)
        self.spine.register_event_handler("modulePing", self._module_ping)
        self.spine.register_event_handler("processReady",
                                          self._process_ready,
                                          scope="app-" +
                                          self.config.application.id)
        self.spine.register_event_handler("WebAppReady",
                                          self._webapp_ready,
                                          scope="app-" +
                                          self.config.application.id)
        self.spine.register_event_handler("websocketReady",
                                          self._websocket_ready,
                                          scope="app-" +
                                          self.config.application.id)
        self._module_processes = []
        self._process_info = []
        self._process_info_lock = threading.Lock()

        self._kervi_modules = []
        self._kervi_modules_lock = threading.Lock()

        from kervi.storage.storage_manager import StorageManager
        self._storage_manager = StorageManager(self._log_queue)

        from kervi.utility.authorization_manager import AuthorizationManager
        self._authorization_manager = AuthorizationManager(self._log_queue)

        from kervi.messaging.message_manager import MessageManager
        self._message_manager = MessageManager(self._log_queue)

        self._app_actions = _AppActions(self)
Example #7
0
    def __init__(self, user_config=None):
        """ Settings is a dictionary with the following content
        """

        print("Starting kervi application")
        import inspect
        import getopt

        config_files = []

        opts, args = getopt.getopt(sys.argv[1:], "c", [
            "config_file=", "as-service", "install-service",
            "uninstall-service", "start-service", "stop-service",
            "restart-service", "status-service", "detect-devices"
        ])
        for opt, arg in opts:
            if opt in ("-c", "--config_file"):
                if os.path.isfile(arg):
                    config_files += [arg]
                else:
                    print("Specified config file not found:", arg)
        script_path = os.path.abspath(inspect.stack()[1][1])
        script_name = os.path.basename(script_path)
        script_name, script_ext = os.path.splitext(script_name)

        config_files += [script_name + ".config.json"]
        config_files += ["kervi.config.json"]

        selected_config_file = None
        for config_file in config_files:
            if os.path.isfile(config_file):
                selected_config_file = config_file
                break
        #if not selected_config_file:
        #    print("no config file found , revert to defaults")

        from kervi.config import load
        self.config = load(config_file=selected_config_file,
                           config_user=user_config,
                           config_base=get_default_config())

        service_commands = []
        detect_devices = None
        self._as_service = False
        for opt, arg in opts:
            if opt in ("--as-service"):
                self._as_service = True

            if opt in ("--detect-devices"):
                detect_devices = True

            if opt in ("--install-service"):
                service_commands += ["install"]

            if opt in ("--uninstall-service"):
                service_commands += ["uninstall"]

            if opt in ("--start-service"):
                service_commands += ["start"]

            if opt in ("--stop-service"):
                service_commands = ["stop"]

            if opt in ("--restart-service"):
                service_commands = ["restart"]
            if opt in ("--service-status"):
                service_commands = ["status"]

        if service_commands:
            import kervi.hal as hal
            hal_driver = hal._load(self.config.platform.driver)
            if hal_driver:
                hal.service_commands(service_commands,
                                     self.config.application.name,
                                     self.config.application.id, script_path)
            exit()

        if detect_devices:
            import kervi.hal as hal
            hal_driver = hal._load(self.config.platform.driver)
            if hal_driver:
                devices = hal.detect_devices()
                print("devices:")
                _pretty_print(devices)
            exit()

        #if settings:
        #    self.settings = app_helpers._deep_update(self.settings, settings)
        #self._validateSettings()
        self.started = False
        import kervi.spine as spine
        from kervi.zmq_spine import _ZMQSpine
        self.spine = _ZMQSpine()
        self.config.network.ipc_root_port = nethelper.get_free_port(
            [self.config.network.ipc_root_port])
        self.spine._init_spine("kervi-main", self.config.network.ipc_root_port,
                               None, self.config.network.ipc_root_address)
        spine.set_spine(self.spine)
        #process._start_root_spine(self.config, True, _ZMQSpine)
        #spine._init_spine("application-" + self.settings["info"]["id"])
        self.spine = spine.Spine()
        self.spine.register_query_handler("GetApplicationInfo",
                                          self._get_application_info)
        self.spine.register_query_handler("getProcessInfo",
                                          self.get_process_info)
        self.spine.register_event_handler("modulePing", self._module_ping)
        self.spine.register_event_handler("processReady",
                                          self._process_ready,
                                          scope="app-" +
                                          self.config.application.id)
        self._module_processes = []
        self._process_info = []
        self._process_info_lock = threading.Lock()

        self._kervi_modules = []
        self._kervi_modules_lock = threading.Lock()

        #from kervi.utility.storage import init_db
        #init_db(script_name)
        from kervi.storage.storage_manager import StorageManager
        self._authentication = StorageManager()

        from kervi.utility.authorization_manager import AuthorizationManager
        self._authentication = AuthorizationManager()

        from kervi.messaging.message_manager import MessageManager
        self._message_handler = MessageManager()

        self._app_actions = _AppActions(self)

        import kervi.hal as hal
        hal_driver = hal._load(self.config.platform.driver)
        if hal_driver:
            print("platform driver:", hal_driver)