printer
All checks were successful
Deploy WebSocket Server / deploy (push) Successful in 3s

This commit is contained in:
jeremygan2021
2026-03-05 20:04:16 +08:00
parent c66f80d0eb
commit 24e5b4d018
3 changed files with 136 additions and 153 deletions

View File

@@ -14,6 +14,10 @@ from dashscope.audio.asr import Recognition, RecognitionCallback, RecognitionRes
from dashscope import ImageSynthesis
from dashscope import Generation
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import convert_img
# 加载环境变量
load_dotenv()
dashscope.api_key = os.getenv("DASHSCOPE_API_KEY")
@@ -883,6 +887,34 @@ async def websocket_endpoint(websocket: WebSocket):
else:
await websocket.send_text("STATUS:ERROR:提示词为空")
elif text == "PRINT_IMAGE":
print("Received PRINT_IMAGE request")
if os.path.exists(GENERATED_IMAGE_FILE):
try:
# Use convert_img logic to get TSPL commands
tspl_data = convert_img.image_to_tspl_commands(GENERATED_IMAGE_FILE)
if tspl_data:
print(f"Sending printer data: {len(tspl_data)} bytes")
await websocket.send_text(f"PRINTER_DATA_START:{len(tspl_data)}")
# Send in chunks
chunk_size = 512
for i in range(0, len(tspl_data), chunk_size):
chunk = tspl_data[i:i+chunk_size]
await websocket.send_bytes(chunk)
# Small delay to prevent overwhelming ESP32 buffer
await asyncio.sleep(0.01)
await websocket.send_text("PRINTER_DATA_END")
print("Printer data sent")
else:
await websocket.send_text("STATUS:ERROR:图片转换失败")
except Exception as e:
print(f"Error converting image for printer: {e}")
await websocket.send_text(f"STATUS:ERROR:打印出错: {str(e)}")
else:
await websocket.send_text("STATUS:ERROR:没有可打印的图片")
elif text.startswith("GET_TASK_STATUS:"):
task_id = text.split(":", 1)[1].strip()
if task_id in active_tasks: