README.rst 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. ===========
  2. ThingFlow
  3. ===========
  4. ThingFlow is a (Python3) framework for building IoT event
  5. processing dataflows. [#]_ The goal of this framework is to support the
  6. creation of robust IoT systems from reusable components. These systems must
  7. account for noisy/missing sensor data, distributed computation, and the need for
  8. local (near the data source) processing.
  9. The source repository for ThingFlow-python is at https://github.com/mpi-sws-rse/thingflow-python.
  10. Introduction
  11. ============
  12. The fundamental abstractions in ThingFlow are:
  13. 1. *sensors*, which provide a means
  14. to sample a changing value representing some quanity in the physical world,
  15. 2. *event streams*, which are push-based sequences of sensor data readings, and
  16. 3. *things*, which are reusable components to generate, transform, or consume the events on these streams.
  17. Things can have simple, stateless logic (e.g. filter events based
  18. on a predicate) or implement more complex, stateful algorithms, such as
  19. Kalman filters or machine learning. Using ThingFlow, you describe the flow of
  20. data through these things rather than programming low-level behaviors.
  21. Although ThingFlow presents a simple dataflow model to the user, internally it
  22. uses an event-driven programming model, building on
  23. Python's ``asyncio`` module. In addition to being a natural programming model for
  24. realtime sensor data, it reduces the potential resource consumption of Ant
  25. Events programs. The details of event scheduling are handled by the framework.
  26. Separate threads may be used on the "edges" of a dataflow, where elements
  27. frequently interact with external components that have blocking APIs.
  28. ThingFlow integrates with standard Python
  29. data analytics frameworks, including NumPy_, Pandas_, and scikit-learn_. This
  30. allows dataflows involving complex elements to be developed and refined offline
  31. and then deployed in an IoT environment using the same code base.
  32. We call the implementation described here "ThingFlow-Python", as it should be
  33. possible to port the ideas of ThingFlow to other languages. Currently, one such
  34. port exists: "ThingFlow-MicroPython". This is a port ThingFlow to MicroPython,
  35. a limited version of Python 3 that runs "bare metal" on embadded devices. The
  36. ThingFlow-MicroPython port is included in the ThingFlow-Python repository
  37. under the subdirector ``micropython``. It is documented in
  38. `a chapter <http://thingflow-python.readthedocs.io/en/latest/micropython.html>`_
  39. of the documentation.
  40. .. _NumPy: http://www.numpy.org/
  41. .. _Pandas: http://pandas.pydata.org/
  42. .. _scikit-learn: http://scikit-learn.org/stable/
  43. Example
  44. -------
  45. To give the flavor of ThingFlow, below is a short code snippet for the
  46. Raspberry Pi that reads a light sensor and then turns on an LED if the running
  47. average of the last five readings is greater than some threshold::
  48. lux = SensorAsOutputThing(LuxSensor())
  49. lux.map(lambda e: e.val).running_avg(5).map(lambda v: v > THRESHOLD)\
  50. .GpioPinOut()
  51. scheduler.schedule_periodic(lux, 60.0)
  52. scheduler.run_forever()
  53. The first line instantiates a light sensor object and wraps it in an *output thing*
  54. to handle sampling and progagation of events.
  55. The next two lines
  56. create a pipeline of things to process the data from the sensor. We call things
  57. which have a single input and output *filters*, as they can be composed to process
  58. a stream of events.
  59. The ``map`` filter extracts the data value from the sensor event, the
  60. ``running_avg`` filter averages the last five values, and the next ``map`` filter converts
  61. the value to a a boolean based on the threshold. The ``GpioPinOut`` thing is
  62. an *adapter* to the outside world. It turns on the LED based on the value of
  63. its input boolean value.
  64. Finally, the last two lines of the example schedule the sensor to be sampled
  65. at a sixty second interval and then start the scheduler's main loop.
  66. Dependencies
  67. ------------
  68. The ThingFlow proper is self-contained. You do not need any dependencies other
  69. than Python 3 (3.4 or later). Specific adapters and sensors may have additional
  70. dependencies (e.g. the MQTT adapters depend on MQTT client libraries).
  71. Documentation
  72. -------------
  73. Documentation is hosted online at http://thingflow-python.readthedocs.io.
  74. The source tree for the documentation is in the ``docs`` subdirectory - it is
  75. built using `Sphinx <http://www.sphinx-doc.org/en/stable/>`_. If you have Sphinx
  76. installed locally (along with the "Read the Docs" theme), you can also build it
  77. directly on your machine.
  78. .. [#] *ThingFlow* was originally known as *AntEvents*.