diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2022-10-31 20:56:23 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2022-10-31 20:56:23 +0100 |
commit | 8fd1aa2422a55e187f1c1c82c68daaf833d63865 (patch) | |
tree | c75870b8c8875272053c0ff77b493329b4026f6d | |
parent | f5932782310806150aa3dceeadacaeae448ca702 (diff) | |
download | glutte-serial-web-8fd1aa2422a55e187f1c1c82c68daaf833d63865.tar.gz glutte-serial-web-8fd1aa2422a55e187f1c1c82c68daaf833d63865.tar.bz2 glutte-serial-web-8fd1aa2422a55e187f1c1c82c68daaf833d63865.zip |
Replace unix timestamps by datetime in cache
-rwxr-xr-x | glutte_serial_web.py | 5 | ||||
-rw-r--r-- | serialrx.py | 7 |
2 files changed, 6 insertions, 6 deletions
diff --git a/glutte_serial_web.py b/glutte_serial_web.py index da61b1b..2c53094 100755 --- a/glutte_serial_web.py +++ b/glutte_serial_web.py @@ -26,7 +26,6 @@ import time import json from geventwebsocket.handler import WebSocketHandler from gevent import pywsgi, Timeout -from time import sleep from flask import Flask, Response, render_template, jsonify from flask_sockets import Sockets import serialrx @@ -49,7 +48,7 @@ def history(): if config.CACHE_FILE: with open(config.CACHE_FILE) as fd: hist = json.load(fd) - text = "\n".join(f"{entry['ts']} {entry['line']}" for entry in hist) + text = "\n".join(f"{entry['ts'].isoformat()} {entry['line']}" for entry in hist) return Response(text, mimetype='text/plain') @app.route('/stats') @@ -88,7 +87,7 @@ def stream(socket): pass except: error = True - sleep(0.1) + time.sleep(0.1) except: raise finally: diff --git a/serialrx.py b/serialrx.py index 6d4d7a5..f6007ea 100644 --- a/serialrx.py +++ b/serialrx.py @@ -27,6 +27,7 @@ import threading import collections import re import time +import datetime import json import config @@ -139,7 +140,7 @@ 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() try: if config.CACHE_FILE: @@ -148,8 +149,8 @@ class SerialRX(threading.Thread): hist = json.load(fd) except FileNotFoundError: hist = [] - hist = [h for h in hist if h['ts'] + config.CACHE_MAX_AGE > now] - hist.append({'ts': now, 'line': line}) + hist = [h for h in hist if h['ts'] + datetime.timedelta(seconds=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: |