Beispiel #1
0
    async def _start(self, loop) -> None:
        error = None
        try:
            # Configuration handling - initial configuration
            category = self._name
            config = self._DEFAULT_CONFIG
            config_descr = self._name
            config_payload = json.dumps({
                "key": category,
                "description": config_descr,
                "value": config,
                "keep_original_items": True
            })
            self._core_microservice_management_client.create_configuration_category(
                config_payload)
            config = self._core_microservice_management_client.get_configuration_category(
                category_name=category)

            try:
                plugin_module_name = config['plugin']['value']
            except KeyError:
                message = self._MESSAGES_LIST['e000002'].format(self._name)
                _LOGGER.error(message)
                raise

            try:
                import_file_name = "{path}.{dir}.{file}".format(
                    path=self._PLUGIN_MODULE_PATH,
                    dir=plugin_module_name,
                    file=plugin_module_name)
                self._plugin = __import__(import_file_name, fromlist=[''])
            except Exception as ex:
                message = self._MESSAGES_LIST['e000003'].format(
                    plugin_module_name, self._name, str(ex))
                _LOGGER.error(message)
                raise

            # Plugin initialization
            self._plugin_info = self._plugin.plugin_info()
            default_config = self._plugin_info['config']
            default_plugin_descr = self._name if (default_config['plugin']['description']).strip() == "" else \
                default_config['plugin']['description']

            # Configuration handling - updates the configuration using information specific to the plugin
            config_payload = json.dumps({
                "key": category,
                "description": default_plugin_descr,
                "value": default_config,
                "keep_original_items": True
            })
            self._core_microservice_management_client.create_configuration_category(
                config_payload)
            config = self._core_microservice_management_client.get_configuration_category(
                category_name=category)

            # Register interest with category and microservice_id
            result = self._core_microservice_management_client.register_interest(
                category, self._microservice_id)

            # KeyError when result (id and message) keys are not found
            registration_id = result['id']
            message = result['message']

            # Ensures the plugin type is the correct one - 'south'
            if self._plugin_info['type'] != 'south':
                message = self._MESSAGES_LIST['e000001'].format(
                    self._name, self._plugin_info['type'])
                _LOGGER.error(message)
                raise exceptions.InvalidPluginTypeError()

            self._plugin_handle = self._plugin.plugin_init(config)

            await Ingest.start(self)

            # Executes the requested plugin type
            if self._plugin_info['mode'] == 'async':
                self._task_main = asyncio.ensure_future(
                    self._exec_plugin_async())
            elif self._plugin_info['mode'] == 'poll':
                self._task_main = asyncio.ensure_future(
                    self._exec_plugin_poll())
        except asyncio.CancelledError:
            pass
        except exceptions.DataRetrievalError:
            _LOGGER.exception('Data retrieval error in plugin {}'.format(
                self._name))
        except (Exception, KeyError) as ex:
            if error is None:
                error = 'Failed to initialize plugin {}'.format(self._name)
            _LOGGER.exception(error)
            asyncio.ensure_future(self._stop(loop))
Beispiel #2
0
    async def _start(self, loop) -> None:
        error = None

        try:
            category = self._name
            # Configuration handling - initial configuration
            config = self._DEFAULT_CONFIG
            config_descr = '{} Device'.format(self._name)

            cfg_manager = ConfigurationManager(self._storage)

            await cfg_manager.create_category(category, config, config_descr,
                                              True)

            config = await cfg_manager.get_category_all_items(category)

            try:
                plugin_module_name = config['plugin']['value']
            except KeyError:
                message = self._MESSAGES_LIST['e000002'].format(self._name)
                _LOGGER.error(message)
                raise

            try:
                import_file_name = "{path}.{dir}.{file}".format(
                    path=self._PLUGIN_MODULE_PATH,
                    dir=plugin_module_name,
                    file=plugin_module_name)
                self._plugin = __import__(import_file_name, fromlist=[''])
            except Exception as ex:
                message = self._MESSAGES_LIST['e000003'].format(
                    plugin_module_name, self._name, str(ex))
                _LOGGER.error(message)
                raise

            # Plugin initialization
            plugin_info = self._plugin.plugin_info()
            default_config = plugin_info['config']

            # Configuration handling - updates the configuration using information specific to the plugin
            await cfg_manager.create_category(category, default_config,
                                              '{} Device'.format(self._name))

            config = await cfg_manager.get_category_all_items(category)

            # TODO: Register for config changes

            # Ensures the plugin type is the correct one - 'south'
            if plugin_info['type'] != 'south':

                message = self._MESSAGES_LIST['e000001'].format(
                    self._name, plugin_info['type'])
                _LOGGER.error(message)

                raise exceptions.InvalidPluginTypeError()

            self._plugin_handle = self._plugin.plugin_init(config)

            # Executes the requested plugin type
            if plugin_info['mode'] == 'async':
                await self._exec_plugin_async(config)

            elif plugin_info['mode'] == 'poll':
                asyncio.ensure_future(self._exec_plugin_poll(config))

        except Exception as ex:
            if error is None:
                error = 'Failed to initialize plugin {}'.format(self._name)
            _LOGGER.exception(error)
            print(error, str(ex))
            asyncio.ensure_future(self._stop(loop))