def test_action_async(): global P1, P2, K1 spine = MockupSpine() set_spine(spine) configuration = get_test_config() P1 = "a" P2 = "b" K1 = "c" @action def action_func_async(p1, p2, **kwargs): global P1, P2, K1, event time.sleep(2) P1 = p1 P2 = p2 K1 = kwargs.get("k1", None) event.set() spine.clear() Actions["action_func_async"]("axa", "bxa", k1="kxa", run_async=True) assert P1 == "a" assert P2 == "b" assert K1 == "c" event.wait(5) assert P1 == "axa" assert P2 == "bxa" assert K1 == "kxa"
def load(self, process_id, spine_port, root_address = None, ip=None): self._plugin_manager.load_managed_plugins() for plugin in self._plugin_manager.plugins: plugin.load(process_id, spine_port, root_address, ip) self._current_bus = plugin.bus break else: self._log.error("No message bus plugin found") if self._current_bus: import kervi.spine as spine spine.set_spine(self._current_bus)
def test_function_action_alternative_id(): spine = MockupSpine() set_spine(spine) configuration = get_test_config() @action(action_id="action_func_x") def action_func(p1, p2): global P1, P2 P1 = p1 P2 = p2 Actions["action_func_x"]("a1", "b1") assert P1 == "a1" assert P2 == "b1"
def test_function_action_direct(): spine = MockupSpine() set_spine(spine) configuration = get_test_config() @action def action_func(p1, p2, **kwargs): global P1, P2, K1 P1 = p1 P2 = p2 K1 = kwargs.get("k1", None) action_func("a", "b", k1="k") assert P1 == "a" assert P2 == "b" assert K1 == "k"
def test_action_messages(): spine = MockupSpine() set_spine(spine) configuration = get_test_config() @action def action_func(p1, p2, **kwargs): global P1, P2, K1 P1 = p1 P2 = p2 K1 = kwargs.get("k1", None) spine.clear() Actions["action_func"]("ax", "bx", k1="kx") commands = spine.get_send_commands("messageManagerSend") assert P1 == "ax" assert P2 == "bx" assert K1 == "kx" assert len(commands) == 2
def test_controller_action_direct(): spine = MockupSpine() set_spine(spine) configuration = get_test_config() class TestController(Controller): def __init__(self): Controller.__init__(self, "test_controller3", "Test controller") self.p1 = None self.p2 = None @action(action_id="actionx") def action1(self, p1, p2): self.p1 = p1 self.p2 = p2 tc = TestController() tc.action1("cax", "cbx") assert tc.p1 == "cax" assert tc.p2 == "cbx"
def test_controller_action(): spine = MockupSpine() set_spine(spine) configuration = get_test_config() class TestController(Controller): def __init__(self): Controller.__init__(self, "test_controller", "Test controller") self.p1 = None self.p2 = None @action def action1(self, p1, p2): self.p1 = p1 self.p2 = p2 tc = TestController() Actions["test_controller.action1"]("ca", "cb") assert tc.p1 == "ca" assert tc.p2 == "cb"
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() print("Starting kervi module, please wait") self.started = False if self.config.module.app_connection_local and not self.config.network.ipc_root_address: print("Locating kervi application...") 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: print("Locate kervi application failed") exit() self._root_address = "tcp://" + self.config.network.ipc_root_address + ":" + str(self.config.network.ipc_root_port) from kervi.zmq_spine import _ZMQSpine self.spine = _ZMQSpine() if self.config.module.app_connection_local: self.spine._init_spine("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.spine._init_spine("kervi-main", self.config.network.ipc_root_port, None, self.config.network.ipc_root_address) spine.set_spine(self.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: print("Using HAL driver:", hal_driver) self._actions = _ModuleActions(self)
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)