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 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
c84e8b8af8
commit
53671a779e
@@ -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():
|
||||
|
||||
@@ -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 }});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user