fix: 자동인쇄 중복 방지 + 인쇄 로깅 추가

문제: 같은 처방이 2번 인쇄됨
원인: printPaaiResult가 2곳에서 호출 (API 응답 + WebSocket 이벤트)

해결:
- window.printedSerials (Set) 으로 중복 인쇄 방지
- 인쇄 성공 시 Set에 추가
- 5분 후 자동 제거 (메모리 관리)

로깅 추가:
- 프론트: 콘솔에 시간 + 환자명 + 상태
- 백엔드: logs/print_history.log 파일에 기록
This commit is contained in:
thug0bin
2026-03-05 12:56:39 +09:00
parent 2eb92daf3e
commit c7169e6679
2 changed files with 52 additions and 9 deletions

View File

@@ -1461,6 +1461,23 @@ _INIT = _ESC + b'@' # 프린터 초기화
_CUT = _ESC + b'd\x03' # 피드 + 커트
def _log_print_history(pre_serial, patient_name, success, error=None):
"""인쇄 이력을 파일에 기록"""
try:
log_dir = Path(__file__).parent / 'logs'
log_dir.mkdir(exist_ok=True)
log_file = log_dir / 'print_history.log'
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
status = '✅ 성공' if success else f'❌ 실패: {error}'
line = f"[{timestamp}] {pre_serial} | {patient_name} | {status}\n"
with open(log_file, 'a', encoding='utf-8') as f:
f.write(line)
except Exception as e:
logging.warning(f"인쇄 로그 기록 실패: {e}")
@pmr_bp.route('/api/paai/print', methods=['POST'])
def paai_print():
"""PAAI 분석 결과 ESC/POS 인쇄"""
@@ -1473,6 +1490,8 @@ def paai_print():
analysis = result.get('analysis', {})
kims_summary = result.get('kims_summary', {})
logging.info(f"[PRINT] 요청 수신: {pre_serial} ({patient_name})")
# 영수증 텍스트 생성
message = _format_paai_receipt(pre_serial, patient_name, analysis, kims_summary)
@@ -1480,13 +1499,17 @@ def paai_print():
success = _print_escpos_text(message)
if success:
logging.info(f"PAAI 인쇄 완료: {pre_serial} ({patient_name})")
logging.info(f"[PRINT] ✅ 완료: {pre_serial} ({patient_name})")
_log_print_history(pre_serial, patient_name, True)
return jsonify({'success': True, 'message': '인쇄 완료'})
else:
logging.error(f"[PRINT] ❌ 프린터 연결 실패: {pre_serial}")
_log_print_history(pre_serial, patient_name, False, '프린터 연결 실패')
return jsonify({'success': False, 'error': '프린터 연결 실패'}), 500
except Exception as e:
logging.error(f"PAAI 인쇄 오류: {e}")
logging.error(f"[PRINT] ❌ 오류: {pre_serial} - {e}")
_log_print_history(pre_serial, patient_name, False, str(e))
return jsonify({'success': False, 'error': str(e)}), 500