def add_sd_file(self, filename, absolutePath, on_success=None, on_failure=None, *args, **kwargs): if not self._comm or self._comm.isBusy() or not self._comm.isSdReady(): self._logger.error("No connection to printer or printer is busy") return self._streamingFinishedCallback = on_success self._streamingFailedCallback = on_failure self.refresh_sd_files(blocking=True) existingSdFiles = map(lambda x: x[0], self._comm.getSdFiles()) if valid_file_type(filename, "gcode"): remoteName = util.get_dos_filename(filename, existing_filenames=existingSdFiles, extension="gco", whitelisted_extensions=["gco", "g"]) else: # probably something else added through a plugin, use it's basename as-is remoteName = os.path.basename(filename) self._create_estimator("stream") self._comm.startFileTransfer(absolutePath, filename, "/" + remoteName, special=not valid_file_type(filename, "gcode"), tags=kwargs.get("tags", set()) | {"trigger:printer.add_sd_file"}) return remoteName
def add_sd_file(self, filename, absolutePath, streamingFinishedCallback): if not self._comm or self._comm.isBusy() or not self._comm.isSdReady(): self._logger.error("No connection to printer or printer is busy") return self._streamingFinishedCallback = streamingFinishedCallback self.refresh_sd_files(blocking=True) existingSdFiles = map(lambda x: x[0], self._comm.getSdFiles()) remoteName = util.get_dos_filename(filename, existing_filenames=existingSdFiles, extension="gco") self._timeEstimationData = TimeEstimationHelper() self._comm.startFileTransfer(absolutePath, filename, "/" + remoteName) return remoteName
def ao_m990_upload_to_sdcard(printer, filename, path, started_f, success_f, failure_f, *args, **kwargs): # limit scope to mpmd_marlin_1.1.x firmware if not printer._comm: return None if "mpmd_marlin_1.1.x" not in printer._comm._firmware_name: return None logger = logging.getLogger(__name__) target = util.get_dos_filename(filename, None, 'gco', ['g', 'gc']) if not target: target = 'CACHE.GCO' logger.info("Uploading {} to {}.".format(filename, target)) started_f(filename, target) def ao_set_progress(pct): # FIXME! yet to be implemented pass def ao_upload_protocol(): TIMEOUT = 3 # time out, seconds # tiny hack for python 2/3 compatibility ORD = (lambda c: ord(c), lambda c: c)[sys.hexversion < 0x3000000] def ao_waitfor(sio, pattern): t = time.time() + TIMEOUT n = len(pattern) i = 0 while time.time() < t: c = sio.read(1) if not c: continue i = i + 1 if ORD(c) == pattern[i] else 0 if i < n: continue return False # success return True # timeout _, port, rate, prof = printer.get_current_connection() printer.disconnect() ERROR = 1 sio = None inp = None try: sio = serial.Serial(port, rate, timeout=TIMEOUT) inp = open(path, "rb") inp.seek(0, os.SEEK_END) fz = inp.tell() # file size, bytes inp.seek(0, os.SEEK_SET) ao_set_progress(0) dT = time.time() N = 0 sio.write("\nM990 S{:d} /{:s}\n".format(fz, target).encode()) if not ao_waitfor(sio, b'BEGIN\n'): BLKSZ = 0x200 # block size, bytes pkt = bytearray(BLKSZ) while True: u = inp.readinto(pkt) if u < BLKSZ: pkt[u:] = b'\0' * (BLKSZ - u) if sio.write(pkt) < BLKSZ: u = 0 if ao_waitfor(sio, b'\n'): u = 0 N += u if u < BLKSZ: break # update progress every 128 blocks if (N & 0xffff) == 0: ao_set_progress(N / fz) time.sleep(TIMEOUT) sio.write(b'\nM29\n') sio.flush() ao_set_progress(100) dT = time.time() - dT ERROR = int(N < fz) S = "{}. Sent {:d} of {:d} B in {:d} s ({:.0f} B/s)." logger.info( S.format(("SUCCESS", "FAILED")[ERROR], N, fz, int(dT), N / dT)) except serial.SerialException as e: logger.exception("{}".format(e)) except IOError as e: logger.exception("{}".format(e)) finally: if inp: inp.close if sio: sio.close printer.connect(port=port, baudrate=rate, profile=prof) # call the appropriate success or failure callback function (success_f, failure_f)[ERROR](filename, target, int(dT)) thread = threading.Thread(target=ao_upload_protocol) thread.daemon = True thread.start() return target