1+ use std:: path:: PathBuf ;
2+
3+ use optional_struct:: optional_struct;
14use serde:: Deserialize ;
25
36use crate :: { error:: Error , moonraker_connection:: MoonrakerConnection } ;
@@ -10,21 +13,59 @@ pub struct MoonrakerFile {
1013 pub permissions : String ,
1114}
1215
13- #[ derive( Debug , Deserialize ) ]
16+ #[ derive( Debug , Deserialize , PartialEq , Clone ) ]
1417pub struct MoonrakerFileThumbnail {
1518 pub width : i32 ,
1619 pub height : i32 ,
1720 pub size : i32 ,
21+ #[ serde( alias = "relative_path" ) ]
1822 pub thumbnail_path : String ,
1923}
2024
25+ #[ optional_struct]
26+ #[ derive( Debug , Deserialize , Default ) ]
27+ pub struct GcodeMetadata {
28+ pub size : i32 ,
29+ pub modified : f32 ,
30+ pub uuid : String ,
31+ pub file_processors : Vec < String > ,
32+ pub slicer : String ,
33+ pub slicer_version : String ,
34+ pub gcode_start_byte : i32 ,
35+ pub gcode_int_byte : i32 ,
36+ pub object_height : f32 ,
37+ pub estimated_time : f32 ,
38+ pub nozzle_diameter : f32 ,
39+ pub layer_height : f32 ,
40+ pub first_layer_height : f32 ,
41+ pub first_layer_extr_temp : f32 ,
42+ pub first_layer_bed_temp : f32 ,
43+ pub chamber_temp : f32 ,
44+ pub filament_name : String ,
45+ pub filament_colors : Vec < String > ,
46+ pub extruder_colors : Vec < String > ,
47+ pub filament_temps : Vec < i32 > ,
48+ pub filament_type : String ,
49+ pub filament_total : f32 ,
50+ pub filament_change_count : i32 ,
51+ pub filament_weight_total : f32 ,
52+ pub filament_weights : Vec < f32 > ,
53+ pub mmu_print : i32 ,
54+ pub referenced_tools : Vec < i32 > ,
55+ pub thumbnails : Vec < MoonrakerFileThumbnail > ,
56+ pub job_id : Option < String > ,
57+ pub print_start_time : Option < f32 > ,
58+ pub filename : String ,
59+ }
60+
2161pub trait FileManagementRequestHandler {
2262 async fn list_files ( & self , root : & str ) -> Result < Vec < MoonrakerFile > , Error > ;
2363 async fn list_gcode_files ( & self ) -> Result < Vec < MoonrakerFile > , Error > ;
2464 async fn get_thumbnails_for_file (
2565 & self ,
2666 file : & str ,
2767 ) -> Result < Vec < MoonrakerFileThumbnail > , Error > ;
68+ async fn get_gcode_metadata_for_file ( & self , filename : & str ) -> Result < GcodeMetadata , Error > ;
2869}
2970
3071impl FileManagementRequestHandler for MoonrakerConnection {
@@ -47,4 +88,75 @@ impl FileManagementRequestHandler for MoonrakerConnection {
4788 self . send_request ( "server.files.thumbnails" , Some ( args) )
4889 . await
4990 }
91+
92+ async fn get_gcode_metadata_for_file ( & self , filename : & str ) -> Result < GcodeMetadata , Error >
93+ {
94+ let args = serde_json:: json!( { "filename" : filename} ) ;
95+ let gcode_metadata : OptionalGcodeMetadata = self . send_request ( "server.files.metadata" , Some ( args) ) . await ?;
96+
97+ Ok ( GcodeMetadata :: from_optional ( gcode_metadata) )
98+ }
5099}
100+
101+ impl GcodeMetadata {
102+ pub fn from_optional ( optional : OptionalGcodeMetadata ) -> Self {
103+ Self {
104+ size : optional. size . unwrap_or_default ( ) ,
105+ modified : optional. modified . unwrap_or_default ( ) ,
106+ uuid : optional. uuid . unwrap_or_default ( ) ,
107+ file_processors : optional. file_processors . unwrap_or_default ( ) ,
108+ slicer : optional. slicer . unwrap_or_default ( ) ,
109+ slicer_version : optional. slicer_version . unwrap_or_default ( ) ,
110+ gcode_start_byte : optional. gcode_start_byte . unwrap_or_default ( ) ,
111+ gcode_int_byte : optional. gcode_int_byte . unwrap_or_default ( ) ,
112+ object_height : optional. object_height . unwrap_or_default ( ) ,
113+ estimated_time : optional. estimated_time . unwrap_or_default ( ) ,
114+ nozzle_diameter : optional. nozzle_diameter . unwrap_or_default ( ) ,
115+ layer_height : optional. layer_height . unwrap_or_default ( ) ,
116+ first_layer_height : optional. first_layer_height . unwrap_or_default ( ) ,
117+ first_layer_extr_temp : optional. first_layer_extr_temp . unwrap_or_default ( ) ,
118+ first_layer_bed_temp : optional. first_layer_bed_temp . unwrap_or_default ( ) ,
119+ chamber_temp : optional. chamber_temp . unwrap_or_default ( ) ,
120+ filament_name : optional. filament_name . unwrap_or_default ( ) ,
121+ filament_colors : optional. filament_colors . unwrap_or_default ( ) ,
122+ extruder_colors : optional. extruder_colors . unwrap_or_default ( ) ,
123+ filament_temps : optional. filament_temps . unwrap_or_default ( ) ,
124+ filament_type : optional. filament_type . unwrap_or_default ( ) ,
125+ filament_total : optional. filament_total . unwrap_or_default ( ) ,
126+ filament_change_count : optional. filament_change_count . unwrap_or_default ( ) ,
127+ filament_weight_total : optional. filament_weight_total . unwrap_or_default ( ) ,
128+ filament_weights : optional. filament_weights . unwrap_or_default ( ) ,
129+ mmu_print : optional. mmu_print . unwrap_or_default ( ) ,
130+ referenced_tools : optional. referenced_tools . unwrap_or_default ( ) ,
131+ thumbnails : optional. thumbnails . unwrap_or_default ( ) ,
132+ job_id : optional. job_id . clone ( ) ,
133+ print_start_time : optional. print_start_time . clone ( ) ,
134+ filename : optional. filename . unwrap_or_default ( ) ,
135+ }
136+ }
137+
138+ pub fn absolute_thumbnails ( & self ) -> Vec < MoonrakerFileThumbnail >
139+ {
140+ let buf = PathBuf :: from ( & self . filename ) ;
141+ let base_path = buf
142+ . parent ( )
143+ . and_then ( |f| Some ( f. to_str ( ) . unwrap ( ) ) )
144+ . unwrap_or ( "" ) ;
145+
146+ self . thumbnails
147+ . iter ( )
148+ . map ( |f| {
149+ MoonrakerFileThumbnail {
150+ width : f. width ,
151+ height : f. height ,
152+ size : f. size ,
153+ thumbnail_path : if base_path. is_empty ( ) {
154+ f. thumbnail_path . clone ( )
155+ } else {
156+ format ! ( "{}/{}" , base_path, f. thumbnail_path)
157+ }
158+ }
159+ } )
160+ . collect ( )
161+ }
162+ }
0 commit comments