def __init__(self, worker_input_queue, aggregator, logger, asyncio_loop): self.logger = logger.getLogger(subsystem='signal_bot') self.worker_input_queue = worker_input_queue self.aggregator = aggregator self.aggregator.signal_bot = self self.bus = ravel.system_bus() self.bus.attach_asyncio(asyncio_loop)
def _setup_loop(self): self._loop = asyncio.new_event_loop() self.bus = ravel.system_bus() self.bus.attach_asyncio(self._loop) self.bus.listen_objects_added(self.object_added) self.bus.listen_objects_removed(self.object_removed)
def main(): logging.basicConfig(level=logging.DEBUG) loop = asyncio.get_event_loop() bus = ravel.system_bus(managed_objects=True) bus.attach_asyncio(loop) loop.run_until_complete(client(bus))
def pair_with_agent(_osmc_bt, device_uuid, adapter_pattern=None, capability='KeyboardDisplay', timeout=15000): global osmc_bt, mainloop, agent, dev_path, device_obj, paired osmc_bt = _osmc_bt paired = False try: device = bluezutils.find_device(device_uuid, adapter_pattern) except: device = None return_status('DEVICE_NOT_FOUND', ['Device not Found']) return False dev_path = device.object_path rvl_conn = ravel.system_bus() # Check if already paired message = dbsy.Message.new_method_call \ ( destination = dbsy.valid_bus_name(BUS_NAME), path = dbsy.valid_path(dev_path), iface = "org.freedesktop.DBus.Properties", method = "Get" ) message.append_objects('ss', "org.bluez.Device1", 'Paired') error = None reply = rvl_conn.connection.send_with_reply_and_block(message, error=error) if error != None and reply.type == DBUS.MESSAGE_TYPE_ERROR: reply.set_error(error) result = None set_trusted(rvl_conn, False) rvl_conn = None return paired else: result = reply.expect_return_objects("v")[0] if result[0] == 'b' and result[1] == True: print('Already paired') paired = True set_trusted(rvl_conn, True) rvl_conn = None return paired # Create the agent object agent = AgentInterface(rvl_conn, capability) # This bit needed to run in Spyder3 IDE try: nest_asyncio.apply() except: pass # there's probably a more elegant way to do this try: mainloop = asyncio.get_running_loop() if mainloop: mainloop.stop() except: pass mainloop = asyncio.new_event_loop() rvl_conn.attach_asyncio(mainloop) message = dbsy.Message.new_method_call \ ( destination = dbsy.valid_bus_name(BUS_NAME), path = dbsy.valid_path(dev_path), iface = "org.bluez.Device1", method = "Pair" ) async def pair(conn, message): print('Pairing') await_reply = await conn.connection.send_await_reply(message) print('Finished') return await_reply reply = mainloop.run_until_complete(pair(rvl_conn, message)) error_name, error_message = handle_reply(reply, None) print(error_name) if error_name == "org.freedesktop.DBus.Error.NoReply" and device: error_message = 'Timed out. Cancelling pairing' message = dbsy.Message.new_method_call \ ( destination = dbsy.valid_bus_name(BUS_NAME), path = dbsy.valid_path(dev_path), iface = "org.bluez.Device1", method = "CancelPairing" ) try: rvl_conn.connection.send_with_reply_and_block(message) set_trusted(rvl_conn, False) except: pass if error_message is not None: print('PAIRING_FAILED ' + error_message) return_status('PAIRING_FAILED', [error_message]) try: set_trusted(rvl_conn, False) except: pass else: print('PAIRING_OK') return_status('PAIRING_OK', []) paired = True set_trusted(rvl_conn, True) agent.close() rvl_conn = None return paired
def call_method(bus_name, path, interface, method_name, *args, **kwargs): interface = BUS[bus_name][path].get_interface(interface) method = getattr(interface, method_name) result = method(*args, **kwargs) first = next(iter(result or []), None) return convert_from_dbussy(first) async def call_async_method(bus_name, path, interface, method_name, *args, **kwargs): interface = await BUS[bus_name][path].get_async_interface(interface) method = getattr(interface, method_name) result = await method(*args, **kwargs) first = next(iter(result or []), None) return convert_from_dbussy(first) def run_method(bus_name, path, interface, method_name, *args, **kwargs): future = asyncio.run_coroutine_threadsafe( call_async_method(bus_name, path, interface, method_name, *args, **kwargs), LOOP) return future.result() LOOP = asyncio.get_event_loop() BUS = ravel.system_bus() BUS.attach_asyncio(LOOP) LOOP_THREAD = LoopThread(LOOP)
import logging import ravel import asyncio import euc.device async def run_cli(system_bus): devices = await euc.device.list(system_bus) print("detected devices:", devices) loop = asyncio.get_event_loop() for device in devices: device.add_properties_changed_callback( lambda self, props: print(f"{self.properties!r}")) loop.create_task(device.run()) if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) system_bus = ravel.system_bus() loop = asyncio.get_event_loop() system_bus.attach_asyncio(loop) loop.create_task(run_cli(system_bus)) loop.run_forever()