@@ -121,53 +121,96 @@ public async Task<PrinterTelemetry> GetPrinterTelemetryAsync()
121121 if ( ! IsConnected ) return LastTelemetry ;
122122
123123 // Get temperature data
124- var tempResponse = await Client . GetAsync ( "/printer/objects/query?heater_bed&extruder" , _cts . Token ) ;
125- if ( tempResponse . IsSuccessStatusCode )
124+ try
126125 {
127- var json = await tempResponse . Content . ReadAsStringAsync ( ) ;
128- using var doc = JsonDocument . Parse ( json ) ;
129- var root = doc . RootElement . GetProperty ( "result" ) . GetProperty ( "status" ) ;
130-
131- LastTelemetry . BedTemp = root . GetProperty ( "heater_bed" ) . GetProperty ( "temperature" ) . GetDouble ( ) ;
132- LastTelemetry . BedTarget = root . GetProperty ( "heater_bed" ) . GetProperty ( "target" ) . GetDouble ( ) ;
133- LastTelemetry . HotendTemp = root . GetProperty ( "extruder" ) . GetProperty ( "temperature" ) . GetDouble ( ) ;
134- LastTelemetry . HotendTarget = root . GetProperty ( "extruder" ) . GetProperty ( "target" ) . GetDouble ( ) ;
126+ var tempResponse = await Client . GetAsync ( "/printer/objects/query?heater_bed&extruder" , _cts . Token ) ;
127+ if ( tempResponse . IsSuccessStatusCode )
128+ {
129+ var json = await tempResponse . Content . ReadAsStringAsync ( ) ;
130+ using var doc = JsonDocument . Parse ( json ) ;
131+ var root = doc . RootElement . GetProperty ( "result" ) . GetProperty ( "status" ) ;
132+
133+ LastTelemetry . BedTemp = root . GetProperty ( "heater_bed" ) . GetProperty ( "temperature" ) . GetDouble ( ) ;
134+ LastTelemetry . BedTarget = root . GetProperty ( "heater_bed" ) . GetProperty ( "target" ) . GetDouble ( ) ;
135+ LastTelemetry . HotendTemp = root . GetProperty ( "extruder" ) . GetProperty ( "temperature" ) . GetDouble ( ) ;
136+ LastTelemetry . HotendTarget = root . GetProperty ( "extruder" ) . GetProperty ( "target" ) . GetDouble ( ) ;
137+ }
135138 }
139+ catch { }
136140
137- var motionResponse = await Client . GetAsync ( "/printer/objects/ query?gcode_move,fan" , _cts . Token ) ;
138- if ( motionResponse . IsSuccessStatusCode )
141+ // Get motion and fan data (each object must be a separate query parameter)
142+ try
139143 {
140- var json = await motionResponse . Content . ReadAsStringAsync ( ) ;
141- using var doc = JsonDocument . Parse ( json ) ;
142- var status = doc . RootElement . GetProperty ( "result" ) . GetProperty ( "status" ) ;
143-
144- // Position data
145- var position = status . GetProperty ( "gcode_move" ) . GetProperty ( "position" ) ;
146- LastTelemetry . Position = new Vector3 (
147- ( float ) position [ 0 ] . GetDecimal ( ) ,
148- ( float ) position [ 1 ] . GetDecimal ( ) ,
149- ( float ) position [ 2 ] . GetDecimal ( )
150- ) ;
151- // Speed and flow data
152- LastTelemetry . FeedRate = ( int ) status . GetProperty ( "gcode_move" )
153- . GetProperty ( "speed" ) . GetDecimal ( ) ;
154- LastTelemetry . FlowRate = ( int ) ( status . GetProperty ( "gcode_move" )
155- . GetProperty ( "extrude_factor" ) . GetDecimal ( ) * 100 ) ;
156-
157- // Fan speed
158- LastTelemetry . FanSpeed = ( int ) ( status . GetProperty ( "fan" )
159- . GetProperty ( "speed" ) . GetDecimal ( ) * 100 ) ;
144+ var motionResponse = await Client . GetAsync ( "/printer/objects/query?gcode_move&fan" , _cts . Token ) ;
145+ if ( motionResponse . IsSuccessStatusCode )
146+ {
147+ var json = await motionResponse . Content . ReadAsStringAsync ( ) ;
148+ using var doc = JsonDocument . Parse ( json ) ;
149+ var status = doc . RootElement . GetProperty ( "result" ) . GetProperty ( "status" ) ;
150+
151+ // Position data
152+ var position = status . GetProperty ( "gcode_move" ) . GetProperty ( "position" ) ;
153+ LastTelemetry . Position = new Vector3 (
154+ ( float ) position [ 0 ] . GetDecimal ( ) ,
155+ ( float ) position [ 1 ] . GetDecimal ( ) ,
156+ ( float ) position [ 2 ] . GetDecimal ( )
157+ ) ;
158+ // Speed and flow data
159+ LastTelemetry . FeedRate = ( int ) status . GetProperty ( "gcode_move" )
160+ . GetProperty ( "speed" ) . GetDecimal ( ) ;
161+ LastTelemetry . FlowRate = ( int ) ( status . GetProperty ( "gcode_move" )
162+ . GetProperty ( "extrude_factor" ) . GetDecimal ( ) * 100 ) ;
163+
164+ // Fan speed
165+ LastTelemetry . FanSpeed = ( int ) ( status . GetProperty ( "fan" )
166+ . GetProperty ( "speed" ) . GetDecimal ( ) * 100 ) ;
167+ }
160168 }
169+ catch { }
161170
162- // Get printer status
163- var statusResponse = await Client . GetAsync ( "/printer/objects/query?print_stats" , _cts . Token ) ;
164- if ( statusResponse . IsSuccessStatusCode )
171+ // Get printer status, print progress, and job info
172+ try
165173 {
166- var json = await statusResponse . Content . ReadAsStringAsync ( ) ;
167- LastTelemetry . Status = json . Contains ( "printing" ) ?
168- PrinterStatus . Printing :
169- PrinterStatus . Connected ;
174+ var statusResponse = await Client . GetAsync ( "/printer/objects/query?print_stats&virtual_sdcard" , _cts . Token ) ;
175+ if ( statusResponse . IsSuccessStatusCode )
176+ {
177+ var json = await statusResponse . Content . ReadAsStringAsync ( ) ;
178+ using var doc = JsonDocument . Parse ( json ) ;
179+ var status = doc . RootElement . GetProperty ( "result" ) . GetProperty ( "status" ) ;
180+
181+ if ( status . TryGetProperty ( "print_stats" , out var printStats ) )
182+ {
183+ var state = printStats . TryGetProperty ( "state" , out var stateProp )
184+ ? stateProp . GetString ( ) ?? ""
185+ : "" ;
186+ LastTelemetry . Status = state switch
187+ {
188+ "printing" => PrinterStatus . Printing ,
189+ "paused" => PrinterStatus . Paused ,
190+ "error" => PrinterStatus . Error ,
191+ "complete" or "cancelled" or "standby" => PrinterStatus . Connected ,
192+ _ => PrinterStatus . Connected
193+ } ;
194+ LastTelemetry . SDCard . Printing = state == "printing" ;
195+
196+ if ( printStats . TryGetProperty ( "filename" , out var filenameProp ) )
197+ LastTelemetry . PrintJobName = filenameProp . GetString ( ) ?? "" ;
198+
199+ if ( printStats . TryGetProperty ( "print_duration" , out var durationProp ) )
200+ LastTelemetry . PrintDuration = TimeSpan . FromSeconds ( durationProp . GetDouble ( ) ) ;
201+
202+ if ( printStats . TryGetProperty ( "filament_used" , out var filamentProp ) )
203+ LastTelemetry . FilamentUsed = filamentProp . GetDouble ( ) ;
204+ }
205+
206+ if ( status . TryGetProperty ( "virtual_sdcard" , out var sdcard ) )
207+ {
208+ if ( sdcard . TryGetProperty ( "progress" , out var progressProp ) )
209+ LastTelemetry . SDCard . Progress = progressProp . GetDouble ( ) * 100 ;
210+ }
211+ }
170212 }
213+ catch { }
171214
172215 RaiseTelemetryUpdated ( ) ;
173216 return LastTelemetry ;
0 commit comments