# HG changeset patch # User Paul Fisher # Date 1569772415 14400 # Node ID 91e22a09b82fa3da4d67c097898070315daac7de # Parent 6dbd9825b3f5d5f311662342a6bab160ac925db7 Create 'daemon.py', a runner to tie it all together. diff -r 6dbd9825b3f5 -r 91e22a09b82f weatherlog/daemon.py --- /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))