changeset 12:91e22a09b82f

Create 'daemon.py', a runner to tie it all together.
author Paul Fisher <paul@pfish.zone>
date Sun, 29 Sep 2019 11:53:35 -0400
parents 6dbd9825b3f5
children 4c81182eaa6b
files weatherlog/daemon.py
diffstat 1 files changed, 33 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/weatherlog/daemon.py	Sun Sep 29 11:53:35 2019 -0400
@@ -0,0 +1,33 @@
+"""Entry point to set up a temperature logging daemon."""
+
+import time
+import typing as t
+
+from . import http_writer
+from . import logger
+from . import reader
+
+DEFAULT_INTERVAL_SECS = 60
+MIN_INTERVAL_SECS = 5
+
+
+def run(
+    directory: str,
+    url: str,
+    preamble: t.Dict[str, t.Any],
+    interval: int = DEFAULT_INTERVAL_SECS,
+):
+    """Sets up and runs a logger daemon."""
+    writer = http_writer.HTTPWriter(url, preamble)
+    log = logger.BufferedLogger(directory, writer)
+    log.start()
+
+    r = reader.DHT22Reader()
+    cycle = 0
+    start = time.time()
+    while True:
+        log.write(r.read())
+        cycle += 1
+        target = start + interval * cycle
+        now = time.time()
+        time.sleep(max(target - now, MIN_INTERVAL_SECS))