This repository was archived by the owner on Dec 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathklipper_handler.py
More file actions
145 lines (116 loc) · 4.8 KB
/
klipper_handler.py
File metadata and controls
145 lines (116 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import requests
import os
import json
import datetime # [필수] 날짜 기록용
MOONRAKER_URL = "http://localhost:7125"
# [수정] timeout을 5초로 넉넉하게 설정 (네트워크 지연 대비)
def mr_get(endpoint):
return requests.get(f"{MOONRAKER_URL}{endpoint}", timeout=5).json()
def mr_post(endpoint, data=None):
return requests.post(f"{MOONRAKER_URL}{endpoint}", json=data, timeout=5).json()
# [수정] 로그 함수 업그레이드 (파일 저장 기능 추가)
def Log(title, content):
# 1. 기본 콘솔 출력
print(f"[ {title} ] >> {content}")
try:
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
current_dir = os.path.dirname(os.path.abspath(__file__)) # 현재 파일 위치 기준
# 2. 정지/취소 로그 저장 (stop_log.txt)
if "Stop" in title or "STOP" in str(content) or "CANCEL" in str(content) or "FORCE" in str(content):
log_path = os.path.join(current_dir, "stop_log.txt")
with open(log_path, "a", encoding="utf-8") as f:
f.write(f"[{timestamp}] [ {title} ] >> {content}\n")
# 3. [신규] 업로드 관련 로그 저장 (upload_log.txt)
if "Upload" in title:
log_path = os.path.join(current_dir, "upload_log.txt")
with open(log_path, "a", encoding="utf-8") as f:
f.write(f"[{timestamp}] [ {title} ] >> {content}\n")
except Exception:
pass # 파일 저장 실패해도 서버는 멈추지 않음
# ===============================
# Klipper 상태 가져오기
# ===============================
def getPrinterStatus():
try:
# [수정] 쿼리 타임아웃도 적용됨 (mr_get 내부에서 처리)
r = mr_get("/printer/objects/query?heater_bed&extruder&toolhead&print_stats&display_status")
status = r["result"]["status"]
bed = status["heater_bed"]["temperature"]
nozzle = status["extruder"]["temperature"]
pos = status["toolhead"]["position"]
x, y, z = pos[0], pos[1], pos[2]
printing = 1 if status["print_stats"]["state"] == "printing" else 0
progress_float = status.get("display_status", {}).get("progress", 0)
progress_percent = int(progress_float * 100)
return {
"bedTemp": bed,
"nozzleTemp": nozzle,
"percent": progress_percent,
"x": x,
"y": y,
"z": z,
"isPrinting": printing,
"isConnected": 1,
}
except Exception as e:
Log("KlipperStatusError", f"Connection Lost: {e}")
return {
"bedTemp": 0,
"nozzleTemp": 0,
"percent": 0,
"x": 0, "y": 0, "z": 0,
"isPrinting": 0,
"isConnected": 0
}
# ===============================
# 파일 업로드 (수정됨)
# ===============================
def uploadFile(local_path):
try:
filename = os.path.basename(local_path)
url = f"{MOONRAKER_URL}/server/files/upload"
with open(local_path, "rb") as f:
files = {"file": (filename, f, "application/octet-stream")}
# 파일 업로드 등 큰 작업은 시간을 넉넉하게 (30초)
response = requests.post(url, files=files, timeout=30)
# 응답 코드 확인
if response.status_code == 201: # 201 Created
Log("Upload", f"Uploaded ready: {filename}")
return filename
else:
Log("UploadError", f"Failed with status code: {response.status_code}, response: {response.text}")
return None
except Exception as e:
Log("UploadError", e)
return None
# ===============================
# 파일명으로 출력 시작
# ===============================
def startPrint(filename):
try:
mr_post("/printer/print/start", {"filename": filename})
Log("Print", f"Print Start: {filename}")
except Exception as e:
Log("StartError", e)
# ===============================
# 개별 GCODE 명령 전송
# ===============================
def sendGcode(cmd: str):
try:
mr_post("/printer/gcode/script", {"script": cmd})
Log("GCODE", f"Executed: {cmd}")
except Exception as e:
Log("GcodeError", e)
# ===============================
# 프린트 중지/취소 (수정됨)
# ===============================
def stopPrint():
try:
Log("Print", "Attempting to STOP...")
res1 = mr_post("/printer/gcode/script", {"script": "CANCEL_PRINT"})
Log("Debug", f"CANCEL_PRINT Result: {res1}")
res2 = mr_post("/printer/gcode/script", {"script": "SDCARD_RESET_FILE"})
Log("Debug", f"SDCARD_RESET Result: {res2}")
Log("Print", "Sent FORCE STOP commands")
except Exception as e:
Log("StopError", e)