def on_connect_sensors(): app.logger.debug("websocket connect to '%s'", SIO_SENSOR_NAMESPACE) from sensors.tdbus_upower import ibatteries, connect_dbus_system, uPowerDeviceGetAll conn = connect_dbus_system() for bat in ibatteries(conn): socketio.emit(SIO_EVT_BATTERY_CHANGED, uPowerDeviceGetAll(conn, bat), namespace=SIO_SENSOR_NAMESPACE) conn.close()
def socketio_background_thread(socketio_emit, socketio_event, namespace): log.debug("Starting upower dispatcher loop for socketio namespace %s", namespace) if not upower_present(connect_dbus_system): raise EnvironmentError("UPower not present on DBUS") conn = connect_dbus_system() hndlr = UPowerDeviceHandler(connect_dbus_system, set(ibatteries(conn))) from functools import partial notify = partial(socketio_notify, socketio_emit, socketio_event, namespace) hndlr.register_observer(notify, devices=None) conn.add_handler(hndlr) conn.subscribe_to_signals() # basic select() loop, i.e. we assume there is no event loop conn.dispatch()