Exemplo n.º 1
0
 def __init__(self, print_fn=print, loop=None):
     self._print_fn = print_fn
     self._driver = WhalesongDriver(profile=path.join(
         path.dirname(__file__), '..', 'profile'),
                                    loadstyles=True,
                                    loop=loop)
Exemplo n.º 2
0
class GetMessages:
    def __init__(self, print_fn=print, loop=None):
        self._print_fn = print_fn
        self._driver = WhalesongDriver(
            profile=path.join(path.dirname(__file__), '..', 'profile'),
            # loadstyles=False,
            loop=loop)

    def echo(self, txt):
        self._print_fn(txt)

    @property
    def loop(self):
        return self._driver.loop

    async def init(self):
        await self._driver.start_driver()
        await self._driver.connect()
        self.echo('Connected')

    async def check_stream(self):
        stream = await self._driver.execute_command('stream|getModel')
        self.echo("Stream: {}".format(stream['stream']))
        self.echo("State: {}".format(stream['state']))

    async def monitor_stream(self):
        self.echo('Monitor stream')
        messages_it = None
        new_message_monitor = None
        message_ack_monitor = None

        async for evt in self._driver.execute_command(
                'stream|monitorField', {'field': 'stream'},
                result_class=MonitorResult):
            self.echo('Stream value: {}'.format(evt['value']))

            if evt['value'] == 'CONNECTED':
                if messages_it is None:
                    messages_it = self._driver.execute_command(
                        'messages|getItems', result_class=IteratorResult)
                    ensure_future(self.list_messages(messages_it))

                if new_message_monitor is None:
                    new_message_monitor = self._driver.execute_command(
                        'messages|monitorNew', result_class=MonitorResult)
                    ensure_future(
                        self.monitor_new_messages(new_message_monitor))

                if message_ack_monitor is None:
                    message_ack_monitor = self._driver.execute_command(
                        'messages|monitorField', {'field': 'ack'},
                        result_class=MonitorResult)
                    ensure_future(
                        self.monitor_message_acks(message_ack_monitor))
            else:
                if messages_it is not None:
                    messages_it = None
                    await self._driver.cancel_iterators()

                if new_message_monitor is not None:
                    self._driver.execute_command(
                        'stopMonitor',
                        {'monitorId': new_message_monitor.result_id})
                    new_message_monitor = None

                if message_ack_monitor is not None:
                    self._driver.execute_command(
                        'stopMonitor',
                        {'monitorId': message_ack_monitor.result_id})
                    message_ack_monitor = None

    async def list_messages(self, it):
        self.echo('List messages')
        async for message in it:
            self.echo('Message: {}'.format(message))

        self.echo('List messages finished')

    async def monitor_new_messages(self, it):
        self.echo('Monitor new messages')
        async for message in it:
            self.echo('New message: {}'.format(message))

        self.echo('Stop new messages bot')

    async def monitor_message_acks(self, it):
        self.echo('Monitor message ack')
        async for ack in it:
            self.echo('ACK: {}'.format(ack))

        self.echo('Stop message acks bot')

    async def start(self):
        await self.init()
        futs = [ensure_future(self.monitor_stream())]

        try:
            await self._driver.whai_until_stop()
        finally:
            await self._driver.cancel_iterators()
            self._driver.result_manager.cancel_all()
            await wait(futs)
            await self._driver.close()
Exemplo n.º 3
0
class StatusMonitor:
    def __init__(self, print_fn=print, loop=None):
        self._print_fn = print_fn
        self._driver = WhalesongDriver(profile=path.join(
            path.dirname(__file__), '..', 'profile'),
                                       loadstyles=True,
                                       loop=loop)

    def echo(self, txt):
        self._print_fn(txt)

    @property
    def loop(self):
        return self._driver.loop

    async def init(self):
        await self._driver.start_driver()
        await self._driver.connect()
        self.echo('Connected')

    async def check_stream(self):
        stream = await self._driver.execute_command('stream|getModel')
        self.echo("Stream: {}".format(stream['stream']))
        self.echo("State: {}".format(stream['state']))

    async def check_conn(self):
        stream = await self._driver.execute_command('conn|getModel')
        self.echo("REF: {}".format(stream['ref']))
        self.echo("Battery: {}".format(stream['battery']))

    async def check_storage(self):
        storage = await self._driver.execute_command('storage|getStorage')
        self.echo("Storage: {}".format(storage))

    async def monitor_stream(self):
        self.echo('Monitor stream')
        async for evt in self._driver.execute_command(
                'stream|monitorField',
                params={'field': 'stream'},
                result_class=MonitorResult):
            self.echo('Stream value: {}'.format(evt['value']))
            img = await self._driver.screenshot()
            with open(path.join(OUTPUT_DIR, 'screenshot.png'), 'wb') as f:
                f.write(img.read())

            self.echo('Screenshot saved!')

    async def monitor_state(self):
        self.echo('Monitor state')
        async for evt in self._driver.execute_command(
                'stream|monitorField', {'field': 'state'},
                result_class=MonitorResult):
            self.echo('State value: {}'.format(evt['value']))
            if evt['value'] == 'UNPAIRED_IDLE':
                self.echo('Refreshing QR')
                self._driver.execute_command('stream|poke')
            elif evt['value'] == 'CONFLICT':
                self.echo('Taking over...')
                self._driver.execute_command('stream|takeover')

    async def monitor_ref(self):
        self.echo('Monitor ref')
        async for evt in self._driver.execute_command(
                'conn|monitorField', {'field': 'ref'},
                result_class=MonitorResult):
            self.echo('New REF value: {}'.format(evt['value']))

            try:
                img = await self._driver.screenshot_element('div[data-ref]')
                with open(path.join(OUTPUT_DIR, 'qr.png'), 'wb') as f:
                    f.write(img.read())

                self.echo('QR saved!')
            except Exception as ex:
                self.echo(ex)
                self.echo('Error getting qr')

    async def monitor_battery(self):
        self.echo('Monitor battery')
        async for evt in self._driver.execute_command(
                'conn|monitorField', {'field': 'battery'},
                result_class=MonitorResult):
            self.echo('Battery level: {}'.format(evt['value']))

    async def monitor_storage(self):
        self.echo('Monitor storage')
        async for evt in self._driver.execute_command(
                'storage|monitorItemStorage', result_class=MonitorResult):
            self.echo('New storage item: {}'.format(evt))

    async def start(self):
        await self.init()
        futs = [
            ensure_future(self.check_stream()),
            ensure_future(self.check_conn()),
            ensure_future(self.check_storage()),
            ensure_future(self.monitor_stream()),
            ensure_future(self.monitor_state()),
            ensure_future(self.monitor_ref()),
            ensure_future(self.monitor_battery()),
            ensure_future(self.monitor_storage())
        ]

        try:
            await self._driver.whai_until_stop()
        finally:
            await self._driver.cancel_iterators()
            self._driver.result_manager.cancel_all()
            await wait(futs)
            await self._driver.close()
Exemplo n.º 4
0
class GetChats:
    def __init__(self, print_fn=print, loop=None):
        self._print_fn = print_fn
        self._driver = WhalesongDriver(
            profile=path.join(path.dirname(__file__), '..', 'profile'),
            # loadstyles=False,
            loop=loop)

    def echo(self, txt):
        self._print_fn(txt)

    @property
    def loop(self):
        return self._driver.loop

    async def init(self):
        await self._driver.start_driver()
        await self._driver.connect()
        self.echo('Connected')

    async def check_stream(self):
        stream = await self._driver.execute_command('stream|getModel')
        self.echo("Stream: {}".format(stream['stream']))
        self.echo("State: {}".format(stream['state']))

    async def monitor_stream(self):
        self.echo('Monitor stream')
        contact_it = None

        async for evt in self._driver.execute_command(
                'stream|monitorField', {'field': 'stream'},
                result_class=MonitorResult):
            self.echo('Stream value: {}'.format(evt['value']))

            if evt['value'] == 'CONNECTED':
                if contact_it is None:
                    contact_it = self._driver.execute_command(
                        'chats|getItems', result_class=IteratorResult)
                    ensure_future(self.list_chats(contact_it))
            else:
                if contact_it is not None:
                    self._driver.cancel_iterators()
                    contact_it = None

    async def list_chats(self, it):
        self.echo('List chats')
        async for chat in it:
            self.echo('Chat: {}'.format(chat))

        self.echo('List chats finished')

    async def start(self):
        await self.init()
        futs = [ensure_future(self.monitor_stream())]

        try:
            await self._driver.whai_until_stop()
        finally:
            await self._driver.cancel_iterators()
            self._driver.result_manager.cancel_all()
            await wait(futs)
            await self._driver.close()