class Application: def __init__(self, config_dir=None, persist_command=None): # Config self.config_manager = ConfigManager(config_dir=config_dir, persist_command=persist_command) self.config = self.config_manager.get_sync() self.name = self.config.get('name', 'brick') self.mode = self.config.get('mode', 'normal') self.mqtt_config = self.config.get('mqtt', dict()) # Logging log_config = self.config.get('log', dict()) self.log_collector = LogCollector() self.stdout_logger = StdoutLogConsumer( self.log_collector, level=log_config.get('default', 'info'), components=log_config.get('components', dict()), ) self.log = self.log_collector.get_logger('app') # ConfigManager log self.config_manager.set_log(self.log_collector.get_logger('config')) # Dispatcher self.dispatcher = Dispatcher( self.log_collector.get_logger('dispatcher')) # Hardware try: self.hardware = HardwareManager( self.log_collector, self.dispatcher, self.config.get('hardware', dict()), ) except Exception as error: self.hardware = FakeComponent() self.log.exception('hardware component discarded', error) # Device try: self.device = DeviceManager( self.log_collector, self.dispatcher, self.hardware, self.config.get('devices', dict()), ) except Exception as error: self.device = FakeComponent() self.log.exception('device component discarded', error) # Ntp try: self.ntp = NtpSync( self.log_collector.get_logger('ntp'), self.dispatcher.get_broker('ntp'), self.config.get('ntp', dict()), ) except Exception as error: self.ntp = FakeComponent() self.log.exception('ntp component discarded', error) # Mqtt try: self.mqtt = Mqtt( self.log_collector.get_logger('mqtt'), self.dispatcher.get_broker('mqtt'), name=self.name, config=self.mqtt_config, ) except Exception as error: self.mqtt = FakeComponent() self.log.exception('mqtt component discarded', error) # Web server try: self.web = web.Server( log_collector=self.log_collector, broker=self.dispatcher.get_broker('web'), config_manager=self.config_manager, config=self.config.get('web', dict()), ) except Exception as error: self.web = FakeComponent() self.log.exception('web component discarded') def start(self): loop = asyncio.get_event_loop() loop.create_task(self.run()) loop.run_forever() async def run(self): await self.device.start() # await self.ntp.start() await self.mqtt.start() await self.web.start()
class DispatcherPublishTest(unittest.IsolatedAsyncioTestCase): def setUp(self): self.log = Logger() self.dispatcher = Dispatcher(log=self.log) self.broker = self.dispatcher.get_broker('c') self.broker_1 = self.dispatcher.get_broker('c1') self.broker_2 = self.dispatcher.get_broker('c2') self.broker_3 = self.dispatcher.get_broker('c3') self.broker_4 = self.dispatcher.get_broker('c4') self.broker_5 = self.dispatcher.get_broker('c5') self.callback_1 = Callback() self.callback_2 = Callback() self.callback_3 = Callback() self.callback_4 = Callback() self.callback_5 = Callback() async def test_no_subcribers(self): await self.broker.publish(topic='state', payload='online') async def test_subcribe_no_payload(self): self.broker_1.subscribe(self.callback_1.function) await self.broker.publish(topic='start') self.assertEqual(self.callback_1.called, [dict(sender='c', topic='start', payload=None)]) async def test_subscribe_everything(self): self.broker_1.subscribe(self.callback_1.function) await self.broker.publish(topic='state', payload='online') self.assertEqual(self.callback_1.called, [dict(sender='c', topic='state', payload='online')]) async def test_subscribe_sender(self): self.broker_1.subscribe(self.callback_1.function, sender='c') self.broker_2.subscribe(self.callback_2.function, sender='c1') await self.broker.publish(topic='state', payload='online') self.assertEqual(self.callback_1.called, [dict(sender='c', topic='state', payload='online')]) self.assertEqual(self.callback_2.called, []) async def test_subscribe_topic(self): self.broker_1.subscribe(self.callback_1.function, topic='state') self.broker_2.subscribe(self.callback_2.function, topic='another') await self.broker.publish(topic='state', payload='online') self.assertEqual(self.callback_1.called, [dict(sender='c', topic='state', payload='online')]) self.assertEqual(self.callback_2.called, []) async def test_subscribe_sender_topic(self): self.broker_1.subscribe(self.callback_1.function, sender='c', topic='state') self.broker_2.subscribe(self.callback_2.function, sender='c1', topic='state') self.broker_3.subscribe(self.callback_3.function, sender='c', topic='another') self.broker_4.subscribe(self.callback_4.function, sender='c1', topic='another') await self.broker.publish(topic='state', payload='online') self.assertEqual(self.callback_1.called, [dict(sender='c', topic='state', payload='online')]) self.assertEqual(self.callback_2.called, []) self.assertEqual(self.callback_3.called, []) self.assertEqual(self.callback_4.called, []) async def test_subscribe_many(self): self.broker_1.subscribe(self.callback_1.function) self.broker_2.subscribe(self.callback_2.function, sender='c') self.broker_3.subscribe(self.callback_3.function, topic='state') self.broker_4.subscribe(self.callback_4.function, sender='c', topic='state') self.broker_5.subscribe(self.callback_5.function, topic='another') await self.broker.publish(topic='state', payload='online') self.assertEqual(self.callback_1.called, [dict(sender='c', topic='state', payload='online')]) self.assertEqual(self.callback_2.called, [dict(sender='c', topic='state', payload='online')]) self.assertEqual(self.callback_3.called, [dict(sender='c', topic='state', payload='online')]) self.assertEqual(self.callback_4.called, [dict(sender='c', topic='state', payload='online')]) self.assertEqual(self.callback_5.called, []) async def test_callback_exception(self): async def wrong(**kwargs): raise Exception self.broker_1.subscribe(wrong) self.broker_2.subscribe(self.callback_2.function) await self.broker.publish(topic='state', payload='online') self.assertEqual(self.log.logged, [('exception', 'Callback error - c1 <- c/state')]) self.assertEqual(self.callback_2.called, [dict(sender='c', topic='state', payload='online')]) async def test_callback_exception_typeerror(self): async def wrong(**kwargs): raise TypeError('custom') self.broker_1.subscribe(wrong) self.broker_2.subscribe(self.callback_2.function) await self.broker.publish(topic='state', payload='online') self.assertEqual(self.log.logged, [('exception', 'Callback error - c1 <- c/state')]) self.assertEqual(self.callback_2.called, [dict(sender='c', topic='state', payload='online')]) async def test_callback_not_async(self): def notasync(**kwargs): return self.broker_1.subscribe(notasync) self.broker_2.subscribe(self.callback_2.function) await self.broker.publish(topic='state', payload='online') self.assertEqual(self.log.logged, [('error', 'Callback not async - c1 <- c/state')]) self.assertEqual(self.callback_2.called, [dict(sender='c', topic='state', payload='online')])