def test_background_consumer(self): """ Test the background consumer mechanism. """ action = master_api.basic_action() in_fields = {"action_type": 1, "action_number": 2} out_fields = {"resp": "OK"} serial_mock = SerialMock([ sout("OL\x00\x01\x03\x0c\r\n"), sin(action.create_input(1, in_fields)), sout("junkOL\x00\x02\x03\x0c\x05\x06\r\n here"), sout(action.create_output(1, out_fields))]) comm = MasterCommunicator(serial_mock, init_master=False) comm.enable_passthrough() got_output = {"phase": 1} def callback(output): """ Callback that check if the correct result was returned for OL. """ if got_output["phase"] == 1: self.assertEquals([(3, int(12 * 10.0 / 6.0))], output["outputs"]) got_output["phase"] = 2 elif got_output["phase"] == 2: self.assertEquals([(3, int(12 * 10.0 / 6.0)), (5, int(6 * 10.0 / 6.0))], output["outputs"]) got_output["phase"] = 3 comm.register_consumer(BackgroundConsumer(master_api.output_list(), 0, callback)) comm.start() self.assertEquals("OK", comm.do_command(action, in_fields)["resp"]) self.assertEquals(3, got_output["phase"]) self.assertEquals("junk here", comm.get_passthrough_data())
def test_background_consumer_passthrough(self): """ Test the background consumer with passing the data to the passthrough. """ serial_mock = SerialMock([sout("OL\x00\x01"), sout("\x03\x0c\r\n")]) comm = MasterCommunicator(serial_mock, init_master=False) comm.enable_passthrough() got_output = {"passed": False} def callback(output): """ Callback that check if the correct result was returned for OL. """ self.assertEquals([(3, int(12 * 10.0 / 6.0))], output["outputs"]) got_output["passed"] = True comm.register_consumer(BackgroundConsumer(master_api.output_list(), 0, callback, True)) comm.start() MasterCommunicatorTest._wait_for_callback(True, got_output, 3) self.assertEquals(True, got_output["passed"]) self.assertEquals("OL\x00\x01\x03\x0c\r\n", comm.get_passthrough_data())
def test_background_consumer_passthrough(self): """ Test the background consumer with passing the data to the passthrough. """ serial_mock = SerialMock([sout("OL\x00\x01"), sout("\x03\x0c\r\n")]) comm = MasterCommunicator(serial_mock, init_master=False) got_output = {"passed": False} def callback(output): """ Callback that check if the correct result was returned for OL. """ self.assertEquals([(3, int(12 * 10.0 / 6.0))], output["outputs"]) got_output["passed"] = True comm.register_consumer( BackgroundConsumer(master_api.output_list(), 0, callback, True)) comm.start() self.assertEquals(True, got_output["passed"]) self.assertEquals("OL\x00\x01\x03\x0c\r\n", comm.get_passthrough_data())
def __init__(self, master_communicator, dbus_service): """ :param master_communicator: Master communicator :type master_communicator: master.master_communicator.MasterCommunicator :param dbus_service: DBusService instance :type dbus_service: bus.dbus_service.DBusService """ self._master_communicator = master_communicator self._dbus_service = dbus_service self._gateway_api = None self._master_subscriptions = {Observer.MasterEvents.ON_OUTPUTS: [], Observer.MasterEvents.ON_SHUTTER_UPDATE: [], Observer.MasterEvents.INPUT_TRIGGER: []} self._event_subscriptions = [] self._input_status = InputStatus() self._output_status = OutputStatus(on_output_change=self._output_changed) self._thermostat_status = ThermostatStatus(on_thermostat_change=self._thermostat_changed, on_thermostat_group_change=self._thermostat_group_changed) self._shutter_status = ShutterStatus(on_shutter_change=self._shutter_changed) self._output_interval = 600 self._output_last_updated = 0 self._output_config = {} self._thermostats_original_interval = 30 self._thermostats_interval = self._thermostats_original_interval self._thermostats_last_updated = 0 self._thermostats_restore = 0 self._thermostats_config = {} self._shutters_interval = 600 self._shutters_last_updated = 0 self._thread = Thread(target=self._monitor) self._thread.daemon = True self._master_communicator.register_consumer(BackgroundConsumer(master_api.output_list(), 0, self._on_output, True)) self._master_communicator.register_consumer(BackgroundConsumer(master_api.input_list(), 0, self._on_input)) self._master_communicator.register_consumer(BackgroundConsumer(master_api.shutter_status(), 0, self._on_shutter_update))
def test_background_consumer(self): """ Test the background consumer mechanism. """ action = master_api.basic_action() in_fields = {"action_type": 1, "action_number": 2} out_fields = {"resp": "OK"} serial_mock = SerialMock([ sout("OL\x00\x01\x03\x0c\r\n"), sin(action.create_input(1, in_fields)), sout("junkOL\x00\x02\x03\x0c\x05\x06\r\n here"), sout(action.create_output(1, out_fields)) ]) SetUpTestInjections(controller_serial=serial_mock) comm = MasterCommunicator(init_master=False) comm.enable_passthrough() got_output = {"phase": 1} def callback(output): """ Callback that check if the correct result was returned for OL. """ if got_output["phase"] == 1: self.assertEquals([(3, int(12 * 10.0 / 6.0))], output["outputs"]) got_output["phase"] = 2 elif got_output["phase"] == 2: self.assertEquals([(3, int(12 * 10.0 / 6.0)), (5, int(6 * 10.0 / 6.0))], output["outputs"]) got_output["phase"] = 3 comm.register_consumer( BackgroundConsumer(master_api.output_list(), 0, callback)) comm.start() self.assertEquals("OK", comm.do_command(action, in_fields)["resp"]) self.assertEquals(3, got_output["phase"]) self.assertEquals("junk here", comm.get_passthrough_data())
def __init__(self, master_communicator=INJECTED, eeprom_controller=INJECTED): """ :type master_communicator: master.master_communicator.MasterCommunicator :type eeprom_controller: master.eeprom_controller.EepromController """ super(MasterClassicController, self).__init__(master_communicator) self._eeprom_controller = eeprom_controller self._output_status = OutputStatus( on_output_change=self._output_changed) self._synchronization_thread = Thread( target=self._synchronize, name='ClassicMasterSynchronization') self._master_version = None self._master_online = False self._output_interval = 600 self._output_last_updated = 0 self._output_config = {} self._master_communicator.register_consumer( BackgroundConsumer(master_api.output_list(), 0, self._on_master_output_change, True))
def main(): """ Main function. """ config = ConfigParser() config.read(constants.get_config_file()) defaults = { 'username': config.get('OpenMotics', 'cloud_user'), 'password': config.get('OpenMotics', 'cloud_pass') } controller_serial_port = config.get('OpenMotics', 'controller_serial') passthrough_serial_port = config.get('OpenMotics', 'passthrough_serial') power_serial_port = config.get('OpenMotics', 'power_serial') gateway_uuid = config.get('OpenMotics', 'uuid') config_lock = threading.Lock() user_controller = UserController(constants.get_config_database_file(), config_lock, defaults, 3600) config_controller = ConfigurationController( constants.get_config_database_file(), config_lock) led_service = LedService() controller_serial = Serial(controller_serial_port, 115200) power_serial = RS485(Serial(power_serial_port, 115200, timeout=None)) master_communicator = MasterCommunicator(controller_serial) if passthrough_serial_port: passthrough_serial = Serial(passthrough_serial_port, 115200) passthrough_service = PassthroughService(master_communicator, passthrough_serial) passthrough_service.start() master_communicator.start( ) # A running master_communicator is required for the startup of services below power_controller = PowerController(constants.get_power_database_file()) power_communicator = PowerCommunicator(power_serial, power_controller) gateway_api = GatewayApi(master_communicator, power_communicator, power_controller) scheduling_controller = SchedulingController( constants.get_scheduling_database_file(), config_lock, gateway_api) maintenance_service = MaintenanceService( gateway_api, constants.get_ssl_private_key_file(), constants.get_ssl_certificate_file()) web_interface = WebInterface(user_controller, gateway_api, maintenance_service, led_service.in_authorized_mode, config_controller, scheduling_controller) scheduling_controller.set_webinterface(web_interface) plugin_controller = PluginController(web_interface, config_controller) web_interface.set_plugin_controller(plugin_controller) gateway_api.set_plugin_controller(plugin_controller) # Metrics metrics_cache_controller = MetricsCacheController( constants.get_metrics_database_file(), threading.Lock()) metrics_collector = MetricsCollector(gateway_api) metrics_controller = MetricsController(plugin_controller, metrics_collector, metrics_cache_controller, config_controller, gateway_uuid) metrics_collector.set_controllers(metrics_controller, plugin_controller) metrics_collector.set_plugin_intervals(plugin_controller.metric_intervals) metrics_controller.add_receiver(metrics_controller.receiver) metrics_controller.add_receiver(web_interface.distribute_metric) plugin_controller.set_metrics_controller(metrics_controller) web_interface.set_metrics_collector(metrics_collector) web_interface.set_metrics_controller(metrics_controller) web_service = WebService(web_interface, config_controller) def _on_output(*args, **kwargs): metrics_collector.on_output(*args, **kwargs) gateway_api.on_outputs(*args, **kwargs) def _on_input(*args, **kwargs): metrics_collector.on_input(*args, **kwargs) gateway_api.on_inputs(*args, **kwargs) master_communicator.register_consumer( BackgroundConsumer(master_api.output_list(), 0, _on_output, True)) master_communicator.register_consumer( BackgroundConsumer(master_api.input_list(), 0, _on_input)) power_communicator.start() plugin_controller.start_plugins() metrics_controller.start() scheduling_controller.start() metrics_collector.start() web_service.start() led_thread = threading.Thread(target=led_driver, args=(led_service, master_communicator, power_communicator)) led_thread.setName("Serial led driver thread") led_thread.daemon = True led_thread.start() def stop(signum, frame): """ This function is called on SIGTERM. """ _ = signum, frame sys.stderr.write("Shutting down") web_service.stop() metrics_collector.stop() metrics_controller.stop() plugin_controller.stop() signal(SIGTERM, stop)