Skip to content

Commit

Permalink
restructures connections so we're only connected to one printer at a …
Browse files Browse the repository at this point in the history
…time. fixes tls python library implementation of storbinary
  • Loading branch information
sphawes committed May 31, 2024
1 parent 6fef4a0 commit b80a113
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 34 deletions.
33 changes: 32 additions & 1 deletion src/BambuFTP.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,36 @@ def storlines(self, cmd, fp, callback=None):
conn.sendall(buf)
if callback:
callback(buf)
# no ssl layer to shut down, so commented out
# if _SSLSocket is not None and isinstance(conn, _SSLSocket):
# conn.unwrap()
return self.voidresp()


def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
"""Store a file in binary mode. A new port is created for you.
Args:
cmd: A STOR command.
fp: A file-like object with a read(num_bytes) method.
blocksize: The maximum data size to read from fp and send over
the connection at once. [default: 8192]
callback: An optional single parameter callable that is called on
each block of data after it is sent. [default: None]
rest: Passed to transfercmd(). [default: None]
Returns:
The response code.
"""
self.voidcmd('TYPE I')
with self.transfercmd(cmd, rest) as conn:
while 1:
buf = fp.read(blocksize)
if not buf:
break
conn.sendall(buf)
if callback:
callback(buf)
# no ssl layer to shut down, so commented out
# if _SSLSocket is not None and isinstance(conn, _SSLSocket):
# conn.unwrap()
return self.voidresp()
80 changes: 47 additions & 33 deletions src/FarmUpload.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,52 +70,66 @@ def chooseFolder(self):
self.v.config(text=self.fileDirectory)
self.v.update_idletasks()

def connect(self):
for i in self.settings["printers"]:
try:
ftp = BambuFTP()
ftp.set_pasv(True)
ftp.connect(host=i["ip"], port=990, timeout=10, source_address=None)
ftp.login('bblp', i["pw"])
ftp.prot_p()
self.printers.append([i["name"], ftp])
self.updateLog("Connected to " + i["name"])
except:
self.updateLog("Was unable to connect to " + i["name"])

def disconnect(self):
for i in self.printers:
try:
i[1].quit()
except:
self.updateLog("Was unable to disconnect from " + i[0])
def connect(self, printerJSON):
try:
ftp = BambuFTP()
ftp.set_debuglevel(2)
ftp.set_pasv(True)
ftp.connect(host=printerJSON["ip"], port=990, timeout=10, source_address=None)
ftp.login('bblp', printerJSON["pw"])
ftp.prot_p()
self.printers.append([printerJSON["name"], ftp])
self.updateLog("Connected to " + printerJSON["name"])
return ftp
except:
self.updateLog("Was unable to connect to " + printerJSON["name"])
return None

def disconnect(self, ftp, name):
try:
ftp.quit()
self.updateLog("Disconnected from " + str(name))
except:
self.updateLog("Was unable to disconnect from " + str(name))

def send(self):

self.wipeLog()

self.connect()

toSend = os.listdir(self.fileDirectory)

self.updateLog("Files to be sent: " + str(toSend))

for printer in self.printers:
for filename in toSend:
try:
with open(os.path.join(self.fileDirectory,filename), 'rb') as file: #Here I open the file using it's full path
self.updateLog("Sending " + str(filename) + " to printer " + printer[0] + "..." )
printer[1].storlines(f'STOR {filename}', file)
self.updateLog("Success")
for printer in self.settings["printers"]:

ftp = self.connect(printer)

if ftp is not None:

for filename in toSend:
try:
with open(os.path.join(self.fileDirectory,filename), 'rb') as file:
self.updateLog("Sending " + str(filename) + " to printer " + printer["name"] + "..." )
ftp.storbinary(f'STOR {filename}', file, callback=self.update)
self.updateLog("Success")

except:
try:
with open(os.path.join(self.fileDirectory,filename), 'rb') as file:
self.updateLog("Reattempting to send " + str(filename) + " to printer " + printer["name"] + "..." )
ftp.storbinary(f'STOR {filename}', file, callback=self.update)
self.updateLog("Success")
except:
self.updateLog("Failure")

self.disconnect(ftp, printer["name"])

except:
self.updateLog("Failure")
self.updateLog("Process Complete.")

self.disconnect()

self.updateLog("Process Complete.")
def update(self, block):
print(str(block))



if __name__ == "__main__":
app = App()

0 comments on commit b80a113

Please sign in to comment.