54 lines
2.1 KiB
Rust
54 lines
2.1 KiB
Rust
|
|
use super::models::*;
|
||
|
|
use super::printer::{spawn_print_task, start_printing_locked, begin_next_locked};
|
||
|
|
use axum::{extract::{Path as AxumPath, State}, http::StatusCode, Json};
|
||
|
|
|
||
|
|
pub async fn job_advance(
|
||
|
|
State(store): State<JobStore>,
|
||
|
|
AxumPath(job_id): AxumPath<String>,
|
||
|
|
) -> Result<Json<JobView>, (StatusCode, String)> {
|
||
|
|
let (view, spawn_info) = {
|
||
|
|
let mut m = store.lock().unwrap();
|
||
|
|
let job = m.get_mut(&job_id).ok_or_else(|| (StatusCode::NOT_FOUND, "Задание не найдено".into()))?;
|
||
|
|
|
||
|
|
let si = match job.phase {
|
||
|
|
JobPhase::WaitingStart => {
|
||
|
|
let next_idx = 0;
|
||
|
|
job.current = Some(next_idx);
|
||
|
|
Some(start_printing_locked(job, next_idx))
|
||
|
|
}
|
||
|
|
JobPhase::AwaitingPickup => {
|
||
|
|
let c = job.current.unwrap_or(0);
|
||
|
|
if job.duplex_first_pass_done {
|
||
|
|
job.duplex_first_pass_done = false;
|
||
|
|
job.files[c].status = FileStatus::AwaitingClearOutput;
|
||
|
|
job.phase = JobPhase::AwaitingClearOutput; None
|
||
|
|
} else {
|
||
|
|
job.files[c].status = FileStatus::Done;
|
||
|
|
begin_next_locked(job, c)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
JobPhase::AwaitingClearOutput => {
|
||
|
|
let c = job.current.unwrap_or(0);
|
||
|
|
job.files[c].status = FileStatus::AwaitingFlip;
|
||
|
|
job.phase = JobPhase::AwaitingFlip; None
|
||
|
|
}
|
||
|
|
JobPhase::AwaitingFlip => {
|
||
|
|
let c = job.current.unwrap();
|
||
|
|
job.files[c].status = FileStatus::Printing;
|
||
|
|
job.phase = JobPhase::Printing;
|
||
|
|
Some(SpawnInfo {
|
||
|
|
job_id: job.id.clone(), idx: c,
|
||
|
|
path: job.files[c].path.to_path_buf(),
|
||
|
|
settings: job.files[c].settings.clone(),
|
||
|
|
is_second_pass: true,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
_ => None,
|
||
|
|
};
|
||
|
|
(job.view(), si)
|
||
|
|
};
|
||
|
|
|
||
|
|
if let Some(si) = spawn_info { spawn_print_task(store.clone(), si); }
|
||
|
|
Ok(Json(view))
|
||
|
|
}
|