-
Notifications
You must be signed in to change notification settings - Fork 8
Plugins: Getting Started
Sean Shahkarami edited this page Oct 14, 2020
·
12 revisions
pywaggle provides an implementation of the Waggle plugin interface to make it easy to hook existing code into the Waggle data pipeline. This allows developers to publish and subscribe to various data streams throughout the pipeline.
We'll start with the basics of publishing. In this example, we make up a fake humidity value and publish it so it can be sent to beehive and other plugins.
import waggle.plugin as plugin
import time
plugin.init()
while True:
# ... make up fake value from sensor...
value = 12.34
# publish value as htu21d temperature
plugin.publish('env.humidity.htu21d', value)
time.sleep(1)import waggle.plugin as plugin
import time
from random import random
plugin.init()
plugin.subscribe('raw.#')
while True:
msg = plugin.get()
if msg.name == 'raw.htu21d':
# ...insert real decode logic...
plugin.publish('env.temperature.htu21d', 20.0 + random())
plugin.publish('env.humidity.htu21d', 80.0 + random())
print('decode htu21d')
elif msg.name == 'raw.tmp112':
# ...insert real decode logic...
plugin.publish('env.temperature.tmp112', 21.0 + random())
print('decode tmp112')
elif msg.name == 'raw.tsys01':
# ...insert real decode logic...
plugin.publish('env.temperature.tsys01',21.0 + random())
print('decode tsys01')Plugins can also publish image. For now, only ndarray formats are support like you get from Numpy or OpenCV.
import waggle.plugin as plugin
from waggle.data import open_data_source
import time
plugin.init()
with open_data_source('image_bottom') as cam:
while True:
ts, image = cam.get()
# TODO fix time units. data shim returns seconds and publish takes nanoseconds.
ts_ns = int(ts*1e9)
plugin.publish('image.bottom_camera', plugin.Image(image), timestamp=ts_ns)
time.sleep(30)Plugins use worker thread and communicate through queues. This makes them safe and easy to use in a range of existing designs (multithreaded, callback based, asyncio based, etc) without interrupting the program flow.