-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.py
360 lines (327 loc) · 9 KB
/
backend.py
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import sys
import configparser
import urllib.request
import json
from urllib.parse import urlencode
def getConfig(index):
config = configparser.ConfigParser()
try:
config.read("config")
except:
print("Caught exception trying to read config file!")
return None
if index == "key":
if "Connection" not in config.sections():
print("No [Connection] section found within config!")
return None
else:
try:
key = config.get("Connection", "apikey")
except:
print("Exception caught trying to read API key from config")
key = None
return key
elif index == "address":
if "Connection" not in config.sections():
print("No [Connection] section found within config!")
return
else:
try:
address = config.get("Connection", "address")
except:
print("Exception caught trying to read server address from config")
address = None
return address
if index == "numheads":
if "Toolheads" not in config.sections():
print("No [Toolheads] section found within config!")
return None
else:
try:
numheads = config.get("Toolheads", "numheads")
except:
print("Exception caught trying to read number of heads from config")
numheads = "1"
return numheads
if index == "mixing":
if "Toolheads" not in config.sections():
print("No [Toolheads] section found within config!")
return None
else:
try:
mix = config.get("Toolheads", "mixing")
except:
print("Exception caught trying to mixing head from config")
mix = "False"
mix = json.loads(mix)
return mix
else:
print("Unrecognized getConfig command!")
return None
def writeConfig(api, server):
config = configparser.ConfigParser()
cfgfile = open("config",'w')
config.add_section('Connection')
config.set('Connection', 'apiKey', api)
config.set('Connection', 'address', server)
config.write(cfgfile)
cfgfile.close()
def getRequest(api, server, url):
data = None
response = None
req_headers = {
'X-Api-Key' : api,
'User-Agent': 'Mozilla/5.0'}
try:
request = urllib.request.Request(url, headers = req_headers)
opener = urllib.request.build_opener()
response = opener.open(request).read().decode('utf-8')
except urllib.error.HTTPError as e:
#print(e.code)
#print(e.msg)
if e.code == 409:
print("Error collecting data from server! Probably due to server-printer connection error.")
data = None
else:
print("General HTTP error!")
if response is not None:
try:
data = json.loads(response)
except urllib.error.HTTPError as e:
#print(e.code)
#print(e.msg)
if e.code == 409:
print("Error collecting data from server! Probably due to server-printer connection error.")
data = None
else:
print("General HTTP error!")
data = None
return data
def postRequest(api, server, url, post_fields):
data = None
dump = json.dumps(post_fields)
bytes = dump.encode('utf-8')
req_headers = {
'X-Api-Key' : api,
'Content-Type' : 'application/json',
'User-Agent': 'Mozilla/5.0'}
try:
request = urllib.request.Request(url, headers = req_headers)
request.add_header('Content-Length', len(bytes))
opener = urllib.request.build_opener()
response = opener.open(request, bytes).read()
except urllib.error.HTTPError as e:
print(e.code)
#print(e.msg)
#print(e.headers)
#print(e.fp.read())
return
def deleteRequest(api, server, url, req_headers):
try:
request = urllib.request.Request(url, headers = req_headers)
request.get_method = lambda: 'DELETE'
opener = urllib.request.build_opener()
response = opener.open(request).read()
except urllib.error.HTTPError as e:
print(e.code)
print(e.msg)
print(e.headers)
print(e.fp.read())
return
def connectTest(api, server):
state = False
url = "http://"+server+"/api/version"
data = getRequest(api, server, url)
if data is not None:
#print("Connection successful, server version is " + data['server'] + ", API version is " + data['api'])
state = True
return state
def getJobData(api, server):
data = None
url = "http://"+server+"/api/job"
data = getRequest(api, server, url)
return data
def getPrinterData(api, server):
data = None
url = "http://"+server+"/api/printer"
data = getRequest(api, server, url)
if data == None:
print("No data received!")
return
return data
def getProgress(data):
progress = 0
if data['progress']['completion'] == None:
progress = 0
else:
progress = float(data['progress']['completion'])
return progress
def getJob(data):
jobName = ""
if data['job']['file']['name'] == None:
jobName = "No Job Printing"
else:
jobName = data['job']['file']['name']
return jobName
def getSize(data):
size = ""
if data['job']['file']['size'] == None:
size = "0 MB"
else:
size = data['job']['file']['size']
if abs(size) < 1024.0:
size = str("{0:.2f}".format(size))+"B"
elif size / 1024.0 < 1024.0:
size /= 1024.0
size = str("{0:.2f}".format(size))+"KB"
else:
size /= 1048576.0
size = str("{0:.2f}".format(size))+"MB"
return size
def getTimeLeft(data):
timeLeft = 0
if data['progress']['printTimeLeft'] == None:
timeLeft = 0
else:
timeLeft = data['progress']['printTimeLeft']
return timeLeft
def getTimeSpent(data):
timeSpent = 0
if data['progress']['printTime'] == None:
timeSpent = 0
else:
timeSpent = data['progress']['printTime']
return timeSpent
def getFiles(api, server):
data = None
url = "http://"+server+"/api/files"
data = getRequest(api, server, url)
return data
def getState(api, server):
state = ""
url = "http://"+server+"/api/connection"
data = getRequest(api, server, url)
if data is not None:
if data['current']['state'] == None:
state = "Unknown"
else:
state = data['current']['state']
return state
def getHeadTemp(data, tool="tool0"):
if data['temperature'][tool]['actual'] == None:
temp = 0
else:
temp = data['temperature'][tool]['actual']
return temp
def getHeadTarget(data, tool='tool0'):
temp = 0
if data['temperature'][tool]['target'] == None:
temp = 0
else:
temp = data['temperature'][tool]['target']
return temp
def getBedTemp(data):
temp = 0
if data['temperature']['bed']['actual'] == None:
temp = 0
else:
temp = data['temperature']['bed']['actual']
return temp
def getBedTarget(data):
temp = 0
if data['temperature']['bed']['target'] == None:
temp = 0
else:
temp = data['temperature']['bed']['target']
return temp
def isDir(api, server, path, name):
dirstate = False
url = "http://"+server+"/api/files/"+path+"/"+name
data = getRequest(api, server, url)
if data['type'] == "folder":
dirstate = True
return dirstate
def postSelect(api, server, name, data):
for file in data['files']:
if name == file['name']:
origin = file['origin']
path = file['path']
break
url = "http://"+server+"/api/files/"+origin+"/"+path
post_fields = {
'command' : 'select',
'print' : 'false'}
response = postRequest(api, server, url, post_fields)
return
def postDelete(api, server, name, data):
for file in data['files']:
if name == file['name']:
origin = file['origin']
path = file['path']
break
url = "http://"+server+"/api/files/"+origin+"/"+path
response = deleteRequest(api, server, url)
return
def postPrint(api, server, name, data):
for file in data['files']:
if name == file['name']:
origin = file['origin']
path = file['path']
break
url = "http://"+server+"/api/files/"+origin+"/"+path
post_fields = {
'command' : 'select',
'print' : 'true'}
response = postRequest(api, server, url, post_fields)
def postJog(api, server, axis, dist):
url = "http://"+server+"/api/printer/printhead"
post_fields = {
'command' : 'jog',
axis : dist}
response = postRequest(api, server, url, post_fields)
def postHome(api, server, axis):
url = "http://"+server+"/api/printer/printhead"
post_fields = {
'command' : 'home',
'axes' : axis}
response = postRequest(api, server, url, post_fields)
def postCommand(api, server, command):
url = "http://"+server+"/api/printer/command"
post_fields = {
'command' : command}
response = postRequest(api, server, url, post_fields)
def postConnect(api, server):
url = "http://"+server+"/api/connection"
post_fields = {
'command' : 'connect'}
response = postRequest(api, server, url, post_fields)
def postStop(api, server):
url = "http://"+server+"/api/job"
post_fields = {
'command' : 'cancel'}
response = postRequest(api, server, url, post_fields)
def postTogglePause(api, server):
url = "http://"+server+"/api/job"
post_fields = {
'command' : 'pause',
'action' : 'toggle'}
response = postRequest(api, server, url, post_fields)
def postFeedRate(api, server, rate):
url = "http://"+server+"/api/printer/printhead"
post_fields = {
'command' : 'feedrate',
'factor' : rate}
response = postRequest(api, server, url, post_fields)
def postHeadTemp(api, server, temp, tool='tool0'):
url = "http://"+server+"/api/printer/tool"
post_fields = {
'command' : 'target',
'targets' : {
tool : temp}}
response = postRequest(api, server, url, post_fields)
def postBedTemp(api, server, temp):
url = "http://"+server+"/api/printer/bed"
post_fields = {
'command' : 'target',
'target' : temp}
response = postRequest(api, server, url, post_fields)