1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Serialize, Clone)]
pub enum PrintStatus {
#[default]
Unknown,
Idle,
Printing,
Paused,
Finished,
Stopped,
Error,
Attention,
Busy,
}
pub trait UpdateFrom<T> {
fn update_from(&mut self, source: &T);
}
#[derive(Debug, Default, Serialize, Clone)]
pub struct PrinterState {
pub status: PrintStatus,
pub bed_temp: f32,
pub target_bed_temp: f32,
pub nozzle_temp: f32,
pub target_nozzle_temp: f32,
pub axis_x: f32,
pub axis_y: f32,
pub axis_z: f32,
pub flow: u32,
pub speed: u32,
pub fan_hotend: u32,
pub fan_print: u32,
pub progress: f32,
pub time_remaining: u32,
pub time_printing: u32,
pub layer_num: u32,
pub total_layer_num: u32,
pub print_speed: Option<String>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
enum PrusaState {
Idle,
Printing,
Paused,
Finished,
Stopped,
Error,
Attention,
Busy,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
enum BambuState {
Idle,
Running,
Pause,
Finish,
Failed,
}
#[derive(Debug, Deserialize)]
struct PrusaJob {
progress: f32,
time_remaining: u32,
time_printing: u32,
}
#[derive(Debug, Deserialize)]
struct PrusaPrinterInfo {
state: PrusaState,
temp_nozzle: f32,
target_nozzle: f32,
temp_bed: f32,
target_bed: f32,
axis_x: f32,
axis_y: f32,
axis_z: f32,
flow: u32,
speed: u32,
fan_hotend: u32,
fan_print: u32,
}
#[derive(Debug, Deserialize)]
pub struct PrusaStatus {
job: Option<PrusaJob>,
printer: PrusaPrinterInfo,
}
impl UpdateFrom<PrusaStatus> for PrinterState {
fn update_from(&mut self, status: &PrusaStatus) {
self.status = match status.printer.state {
PrusaState::Idle => PrintStatus::Idle,
PrusaState::Printing => PrintStatus::Printing,
PrusaState::Paused => PrintStatus::Paused,
PrusaState::Finished => PrintStatus::Finished,
PrusaState::Stopped => PrintStatus::Stopped,
PrusaState::Error => PrintStatus::Error,
PrusaState::Attention => PrintStatus::Attention,
PrusaState::Busy => PrintStatus::Busy,
};
let p = &status.printer;
self.bed_temp = p.temp_bed;
self.target_bed_temp = p.target_bed;
self.nozzle_temp = p.temp_nozzle;
self.target_nozzle_temp = p.target_nozzle;
self.axis_x = p.axis_x;
self.axis_y = p.axis_y;
self.axis_z = p.axis_z;
self.flow = p.flow;
self.speed = p.speed;
self.fan_hotend = p.fan_hotend;
self.fan_print = p.fan_print;
if let Some(job) = &status.job {
self.progress = job.progress;
self.time_remaining = job.time_remaining;
self.time_printing = job.time_printing;
}
}
}
#[derive(Deserialize)]
struct BambuPrintMessage {
bed_target_temper: Option<f32>,
bed_temper: Option<f32>,
gcode_state: Option<BambuState>,
layer_num: Option<u32>,
mc_percent: Option<u32>,
mc_remaining_time: Option<u32>,
nozzle_target_temper: Option<f32>,
nozzle_temper: Option<f32>,
print_speed: Option<String>,
total_layer_num: Option<u32>,
}
#[derive(Deserialize)]
pub struct BambuStatus {
print: Option<BambuPrintMessage>,
}
impl UpdateFrom<BambuStatus> for PrinterState {
fn update_from(&mut self, status: &BambuStatus) {
if let Some(print) = &status.print {
if let Some(v) = print.bed_temper { self.bed_temp = v; }
if let Some(v) = print.bed_target_temper { self.target_bed_temp = v; }
if let Some(v) = print.nozzle_temper { self.nozzle_temp = v; }
if let Some(v) = print.nozzle_target_temper { self.target_nozzle_temp = v; }
if let Some(v) = print.mc_percent { self.progress = v as f32; }
if let Some(v) = print.mc_remaining_time { self.time_remaining = v; }
if let Some(v) = print.layer_num { self.layer_num = v; }
if let Some(v) = print.total_layer_num { self.total_layer_num = v; }
if let Some(v) = &print.print_speed { self.print_speed = Some(v.clone()); }
if let Some(gcode_state) = &print.gcode_state {
self.status = match gcode_state {
BambuState::Idle => PrintStatus::Idle,
BambuState::Running => PrintStatus::Printing,
BambuState::Pause => PrintStatus::Paused,
BambuState::Finish => PrintStatus::Finished,
BambuState::Failed => PrintStatus::Error,
};
}
}
}
}
#[derive(Debug, Deserialize)]
pub struct Config {
// This allows you to have a list of different printer types
pub printers: Vec<Printer>,
}
impl Config {
pub fn load(toml: &str) -> Config {
toml::from_str(toml).expect("Couldn't parse config.toml")
}
}
#[derive(Debug, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum Printer {
Prusa {
name: String,
host: String,
api_key: String,
},
Bambu {
name: String,
host: String,
access_code: String,
serial_number: String,
},
}
|