补充遗漏翻译,新增启动脚本,整理import

This commit is contained in:
2026-05-09 16:42:17 +08:00
parent e542c482d7
commit 75ceec0798
16 changed files with 152 additions and 103 deletions

View File

@@ -1,8 +1,7 @@
import functools
from flask import Blueprint, request, jsonify
from app.models import ApiKey
from app.models import ApiKey, PrintFile, SystemConfig
from app.utils.octoprint_client import OctoPrintClient
from app.models import SystemConfig
api_bp = Blueprint('api_handle', __name__, url_prefix='/api/v1')
@@ -13,6 +12,17 @@ def get_octo_client():
return OctoPrintClient(url.value, apikey.value)
return None
def _enrich_job_data(job_data):
if job_data and job_data.get('job', {}).get('file', {}).get('name'):
internal_name = job_data['job']['file']['name']
internal_stl_name = str(internal_name)[:-5]+"stl"
pf = PrintFile.query.filter_by(filename=internal_stl_name).first()
if pf:
job_data['job']['file']['display_name'] = pf.original_filename
else:
job_data['job']['file']['display_name'] = internal_name
return job_data
def require_api_key(f):
@functools.wraps(f)
def decorated(*args, **kwargs):
@@ -30,15 +40,60 @@ def require_api_key(f):
@api_bp.route('/status', methods=['GET'])
@require_api_key
def get_status():
client = get_octo_client()
if not client:
return jsonify({'error': 'Printer not configured'}), 503
try:
status_data = client.get_printer_status()
job_data = client.get_job_info()
return jsonify({'status': status_data, 'job': job_data})
except Exception as e:
return jsonify({'error': str(e)}), 500
test_data = {
'job': {
'job': {
'estimatedPrintTime': 1234,
'filament': {'length': 765, 'volume': 24356},
'file': {'display_name': 'Test File','date': None, 'name': '20260414135441_42bff5215c6148b8b5f4d8c4f15d5ddc.gcode', 'origin': 'local', 'path': None, 'size': 1468987},
'lastPrintTime': None,
'user': None
},
'progress': {
'completion': 74.8,
'filepos': 1234,
'printTime': 1235,
'printTimeLeft': 6353,
'printTimeLeftOrigin': 5366
},
'state': 'Operational'
},
'status': {
'sd': {'ready': False},
'state': {
'error': '',
'flags': {
'cancelling': False,
'closedOrError': False,
'error': False,
'finishing': False,
'operational': True,
'paused': False,
'pausing': False,
'printing': False,
'ready': True,
'resuming': False,
'sdReady': False
},
'text': 'Operational test'
},
'temperature': {
'bed': {'actual': 85, 'offset': 0, 'target': 56},
'tool0': {'actual': 0.0, 'offset': 0, 'target': 340}
}
}
}
return jsonify(test_data)
# client = get_octo_client()
# if not client:
# return jsonify({'error': 'Printer not configured'}), 503
# try:
# status_data = client.get_printer_status()
# job_data = client.get_job_info()
# job_data = _enrich_job_data(job_data)
# return jsonify({'status': status_data, 'job': job_data})
# except Exception as e:
# return jsonify({'error': str(e)}), 500
@api_bp.route('/octoprint_client', methods=['POST'])
@require_api_key