From f2e50faab6a3447c061500cd1a735424af055669 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Mon, 31 Oct 2022 21:24:00 +0100 Subject: Go back to using time() --- glutte_serial_web.py | 8 +++++++- serialrx.py | 6 +++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/glutte_serial_web.py b/glutte_serial_web.py index 2c53094..5ca905c 100755 --- a/glutte_serial_web.py +++ b/glutte_serial_web.py @@ -23,6 +23,7 @@ # SOFTWARE. import time +import datetime import json from geventwebsocket.handler import WebSocketHandler from gevent import pywsgi, Timeout @@ -48,7 +49,12 @@ def history(): if config.CACHE_FILE: with open(config.CACHE_FILE) as fd: hist = json.load(fd) - text = "\n".join(f"{entry['ts'].isoformat()} {entry['line']}" for entry in hist) + + 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) return Response(text, mimetype='text/plain') @app.route('/stats') diff --git a/serialrx.py b/serialrx.py index f6007ea..f3fc9af 100644 --- a/serialrx.py +++ b/serialrx.py @@ -140,16 +140,16 @@ class SerialRX(threading.Thread): try: line = b"".join(self.line_accumulator).decode('ascii') self._parser.parse_message(line) - now = datetime.datetime.utcnow() + now = time.time() try: if config.CACHE_FILE: try: with open(config.CACHE_FILE) as fd: hist = json.load(fd) - except FileNotFoundError: + except: hist = [] - hist = [h for h in hist if h['ts'] + datetime.timedelta(seconds=config.CACHE_MAX_AGE) > now] + 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) -- cgit v1.2.3