Mercurial > personal > weatherlog
view weatherlog/daemon.py @ 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 | |
children | 4c81182eaa6b |
line wrap: on
line source
"""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))