예제 #1
0
파일: core.py 프로젝트: wrobell/dshrub
def workflow(topic, device, sensors, files=None, channel=None,
        replay=None, cache=None, dbus_bus=None):

    interval = 1
    scheduler = n23.Scheduler(interval)

    dlog = None
    if files:
        fout = h5py.File(next(files), 'w')
        dlog = n23.DLog(fout, interval, n_chunk=60, debug=True)
        scheduler.debug = dlog
        scheduler.add_observer(dlog.notify)

    items = sensor_replay(replay, sensors) if replay \
        else sensor_tag(dbus_bus, device, sensors)

    for name, read in items:
        if dlog:
            shape = ()
            if name == 'accelerometer':
                shape += (3,)
            dlog.add(name, shape=shape)

        @n23.coroutine
        def calc(callback):
            import math
            while True:
                item = yield
                value = math.sqrt(sum(v ** 2 for v in item.value))
                item = item._replace(value=value)
                callback(item)
        pt = calc(topic.put_nowait) if name == 'accelerometer' else topic.put_nowait
        consume = n23.split(pt, dlog)
        scheduler.add(name, read, consume)

    tasks = [scheduler]
    if channel:
        p = publish(topic, channel)
        tasks.append(p)
        logger.info('publish data to redis channel {}'.format(channel))

    if cache:
        t = cache_data(topic.get, cache)
        tasks.append(t)

    try:
        yield asyncio.gather(*tasks)
    finally:
        scheduler.close()
        dlog.close()
        fout.close()
예제 #2
0
def workflow(topic, device, files=None, channel=None, replay=None):
    scheduler = n23.Scheduler(1, timeout=0.25)
    fout = next(files) if files else None

    if replay:
        logger.info('replaying a data file {}'.format(replay))
        fin = h5py.File(replay)

        # for each sensor
        names = ['light', 'humidity']
        for name in names:
            data_log = n23.data_logger(fout, name, 60) if fout else None
            consume = n23.split(topic.put_nowait, data_log)
            scheduler.add(name, replay_file(fin, name), consume)
    else:
        raise ValueError('Reading from a device not supported yet')

    tasks = [scheduler()]
    if channel:
        p = publish(topic, channel)
        tasks.append(p)
        logger.info('publish data to redis channel {}'.format(channel))

    return asyncio.gather(*tasks)