From 5928f99e29046c60cdf9ac2c27d8b099b1ac4de2 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Fri, 28 Aug 2020 12:25:22 +0100 Subject: [PATCH] Add support for history --- library/grow/moisture.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/library/grow/moisture.py b/library/grow/moisture.py index 66ca6bf..3d563e8 100644 --- a/library/grow/moisture.py +++ b/library/grow/moisture.py @@ -30,6 +30,8 @@ class Moisture(object): self._count = 0 self._reading = 0 + self._history = [] + self._history_length = 200 self._last_pulse = time.time() self._new_data = False self._wet_point = wet_point if wet_point is not None else 0.7 @@ -56,10 +58,23 @@ dtoverlay=spi0-cs,cs0_pin=14 # Re-assign CS0 from BCM 8 so that Grow can use it self._last_pulse = time.time() if self._time_elapsed >= 1.0: self._reading = self._count / self._time_elapsed + self._history.insert(0, self._reading) + self._history = self._history[:self._history_length] self._count = 0 self._time_last_reading = time.time() self._new_data = True + @property + def history(self): + history = [] + + for moisture in self._history: + saturation = float(moisture - self._dry_point) / self.range + saturation = round(saturation, 3) + history.append(max(0.0, min(1.0, saturation))) + + return history + @property def _time_elapsed(self): return time.time() - self._time_last_reading