Esempio n. 1
0
def start(mqtt_link):
    global reset_count
    mqtt_link.subscribe('green', qos, cbgreen)    # LED control qos 1
    loop = asyncio.get_event_loop()
    loop.create_task(asyn.Cancellable(ramcheck, mqtt_link)())  # Check RAM every 30 minutes
    loop.create_task(asyn.Cancellable(publish, mqtt_link, 10)()) # Publish a count every 10 seconds
    loop.create_task(pulse(blue))  # Flash blue LED each time we restart ESP8266
    reset_count += 1
Esempio n. 2
0
 async def start(self, loop):
     loop.create_task(asyn.Cancellable(
         self.foo, 1)())  # 3 instances in default group 0
     loop.create_task(asyn.Cancellable(self.foo, 2)())
     loop.create_task(asyn.Cancellable(self.foo, 3)())
     loop.create_task(asyn.NamedTask('my bar', self.bar, 4, y=42)())
     await asyncio.sleep(4.5)
     await asyn.NamedTask.cancel('my bar')
     await asyn.Cancellable.cancel_all()
     await asyncio.sleep(1)
     print('Done')
Esempio n. 3
0
 async def start(self, loop):
     loop.create_task(asyn.Cancellable(self.foo, 1)())  # 3 instances in default group 0
     loop.create_task(asyn.Cancellable(self.foo, 2)())
     loop.create_task(asyn.Cancellable(self.foo, 3)())
     loop.create_task(asyn.NamedTask('my bar', self.bar, 4)())
     print('bar running status is', asyn.NamedTask.is_running('my bar'))
     await asyncio.sleep(4.5)
     asyn.NamedTask.cancel('my bar')
     print('bar instance scheduled for cancellation.')
     await asyn.Cancellable.cancel_all()
     print('foo instances have been cancelled.')
     print('bar running status is', asyn.NamedTask.is_running('my bar'))
     print('Done')
Esempio n. 4
0
async def cond_go(loop):
    ntasks = 7
    barrier = asyn.Barrier(ntasks + 1)
    loop.create_task(asyn.Cancellable(cond01)())
    loop.create_task(asyn.Cancellable(cond03)())
    for n in range(ntasks):
        loop.create_task(cond02(n, barrier))
    await barrier  # All instances of cond02 have completed
    # Test wait_for
    barrier = asyn.Barrier(2)
    loop.create_task(cond04(99, barrier))
    await barrier
    # cancel continuously running coros.
    await asyn.Cancellable.cancel_all()
    print('Done.')
Esempio n. 5
0
async def run_cancel_test(loop):
    res = await asyn.Cancellable(add_one, 41)
    print('Result: ', res)
    loop.create_task(asyn.Cancellable(print_nums, res)())
    await asyn.sleep(7.5)
    # Cancel any cancellable tasks still running
    await asyn.Cancellable.cancel_all()
    print('Done')
async def main(loop):
    print('Press Pyboard usr button to stop test.')
    bar = asyn.Barrier(4, refresh, (ssd,))
    # Asynchronously flash Pyboard LED's. Because we can.
    loop.create_task(asyn.Cancellable(flash, 1, 200)())
    loop.create_task(asyn.Cancellable(flash, 2, 233)())
    # Task for each meter and GUI LED
    loop.create_task(asyn.Cancellable(meter, bar,  2, 'left')())
    loop.create_task(asyn.Cancellable(meter, bar, 50, 'right')())
    loop.create_task(asyn.Cancellable(meter, bar, 98, 'bass')())
    try:
        while True:
            await asyncio.sleep_ms(800)
            # If necessary wait until all meters have updated.
            # Barrier callback updates display.
            await bar
    except asyn.StopTask:
        ssd.fill(0)  # Clear display at end.
        refresh(ssd)
Esempio n. 7
0
async def run_cancel_test4():
    await asyn.Cancellable(cant41, 0, 5)
    loop = asyncio.get_event_loop()
    loop.create_task(asyn.Cancellable(cant40,
                                      1)())  # 3 instances in default group 0
    loop.create_task(asyn.Cancellable(cant40, 2)())
    loop.create_task(asyn.Cancellable(cant40, 3)())
    loop.create_task(
        asyn.Cancellable(chained, 4, 1, 2, red=3, blue=4, group=1)())
    loop.create_task(asyn.Cancellable(cant41, 5)())  # Runs to completion
    print('Running tasks')
    await asyncio.sleep(3)
    print('About to cancel group 0 tasks')
    await asyn.Cancellable.cancel_all()  # All in default group 0
    print('Group 0 tasks were cancelled')
    await asyncio.sleep(1)  # Demo chained still running
    print('About to cancel group 1 tasks')
    await asyn.Cancellable.cancel_all(1)  # Group 1
    print('Group 1 tasks were cancelled')
    await asyncio.sleep(1)
Esempio n. 8
0
    async def action_handler(self, action: str, *args, **kwargs):
        """ Run specified action after cancelled previous
        """
        if action in ['front', 'tail', 'blink_tail']:
            getattr(self.lights, action)(*args, **kwargs)
            return

        await self.lights.rgb_cancel()

        if action != 'cancel':
            asyncio.get_event_loop().create_task(
                asyn.Cancellable(getattr(self.lights, 'rgb_' + action), *args,
                                 **kwargs)())
Esempio n. 9
0
async def run_cancel_test6(loop):
    for name in ('complete', 'cancel me'):
        loop.create_task(asyn.NamedTask(name, cant60, name)())
    loop.create_task(asyn.Cancellable(cant61)())
    await asyncio.sleep(4.5)
    print('Cancelling task \"{}\". 1.5 secs latency.'.format(name))
    await asyn.NamedTask.cancel(name)
    await asyncio.sleep(7)
    name = 'cancel wait'
    loop.create_task(asyn.NamedTask(name, cant60, name)())
    await asyncio.sleep(0.5)
    print('Cancelling task \"{}\". 1.5 secs latency.'.format(name))
    t = time.ticks_ms()
    await asyn.NamedTask.cancel('cancel wait', nowait=False)
    print('Was cancelled in {} ms'.format(time.ticks_diff(time.ticks_ms(), t)))
    print('Cancelling cant61')
    await asyn.Cancellable.cancel_all()
    print('Done')
@asyn.cancellable
async def main(loop):
    print('Press Pyboard usr button to stop test.')
    bar = asyn.Barrier(4, refresh, (ssd,))
    # Asynchronously flash Pyboard LED's. Because we can.
    loop.create_task(asyn.Cancellable(flash, 1, 200)())
    loop.create_task(asyn.Cancellable(flash, 2, 233)())
    # Task for each meter and GUI LED
    loop.create_task(asyn.Cancellable(meter, bar,  2, 'left')())
    loop.create_task(asyn.Cancellable(meter, bar, 50, 'right')())
    loop.create_task(asyn.Cancellable(meter, bar, 98, 'bass')())
    try:
        while True:
            await asyncio.sleep_ms(800)
            # If necessary wait until all meters have updated.
            # Barrier callback updates display.
            await bar
    except asyn.StopTask:
        ssd.fill(0)  # Clear display at end.
        refresh(ssd)

async def killer():
    sw = pyb.Switch()
    while not sw():
        await asyncio.sleep_ms(100)
    await asyn.Cancellable.cancel_all()

loop = asyncio.get_event_loop()
loop.create_task(asyn.Cancellable(main, loop)())
loop.run_until_complete(killer())
Esempio n. 11
0
        l.text(txt(v), fgcolor=color(v))
        refresh(ssd)
        await asyncio.sleep_ms(t)

@asyn.cancellable
async def flash(n, t):
    led = pyb.LED(n)
    try:
        while True:
            led.toggle()
            await asyncio.sleep_ms(t)
    except asyn.StopTask:
        led.off()
        print('LED {} was cancelled.'.format(n))

async def killer():
    sw = pyb.Switch()
    while not sw():
        await asyncio.sleep_ms(100)
    await asyn.Cancellable.cancel_all()

print('Press Pyboard usr button to stop test.')
loop = asyncio.get_event_loop()
loop.create_task(asyn.Cancellable(meter, 1, 2, 'left', 700)())
loop.create_task(asyn.Cancellable(meter, 2, 50, 'right', 1000)())
loop.create_task(asyn.Cancellable(meter, 3, 98, 'bass', 1500)())
loop.create_task(asyn.Cancellable(flash, 1, 200)())
loop.create_task(asyn.Cancellable(flash, 2, 233)())
loop.run_until_complete(killer())

Esempio n. 12
0
def start(mqtt_link):
    mqtt_link.subscribe('green', qos, cbgreen)  # LED control qos 1
    mqtt_link.wifi_handler(cbnet)  # Detect WiFi changes
    loop = asyncio.get_event_loop()
    loop.create_task(asyn.Cancellable(
        publish, mqtt_link, 10)())  # Publish a count every 10 seconds
Esempio n. 13
0
    async def start(self, channel):
        self.vbprint('Starting...')
        self.subs = {} # Callbacks indexed by topic
        self._pub_free = True  # No publication in progress
        self._running = False
        if self.lw_topic is not None:
            channel.send(argformat(WILL, self.lw_topic, self.lw_msg, self.lw_retain, self.lw_qos))
            res = await channel.await_obj()
            if res is None:  # SynCom timeout
                await self.s_han(self, ESP_FAIL)
                return self.quit('ESP8266 fail 1. Resetting.')
            command, action = self.get_cmd(res)
            if command == STATUS:
                iact = self.do_status(action, -1)
                await self.s_han(self, iact)
                if iact in _BAD_STATUS:
                    return self.quit('Bad status: {}. Resetting.'.format(iact))
            else:
                self.vbprint('Expected will OK got: ', command, action)
        channel.send(self.init_str)
        while not self._running:  # Until RUNNING status received
            res = await channel.await_obj()
            if res is None:
                await self.s_han(self, ESP_FAIL)
                return self.quit('ESP8266 fail 2. Resetting.')
            command, action = self.get_cmd(res)
            if command == STATUS:
                iact = self.do_status(action, -1)
                result = await self.s_han(self, iact)
                if iact == SPECNET:
                    if result == 1:
                        channel.send('1')  # Any string will do
                    else:
                        return self.quit()
                if iact in _BAD_STATUS:
                    return self.quit('Bad status. Resetting.')
            else:
                self.vbprint('Init got: ', command, action)

        self.vbprint('About to run user program.')
        if self.user_start is not None:
            self.user_start[0](self, *self.user_start[1])  # User start function
        loop = asyncio.get_event_loop()
        loop.create_task(asyn.Cancellable(self._publish)())
        self.rtc_synchroniser._start()  # Run coro if synchronisation is required.
        cb, args = self.wifi_han
        cb(True, *args)

# Initialisation is complete. Process messages from ESP8266.
        iact = -1                   # Invalidate last status for change detection
        while True:                 # print incoming messages, handle subscriptions
            chan_state = channel.any()
            if chan_state is None:  # SynCom Timeout
                self._running = False
            elif chan_state > 0:
                res = await channel.await_obj()
                command, action = self.get_cmd(res)
                if command == SUBSCRIPTION:
                    if action[0] in self.subs: # topic found
                        cb, args = self.subs[action[0]]
                        action += args
                        cb(*action)  # Run the callback
                elif command == STATUS:  # 1st arg of status is an integer
                    iact = self.do_status(action, iact) # Update pub q and wifi status
                    await self.s_han(self, iact)
                    if iact in _DIRE_STATUS:
                        return self.quit('Fatal status. Resetting.')
                elif command == TIME:
                    self.rtc_synchroniser._do_time(action)
                elif command == MEM:  # Wouldn't ask for this if we didn't want a printout
                    print('ESP8266 RAM free: {} allocated: {}'.format(action[0], action[1]))
                else:
                    await self.s_han(self, UNKNOWN)
                    return self.quit('Got unhandled command, resetting ESP8266:', command, action)  # ESP8266 has failed

            await asyncio.sleep_ms(20)
            if not self._running:  # self.quit() has been called.
                await self.s_han(self, NO_NET)
                return self.quit('Not running, resetting ESP8266')
Esempio n. 14
0
 def _start(self):
     i = self._rtc_interval  # 0 == no synch. -1 == once only. > 1 = secs
     if i and (i > 0 or self._rtc_last_syn == 0):
         loop = asyncio.get_event_loop()
         loop.create_task(asyn.Cancellable(self._do_rtc)())