use serde_json::{Map, Value, json};
use crate::core::command::Command;
pub struct FakePrinter {
gcode_state: String,
print_error: i64,
}
impl FakePrinter {
pub fn idle() -> Self {
Self {
gcode_state: "IDLE".to_string(),
print_error: 0,
}
}
pub fn with_error(code: i64) -> Self {
Self {
gcode_state: "IDLE".to_string(),
print_error: code,
}
}
pub fn snapshot(&self) -> Value {
json!({ "print": {
"command": "push_status",
"msg": 0,
"gcode_state": self.gcode_state,
"print_error": self.print_error,
}})
}
pub fn ack(&self, cmd: &Command, seq: &str, success: bool, reason: &str) -> Value {
let mut m = Map::new();
m.insert(
cmd.category().to_string(),
json!({
"sequence_id": seq,
"result": if success { "success" } else { "fail" },
"reason": reason,
}),
);
Value::Object(m)
}
pub fn effect_delta(&mut self, cmd: &Command) -> Value {
match cmd {
Command::ProjectFile(_) | Command::GcodeFile(_) | Command::Calibration { .. } => {
self.gcode_state = "RUNNING".to_string();
self.print_delta(json!({ "gcode_state": "RUNNING" }))
}
Command::Pause => {
self.gcode_state = "PAUSE".to_string();
self.print_delta(json!({ "gcode_state": "PAUSE" }))
}
Command::Resume => {
self.gcode_state = "RUNNING".to_string();
self.print_delta(json!({ "gcode_state": "RUNNING" }))
}
Command::Stop => {
self.gcode_state = "FINISH".to_string();
self.print_delta(json!({ "gcode_state": "FINISH" }))
}
Command::PrintSpeed(level) => self.print_delta(json!({ "spd_lvl": level.level() })),
Command::Led { node, on } => self.print_delta(json!({
"lights_report": [ { "node": node.as_str(), "mode": if *on { "on" } else { "off" } } ]
})),
Command::IpcamTimelapse(control) => {
self.print_delta(json!({ "ipcam": { "timelapse": control.as_str() } }))
}
_ => self.print_delta(json!({})),
}
}
pub fn new_error_delta(&mut self, code: i64) -> Value {
self.print_error = code;
self.print_delta(json!({ "print_error": code }))
}
fn print_delta(&self, fields: Value) -> Value {
let mut print = fields.as_object().cloned().unwrap_or_default();
print.insert("command".to_string(), json!("push_status"));
print.insert("msg".to_string(), json!(1));
json!({ "print": Value::Object(print) })
}
}