async def start(self, state_updater=False, daemon_mode=False, connection_config=None): """Start XKNX module. Connect to KNX/IP devices and start state updater.""" if connection_config is None: if self.connection_config is None: connection_config = ConnectionConfig() else: connection_config = self.connection_config self.knxip_interface = KNXIPInterface( self, connection_config=connection_config) self.logger.info('XKNX v%s starting %s connection to KNX bus.', VERSION, connection_config.connection_type.name.lower()) await self.knxip_interface.start() await self.telegram_queue.start() if state_updater: from xknx.core import StateUpdater self.state_updater = StateUpdater(self) await self.state_updater.start() if daemon_mode: await self.loop_until_sigint() self.started = True
def start(self, state_updater=False, daemon_mode=False, connection_config=ConnectionConfig()): """Start XKNX module. Connect to KNX/IP devices and start state updater.""" self.knxip_interface = KNXIPInterface( self, connection_config=connection_config) yield from self.knxip_interface.start() yield from self.telegram_queue.start() if state_updater: from xknx.core import StateUpdater self.state_updater = StateUpdater(self) yield from self.state_updater.start() if daemon_mode: yield from self.loop_until_sigint() self.started = True
def async_start(self, state_updater=False, daemon_mode=False, telegram_received_callback=None): self.knxip_interface = KNXIPInterface(self) yield from self.knxip_interface.start() if telegram_received_callback is not None: self.telegram_queue.telegram_received_callback =\ telegram_received_callback yield from self.telegram_queue.start() if state_updater: from .stateupdater import StateUpdater self.state_updater = StateUpdater(self) yield from self.state_updater.start() if daemon_mode: yield from self.loop_until_sigint()
async def start(self): """Start XKNX module. Connect to KNX/IP devices and start state updater.""" self.knxip_interface = KNXIPInterface( self, connection_config=self.connection_config) logger.info( "XKNX v%s starting %s connection to KNX bus.", VERSION, self.connection_config.connection_type.name.lower(), ) await self.knxip_interface.start() await self.telegram_queue.start() if self.start_state_updater: self.state_updater.start() self.started.set() if self.daemon_mode: await self.loop_until_sigint()
class XKNX: """Class for reading and writing KNX/IP packets.""" # pylint: disable=too-many-instance-attributes DEFAULT_ADDRESS = '15.15.250' def __init__(self, config=None, loop=None, own_address=Address(DEFAULT_ADDRESS), address_format=AddressFormat.LEVEL3, telegram_received_cb=None, device_updated_cb=None): """Initialize XKNX class.""" # pylint: disable=too-many-arguments self.devices = Devices() self.telegrams = asyncio.Queue() self.loop = loop or asyncio.get_event_loop() self.sigint_received = asyncio.Event() self.telegram_queue = TelegramQueue(self) self.state_updater = None self.knxip_interface = None self.started = False self.address_format = address_format self.own_address = own_address self.logger = logging.getLogger('xknx.log') self.knx_logger = logging.getLogger('xknx.knx') self.telegram_logger = logging.getLogger('xknx.telegram') if config is not None: Config(self).read(config) if telegram_received_cb is not None: self.telegram_queue.register_telegram_received_cb( telegram_received_cb) if device_updated_cb is not None: self.devices.register_device_updated_cb(device_updated_cb) def __del__(self): """Destructor. Cleaning up if this was not done before.""" if self.started: try: task = self.loop.create_task(self.stop()) self.loop.run_until_complete(task) except RuntimeError as exp: self.logger.warning("Could not close loop, reason: %s", exp) @asyncio.coroutine def start(self, state_updater=False, daemon_mode=False, connection_config=ConnectionConfig()): """Start XKNX module. Connect to KNX/IP devices and start state updater.""" self.knxip_interface = KNXIPInterface( self, connection_config=connection_config) yield from self.knxip_interface.start() yield from self.telegram_queue.start() if state_updater: from xknx.core import StateUpdater self.state_updater = StateUpdater(self) yield from self.state_updater.start() if daemon_mode: yield from self.loop_until_sigint() self.started = True @asyncio.coroutine def join(self): """Wait until all telegrams were processed.""" yield from self.telegrams.join() def _stop_knxip_interface_if_exists(self): """Stop KNXIPInterface if initialized.""" if self.knxip_interface is not None: yield from self.knxip_interface.stop() self.knxip_interface = None @asyncio.coroutine def stop(self): """Stop XKNX module.""" yield from self.join() yield from self.telegram_queue.stop() yield from self._stop_knxip_interface_if_exists() self.started = False @asyncio.coroutine def loop_until_sigint(self): """Loop until Crtl-C was pressed.""" def sigint_handler(): """End loop.""" self.sigint_received.set() self.loop.add_signal_handler(signal.SIGINT, sigint_handler) self.logger.warning('Press Ctrl+C to stop') yield from self.sigint_received.wait()
class XKNX: # pylint: disable=too-many-instance-attributes def __init__(self, loop=None, own_address=None, start=True, state_updater=False, daemon_mode=False, telegram_received_callback=None): self.globals = Globals() self.devices = Devices() self.telegrams = asyncio.Queue() self.loop = loop or asyncio.get_event_loop() self.sigint_recieved = asyncio.Event() self.telegram_queue = TelegramQueue(self) self.state_updater = None self.knxip_interface = None if own_address is not None: self.globals.own_address = Address(own_address) if start: self.start(state_updater=state_updater, daemon_mode=daemon_mode, telegram_received_callback=telegram_received_callback) def __del__(self): try: task = asyncio.Task(self.stop_knxip_interface_if_exists()) self.loop.run_until_complete(task) except: pass def start(self, state_updater=False, daemon_mode=False, telegram_received_callback=None): task = asyncio.Task( self.async_start( state_updater=state_updater, daemon_mode=daemon_mode, telegram_received_callback=telegram_received_callback)) self.loop.run_until_complete(task) @asyncio.coroutine def async_start(self, state_updater=False, daemon_mode=False, telegram_received_callback=None): self.knxip_interface = KNXIPInterface(self) yield from self.knxip_interface.start() if telegram_received_callback is not None: self.telegram_queue.telegram_received_callback =\ telegram_received_callback yield from self.telegram_queue.start() if state_updater: from .stateupdater import StateUpdater self.state_updater = StateUpdater(self) yield from self.state_updater.start() if daemon_mode: yield from self.loop_until_sigint() def process_all_telegrams(self): task = asyncio.Task(self.telegram_queue.process_all_telegrams()) self.loop.run_until_complete(task) @asyncio.coroutine def join(self): """ Wait until all telegrams were processed """ yield from self.telegrams.join() def stop_knxip_interface_if_exists(self): if self.knxip_interface is not None: yield from self.knxip_interface.async_stop() self.knxip_interface = None def stop(self): task = asyncio.Task(self.async_stop()) self.loop.run_until_complete(task) @asyncio.coroutine def async_stop(self): yield from self.join() yield from self.stop_knxip_interface_if_exists() @asyncio.coroutine def loop_until_sigint(self): def sigint_handler(): self.sigint_recieved.set() self.loop.add_signal_handler(signal.SIGINT, sigint_handler) print('Press Ctrl+C to stop') yield from self.sigint_recieved.wait() yield from self.async_stop()