Label print API
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
from fastapi import FastAPI, Depends, HTTPException, APIRouter, status
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List, Optional
|
||||
from typing import List, Optional, Dict, Any
|
||||
from datetime import datetime, timedelta
|
||||
from .database import engine, get_db, Base
|
||||
from .models import Drug, DrugVariant, Dispensing, User
|
||||
from .auth import hash_password, verify_password, create_access_token, get_current_user, get_current_admin_user, ACCESS_TOKEN_EXPIRE_MINUTES
|
||||
from .mqtt_service import publish_label_print
|
||||
from pydantic import BaseModel
|
||||
|
||||
# Create tables
|
||||
@@ -117,6 +118,21 @@ class DispensingResponse(BaseModel):
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
class LabelVariables(BaseModel):
|
||||
practice_name: str
|
||||
animal_name: str
|
||||
drug_name: str
|
||||
dosage: str
|
||||
quantity: str
|
||||
expiry_date: str
|
||||
|
||||
class LabelPrintRequest(BaseModel):
|
||||
variables: LabelVariables
|
||||
|
||||
class LabelPrintResponse(BaseModel):
|
||||
success: bool
|
||||
message: str
|
||||
|
||||
# Authentication Routes
|
||||
@router.post("/auth/register", response_model=TokenResponse)
|
||||
def register(user_data: UserCreate, db: Session = Depends(get_db)):
|
||||
@@ -475,5 +491,49 @@ def get_variant_dispensings(variant_id: int, db: Session = Depends(get_db), curr
|
||||
|
||||
return db.query(Dispensing).filter(Dispensing.drug_variant_id == variant_id).order_by(Dispensing.dispensed_at.desc()).all()
|
||||
|
||||
# Label printing endpoint
|
||||
@router.post("/labels/print", response_model=LabelPrintResponse)
|
||||
def print_label(label_request: LabelPrintRequest, current_user: User = Depends(get_current_user)):
|
||||
"""
|
||||
Print a drug label by publishing an MQTT message
|
||||
|
||||
This endpoint publishes a label print request to the MQTT broker,
|
||||
which will be picked up by the label printing service.
|
||||
"""
|
||||
try:
|
||||
# Get label configuration from environment
|
||||
import os
|
||||
template_id = os.getenv("LABEL_TEMPLATE_ID", "vet_label")
|
||||
label_size = os.getenv("LABEL_SIZE", "29x90")
|
||||
test_mode = os.getenv("LABEL_TEST", "false").lower() == "true"
|
||||
|
||||
# Convert the request to the MQTT message format
|
||||
mqtt_message = {
|
||||
"template_id": template_id,
|
||||
"label_size": label_size,
|
||||
"variables": label_request.variables.dict(),
|
||||
"test": test_mode
|
||||
}
|
||||
|
||||
# Publish to MQTT
|
||||
success = publish_label_print(mqtt_message)
|
||||
|
||||
if success:
|
||||
return LabelPrintResponse(
|
||||
success=True,
|
||||
message="Label print request sent successfully"
|
||||
)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail="Failed to send label print request to MQTT broker"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail=f"Error sending label print request: {str(e)}"
|
||||
)
|
||||
|
||||
# Include router with /api prefix
|
||||
app.include_router(router)
|
||||
|
||||
Reference in New Issue
Block a user