-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
185 lines (132 loc) · 5.56 KB
/
Copy pathclient.py
File metadata and controls
185 lines (132 loc) · 5.56 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import json
from threading import Thread
import requests
class OctoPiClient:
def __init__(self, server_url, api_key):
self.api_key = api_key
self.addkey = '?apikey={0}'.format(api_key)
self.apiurl_printhead = '{0}/api/printer/printhead'.format(server_url)
self.apiurl_tool = '{0}/api/printer/tool'.format(server_url)
self.apiurl_bed = '{0}/api/printer/bed'.format(server_url)
self.apiurl_job = '{0}/api/job'.format(server_url)
self.apiurl_status = '{0}/api/printer?apikey={1}'.format(server_url, api_key)
self.apiurl_connection = '{0}/api/connection'.format(server_url)
self.thread = None
def get_printer_status(self, printer):
req = requests.get(self.apiurl_status)
if req.status_code == 200:
state = json.loads(req.text)
# Set status flags
printer.HotEndTemp = state['temperature']['tool0']['actual']
printer.BedTemp = state['temperature']['bed']['actual']
printer.HotEndTempTarget = state['temperature']['tool0']['target']
printer.BedTempTarget = state['temperature']['bed']['target']
if not printer.HotEndTempTarget:
printer.HotEndTempTarget = 0.0
if not printer.BedTempTarget:
printer.BedTempTarget = 0.0
if printer.HotEndTempTarget > 0.0:
printer.HotHotEnd = True
else:
printer.HotHotEnd = False
if printer.BedTempTarget > 0.0:
printer.HotBed = True
else:
printer.HotBed = False
def get_job_status(self, printer):
req = requests.get(self.apiurl_job + self.addkey)
if req.status_code == 200:
jobState = json.loads(req.text)
printer.Completion = jobState['progress']['completion'] or 0
printer.PrintTimeLeft = jobState['progress']['printTimeLeft'] or 0
printer.FileName = jobState['job']['file']['name'] or ""
def get_connection_status(self, printer):
req = requests.get(self.apiurl_connection + self.addkey)
if req.status_code == 200:
connState = json.loads(req.text)
# Save temperatures to lists
printer.HotEndTempList.popleft()
printer.HotEndTempList.append(printer.HotEndTemp)
printer.BedTempList.popleft()
printer.BedTempList.append(printer.BedTemp)
printer.Paused = connState['current']['state'] == "Paused"
printer.Printing = connState['current']['state'] == "Printing"
# Send API-data to OctoPrint
def send_api_command(self, url, data):
headers = {'content-type': 'application/json', 'X-Api-Key': self.api_key}
# Only allow executing one action at a time. Should prevent printers running away with queued commands
#if self.thread and self.thread.isAlive():
# return
#self.thread = Thread(target=self.send_request, args=(url, data, headers))
#self.thread.start()
self.send_request(url, data, headers)
def send_request(self, url, data, headers):
requests.post(url, data=json.dumps(data), headers=headers, timeout=5.0)
print "Request done. URL: " + url + " Data: " + json.dumps(data)
return
def home_xy(self):
data = {"command": "home", "axes": ["x", "y"]}
# Send command
self.send_api_command(self.apiurl_printhead, data)
return
def home_x(self):
data = {"command": "home", "axes": ["x"]}
# Send command
self.send_api_command(self.apiurl_printhead, data)
return
def home_y(self):
data = {"command": "home", "axes": ["y"]}
# Send command
self.send_api_command(self.apiurl_printhead, data)
return
def home_z(self):
data = {"command": "home", "axes": ["z"]}
# Send command
self.send_api_command(self.apiurl_printhead, data)
return
def jog_axis(self, x=0, y=0, z=0):
data = {"command": "jog", "x": x, "y": y, "z": z}
# Send command
self.send_api_command(self.apiurl_printhead, data)
return
def z_up(self):
data = {"command": "jog", "x": 0, "y": 0, "z": 25}
# Send command
self.send_api_command(self.apiurl_printhead, data)
return
def heat_bed(self):
# is the bed already hot, in that case turn it off
if self.HotBed:
data = {"command": "target", "target": 0}
else:
data = {"command": "target", "target": 50}
# Send command
self.send_api_command(self.apiurl_bed, data)
return
def heat_hotend(self):
# is the bed already hot, in that case turn it off
if self.HotHotEnd:
data = {"command": "target", "targets": {"tool0": 0}}
else:
data = {"command": "target", "targets": {"tool0": 190}}
# Send command
self.send_api_command(self.apiurl_tool, data)
return
def start_print(self):
# here we should display a yes/no box somehow
data = {"command": "start"}
# Send command
self.send_api_command(self.apiurl_job, data)
return
def abort_print(self):
# here we should display a yes/no box somehow
data = {"command": "cancel"}
# Send command
self.send_api_command(self.apiurl_job, data)
return
# Pause or resume print
def pause_print(self):
data = {"command": "pause"}
# Send command
self.send_api_command(self.apiurl_job, data)
return