async def fake_connect(): """Use by SmartBridge to connect to the test.""" closed = asyncio.Event() reader = _FakeLeapReader(closed, get_loop()) writer = _FakeLeapWriter(closed, get_loop()) await self.connections.put((reader, writer)) return (reader, writer)
async def _login(self): """Connect and login to the Smart Bridge LEAP server using SSL.""" async with self._login_lock: if self._reader is not None and self._writer is not None: if (self.logged_in and self._reader.exception() is None and not self._reader.at_eof()): return self._writer.abort() self._reader = self._writer = None self.logged_in = False _LOG.debug("Connecting to Smart Bridge via SSL") self._reader, self._writer = await self._connect() _LOG.debug("Successfully connected to Smart Bridge.") await self._load_devices() await self._load_scenes() await self._load_areas() await self._load_occupancy_groups() await self._subscribe_to_occupancy_groups() for device in self.devices.values(): if device.get('zone') is not None: _LOG.debug("Requesting zone information from %s", device) cmd = { "CommuniqueType": "ReadRequest", "Header": { "Url": "/zone/%s/status" % device['zone'] } } self._writer.write(cmd) self._ping_task = get_loop().create_task(self._ping()) self.logged_in = True
async def connect(self): """Connect to the bridge.""" # reset any existing connection state if self._login_task is not None: self._login_task.cancel() self._login_task = None if self._monitor_task is not None: self._monitor_task.cancel() self._monitor_task = None if self._ping_task is not None: self._ping_task.cancel() self._ping_task = None if self._leap is not None: self._leap.close() self._leap = None if not self._login_completed.done(): self._login_completed.cancel() self._login_completed = asyncio.get_running_loop().create_future() self._monitor_task = get_loop().create_task(self._monitor()) await self._login_completed
async def test_reconnect_timeout(): """Test that SmartBridge can reconnect if the remote does not respond.""" bridge = Bridge() time = 0.0 get_loop().time = lambda: time await bridge.initialize() time = smartbridge.PING_INTERVAL ping = await bridge.writer.queue.get() assert ping == { "CommuniqueType": "ReadRequest", "Header": {"Url": "/server/1/status/ping"}} time += smartbridge.PING_DELAY await bridge.accept_connection() bridge.target.set_value('2', 50) command = await bridge.writer.queue.get() bridge.writer.queue.task_done() assert command is not None await bridge.target.close()
async def o(ctx): channel = ctx.channel try: _, code, data = ctx.message.content.split("```", maxsplit=2) except ValueError as e: return await channel.send( "usage: ,,o \```[o5ab1e code]``` {input}\n\nEmpty input uses the previous message as input." ) if not data: try: data = [m.content async for m in channel.history(limit=2)][1] except IndexError: pass elif data[0] == " ": data = data[1:] print(f"Running in ({channel.guild}){channel.name}:\n" f"{code}\n" + f"+++++ With input: +++++\n{data}\n\n" if data else "\n\n") result = get_loop().run_in_executor(None, osable, code, data, default_timeout) result_message = await channel.send("...") try: await result_message.edit(content=(await result)) except TimeoutError: await result_message.edit(content="Program timed out.") except subprocess.CalledProcessError as e: await result_message.edit(content=f"Error running:\n\n{e}")
async def wait(coro): # abort if SmartBridge reports it has finished connecting early task = get_loop().create_task(coro) r = await asyncio.wait((connect_task, task), timeout=10, return_when=asyncio.FIRST_COMPLETED) done, pending = r assert len(done) > 0, "operation timed out" if len(done) == 1 and connect_task in done: raise connect_task.exception() result = await task return result
async def initialize(self): """Perform the initial connection with SmartBridge.""" connect_task = get_loop().create_task(self.target.connect()) reader, writer = await self.connections.get() async def wait(coro): # abort if SmartBridge reports it has finished connecting early task = get_loop().create_task(coro) r = await asyncio.wait((connect_task, task), timeout=10, return_when=asyncio.FIRST_COMPLETED) done, pending = r assert len(done) > 0, "operation timed out" if len(done) == 1 and connect_task in done: raise connect_task.exception() result = await task return result await self._accept_connection(reader, writer, wait) await connect_task self.reader = reader self.writer = writer self.connections.task_done()
async def connect(self): """Connect to the bridge.""" await self._login() self._monitor_task = get_loop().create_task(self._monitor())