Skip to content

Commit 0872789

Browse files
committed
keep history on the server side
1 parent d49db50 commit 0872789

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

octoprint_external_temp_reader/ExternalTempReaderPlugin.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
from xml.etree import ElementTree as ET
44

55
import requests
6-
from octoprint.plugin import SettingsPlugin, StartupPlugin, ShutdownPlugin, TemplatePlugin, AssetPlugin
6+
from octoprint.plugin import SettingsPlugin, StartupPlugin, ShutdownPlugin, TemplatePlugin, AssetPlugin, SimpleApiPlugin
77

88

9-
class ExternalTempReaderPlugin(SettingsPlugin, TemplatePlugin, AssetPlugin, StartupPlugin, ShutdownPlugin):
9+
class ExternalTempReaderPlugin(SettingsPlugin, TemplatePlugin, AssetPlugin, StartupPlugin, ShutdownPlugin, SimpleApiPlugin):
1010
def __init__(self):
1111
super().__init__()
1212
self.polling_thread = None
@@ -16,9 +16,14 @@ def __init__(self):
1616
self.polling_interval = 10
1717
self.xml_temp_path = None # XPath-like path for XML parsing
1818
self.current_chamber_temp = None
19+
self.temperature_history = [] # Store temperature history
20+
self.max_history_hours = 24 # Maximum hours of history to keep
1921

2022
def on_after_startup(self):
2123
self._logger.info("ExternalTempReader plugin loaded. Loading configuration...")
24+
# Clear history on startup
25+
self.temperature_history = []
26+
self._logger.info("Temperature history cleared on startup")
2227
self.load_configuration()
2328
self.start_polling_thread()
2429

@@ -88,6 +93,9 @@ def poll_external_temp(self):
8893
self.current_chamber_temp = temp
8994
self._logger.info(f"Chamber temperature updated: {temp:.2f}°C")
9095

96+
# Add to history
97+
self._add_to_history(temp)
98+
9199
# Send temperature update to frontend
92100
self._send_temperature_update(temp)
93101
except Exception as e:
@@ -195,3 +203,31 @@ def _send_temperature_update(self, temperature):
195203
self._logger.debug(f"Sent temperature update to frontend: {temperature:.2f}°C")
196204
except Exception as e:
197205
self._logger.error(f"Failed to send temperature update to frontend: {e}")
206+
207+
def _add_to_history(self, temperature):
208+
"""Add temperature reading to history and cleanup old data."""
209+
import time
210+
current_time = int(time.time() * 1000) # milliseconds
211+
212+
# Add new data point
213+
self.temperature_history.append({
214+
"time": current_time,
215+
"temperature": temperature
216+
})
217+
218+
# Cleanup old data (older than max_history_hours)
219+
cutoff_time = current_time - (self.max_history_hours * 3600 * 1000)
220+
self.temperature_history = [
221+
point for point in self.temperature_history
222+
if point["time"] >= cutoff_time
223+
]
224+
225+
self._logger.debug(f"History size: {len(self.temperature_history)} points")
226+
227+
def on_api_get(self, request):
228+
"""Handle API GET requests."""
229+
# Send full temperature history when requested
230+
return {
231+
"history": self.temperature_history,
232+
"current_temp": self.current_chamber_temp
233+
}

octoprint_external_temp_reader/static/js/external_temp_reader.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,49 @@ $(function() {
274274
};
275275

276276
self.onAfterBinding = function() {
277+
// Load temperature history from server
278+
$.ajax({
279+
url: API_BASEURL + "plugin/external_temp_reader",
280+
type: "GET",
281+
dataType: "json",
282+
success: function(response) {
283+
if (response.history && Array.isArray(response.history)) {
284+
console.log("External Temp Reader: Loaded " + response.history.length + " historical data points");
285+
self.temperatureHistory = response.history;
286+
287+
// Update statistics with loaded data
288+
self.updateStatistics();
289+
290+
// Update current temperature if available
291+
if (response.current_temp !== null && response.current_temp !== undefined) {
292+
self.temperature(response.current_temp.toFixed(1) + "°C");
293+
self.connectionStatus("Connected");
294+
var lastPoint = response.history[response.history.length - 1];
295+
if (lastPoint) {
296+
self.lastUpdate(new Date(lastPoint.time).toLocaleTimeString());
297+
}
298+
}
299+
300+
// Initialize chart with loaded data
301+
if ($("#chamber-temperature-chart").length > 0) {
302+
self.initializeChart();
303+
self.updateChart();
304+
}
305+
}
306+
},
307+
error: function(xhr, status, error) {
308+
console.error("External Temp Reader: Failed to load history:", error);
309+
}
310+
});
311+
277312
// Wait for tab to be visible and Flot to be available
278313
setTimeout(function() {
279314
// Check if we're on the tab and Flot is available
280315
if ($("#chamber-temperature-chart").length > 0) {
281316
console.log("External Temp Reader: Initializing chart");
282-
self.initializeChart();
317+
if (!self.chart) {
318+
self.initializeChart();
319+
}
283320

284321
// Set up periodic chart updates (every 5 seconds)
285322
setInterval(function() {

0 commit comments

Comments
 (0)