Skip to content

Commit 829ebf5

Browse files
author
AndyEveritt
committed
update format
1 parent 5b05c47 commit 829ebf5

1 file changed

Lines changed: 100 additions & 92 deletions

File tree

DuetWebAPI/DuetWebAPI.py

Lines changed: 100 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77
# Not intended to be a gerneral purpose interface; instead, it contains methods
88
# to issue commands or return specific information. Feel free to extend with new
9-
# methods for other information; please keep the abstraction for V2 V3
9+
# methods for other information; please keep the abstraction for V2 V3
1010
#
1111
# Copyright (C) 2020 Danal Estes all rights reserved.
1212
# Released under The MIT License. Full text available via https://opensource.org/licenses/MIT
@@ -21,29 +21,28 @@ class DuetWebAPI:
2121
pt = 0
2222
_base_url = ''
2323

24-
25-
def __init__(self,base_url):
24+
def __init__(self, base_url):
2625
self._base_url = base_url
2726
try:
28-
URL=(f'{self._base_url}'+'/rr_status?type=1')
29-
r = self.requests.get(URL,timeout=(2,60))
27+
URL = (f'{self._base_url}'+'/rr_model?type=1')
28+
r = self.requests.get(URL, timeout=(2, 60))
3029
j = self.json.loads(r.text)
31-
_=j['coords']
30+
_ = j['coords']
3231
self.pt = 2
3332
return
3433
except:
3534
try:
36-
URL=(f'{self._base_url}'+'/machine/status')
37-
r = self.requests.get(URL,timeout=(2,60))
35+
URL = (f'{self._base_url}'+'/machine/status')
36+
r = self.requests.get(URL, timeout=(2, 60))
3837
j = self.json.loads(r.text)
39-
_=j['boards'][0]['firmwareVersion']
38+
_ = j['boards'][0]['firmwareVersion']
4039
self.pt = 3
4140
return
4241
except:
43-
print(self._base_url," does not appear to be a RRF2 or RRF3 printer", file=self.sys.stderr)
44-
return
42+
print(self._base_url, " does not appear to be a RRF2 or RRF3 printer", file=self.sys.stderr)
43+
return
4544
####
46-
# The following methods are a more atomic, reading/writing basic data structures in the printer.
45+
# The following methods are a more atomic, reading/writing basic data structures in the printer.
4746
####
4847

4948
def printerType(self):
@@ -54,191 +53,200 @@ def baseURL(self):
5453

5554
def getCoords(self):
5655
if (self.pt == 2):
57-
URL=(f'{self._base_url}'+'/rr_status?type=2')
56+
URL = (f'{self._base_url}'+'/rr_status?type=2')
5857
r = self.requests.get(URL)
5958
j = self.json.loads(r.text)
60-
jc=j['coords']['xyz']
61-
an=j['axisNames']
62-
ret=self.json.loads('{}')
63-
for i in range(0,len(jc)):
64-
ret[ an[i] ] = jc[i]
59+
jc = j['coords']['xyz']
60+
an = j['axisNames']
61+
ret = self.json.loads('{}')
62+
for i in range(0, len(jc)):
63+
ret[an[i]] = jc[i]
6564
return(ret)
6665
if (self.pt == 3):
67-
URL=(f'{self._base_url}'+'/machine/status')
66+
URL = (f'{self._base_url}'+'/machine/status')
6867
r = self.requests.get(URL)
6968
j = self.json.loads(r.text)
70-
ja=j['result']['move']['axes']
71-
#d=j['result']['move']['drives']
72-
#ad=self.json.loads('{}')
73-
#for i in range(0,len(ja)):
69+
ja = j['result']['move']['axes']
70+
# d=j['result']['move']['drives']
71+
# ad=self.json.loads('{}')
72+
# for i in range(0,len(ja)):
7473
# ad[ ja[i]['letter'] ] = ja[i]['drives'][0]
75-
ret=self.json.loads('{}')
76-
for i in range(0,len(ja)):
77-
ret[ ja[i]['letter'] ] = ja[i]['userPosition']
74+
ret = self.json.loads('{}')
75+
for i in range(0, len(ja)):
76+
ret[ja[i]['letter']] = ja[i]['userPosition']
7877
return(ret)
7978

8079
def getLayer(self):
8180
if (self.pt == 2):
82-
URL=(f'{self._base_url}'+'/rr_status?type=3')
83-
r = self.requests.get(URL)
84-
j = self.json.loads(r.text)
85-
s = j['currentLayer']
86-
return (s)
81+
URL = (f'{self._base_url}'+'/rr_status?type=3')
82+
r = self.requests.get(URL)
83+
j = self.json.loads(r.text)
84+
s = j['currentLayer']
85+
return (s)
8786
if (self.pt == 3):
88-
URL=(f'{self._base_url}'+'/machine/status')
87+
URL = (f'{self._base_url}'+'/machine/status')
8988
r = self.requests.get(URL)
9089
j = self.json.loads(r.text)
9190
s = j['result']['job']['layer']
92-
if (s == None): s=0
91+
if (s == None):
92+
s = 0
9393
return(s)
9494

95-
def getG10ToolOffset(self,tool):
95+
def getG10ToolOffset(self, tool):
9696
if (self.pt == 3):
97-
URL=(f'{self._base_url}'+'/machine/status')
97+
URL = (f'{self._base_url}'+'/machine/status')
9898
r = self.requests.get(URL)
9999
j = self.json.loads(r.text)
100-
ja=j['result']['move']['axes']
101-
jt=j['result']['tools']
102-
ret=self.json.loads('{}')
100+
ja = j['result']['move']['axes']
101+
jt = j['result']['tools']
102+
ret = self.json.loads('{}')
103103
to = jt[tool]['offsets']
104-
for i in range(0,len(to)):
105-
ret[ ja[i]['letter'] ] = to[i]
104+
for i in range(0, len(to)):
105+
ret[ja[i]['letter']] = to[i]
106106
return(ret)
107-
return({'X':0,'Y':0,'Z':0}) # Dummy for now
107+
return({'X': 0, 'Y': 0, 'Z': 0}) # Dummy for now
108108

109109
def getNumExtruders(self):
110110
if (self.pt == 2):
111-
URL=(f'{self._base_url}'+'/rr_status?type=2')
111+
URL = (f'{self._base_url}'+'/rr_status?type=2')
112112
r = self.requests.get(URL)
113113
j = self.json.loads(r.text)
114-
jc=j['coords']['extr']
114+
jc = j['coords']['extr']
115115
return(len(jc))
116116
if (self.pt == 3):
117-
URL=(f'{self._base_url}'+'/machine/status')
117+
URL = (f'{self._base_url}'+'/machine/status')
118118
r = self.requests.get(URL)
119119
j = self.json.loads(r.text)
120120
return(len(j['result']['move']['extruders']))
121121

122122
def getNumTools(self):
123123
if (self.pt == 2):
124-
URL=(f'{self._base_url}'+'/rr_status?type=2')
124+
URL = (f'{self._base_url}'+'/rr_status?type=2')
125125
r = self.requests.get(URL)
126126
j = self.json.loads(r.text)
127-
jc=j['tools']
127+
jc = j['tools']
128128
return(len(jc))
129129
if (self.pt == 3):
130-
URL=(f'{self._base_url}'+'/machine/status')
130+
URL = (f'{self._base_url}'+'/machine/status')
131131
r = self.requests.get(URL)
132132
j = self.json.loads(r.text)
133133
return(len(j['result']['tools']))
134134

135135
def getStatus(self):
136136
if (self.pt == 2):
137-
URL=(f'{self._base_url}'+'/rr_status?type=2')
137+
URL = (f'{self._base_url}'+'/rr_status?type=2')
138138
r = self.requests.get(URL)
139139
j = self.json.loads(r.text)
140-
s=j['status']
141-
if ('I' in s): return('idle')
142-
if ('P' in s): return('processing')
143-
if ('S' in s): return('paused')
144-
if ('B' in s): return('canceling')
140+
s = j['status']
141+
if ('I' in s):
142+
return('idle')
143+
if ('P' in s):
144+
return('processing')
145+
if ('S' in s):
146+
return('paused')
147+
if ('B' in s):
148+
return('canceling')
145149
return(s)
146150
if (self.pt == 3):
147-
URL=(f'{self._base_url}'+'/machine/status')
151+
URL = (f'{self._base_url}'+'/machine/status')
148152
r = self.requests.get(URL)
149153
j = self.json.loads(r.text)
150154
return(j['result']['state']['status'])
151155

152-
def gCode(self,command):
156+
def gCode(self, command):
153157
if (self.pt == 2):
154-
URL=(f'{self._base_url}'+'/rr_gcode?gcode='+command)
158+
URL = (f'{self._base_url}'+'/rr_gcode?gcode='+command)
155159
r = self.requests.get(URL)
156160
if (self.pt == 3):
157-
URL=(f'{self._base_url}'+'/machine/code/')
161+
URL = (f'{self._base_url}'+'/machine/code/')
158162
r = self.requests.post(URL, data=command)
159163
if (r.ok):
160-
return(0)
164+
return(0)
161165
else:
162-
print("gCode command return code = ",r.status_code)
166+
print("gCode command return code = ", r.status_code)
163167
print(r.reason)
164168
return(r.status_code)
165169

166-
def getFilenamed(self,filename):
170+
def getFilenamed(self, filename):
167171
if (self.pt == 2):
168-
URL=(f'{self._base_url}'+'/rr_download?name='+filename)
172+
URL = (f'{self._base_url}'+'/rr_download?name='+filename)
169173
if (self.pt == 3):
170-
URL=(f'{self._base_url}'+'/machine/file/'+filename)
174+
URL = (f'{self._base_url}'+'/machine/file/'+filename)
171175
r = self.requests.get(URL)
172-
return(r.text.splitlines()) # replace('\n',str(chr(0x0a))).replace('\t',' '))
176+
return(r.text.splitlines()) # replace('\n',str(chr(0x0a))).replace('\t',' '))
173177

174178
def getDirectory(self, directory):
175179
if (self.pt == 2):
176-
URL=(f'{self._base_url}'+'/rr_download?name='+directory)
180+
URL = (f'{self._base_url}'+'/rr_download?name='+directory)
177181
if (self.pt == 3):
178-
URL=(f'{self._base_url}'+'/machine/directory/'+directory)
182+
URL = (f'{self._base_url}'+'/machine/directory/'+directory)
179183
r = self.requests.get(URL)
180-
return(r.text.splitlines()) # replace('\n',str(chr(0x0a))).replace('\t',' '))
184+
return(r.text.splitlines()) # replace('\n',str(chr(0x0a))).replace('\t',' '))
181185

182186
def putFile(self, filepath, file):
183-
187+
184188
if (self.pt == 2):
185-
URL=(f'{self._base_url}'+'/rr_upload?name='+filepath) #lol, probably not correct, fix welcome
189+
URL = (f'{self._base_url}'+'/rr_upload?name='+filepath) # lol, probably not correct, fix welcome
186190
if (self.pt == 3):
187-
URL=(f'{self._base_url}'+'/machine/file/'+filepath)
191+
URL = (f'{self._base_url}'+'/machine/file/'+filepath)
188192
r = self.requests.put(URL, data=file)
189193
if (r.ok):
190194
return(0)
191195
else:
192-
print("gCode command return code = ",r.status_code)
196+
print("gCode command return code = ", r.status_code)
193197
print(r.reason)
194198

195199
def getTemperatures(self):
196200
if (self.pt == 2):
197-
URL=(f'{self._base_url}'+'/rr_status?type=2')
201+
URL = (f'{self._base_url}'+'/rr_status?type=2')
198202
r = self.requests.get(URL)
199203
j = self.json.loads(r.text)
200204
return('Error: getTemperatures not implemented (yet) for RRF V2 printers.')
201205
if (self.pt == 3):
202-
URL=(f'{self._base_url}'+'/machine/status')
203-
r = self.requests.get(URL)
204-
j = self.json.loads(r.text)
205-
jsa=j['result']['sensors']['analog']
206+
URL = (f'{self._base_url}'+'/machine/status')
207+
r = self.requests.get(URL)
208+
j = self.json.loads(r.text)
209+
jsa = j['result']['sensors']['analog']
206210
return(jsa)
207211

208212

209-
210213
####
211-
# The following methods provide services built on the atomics above.
214+
# The following methods provide services built on the atomics above.
212215
####
213216

214-
215217
# Given a line from config g that defines an endstop (N574) or Z probe (M558),
216218
# Return a line that will define the same thing to a "nil" pin, i.e. undefine it
217-
def _nilEndstop(self,configLine):
219+
220+
def _nilEndstop(self, configLine):
218221
ret = ''
219-
for each in [word for word in configLine.split()]: ret = ret + (each if (not (('P' in each[0]) or ('p' in each[0]))) else 'P"nil"') + ' '
222+
for each in [word for word in configLine.split()]:
223+
ret = ret + (each if (not (('P' in each[0]) or ('p' in each[0]))) else 'P"nil"') + ' '
220224
return(ret)
221225

222226
def clearEndstops(self):
223-
c = self.getFilenamed('/sys/config.g')
224-
for each in [line for line in c if (('M574 ' in line) or ('M558 ' in line) )]: self.gCode(self._nilEndstop(each))
227+
c = self.getFilenamed('/sys/config.g')
228+
for each in [line for line in c if (('M574 ' in line) or ('M558 ' in line))]:
229+
self.gCode(self._nilEndstop(each))
225230

226231
def resetEndstops(self):
227-
c = self.getFilenamed('/sys/config.g')
228-
for each in [line for line in c if (('M574 ' in line) or ('M558 ' in line) )]: self.gCode(self._nilEndstop(each))
229-
for each in [line for line in c if (('M574 ' in line) or ('M558 ' in line) or ('G31 ' in line))]: self.gCode(each)
232+
c = self.getFilenamed('/sys/config.g')
233+
for each in [line for line in c if (('M574 ' in line) or ('M558 ' in line))]:
234+
self.gCode(self._nilEndstop(each))
235+
for each in [line for line in c if (('M574 ' in line) or ('M558 ' in line) or ('G31 ' in line))]:
236+
self.gCode(each)
230237

231238
def resetAxisLimits(self):
232-
c = self.getFilenamed('/sys/config.g')
233-
for each in [line for line in c if 'M208 ' in line]: self.gCode(each)
239+
c = self.getFilenamed('/sys/config.g')
240+
for each in [line for line in c if 'M208 ' in line]:
241+
self.gCode(each)
234242

235243
def resetG10(self):
236-
c = self.getFilenamed('/sys/config.g')
237-
for each in [line for line in c if 'G10 ' in line]: self.gCode(each)
244+
c = self.getFilenamed('/sys/config.g')
245+
for each in [line for line in c if 'G10 ' in line]:
246+
self.gCode(each)
238247

239248
def yeetJob(self, filename, file):
240-
path=f"gcodes/{filename}"
249+
path = f"gcodes/{filename}"
241250
ret = self.putFile(path, file)
242-
if ret==0:
251+
if ret == 0:
243252
print('YAY WE DID THE THING')
244-

0 commit comments

Comments
 (0)