async def setup(self): # shutdown shutdown.register_func(self.shutdown, msg='Cancel rule schedulers') if cmd_args.DO_BENCH: from HABApp.rule_manager.benchmark import BenchFile self.files['bench'] = file = BenchFile(self) ok = await HABApp.core.const.loop.run_in_executor( HABApp.core.WrappedFunction._WORKERS, file.load) if not ok: log.error('Failed to load Benchmark!') HABApp.runtime.shutdown.request_shutdown() return None file.check_all_rules() return class HABAppRuleFile(HABAppFile): LOGGER = log LOAD_FUNC = self.request_file_load UNLOAD_FUNC = self.request_file_unload path = HABApp.CONFIG.directories.rules folder = add_habapp_folder('rules/', path, 0) folder.add_file_type(HABAppRuleFile) self.watcher = folder.add_watch('.py', True) # Initial loading of rules HABApp.core.WrappedFunction(self.load_rules_on_startup, logger=log).run()
async def create_client(): global CLIENT assert CLIENT is None CLIENT = aiohttp.ClientSession(json_serialize=dump_json, loop=loop) from HABApp.runtime import shutdown shutdown.register_func(CLIENT.close, msg='Closing generic http connection')
def __init__(self): self.config: HABApp.config.Config = None # Rule engine self.rule_manager: HABApp.rule_manager.RuleManager = None # Async Workers & shutdown callback shutdown.register_func(HABApp.core.WrappedFunction._WORKERS.shutdown, msg='Stopping workers')
def setup(): config = HABApp.config.CONFIG.mqtt # config changes config.subscribe.subscribe_for_changes(subscription_changed) config.connection.subscribe_for_changes(connect) # shutdown shutdown.register_func(disconnect, msg='Disconnecting MQTT')
def start(): global OBSERVER # start only once! assert OBSERVER is None OBSERVER = Observer() OBSERVER.start() # register for proper shutdown from HABApp.runtime import shutdown shutdown.register_func(OBSERVER.stop, msg='Stopping folder observer') shutdown.register_func(OBSERVER.join, last=True, msg='Joining folder observer threads') return None
def setup(): from HABApp.runtime import shutdown # initialize callbacks http_connection.ON_CONNECTED = on_connect http_connection.ON_DISCONNECTED = on_disconnect # shutdown handler for connection shutdown.register_func(http_connection.stop_connection, msg='Stopping openHAB connection') # shutdown handler for plugins shutdown.register_func(on_disconnect, msg='Stopping openHAB plugins') # initialize all plugins setup_plugins() return None
async def start(self, config_folder: Path): try: token = async_context.set('HABApp startup') # setup exception handler for the scheduler eascheduler.set_exception_handler(lambda x: process_exception('HABApp.scheduler', x)) # Start Folder watcher! HABApp.core.files.watcher.start() self.config_loader = HABApp.config.HABAppConfigLoader(config_folder) await HABApp.core.files.setup() # generic HTTP await HABApp.rule.interfaces._http.create_client() # openhab openhab_connection.setup() # Parameter Files await HABApp.parameters.parameter_files.setup_param_files() # Rule engine self.rule_manager = HABApp.rule_manager.RuleManager(self) await self.rule_manager.setup() # MQTT HABApp.mqtt.mqtt_connection.setup() HABApp.mqtt.mqtt_connection.connect() await openhab_connection.start() shutdown.register_func(HABApp.core.const.loop.stop, msg='Stopping asyncio loop') async_context.reset(token) except asyncio.CancelledError: pass except Exception as e: process_exception('Runtime.start', e) await asyncio.sleep(1) # Sleep so we can do a graceful shutdown shutdown.request_shutdown()