3
3
from xml .etree import ElementTree as ET
4
4
5
5
import requests
6
- from octoprint .plugin import SettingsPlugin , StartupPlugin , ShutdownPlugin , TemplatePlugin , AssetPlugin
6
+ from octoprint .plugin import SettingsPlugin , StartupPlugin , ShutdownPlugin , TemplatePlugin , AssetPlugin , SimpleApiPlugin
7
7
8
8
9
- class ExternalTempReaderPlugin (SettingsPlugin , TemplatePlugin , AssetPlugin , StartupPlugin , ShutdownPlugin ):
9
+ class ExternalTempReaderPlugin (SettingsPlugin , TemplatePlugin , AssetPlugin , StartupPlugin , ShutdownPlugin , SimpleApiPlugin ):
10
10
def __init__ (self ):
11
11
super ().__init__ ()
12
12
self .polling_thread = None
@@ -16,9 +16,14 @@ def __init__(self):
16
16
self .polling_interval = 10
17
17
self .xml_temp_path = None # XPath-like path for XML parsing
18
18
self .current_chamber_temp = None
19
+ self .temperature_history = [] # Store temperature history
20
+ self .max_history_hours = 24 # Maximum hours of history to keep
19
21
20
22
def on_after_startup (self ):
21
23
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" )
22
27
self .load_configuration ()
23
28
self .start_polling_thread ()
24
29
@@ -88,6 +93,9 @@ def poll_external_temp(self):
88
93
self .current_chamber_temp = temp
89
94
self ._logger .info (f"Chamber temperature updated: { temp :.2f} °C" )
90
95
96
+ # Add to history
97
+ self ._add_to_history (temp )
98
+
91
99
# Send temperature update to frontend
92
100
self ._send_temperature_update (temp )
93
101
except Exception as e :
@@ -195,3 +203,31 @@ def _send_temperature_update(self, temperature):
195
203
self ._logger .debug (f"Sent temperature update to frontend: { temperature :.2f} °C" )
196
204
except Exception as e :
197
205
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
+ }
0 commit comments