From 53671a779e4ccb8eaf9fb53f35e51dbf67338018 Mon Sep 17 00:00:00 2001 From: Vlad Doloman Date: Fri, 6 Nov 2026 23:51:45 +0200 Subject: [PATCH] Cache inverter readings for poll interval minus 0.2 s Define CLIENT_POLL_INTERVAL (5 s) in app.py, cache a reading for 4.8 s and pass the interval to the page, so several clients share one inverter read instead of each triggering their own. Co-Authored-By: Claude Sonnet 5.5 --- app.py | 8 ++++++-- templates/index.html | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 1b256b0..b42a818 100644 --- a/app.py +++ b/app.py @@ -14,7 +14,11 @@ except ImportError: app = Flask(__name__) SOCKET_TIMEOUT = getattr(config, "INVERTER_SOCKET_TIMEOUT", 5) -CACHE_TTL = 2.0 # seconds; the logger only tolerates one session at a time +# The dashboard polls every CLIENT_POLL_INTERVAL seconds. Cache a reading for slightly +# less than that, so one client's poll refreshes the data and any other clients polling +# within the window get the cached reply (the logger only tolerates one session at a time). +CLIENT_POLL_INTERVAL = 5.0 +CACHE_TTL = CLIENT_POLL_INTERVAL - 0.2 _lock = threading.Lock() _cache = {"time": 0.0, "data": None} @@ -243,7 +247,7 @@ def write_inverter_registers(writes): @app.route('/') def index(): - return render_template('index.html', writable=WRITABLE) + return render_template('index.html', writable=WRITABLE, poll_interval_ms=int(CLIENT_POLL_INTERVAL * 1000)) @app.route('/api/data') def api_data(): diff --git a/templates/index.html b/templates/index.html index b50cdda..ef11e38 100644 --- a/templates/index.html +++ b/templates/index.html @@ -663,9 +663,9 @@ } } - // Fetch immediately, then every 5 seconds + // Fetch immediately, then at the server-defined interval (CLIENT_POLL_INTERVAL in app.py) fetchData(); - setInterval(fetchData, 5000); + setInterval(fetchData, {{ poll_interval_ms }});