change status output

This commit is contained in:
Robin Appelman 2025-09-18 19:17:15 +02:00
commit 9613b2c3ef
5 changed files with 142 additions and 31 deletions

1
Cargo.lock generated
View file

@ -1225,7 +1225,6 @@ dependencies = [
"thiserror 1.0.69", "thiserror 1.0.69",
"tokio", "tokio",
"toml 0.9.6", "toml 0.9.6",
"tower-service",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
] ]

View file

@ -10,7 +10,6 @@ tokio = { version = "1.47.1", features = ["macros", "rt-multi-thread", "signal"]
image = { version = "0.25.8", features = ["png", "jpeg", "bmp"] } image = { version = "0.25.8", features = ["png", "jpeg", "bmp"] }
axum = { version = "0.8.4", features = ["macros", "json"] } axum = { version = "0.8.4", features = ["macros", "json"] }
listenfd = "1.0.2" listenfd = "1.0.2"
tower-service = "0.3.3"
serde = { version = "1.0.225", features = ["derive"] } serde = { version = "1.0.225", features = ["derive"] }
thiserror = "1.0.69" thiserror = "1.0.69"
toml = "0.9.6" toml = "0.9.6"

View file

@ -25,11 +25,19 @@ ptouch-api [--config config.toml]
## API ## API
- GET `/status`: get printer and tape status, see the - GET `/status`: get printer and tape status for details about the fields.
[ptouch-rs documentation](https://docs.rs/ptouch-rs/latest/ptouch_rs/struct.Status.html)
for details about the fields.
- PUT `/print`: print the uploaded image, supports png, jpg and bmp - PUT `/print`: print the uploaded image, supports png, jpg and bmp
### Status
The returned status is a json with the following fields
- `media_width`: width of the loaded tape in mm.
- `pixel_width`: the width of the printed image in pixels.
- `media_type`: material type of the loaded tape.
- `text_color`: text color for the loaded tape.
- `tape_color`: tape color for the loaded tape.
## Supported printers ## Supported printers
The following printers are currently supported The following printers are currently supported

View file

@ -1,4 +1,5 @@
mod config; mod config;
mod status;
use crate::config::{Config, ListenConfig}; use crate::config::{Config, ListenConfig};
use axum::body::Bytes; use axum::body::Bytes;
@ -12,7 +13,7 @@ use image::imageops::FilterType;
use image::{EncodableLayout, ImageError, ImageReader}; use image::{EncodableLayout, ImageError, ImageReader};
use listenfd::ListenFd; use listenfd::ListenFd;
use main_error::MainResult; use main_error::MainResult;
use ptouch_rs::{Printer, Status}; use ptouch_rs::Printer;
use std::fs::{create_dir_all, remove_file, set_permissions}; use std::fs::{create_dir_all, remove_file, set_permissions};
use std::io::Cursor; use std::io::Cursor;
use std::net::SocketAddr; use std::net::SocketAddr;
@ -24,6 +25,7 @@ use tokio::net::UnixListener;
use tokio::signal::ctrl_c; use tokio::signal::ctrl_c;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tracing::{debug, info}; use tracing::{debug, info};
use crate::status::PStatus;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub struct Args { pub struct Args {
@ -171,11 +173,11 @@ impl IntoResponse for ApiError {
} }
} }
async fn status(State(state): State<App>) -> Result<Json<Status>, ApiError> { async fn status(State(state): State<App>) -> Result<Json<PStatus>, ApiError> {
let mut printer = state.printer.lock().await; let mut printer = state.printer.lock().await;
debug!("reloading printer status"); debug!("reloading printer status");
printer.reload_status().await?; printer.reload_status().await?;
Ok(Json(printer.status())) Ok(Json(PStatus::new(printer.status(), printer.ty())))
} }
async fn print(State(state): State<App>, bytes: Bytes) -> Result<(), ApiError> { async fn print(State(state): State<App>, bytes: Bytes) -> Result<(), ApiError> {

103
src/status.rs Normal file
View file

@ -0,0 +1,103 @@
use ptouch_rs::{MediaType, PrinterType, Status, TapeColor, TapeSize, TextColor};
use serde::Serialize;
#[derive(Debug, Serialize)]
pub struct PStatus {
/// width in mm
media_width: f32,
pixel_width: u32,
media_type: &'static str,
text_color: &'static str,
tape_color: &'static str,
}
impl PStatus {
pub fn new(status: Status, printer_type: PrinterType) -> Self {
PStatus {
media_width: tape_size(&status.media_width),
pixel_width: printer_type.info().max_px,
media_type: media_type(&status.media_type),
text_color: text_color(&status.text_color),
tape_color: tape_color(&status.tape_color),
}
}
}
fn tape_color(color: &TapeColor) -> &'static str {
match color {
TapeColor::None => "none",
TapeColor::BerryPink_TZe_MQP35 => "berry-pink",
TapeColor::Black => "black",
TapeColor::Blue_TZe_5_345_5 => "blue",
TapeColor::Blue => "blue",
TapeColor::Cleaning => "cleaning",
TapeColor::Clear => "clear",
TapeColor::ClearMatte => "clear-matte",
TapeColor::GoldSatin => "gold-satin",
TapeColor::Green => "green",
TapeColor::HeatShrinkTube => "heat-shrink-tube",
TapeColor::Incompatible => "incompatible",
TapeColor::LightGray_TZe_MQL35 => "light-gray",
TapeColor::LimeGreen_TZe_MQG35 => "lime-green",
TapeColor::OrangeFluorescent => "orange-fluorescent",
TapeColor::Pink => "pink",
TapeColor::Red_TZe_435 => "red",
TapeColor::Red => "red",
TapeColor::SilverMatte => "silver-matte",
TapeColor::SilverSatin => "silver-satin",
TapeColor::Stencil => "stencil",
TapeColor::White => "white",
TapeColor::WhiteFlexId => "white-flex-id",
TapeColor::WhiteMatte => "white-matte",
TapeColor::Yellow => "yellow",
TapeColor::YellowFlexId => "yellow-flex-id",
TapeColor::YellowFluorescent => "yellow-fluorescent",
TapeColor::Other => "other",
TapeColor::Unknown(_) => "unknown",
}
}
fn text_color(color: &TextColor) -> &'static str {
match color {
TextColor::None => "none",
TextColor::Black => "black",
TextColor::Blue => "blue",
TextColor::BlueF => "blue",
TextColor::Cleaning => "cleaning",
TextColor::Gold => "gold",
TextColor::Incompatible => "incompatible",
TextColor::Red => "red",
TextColor::Stencil => "stencil",
TextColor::White => "white",
TextColor::Other => "other",
TextColor::Unknown(_) => "unknown",
}
}
fn media_type(ty: &MediaType) -> &'static str {
match ty {
MediaType::None => "none",
MediaType::Laminated => "laminated",
MediaType::NonLaminated => "non-laminated",
MediaType::Fabric => "fabric",
MediaType::HeatShrink => "heat-shrink",
MediaType::Fle => "flex",
MediaType::FlexibleId => "flexible-id",
MediaType::Satin => "satin",
MediaType::Incompatible => "incompatible",
MediaType::Unknown(_) => "unknown",
}
}
fn tape_size(size: &TapeSize) -> f32 {
match size {
TapeSize::None => 0.0,
TapeSize::ThreePointFive => 3.5,
TapeSize::Six => 6.0,
TapeSize::Nine => 9.0,
TapeSize::Twelve => 12.0,
TapeSize::Eighteen => 18.0,
TapeSize::TwentyFour => 24.0,
TapeSize::ThirtySix => 36.0,
}
}