diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2022-10-31 20:35:58 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2022-10-31 20:35:58 +0100 |
commit | 1546176881719a4fa7e63109f691eba32dd7bf85 (patch) | |
tree | cabfe48ece43578957c37d5400258d70401713ec | |
parent | d0c54a086b9ea67c067193b51dd8f16e8e75f454 (diff) | |
download | glutte-serial-web-1546176881719a4fa7e63109f691eba32dd7bf85.tar.gz glutte-serial-web-1546176881719a4fa7e63109f691eba32dd7bf85.tar.bz2 glutte-serial-web-1546176881719a4fa7e63109f691eba32dd7bf85.zip |
Add history in json file
-rwxr-xr-x | glutte_serial_web.py | 15 | ||||
-rw-r--r-- | serialrx.py | 13 |
2 files changed, 25 insertions, 3 deletions
diff --git a/glutte_serial_web.py b/glutte_serial_web.py index a1a10ee..da61b1b 100755 --- a/glutte_serial_web.py +++ b/glutte_serial_web.py @@ -2,7 +2,7 @@ # # The MIT License (MIT) # -# Copyright (c) 2020 Matthias P. Braendli, Maximilien Cuony +# Copyright (c) 2022 Matthias P. Braendli, Maximilien Cuony # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -23,13 +23,15 @@ # SOFTWARE. import time +import json from geventwebsocket.handler import WebSocketHandler from gevent import pywsgi, Timeout from time import sleep -from flask import Flask, render_template, jsonify +from flask import Flask, Response, render_template, jsonify from flask_sockets import Sockets import serialrx import adsl +import config app = Flask(__name__) sockets = Sockets(app) @@ -41,6 +43,15 @@ adsl = adsl.ADSL(ser) def index(): return render_template('index.html', last_lines=ser.get_last_lines()) +@app.route('/history') +def history(): + hist = [] + 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) + return Response(text, mimetype='text/plain') + @app.route('/stats') def stats(): t_now = time.time() diff --git a/serialrx.py b/serialrx.py index 263ed1b..a10db0a 100644 --- a/serialrx.py +++ b/serialrx.py @@ -2,7 +2,7 @@ # # The MIT License (MIT) # -# Copyright (c) 2020 Matthias P. Braendli, Maximilien Cuony +# Copyright (c) 2022 Matthias P. Braendli, Maximilien Cuony # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -27,6 +27,7 @@ import threading import collections import re import time +import json import config @@ -138,6 +139,16 @@ class SerialRX(threading.Thread): try: line = b"".join(self.line_accumulator).decode('ascii') self._parser.parse_message(line) + now = time.time() + + if config.CACHE_FILE: + with open(config.CACHE_FILE) as fd: + hist = json.load(fd) + hist = [h for h in hist if h['ts'] + config.CACHE_MAX_AGE > now] + host.append({'ts': now, 'line': line}) + with open(config.CACHE_FILE, 'w') as fd: + json.dump(hist, fd) + self.data_lock.acquire() try: for queue in self.clients: |