diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2022-11-01 13:01:48 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2022-11-01 13:01:48 +0100 |
commit | ae9fc54e62e528a4ec18228e81fd111cdc81213b (patch) | |
tree | b9b97d02f26433ee78ecfc562676ad0a42d7c1ea | |
parent | f2e50faab6a3447c061500cd1a735424af055669 (diff) | |
download | glutte-serial-web-ae9fc54e62e528a4ec18228e81fd111cdc81213b.tar.gz glutte-serial-web-ae9fc54e62e528a4ec18228e81fd111cdc81213b.tar.bz2 glutte-serial-web-ae9fc54e62e528a4ec18228e81fd111cdc81213b.zip |
Simplify history
-rw-r--r-- | config.py.dist | 4 | ||||
-rwxr-xr-x | glutte_serial_web.py | 11 | ||||
-rw-r--r-- | serialrx.py | 24 |
3 files changed, 11 insertions, 28 deletions
diff --git a/config.py.dist b/config.py.dist index f958a6c..c195765 100644 --- a/config.py.dist +++ b/config.py.dist @@ -3,9 +3,7 @@ BAUDRATE = 9600 LINES_TO_KEEP = 200 LAST_LINE_TO_KEEP = 1000 -# cache contains [ { 'ts': 'TIMESTMAP', 'line': 'LINE' } ] -CACHE_FILE = "/tmp/glutte_serial_web.json" -CACHE_MAX_AGE = 3600 * 24 * 7 +CACHE_MAX_AGE = 3600 * 24 * 4 TELEGRAM_API_TOKEN = '' TELEGRAM_GROUP = '' diff --git a/glutte_serial_web.py b/glutte_serial_web.py index 5ca905c..7710c9e 100755 --- a/glutte_serial_web.py +++ b/glutte_serial_web.py @@ -45,16 +45,7 @@ def index(): @app.route('/history') def history(): - hist = [] - if config.CACHE_FILE: - with open(config.CACHE_FILE) as fd: - hist = json.load(fd) - - def format_entry(entry): - dt = datetime.datetime.fromtimestamp(entry['ts']).isoformat() - return f"{dt} {entry['line']}" - - text = "\n".join(format_entry(entry) for entry in hist) + text = "\n".join(f"{entry['ts'].isoformat()} {entry['line']}" for entry in ser.get_cache()) return Response(text, mimetype='text/plain') @app.route('/stats') diff --git a/serialrx.py b/serialrx.py index f3fc9af..13f34b4 100644 --- a/serialrx.py +++ b/serialrx.py @@ -28,7 +28,6 @@ import collections import re import time import datetime -import json import config @@ -125,8 +124,13 @@ class SerialRX(threading.Thread): self.line_accumulator = [] self.last_lines = [] + self.cache = [] # contains [ { 'ts': 'TIMESTAMP', 'line': 'LINE' }, ... ] + print("Serial port ready") + def get_cache(self): + return self.cache + def get_parsed_values(self): return self._parser.get_last_data() @@ -140,21 +144,11 @@ class SerialRX(threading.Thread): try: line = b"".join(self.line_accumulator).decode('ascii') self._parser.parse_message(line) - now = time.time() + now = datetime.datetime.utcnow() + max_age = datetime.timedelta(seconds=config.CACHE_MAX_AGE) - try: - if config.CACHE_FILE: - try: - with open(config.CACHE_FILE) as fd: - hist = json.load(fd) - except: - hist = [] - hist = [h for h in hist if h['ts'] + config.CACHE_MAX_AGE > now] - hist.append({'ts': now, 'line': line.strip()}) - with open(config.CACHE_FILE, 'w') as fd: - json.dump(hist, fd) - except Exception as e: - print(e) + self.cache = [h for h in self.cache if h['ts'] + max_age > now] + self.cache.append({'ts': now, 'line': line.strip()}) self.data_lock.acquire() try: |