Mercurial > personal > weatherlog
comparison 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 |
comparison
equal
deleted
inserted
replaced
11:6dbd9825b3f5 | 12:91e22a09b82f |
---|---|
1 """Entry point to set up a temperature logging daemon.""" | |
2 | |
3 import time | |
4 import typing as t | |
5 | |
6 from . import http_writer | |
7 from . import logger | |
8 from . import reader | |
9 | |
10 DEFAULT_INTERVAL_SECS = 60 | |
11 MIN_INTERVAL_SECS = 5 | |
12 | |
13 | |
14 def run( | |
15 directory: str, | |
16 url: str, | |
17 preamble: t.Dict[str, t.Any], | |
18 interval: int = DEFAULT_INTERVAL_SECS, | |
19 ): | |
20 """Sets up and runs a logger daemon.""" | |
21 writer = http_writer.HTTPWriter(url, preamble) | |
22 log = logger.BufferedLogger(directory, writer) | |
23 log.start() | |
24 | |
25 r = reader.DHT22Reader() | |
26 cycle = 0 | |
27 start = time.time() | |
28 while True: | |
29 log.write(r.read()) | |
30 cycle += 1 | |
31 target = start + interval * cycle | |
32 now = time.time() | |
33 time.sleep(max(target - now, MIN_INTERVAL_SECS)) |