def __init__(self, component_id, component_type, name, **kwargs): self.spine = kwargs.get("spine", spine.Spine()) self._configuration = kwargs.get("configuration", Configuration) self._component_id = component_id self._component_type = component_type self._admin_groups = kwargs.get("admin_groups", []) self._user_groups = kwargs.get("user_groups", []) self._user_groups += self.admin_groups self._name = name self._icon = None self._visible = True self._dashboard_links = [] self._ui_parameters = {} self._settings = Settings(self.component_type + "_" + self.component_id) if self.spine: self.spine.log.debug("component created:{0}", self.component_id) self.spine.register_query_handler("getDashboardComponents", self._get_dashboard_components, groups=self.user_groups) self.spine.register_query_handler("getComponentInfo", self._get_component_info, groups=self.user_groups) self.spine.register_query_handler("getComponentRoutes", self._get_component_bus_routes)
def __init__(self): KerviThread.__init__(self) self.spine = spine.Spine() self.alive = False if self.spine: self.spine.register_command_handler("startThreads", self._start_command) self.spine.register_command_handler("stopThreads", self._stop_command)
def __init__(self, camera, mutex): KerviThread.__init__(self) self.spine = spine.Spine() self.alive = False self.camera = camera self.mutex = mutex if self.spine: self.spine.register_command_handler("startThreads", self._start_command) self.spine.register_command_handler("stopThreads", self._stop_command)
def stop(self, force_exit=True, restart=False): self.spine.send_command("kervi_action_module_exit") self.spine.trigger_event("moduleStopped", self.config.module.id) self._logger.warn("stopping processes") process._stop_processes("module-" + self.config.module.id) self.spine.trigger_event("processTerminating", None, local_only=True) time.sleep(1) spine.Spine().stop() #for thread in threading.enumerate(): # print("running thread",thread.name) self._logger.info("module stopped") if force_exit: import os os._exit(0)
def __init__(self, config): self._spine = spine.Spine() self._spine.register_event_handler("appReady", self._app_ready) self._spine.register_event_handler("moduleReady", self._module_ready) self._spine.register_event_handler("appReady", self._app_ready) self._routes_in = [] self._routes_out = [] self._ids = [] self._prepare_thread = None self._route_table_ready = False self._router_id = uuid.uuid4().hex self._connection_id = None self._connections = {} self._mq_sessions = {}
def __init__(self, panel_id, **kwargs): self.spine = spine.Spine() self.panel_id = panel_id self.ui_parameters = { "label": kwargs.pop("title", ""), "width": kwargs.pop("width", 0), "height": kwargs.pop("height", 0), "userLog": kwargs.pop("user_log", False), "app_health": kwargs.pop("app_health", False), "logLength": kwargs.pop("log_length", 5), "gauge_width": kwargs.pop("gauge_width", 0), "gauge_height": kwargs.pop("gauge_height", 0), } self.dashboard = None self.panel_id = panel_id self._user_groups = kwargs.pop("user_groups", [])
def __init__(self, panels=None, **kwargs): self.spine = spine.Spine() self.group_id = kwargs.get("title", None) self.ui_parameters = { "label": kwargs.get("title", ""), "width": kwargs.get("width", 0), "height": kwargs.get("height", 0), "layout": kwargs.get("layout", "row"), "gauge_width": kwargs.get("gauge_width", 0), "gauge_height": kwargs.get("gauge_height", 0), "panel_width": kwargs.get("panel_width", 0), "panel_height": kwargs.get("panel_height", 0), } self._dashboard = None self._user_groups = kwargs.get("user_groups", []) self._panels = [] for panel in panels: self.add_panel(panel)
def __init__(self, name, config, manager): KerviPlugin.__init__(self, name, config, manager) self._router_name = "" self._spine = spine.Spine() self._spine.register_event_handler("appReady", self._app_ready) self._spine.register_event_handler("moduleReady", self._module_ready) self._spine.register_event_handler("appReady", self._app_ready) self._routes_in = [] self._routes_out = [] self._ids = [] self._prepare_thread = None self._route_table_ready = False self._router_id = uuid.uuid4().hex self._connected = False self._connection_id = None self._connection_type = None self._connections = {} self._connections_lock = threading.Lock() self._mq_sessions = {} self._pending_queries = {} self._response_events = [] self._check_dead_connections_thread = _CheckDeadConnectionsThread(self) self._check_dead_connections_thread.start()
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)
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. """ general DB handling in Kervi, store sensor values and settings """ import os import time from datetime import datetime import json import inspect import threading import sqlite3 as lite import kervi.spine as spine from kervi.core.utility.thread import KerviThread SPINE = spine.Spine() FILE_CON = None MEMORY_CON = None MEMORY_LOCK = threading.Lock() FILE_LOCK = threading.Lock() def create_file_con(db_name): return lite.connect(db_name + '.db', check_same_thread=False) def create_memory_con(db_name): return lite.connect(db_name + "_mem.db", check_same_thread=False) #return lite.connect(":memory:", check_same_thread=False)
def __init__(self, config, manager): MessagePlugin.__init__(self, "user_log", config, manager) #self._config = Configuration.messaging.channels.user_log self.spine = spine.Spine()
def _stop_processes(scope): spine.Spine().send_command("terminateProcess", scope=[scope])
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)
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)