def printerCommand(): # TODO: document me if not printer.isOperational(): return make_response("Printer is not operational", 409) if not "application/json" in request.headers["Content-Type"]: return make_response("Expected content type JSON", 400) data = request.json parameters = {} if "parameters" in data.keys(): parameters = data["parameters"] commands = [] if "command" in data.keys(): commands = [data["command"]] elif "commands" in data.keys(): commands = data["commands"] commandsToSend = [] for command in commands: commandToSend = command if len(parameters) > 0: commandToSend = command % parameters commandsToSend.append(commandToSend) printer.commands(commandsToSend) return NO_CONTENT
def printerState(): if not printer.isOperational(): return make_response("Printer is not operational", 409) # process excludes excludes = [] if "exclude" in request.values: excludeStr = request.values["exclude"] if len(excludeStr.strip()) > 0: excludes = filter(lambda x: x in ["temperature", "sd", "state"], map(lambda x: x.strip(), excludeStr.split(","))) result = {} # add temperature information if not "temperature" in excludes: result.update({"temperature": _getTemperatureData(lambda x: x)}) # add sd information if not "sd" in excludes and settings().getBoolean(["feature", "sdSupport"]): result.update({"sd": {"ready": printer.isSdReady()}}) # add state information if not "state" in excludes: state = printer.getCurrentData()["state"] result.update({"state": state}) return jsonify(result)
def printerCommand(): if not printer.isOperational(): return make_response("Printer is not operational", 409) if not "application/json" in request.headers["Content-Type"]: return make_response("Expected content type JSON", 400) data = request.json parameters = dict() if "parameters" in data.keys(): parameters = data["parameters"] if "command" in data and "commands" in data: return make_response("'command' and 'commands' are mutually exclusive", 400) elif "command" in data: commands = [data["command"]] elif "commands" in data and isinstance(data["commands"], (list, tuple)): commands = data["commands"] else: return make_response("Need either single 'command' or list of 'commands'", 400) commandsToSend = [] for command in commands: commandToSend = command if len(parameters) > 0: commandToSend = command % parameters commandsToSend.append(commandToSend) printer.commands(commandsToSend) return NO_CONTENT
def uploadFirmwareFile(target): print("lkj uploadFirmwareFile target:%s" % str(target)) target = FileDestinations.LOCAL if not target in [FileDestinations.LOCAL, FileDestinations.SDCARD]: return make_response("Unknown target: %s" % target, 404) print("lkj post target:%s" % str(target)) print("lkj post request.values:%s" % str(request.values)) print("lkj post request.files:%s" % str(request.files)) input_name = "file" input_upload_name = input_name + "." + settings().get(["server", "uploads", "nameSuffix"]) input_upload_path = input_name + "." + settings().get(["server", "uploads", "pathSuffix"]) if input_upload_name in request.values and input_upload_path in request.values: print("lkj here 1") import shutil upload = util.Object() upload.filename = request.values[input_upload_name] upload.save = lambda new_path: shutil.move(request.values[input_upload_path], new_path) elif input_name in request.files: print("lkj here 2") upload = request.files[input_name] else: return make_response("No file included", 400) print("lkj post 2") # determine future filename of file to be uploaded, abort if it can't be uploaded try: futureFilename = fileManager.sanitize_name(FileDestinations.LOCAL, upload.filename) except: futureFilename = None print("lkj futureFilename:%s" % futureFilename) try : added_file = fileManager.add_firmware_file(FileDestinations.LOCAL, upload.filename, upload, allow_overwrite=True) except : added_file = None if added_file is None: print("lkj post added_file is None") return make_response("Could not upload the file %s" % upload.filename, 500) print("lkj added_file: %s" % added_file) files = {} done = True files.update({ FileDestinations.LOCAL: { "name": added_file, "origin": FileDestinations.LOCAL } }) r = make_response(jsonify(files=files, done=done), 201) #lkj from octoprint.server import printer if printer.isOperational(): #cmd = ("M205", param=added_file) #printer.command() pass #r.headers["Location"] = added_file return r
def add_temp_curve_file(self, destination, path, file_object, links=None, allow_overwrite=False, printer_profile=None, target=None): if printer_profile is None: printer_profile = self._printer_profile_manager.get_current_or_default() _,ext=os.path.splitext(path) print("lkj ext:%s" % ext) if ext != ".fbot": return None file_path = self._storage(destination).add_file(path, file_object, links=links, printer_profile=printer_profile, allow_overwrite=allow_overwrite) absolute_path = self._storage(destination).get_absolute_path(file_path) from octoprint.server import printer from octoprint.comm.protocol.reprap.util import GcodeCommand if printer.isOperational(): try: cmd = GcodeCommand("M504", f=1, param= target + ":" + absolute_path) print("cmd=%s" % str(cmd)) printer.command(cmd) except : exceptionString = getExceptionString() print("ex:%s" % str(exceptionString)) ''' ''' return absolute_path
def jog(): if not printer.isOperational() or printer.isPrinting(): # do not jog when a print job is running or we don't have a connection return jsonify(SUCCESS) (movementSpeedX, movementSpeedY, movementSpeedZ, movementSpeedE) = settings().get(["printerParameters", "movementSpeed", ["x", "y", "z", "e"]]) if "x" in request.values.keys(): # jog x x = request.values["x"] printer.commands(["G91", "G1 X%s F%d" % (x, movementSpeedX), "G90", "M114"]) if "y" in request.values.keys(): # jog y y = request.values["y"] printer.commands(["G91", "G1 Y%s F%d" % (y, movementSpeedY), "G90", "M114"]) if "z" in request.values.keys(): # jog z z = request.values["z"] printer.commands(["G91", "G1 Z%s F%d" % (z, movementSpeedZ), "G90", "M114"]) if "homeXY" in request.values.keys(): # home x/y printer.command("G28 X0 Y0") if "posX" in request.values.keys() and "posY" in request.values.keys(): # go to position printer.commands(["G1 X%s Y%s F%d" % (request.values["posX"], request.values["posY"], movementSpeedX), "M114"]) if "homeZ" in request.values.keys(): # home z printer.command("G28 Z0") if "extrude" in request.values.keys(): # extrude/retract length = request.values["extrude"] printer.commands(["G91", "G1 E%s F%d" % (length, movementSpeedE), "G90"]) return jsonify(SUCCESS)
def controlJob(): if not printer.isOperational(): return make_response("Printer is not operational", 409) valid_commands = { "start": [], "restart": [], "pause": [], "cancel": [] } command, data, response = util.getJsonCommandFromRequest(request, valid_commands) if response is not None: return response activePrintjob = printer.isPrinting() or printer.isPaused() if command == "start": if activePrintjob: return make_response("Printer already has an active print job, did you mean 'restart'?", 409) printer.startPrint() elif command == "restart": if not printer.isPaused(): return make_response("Printer does not have an active print job or is not paused", 409) printer.startPrint() elif command == "pause": if not activePrintjob: return make_response("Printer is neither printing nor paused, 'pause' command cannot be performed", 409) printer.togglePausePrint() elif command == "cancel": if not activePrintjob: return make_response("Printer is neither printing nor paused, 'cancel' command cannot be performed", 409) printer.cancelPrint() return NO_CONTENT
def jog(): if not printer.isOperational() or printer.isPrinting(): # do not jog when a print job is running or we don't have a connection return jsonify(SUCCESS) (movementSpeedX, movementSpeedY, movementSpeedZ, movementSpeedE) = settings().get( ["printerParameters", "movementSpeed", ["x", "y", "z", "e"]]) if "x" in request.values.keys(): # jog x x = request.values["x"] printer.commands(["G91", "G1 X%s F%d" % (x, movementSpeedX), "G90"]) if "y" in request.values.keys(): # jog y y = request.values["y"] printer.commands(["G91", "G1 Y%s F%d" % (y, movementSpeedY), "G90"]) if "z" in request.values.keys(): # jog z z = request.values["z"] printer.commands(["G91", "G1 Z%s F%d" % (z, movementSpeedZ), "G90"]) if "homeXY" in request.values.keys(): # home x/y printer.command("G28 X0 Y0") if "homeZ" in request.values.keys(): # home z printer.command("G28 Z0") if "extrude" in request.values.keys(): # extrude/retract length = request.values["extrude"] printer.commands( ["G91", "G1 E%s F%d" % (length, movementSpeedE), "G90"]) return jsonify(SUCCESS)
def gcodeFileCommand(filename, target): if not target in [FileDestinations.LOCAL, FileDestinations.SDCARD]: return make_response("Unknown target: %s" % target, 404) if not _verifyFileExists(target, filename): return make_response("File not found on '%s': %s" % (target, filename), 404) # valid file commands, dict mapping command name to mandatory parameters valid_commands = {"select": []} command, data, response = util.getJsonCommandFromRequest( request, valid_commands) if response is not None: return response if command == "select": # selects/loads a file printAfterLoading = False if "print" in data.keys() and data["print"]: if not printer.isOperational(): return make_response( "Printer is not operational, cannot directly start printing", 409) printAfterLoading = True sd = False if target == FileDestinations.SDCARD: filenameToSelect = filename sd = True else: filenameToSelect = gcodeManager.getAbsolutePath(filename) printer.selectFile(filenameToSelect, sd, printAfterLoading) return NO_CONTENT
def gcodeFileCommand(filename, target): if not target in [FileDestinations.LOCAL, FileDestinations.SDCARD]: return make_response("Unknown target: %s" % target, 404) if not _verifyFileExists(target, filename): return make_response("File not found on '%s': %s" % (target, filename), 404) # valid file commands, dict mapping command name to mandatory parameters valid_commands = { "select": [] } command, data, response = util.getJsonCommandFromRequest(request, valid_commands) if response is not None: return response if command == "select": # selects/loads a file printAfterLoading = False if "print" in data.keys() and data["print"]: if not printer.isOperational(): return make_response("Printer is not operational, cannot directly start printing", 409) printAfterLoading = True sd = False if target == FileDestinations.SDCARD: filenameToSelect = filename sd = True else: filenameToSelect = gcodeManager.getAbsolutePath(filename) printer.selectFile(filenameToSelect, sd, printAfterLoading) return NO_CONTENT
def printerBedCommand(): if not printer.isOperational(): return make_response("Printer is not operational", 409) valid_commands = {"target": ["target"], "offset": ["offset"]} command, data, response = util.getJsonCommandFromRequest(request, valid_commands) if response is not None: return response ##~~ temperature if command == "target": target = data["target"] # make sure the target is a number if not isinstance(target, (int, long, float)): return make_response("Not a number: %r" % target, 400) # perform the actual temperature command printer.setTemperature("bed", target) ##~~ temperature offset elif command == "offset": offset = data["offsets"] # make sure the offset is valid if not isinstance(offset, (int, long, float)): return make_response("Not a number: %r" % offset, 400) if not -50 <= offset <= 50: return make_response("Offset not in range [-50, 50]: %f" % offset, 400) # set the offsets printer.setTemperatureOffset({"bed": offset}) return NO_CONTENT
def jog(): if not printer.isOperational() or printer.isPrinting(): # do not jog when a print job is running or we don't have a connection return jsonify(SUCCESS) (movementSpeedX, movementSpeedY, movementSpeedZ, movementSpeedE) = ( settings.get('movement_speed', s) for s in 'xyze') if "x" in request.values.keys(): # jog x x = request.values["x"] printer.commands(["G91", "G1 X%s F%d" % (x, movementSpeedX), "G90"]) if "y" in request.values.keys(): # jog y y = request.values["y"] printer.commands(["G91", "G1 Y%s F%d" % (y, movementSpeedY), "G90"]) if "z" in request.values.keys(): # jog z z = request.values["z"] printer.commands(["G91", "G1 Z%s F%d" % (z, movementSpeedZ), "G90"]) if "homeXY" in request.values.keys(): # home x/y printer.command("G28 X0 Y0") if "homeZ" in request.values.keys(): # home z printer.command("G28 Z0") if "extrude" in request.values.keys(): # extrude/retract length = request.values["extrude"] printer.commands( ["G91", "G1 E%s F%d" % (length, movementSpeedE), "G90"]) return jsonify(SUCCESS)
def gcodeFileCommand(filename=None, target=None, request=None): if not target in [FileDestinations.LOCAL, FileDestinations.SDCARD]: return make_response("Unknown target: %s" % target, 404) # not _verifyFileExists(target, filename): #return make_response("File not found on '%s': %s" % (target, filename), 404) filepath = gcodeManager._uploadFolder verifyResult = _verifyFileExists(target, filename) if verifyResult and gcodeManager._uploadFolder == gcodeManager._usbpath: filename = gcodeManager.startThreadToCopyFile(filename, timeout=3 * 60) #wait for 3 mins fileTempPath = gcodeManager._usbpath gcodeManager._uploadFolder = gcodeManager._localpath elif not verifyResult and gcodeManager._uploadFolder == gcodeManager._usbpath: fileTempPath = gcodeManager._usbpath gcodeManager._uploadFolder = gcodeManager._localpath if not _verifyFileExists(target, filename): gcodeManager._uploadFolder = filepath return make_response( "File not found on '%s': %s" % (target, filename), 404) elif not verifyResult: return make_response("File not found on '%s': %s" % (target, filename), 404) # valid file commands, dict mapping command name to mandatory parameters valid_commands = {"select": []} command, data, response = util.getJsonCommandFromRequest( request, valid_commands) #print "gcodeFileCommand",filename,target,command, data, response,"gcodeFileCommand" #if response is not None: #3return response if response is not None: gcodeManager._uploadFolder = filepath return response if command == "select": # selects/loads a file printAfterLoading = False if "print" in data.keys() and data["print"]: if not printer.isOperational(): gcodeManager._uploadFolder = filepath return make_response( "Printer is not operational, cannot directly start printing", 409) printAfterLoading = True sd = False if target == FileDestinations.SDCARD: filenameToSelect = filename sd = True else: filenameToSelect = gcodeManager.getAbsolutePath(filename) printer.selectFile(filenameToSelect, sd, printAfterLoading) gcodeManager._uploadFolder = filepath return NO_CONTENT
def printerState(): if not printer.isOperational(): return make_response("Printer is not operational", 409) result = {} result.update(_getTemperatureData(lambda x: x)) return jsonify(result)
def controlPrinterHotend(): if not printer.isOperational(): return make_response("Printer is not operational", 403) valid_commands = { "temp": ["temps"], "offset": ["offsets"] } command, data, response = util.getJsonCommandFromRequest(request, valid_commands) if response is not None: return response valid_targets = ["hotend", "bed"] ##~~ temperature if command == "temp": temps = data["temps"] # make sure the targets are valid and the values are numbers validated_values = {} for type, value in temps.iteritems(): if not type in valid_targets: return make_response("Invalid target for setting temperature: %s" % type, 400) if not isinstance(value, (int, long, float)): return make_response("Not a number for %s: %r" % (type, value), 400) validated_values[type] = value # perform the actual temperature commands # TODO make this a generic method call (printer.setTemperature(type, value)) to get rid of gcode here if "hotend" in validated_values: printer.command("M104 S%f" % validated_values["hotend"]) if "bed" in validated_values: printer.command("M140 S%f" % validated_values["bed"]) ##~~ temperature offset elif command == "offset": offsets = data["offsets"] # make sure the targets are valid, the values are numbers and in the range [-50, 50] validated_values = {} for type, value in offsets.iteritems(): if not type in valid_targets: return make_response("Invalid target for setting temperature: %s" % type, 400) if not isinstance(value, (int, long, float)): return make_response("Not a number for %s: %r" % (type, value), 400) if not -50 <= value <= 50: return make_response("Offset %s not in range [-50, 50]: %f" % (type, value), 400) validated_values[type] = value # set the offsets if "hotend" in validated_values and "bed" in validated_values: printer.setTemperatureOffset(validated_values["hotend"], validated_values["bed"]) elif "hotend" in validated_values: printer.setTemperatureOffset(validated_values["hotend"], None) elif "bed" in validated_values: printer.setTemperatureOffset(None, validated_values["bed"]) return jsonify(SUCCESS)
def printerPrintheadCommand(): valid_commands = { "jog": [], "fastbot":[], #lkj "home": ["axes"] } command, data, response = util.getJsonCommandFromRequest(request, valid_commands) if response is not None: return response if not printer.isOperational() or ( printer.isPrinting() and command != "fastbot"): # do not jog when a print job is running or we don't have a connection return make_response("Printer is not operational or currently printing", 409) valid_axes = ["x", "y", "z"] speedTarget = 0 ##~~ jog command if command == "jog": # validate all jog instructions, make sure that the values are numbers validated_values = {} for axis in valid_axes: if axis in data: value = data[axis] if not isinstance(value, (int, long, float)): return make_response("Not a number for axis %s: %r" % (axis, value), 400) validated_values[axis] = value #lkj add if "speed" in data: speedTarget = data["speed"] print("speed=%s" % str(speedTarget)) # execute the jog commands for axis, value in validated_values.iteritems(): #lkj printer.jog(axis, value) printer.jogSpeed(axis, value, speedTarget) ##~~ home command elif command == "home": validated_values = [] axes = data["axes"] for axis in axes: if not axis in valid_axes: return make_response("Invalid axis: %s" % axis, 400) validated_values.append(axis) # execute the home command printer.home(validated_values) elif command == "fastbot": #lkj if "FeedSpeed" in data: print("FeedSpeed=%s" % str(data["FeedSpeed"])) printer.feedSpeed(data["FeedSpeed"]) if "FanID" in data: print("FanID=%s" % str(data["FanID"])) print("on=%s" % str(data["on"])) printer.fanControl(data["FanID"], data["on"]) return NO_CONTENT
def gcodeFileCommand(filename, target): if not target in [FileDestinations.LOCAL, FileDestinations.SDCARD]: return make_response("Unknown target: %s" % target, 404) if not _verifyFileExists(target, filename): return make_response("File not found on '%s': %s" % (target, filename), 404) # valid file commands, dict mapping command name to mandatory parameters valid_commands = { "select": [] } command, data, response = util.getJsonCommandFromRequest(request, valid_commands) if response is not None: return response if command == "select": # selects/loads a file printAfterLoading = False if "print" in data.keys() and data["print"]: if not printer.isOperational(): #We try at least once printer.connect() start = time.time() connect_timeout = 5 #5 secs while not printer.isOperational() and not printer.isClosedOrError() and time.time() - start < connect_timeout: time.sleep(1) if not printer.isOperational(): return make_response("The printer is not responding, can't start printing", 409) printAfterLoading = True sd = False if target == FileDestinations.SDCARD: filenameToSelect = filename sd = True else: filenameToSelect = gcodeManager.getAbsolutePath(filename) printer.selectFile(filenameToSelect, sd, printAfterLoading) return NO_CONTENT
def sendPreferenctParameter(self, inProfile): from octoprint.server import printer if printer.isOperational(): cmds = self.__send_all_update_epprom(inProfile) printer.commands(cmds) #cmd_eeprom = GcodeCommand("M500") #printer.command(cmd_eeprom) print("lkj sendPreferenctParameter") '''''' return
def printerToolState(): if not printer.isOperational(): return make_response("Printer is not operational", 409) def deleteBed(x): data = dict(x) if "bed" in data.keys(): del data["bed"] return data return jsonify(_getTemperatureData(deleteBed))
def controlJob(request=None): if not printer.isOperational(): return make_response("Printer is not operational", 409) valid_commands = { "start": [], "restart": [], "pause": [], "cancel": [], "stop": [] #add by kevin, for emergency stop } command, data, response = util.getJsonCommandFromRequest( request, valid_commands) if response is not None: return response activePrintjob = printer.isPrinting() or printer.isPaused() if command == "start": if activePrintjob: return make_response( "Printer already has an active print job, did you mean 'restart'?", 409) printer.startPrint() elif command == "restart": if not printer.isPaused(): return make_response( "Printer does not have an active print job or is not paused", 409) printer.startPrint() elif command == "pause": if not activePrintjob: return make_response( "Printer is neither printing nor paused, 'pause' command cannot be performed", 409) printer.togglePausePrint() elif command == "cancel": if not activePrintjob: return make_response( "Printer is neither printing nor paused, 'cancel' command cannot be performed", 409) printer.cancelPrint() #add by kevin, for emergency stop elif "stop" == command: printer.stopPrint() if not activePrintjob: return make_response( "Printer is neither printing nor paused, 'cancel' command cannot be performed", 409) printer.cancelPrint() #add end, stop return NO_CONTENT
def add_firmware_file(self, destination, path, file_object, links=None, allow_overwrite=False, printer_profile=None, analysis=None): if printer_profile is None: printer_profile = self._printer_profile_manager.get_current_or_default() _,ext=os.path.splitext(path) print("lkj ext:%s" % ext) if ext != ".fbot": return None file_path = self._storage(destination).add_file(path, file_object, links=links, printer_profile=printer_profile, allow_overwrite=allow_overwrite) absolute_path = self._storage(destination).get_absolute_path(file_path) firmware = firmwareFastBot.FirmwareFastbot() print("firmware absolute path:%s" % absolute_path) firmware_version = firmware.loadFirmware(absolute_path, "/tmp/") print("firmware_version :%s" % firmware_version) from octoprint.settings import settings settings().set(["fastbotArgs", "firmwareVersion"], firmware_version) settings().save() print("firmware_version 22 :%s" % firmware_version) from octoprint.server import printer from octoprint.comm.protocol.reprap.util import GcodeCommand if printer.isOperational(): try: cmd = GcodeCommand("M504",s=firmware_version.strip("\x00")) #print("cmd=%s" % str(cmd)) printer.command(cmd) except : exceptionString = getExceptionString() print("ex:%s" % str(exceptionString)) print("firmware_version 333 :%s" % firmware_version) firmware.rebootSystem(5) print("firmware absolute path:%s" % absolute_path) ''' if analysis is None: file_type = get_file_type(absolute_path) if file_type: queue_entry = QueueEntry(file_path, file_type[-1], destination, absolute_path, _profile) self._analysis_queue.enqueue(queue_entry, high_priority=True) else: self._add_analysis_result(destination, path, analysis) eventManager().fire(Events.UPDATED_FILES, dict(type="printables")) ''' return absolute_path
def sdCommand(): if not settings().getBoolean(["feature", "sdSupport"]) or not printer.isOperational() or printer.isPrinting(): return jsonify(SUCCESS) if "command" in request.values.keys(): command = request.values["command"] if command == "init": printer.initSdCard() elif command == "refresh": printer.refreshSdFiles() elif command == "release": printer.releaseSdCard() return jsonify(SUCCESS)
def sdCommand(): if not settings().getBoolean([ "feature", "sdSupport" ]) or not printer.isOperational() or printer.isPrinting(): return jsonify(SUCCESS) if "command" in request.values.keys(): command = request.values["command"] if command == "init": printer.initSdCard() elif command == "refresh": printer.refreshSdFiles() elif command == "release": printer.releaseSdCard() return jsonify(SUCCESS)
def controlPrinterPrinthead(): if not printer.isOperational() or printer.isPrinting(): # do not jog when a print job is running or we don't have a connection return make_response("Printer is not operational or currently printing", 403) valid_commands = { "jog": [], "home": ["axes"] } command, data, response = util.getJsonCommandFromRequest(request, valid_commands) if response is not None: return response movementSpeed = settings().get(["printerParameters", "movementSpeed", ["x", "y", "z"]], asdict=True) valid_axes = ["x", "y", "z"] ##~~ jog command if command == "jog": # validate all jog instructions, make sure that the values are numbers validated_values = {} for axis in valid_axes: if axis in data: value = data[axis] if not isinstance(value, (int, long, float)): return make_response("Not a number for axis %s: %r" % (axis, value), 400) validated_values[axis] = value # execute the jog commands for axis, value in validated_values.iteritems(): # TODO make this a generic method call (printer.jog(axis, value)) to get rid of gcode here printer.commands(["G91", "G1 %s%.4f F%d" % (axis.upper(), value, movementSpeed[axis]), "G90"]) ##~~ home command elif command == "home": validated_values = [] axes = data["axes"] for axis in axes: if not axis in valid_axes: return make_response("Invalid axis: %s" % axis, 400) validated_values.append(axis) # execute the home command # TODO make this a generic method call (printer.home(axis, ...)) to get rid of gcode here printer.commands(["G91", "G28 %s" % " ".join(map(lambda x: "%s0" % x.upper(), validated_values)), "G90"]) return jsonify(SUCCESS)
def printerBedState(): if not printer.isOperational(): return make_response("Printer is not operational", 409) def deleteTools(x): data = dict(x) for k in data.keys(): if k.startswith("tool"): del data[k] return data data = _getTemperatureData(deleteTools) if isinstance(data, Response): return data else: return jsonify(data)
def printerPrintheadCommand(request=None): if not printer.isOperational() or printer.isPrinting(): # do not jog when a print job is running or we don't have a connection return make_response( "Printer is not operational or currently printing", 409) valid_commands = {"jog": [], "home": ["axes"]} command, data, response = util.getJsonCommandFromRequest( request, valid_commands) print "command:", command, "data:", data, "response:", response if response is not None: return response valid_axes = ["x", "y", "z"] ##~~ jog command if command == "jog": # validate all jog instructions, make sure that the values are numbers validated_values = {} for axis in valid_axes: if axis in data: value = data[axis] if not isinstance(value, (int, long, float)): return make_response( "Not a number for axis %s: %r" % (axis, value), 400) validated_values[axis] = value # execute the jog commands for axis, value in validated_values.iteritems(): printer.jog(axis, value) ##~~ home command elif command == "home": validated_values = [] axes = data["axes"] for axis in axes: if not axis in valid_axes: return make_response("Invalid axis: %s" % axis, 400) validated_values.append(axis) # execute the home command printer.home(validated_values) return NO_CONTENT
def printerPrintheadCommand(): if not printer.isOperational() or printer.isPrinting(): # do not jog when a print job is running or we don't have a connection return make_response("Printer is not operational or currently printing", 409) valid_commands = { "jog": [], "home": ["axes"] } command, data, response = util.getJsonCommandFromRequest(request, valid_commands) if response is not None: return response valid_axes = ["x", "y", "z"] ##~~ jog command if command == "jog": # validate all jog instructions, make sure that the values are numbers validated_values = {} for axis in valid_axes: if axis in data: value = data[axis] if not isinstance(value, (int, long, float)): return make_response("Not a number for axis %s: %r" % (axis, value), 400) validated_values[axis] = value # execute the jog commands for axis, value in validated_values.iteritems(): printer.jog(axis, value) ##~~ home command elif command == "home": validated_values = [] axes = data["axes"] for axis in axes: if not axis in valid_axes: return make_response("Invalid axis: %s" % axis, 400) validated_values.append(axis) # execute the home command printer.home(validated_values) return NO_CONTENT
def _getTemperatureData(filter): if not printer.isOperational(): return make_response("Printer is not operational", 409) tempData = printer.getCurrentTemperatures() result = {"temps": filter(tempData)} if "history" in request.values.keys() and request.values["history"] in valid_boolean_trues: tempHistory = printer.getTemperatureHistory() limit = 300 if "limit" in request.values.keys() and unicode(request.values["limit"]).isnumeric(): limit = int(request.values["limit"]) history = list(tempHistory) limit = min(limit, len(history)) result.update({"history": map(lambda x: filter(x), history[-limit:])}) return result
def printerSdCommand(): if not settings().getBoolean(["feature", "sdSupport"]): return make_response("SD support is disabled", 404) if not printer.isOperational() or printer.isPrinting() or printer.isPaused(): return make_response("Printer is not operational or currently busy", 409) valid_commands = {"init": [], "refresh": [], "release": []} command, data, response = util.getJsonCommandFromRequest(request, valid_commands) if response is not None: return response if command == "init": printer.initSdCard() elif command == "refresh": printer.refreshSdFiles() elif command == "release": printer.releaseSdCard() return NO_CONTENT
def sdCommand(): if not settings().getBoolean(["feature", "sdSupport"]) or not printer.isOperational() or printer.isPrinting(): return make_response("SD support is disabled", 403) valid_commands = { "init": [], "refresh": [], "release": [] } command, data, response = util.getJsonCommandFromRequest(request, valid_commands) if response is not None: return response if command == "init": printer.initSdCard() elif command == "refresh": printer.refreshSdFiles() elif command == "release": printer.releaseSdCard() return jsonify(SUCCESS)
def controlJob(): if not printer.isOperational(): return make_response("Printer is not operational", 403) valid_commands = { "start": [], "pause": [], "cancel": [] } command, data, response = util.getJsonCommandFromRequest(request, valid_commands) if response is not None: return response if command == "start": printer.startPrint() elif command == "pause": printer.togglePausePrint() elif command == "cancel": printer.cancelPrint() return jsonify(SUCCESS)
def _getTemperatureData(filter): if not printer.isOperational(): return make_response("Printer is not operational", 409) tempData = printer.getCurrentTemperatures() if "history" in request.values.keys() and request.values["history"] in valid_boolean_trues: tempHistory = printer.getTemperatureHistory() limit = 300 if "limit" in request.values.keys() and unicode(request.values["limit"]).isnumeric(): limit = int(request.values["limit"]) history = list(tempHistory) limit = min(limit, len(history)) tempData.update({ "history": map(lambda x: filter(x), history[-limit:]) }) return filter(tempData)
def add_temp_curve_file(self, destination, path, file_object, links=None, allow_overwrite=False, printer_profile=None, target=None): if printer_profile is None: printer_profile = self._printer_profile_manager.get_current_or_default( ) _, ext = os.path.splitext(path) print("lkj ext:%s" % ext) if ext != ".fbot": return None file_path = self._storage(destination).add_file( path, file_object, links=links, printer_profile=printer_profile, allow_overwrite=allow_overwrite) absolute_path = self._storage(destination).get_absolute_path(file_path) from octoprint.server import printer from octoprint.comm.protocol.reprap.util import GcodeCommand if printer.isOperational(): try: cmd = GcodeCommand("M504", f=1, param=target + ":" + absolute_path) print("cmd=%s" % str(cmd)) printer.command(cmd) except: exceptionString = getExceptionString() print("ex:%s" % str(exceptionString)) ''' ''' return absolute_path
def printerCommand(data=None): # TODO: document me if not printer.isOperational(): return make_response("Printer is not operational", 409) if data is None: return make_response("Expected content type JSON", 400) parameters = {} if "parameters" in data.keys(): parameters = data["parameters"] commands = [] if "command" in data.keys(): commands = [data["command"]] elif "commands" in data.keys(): commands = data["commands"] commandsToSend = [] for command in commands: commandToSend = command if len(parameters) > 0: commandToSend = command % parameters commandsToSend.append(commandToSend) printer.commands(commandsToSend) return NO_CONTENT
def controlPrinterFeeder(): if not printer.isOperational() or printer.isPrinting(): # do not jog when a print job is running or we don't have a connection return make_response("Printer is not operational or currently printing", 403) valid_commands = { "extrude": ["amount"] } command, data, response = util.getJsonCommandFromRequest(request, valid_commands) if response is not None: return response extrusionSpeed = settings().get(["printerParameters", "movementSpeed", "e"]) if command == "extrude": amount = data["amount"] if not isinstance(amount, (int, long, float)): return make_response("Not a number for extrusion amount: %r" % amount, 400) # TODO make this a generic method call (printer.extruder([hotend,] amount)) to get rid of gcode here printer.commands(["G91", "G1 E%s F%d" % (data["amount"], extrusionSpeed), "G90"]) return jsonify(SUCCESS)
def printerSdCommand(): if not settings().getBoolean(["feature", "sdSupport"]): return make_response("SD support is disabled", 404) if not printer.isOperational() or printer.isPrinting() or printer.isPaused( ): return make_response("Printer is not operational or currently busy", 409) valid_commands = {"init": [], "refresh": [], "release": []} command, data, response = util.getJsonCommandFromRequest( request, valid_commands) if response is not None: return response if command == "init": printer.initSdCard() elif command == "refresh": printer.refreshSdFiles() elif command == "release": printer.releaseSdCard() return NO_CONTENT
def printerBedCommand(): if not printer.isOperational(): return make_response("Printer is not operational", 409) valid_commands = { "target": ["target"], "offset": ["offset"] } command, data, response = util.getJsonCommandFromRequest(request, valid_commands) if response is not None: return response ##~~ temperature if command == "target": target = data["target"] # make sure the target is a number if not isinstance(target, (int, long, float)): return make_response("Not a number: %r" % target, 400) # perform the actual temperature command printer.setTemperature("bed", target) ##~~ temperature offset elif command == "offset": offset = data["offset"] # make sure the offset is valid if not isinstance(offset, (int, long, float)): return make_response("Not a number: %r" % offset, 400) if not -50 <= offset <= 50: return make_response("Offset not in range [-50, 50]: %f" % offset, 400) # set the offsets printer.setTemperatureOffset({"bed": offset}) return NO_CONTENT
def printerToolCommand(): if not printer.isOperational(): return make_response("Printer is not operational", 409) valid_commands = { "select": ["tool"], "target": ["targets"], "offset": ["offsets"], "extrude": ["amount"] } command, data, response = util.getJsonCommandFromRequest(request, valid_commands) if response is not None: return response validation_regex = re.compile("tool\d+") ##~~ tool selection if command == "select": tool = data["tool"] if re.match(validation_regex, tool) is None: return make_response("Invalid tool: %s" % tool, 400) if not tool.startswith("tool"): return make_response("Invalid tool for selection: %s" % tool, 400) printer.changeTool(tool) ##~~ temperature elif command == "target": targets = data["targets"] # make sure the targets are valid and the values are numbers validated_values = {} for tool, value in targets.iteritems(): if re.match(validation_regex, tool) is None: return make_response("Invalid target for setting temperature: %s" % tool, 400) if not isinstance(value, (int, long, float)): return make_response("Not a number for %s: %r" % (tool, value), 400) validated_values[tool] = value # perform the actual temperature commands for tool in validated_values.keys(): printer.setTemperature(tool, validated_values[tool]) ##~~ temperature offset elif command == "offset": offsets = data["offsets"] # make sure the targets are valid, the values are numbers and in the range [-50, 50] validated_values = {} for tool, value in offsets.iteritems(): if re.match(validation_regex, tool) is None: return make_response("Invalid target for setting temperature: %s" % tool, 400) if not isinstance(value, (int, long, float)): return make_response("Not a number for %s: %r" % (tool, value), 400) if not -50 <= value <= 50: return make_response("Offset %s not in range [-50, 50]: %f" % (tool, value), 400) validated_values[tool] = value # set the offsets printer.setTemperatureOffset(validated_values) ##~~ extrusion elif command == "extrude": if printer.isPrinting(): # do not extrude when a print job is running return make_response("Printer is currently printing", 409) amount = data["amount"] if not isinstance(amount, (int, long, float)): return make_response("Not a number for extrusion amount: %r" % amount, 400) printer.extrude(amount, 250 if amount > 0 else 400) return NO_CONTENT
def gcodeFileCommand(filename, target): if not target in [FileDestinations.LOCAL, FileDestinations.SDCARD]: return make_response("Unknown target: %s" % target, 404) if not _verifyFileExists(target, filename): return make_response("File not found on '%s': %s" % (target, filename), 404) # valid file commands, dict mapping command name to mandatory parameters valid_commands = { "select": [], "slice": [] } command, data, response = util.getJsonCommandFromRequest(request, valid_commands) if response is not None: return response if command == "select": # selects/loads a file printAfterLoading = False if "print" in data.keys() and data["print"] in valid_boolean_trues: if not printer.isOperational(): return make_response("Printer is not operational, cannot directly start printing", 409) printAfterLoading = True sd = False if target == FileDestinations.SDCARD: filenameToSelect = filename sd = True else: filenameToSelect = fileManager.get_absolute_path(target, filename) printer.selectFile(filenameToSelect, sd, printAfterLoading) elif command == "slice": if "slicer" in data.keys(): slicer = data["slicer"] del data["slicer"] if not slicer in slicingManager.registered_slicers: return make_response("Slicer {slicer} is not available".format(**locals()), 400) slicer_instance = slicingManager.get_slicer(slicer) elif "cura" in slicingManager.registered_slicers: slicer = "cura" slicer_instance = slicingManager.get_slicer("cura") else: return make_response("Cannot slice {filename}, no slicer available".format(**locals()), 415) if not octoprint.filemanager.valid_file_type(filename, type="stl"): return make_response("Cannot slice {filename}, not an STL file".format(**locals()), 415) if slicer_instance.get_slicer_properties()["same_device"] and (printer.isPrinting() or printer.isPaused()): # slicer runs on same device as OctoPrint, slicing while printing is hence disabled return make_response("Cannot slice on {slicer} while printing due to performance reasons".format(**locals()), 409) if "gcode" in data.keys() and data["gcode"]: gcode_name = data["gcode"] del data["gcode"] else: import os name, _ = os.path.splitext(filename) gcode_name = name + ".gco" # prohibit overwriting the file that is currently being printed currentOrigin, currentFilename = _getCurrentFile() if currentFilename == gcode_name and currentOrigin == target and (printer.isPrinting() or printer.isPaused()): make_response("Trying to slice into file that is currently being printed: %s" % gcode_name, 409) if "profile" in data.keys() and data["profile"]: profile = data["profile"] del data["profile"] else: profile = None if "printerProfile" in data.keys() and data["printerProfile"]: printerProfile = data["printerProfile"] del data["printerProfile"] else: printerProfile = None if "position" in data.keys() and data["position"] and isinstance(data["position"], dict) and "x" in data["position"] and "y" in data["position"]: position = data["position"] del data["position"] else: position = None select_after_slicing = False if "select" in data.keys() and data["select"] in valid_boolean_trues: if not printer.isOperational(): return make_response("Printer is not operational, cannot directly select for printing", 409) select_after_slicing = True print_after_slicing = False if "print" in data.keys() and data["print"] in valid_boolean_trues: if not printer.isOperational(): return make_response("Printer is not operational, cannot directly start printing", 409) select_after_slicing = print_after_slicing = True override_keys = [k for k in data if k.startswith("profile.") and data[k] is not None] overrides = dict() for key in override_keys: overrides[key[len("profile."):]] = data[key] def slicing_done(target, gcode_name, select_after_slicing, print_after_slicing): if select_after_slicing or print_after_slicing: sd = False if target == FileDestinations.SDCARD: filenameToSelect = gcode_name sd = True else: filenameToSelect = fileManager.get_absolute_path(target, gcode_name) printer.selectFile(filenameToSelect, sd, print_after_slicing) ok, result = fileManager.slice(slicer, target, filename, target, gcode_name, profile=profile, printer_profile_id=printerProfile, position=position, overrides=overrides, callback=slicing_done, callback_args=(target, gcode_name, select_after_slicing, print_after_slicing)) if ok: files = {} location = url_for(".readGcodeFile", target=target, filename=gcode_name, _external=True) result = { "name": gcode_name, "origin": FileDestinations.LOCAL, "refs": { "resource": location, "download": url_for("index", _external=True) + "downloads/files/" + target + "/" + gcode_name } } r = make_response(jsonify(result), 202) r.headers["Location"] = location return r else: return make_response("Could not slice: {result}".format(result=result), 500) return NO_CONTENT
def uploadFirmwareFile(target): print("lkj uploadFirmwareFile target:%s" % str(target)) target = FileDestinations.LOCAL if not target in [FileDestinations.LOCAL, FileDestinations.SDCARD]: return make_response("Unknown target: %s" % target, 404) print("lkj post target:%s" % str(target)) print("lkj post request.values:%s" % str(request.values)) print("lkj post request.files:%s" % str(request.files)) input_name = "file" input_upload_name = input_name + "." + settings().get( ["server", "uploads", "nameSuffix"]) input_upload_path = input_name + "." + settings().get( ["server", "uploads", "pathSuffix"]) if input_upload_name in request.values and input_upload_path in request.values: print("lkj here 1") import shutil upload = util.Object() upload.filename = request.values[input_upload_name] upload.save = lambda new_path: shutil.move( request.values[input_upload_path], new_path) elif input_name in request.files: print("lkj here 2") upload = request.files[input_name] else: return make_response("No file included", 400) print("lkj post 2") # determine future filename of file to be uploaded, abort if it can't be uploaded try: futureFilename = fileManager.sanitize_name(FileDestinations.LOCAL, upload.filename) except: futureFilename = None print("lkj futureFilename:%s" % futureFilename) try: added_file = fileManager.add_firmware_file(FileDestinations.LOCAL, upload.filename, upload, allow_overwrite=True) except: added_file = None if added_file is None: print("lkj post added_file is None") return make_response("Could not upload the file %s" % upload.filename, 500) print("lkj added_file: %s" % added_file) files = {} done = True files.update({ FileDestinations.LOCAL: { "name": added_file, "origin": FileDestinations.LOCAL } }) r = make_response(jsonify(files=files, done=done), 201) #lkj from octoprint.server import printer if printer.isOperational(): #cmd = ("M205", param=added_file) #printer.command() pass #r.headers["Location"] = added_file return r
def uploadGcodeFile(target): print("lkj uploadGcodeFile target:%s" % str(target)) if target in ["extruder1", "extruder2", "bed"]: return upload_temp_curve_firmware(target) if target == "firmware": return uploadFirmwareFile(target) if target == FileDestinations.FastbotSDCARD: return uploadFastBotSDCARD(target) if not target in [FileDestinations.LOCAL, FileDestinations.SDCARD]: return make_response("Unknown target: %s" % target, 404) input_name = "file" input_upload_name = input_name + "." + settings().get( ["server", "uploads", "nameSuffix"]) input_upload_path = input_name + "." + settings().get( ["server", "uploads", "pathSuffix"]) if input_upload_name in request.values and input_upload_path in request.values: import shutil upload = util.Object() upload.filename = request.values[input_upload_name] upload.save = lambda new_path: shutil.move( request.values[input_upload_path], new_path) elif input_name in request.files: upload = request.files[input_name] else: return make_response("No file included", 400) if target == FileDestinations.SDCARD and not settings().getBoolean( ["feature", "sdSupport"]): return make_response("SD card support is disabled", 404) sd = target == FileDestinations.SDCARD selectAfterUpload = "select" in request.values.keys( ) and request.values["select"] in valid_boolean_trues printAfterSelect = "print" in request.values.keys( ) and request.values["print"] in valid_boolean_trues if sd: # validate that all preconditions for SD upload are met before attempting it if not (printer.isOperational() and not (printer.isPrinting() or printer.isPaused())): return make_response( "Can not upload to SD card, printer is either not operational or already busy", 409) if not printer.isSdReady(): return make_response( "Can not upload to SD card, not yet initialized", 409) # determine current job currentFilename = None currentOrigin = None currentJob = printer.getCurrentJob() if currentJob is not None and "file" in currentJob.keys(): currentJobFile = currentJob["file"] if "name" in currentJobFile.keys() and "origin" in currentJobFile.keys( ): currentFilename = currentJobFile["name"] currentOrigin = currentJobFile["origin"] # determine future filename of file to be uploaded, abort if it can't be uploaded try: futureFilename = fileManager.sanitize_name(FileDestinations.LOCAL, upload.filename) except: futureFilename = None if futureFilename is None or not (slicingManager.slicing_enabled or octoprint.filemanager.valid_file_type( futureFilename, type="gcode")): return make_response( "Can not upload file %s, wrong format?" % upload.filename, 415) # prohibit overwriting currently selected file while it's being printed if futureFilename == currentFilename and target == currentOrigin and printer.isPrinting( ) or printer.isPaused(): return make_response( "Trying to overwrite file that is currently being printed: %s" % currentFilename, 409) def fileProcessingFinished(filename, absFilename, destination): """ Callback for when the file processing (upload, optional slicing, addition to analysis queue) has finished. Depending on the file's destination triggers either streaming to SD card or directly calls selectAndOrPrint. """ if destination == FileDestinations.SDCARD and octoprint.filemanager.valid_file_type( filename, "gcode"): return filename, printer.addSdFile(filename, absFilename, selectAndOrPrint) else: selectAndOrPrint(filename, absFilename, destination) return filename def selectAndOrPrint(filename, absFilename, destination): """ Callback for when the file is ready to be selected and optionally printed. For SD file uploads this is only the case after they have finished streaming to the printer, which is why this callback is also used for the corresponding call to addSdFile. Selects the just uploaded file if either selectAfterUpload or printAfterSelect are True, or if the exact file is already selected, such reloading it. """ if octoprint.filemanager.valid_file_type(added_file, "gcode") and ( selectAfterUpload or printAfterSelect or (currentFilename == filename and currentOrigin == destination)): printer.selectFile(absFilename, destination == FileDestinations.SDCARD, printAfterSelect) added_file = fileManager.add_file(FileDestinations.LOCAL, upload.filename, upload, allow_overwrite=True) if added_file is None: return make_response("Could not upload the file %s" % upload.filename, 500) if octoprint.filemanager.valid_file_type(added_file, "stl"): filename = added_file done = True else: filename = fileProcessingFinished( added_file, fileManager.get_absolute_path(FileDestinations.LOCAL, added_file), target) done = True sdFilename = None if isinstance(filename, tuple): filename, sdFilename = filename eventManager.fire(Events.UPLOAD, {"file": filename, "target": target}) files = {} location = url_for(".readGcodeFile", target=FileDestinations.LOCAL, filename=filename, _external=True) files.update({ FileDestinations.LOCAL: { "name": filename, "origin": FileDestinations.LOCAL, "refs": { "resource": location, "download": url_for("index", _external=True) + "downloads/files/" + FileDestinations.LOCAL + "/" + filename } } }) if sd and sdFilename: location = url_for(".readGcodeFile", target=FileDestinations.SDCARD, filename=sdFilename, _external=True) files.update({ FileDestinations.SDCARD: { "name": sdFilename, "origin": FileDestinations.SDCARD, "refs": { "resource": location } } }) r = make_response(jsonify(files=files, done=done), 201) r.headers["Location"] = location return r
def gcodeFileCommand(filename, target): if not target in [ FileDestinations.LOCAL, FileDestinations.SDCARD, FileDestinations.FastbotSDCARD ]: return make_response("Unknown target: %s" % target, 404) if not _verifyFileExists(target, filename): return make_response("File not found on '%s': %s" % (target, filename), 404) # valid file commands, dict mapping command name to mandatory parameters valid_commands = {"select": [], "slice": []} command, data, response = util.getJsonCommandFromRequest( request, valid_commands) if response is not None: return response if command == "select": # selects/loads a file printAfterLoading = False if "print" in data.keys() and data["print"] in valid_boolean_trues: if not printer.isOperational(): return make_response( "Printer is not operational, cannot directly start printing", 409) printAfterLoading = True if target == FileDestinations.SDCARD: filenameToSelect = filename else: filenameToSelect = fileManager.get_absolute_path(target, filename) printer.selectFile(filenameToSelect, target, printAfterLoading) elif command == "slice": if "slicer" in data.keys(): slicer = data["slicer"] del data["slicer"] if not slicer in slicingManager.registered_slicers: return make_response( "Slicer {slicer} is not available".format(**locals()), 400) slicer_instance = slicingManager.get_slicer(slicer) elif "cura" in slicingManager.registered_slicers: slicer = "cura" slicer_instance = slicingManager.get_slicer("cura") else: return make_response( "Cannot slice {filename}, no slicer available".format( **locals()), 415) if not octoprint.filemanager.valid_file_type(filename, type="stl"): return make_response( "Cannot slice {filename}, not an STL file".format(**locals()), 415) if slicer_instance.get_slicer_properties()["same_device"] and ( printer.isPrinting() or printer.isPaused()): # slicer runs on same device as OctoPrint, slicing while printing is hence disabled return make_response( "Cannot slice on {slicer} while printing due to performance reasons" .format(**locals()), 409) if "gcode" in data.keys() and data["gcode"]: gcode_name = data["gcode"] del data["gcode"] else: import os name, _ = os.path.splitext(filename) gcode_name = name + ".gco" # prohibit overwriting the file that is currently being printed currentOrigin, currentFilename = _getCurrentFile() if currentFilename == gcode_name and currentOrigin == target and ( printer.isPrinting() or printer.isPaused()): make_response( "Trying to slice into file that is currently being printed: %s" % gcode_name, 409) if "profile" in data.keys() and data["profile"]: profile = data["profile"] del data["profile"] else: profile = None if "printerProfile" in data.keys() and data["printerProfile"]: printerProfile = data["printerProfile"] del data["printerProfile"] else: printerProfile = None if "position" in data.keys() and data["position"] and isinstance( data["position"], dict) and "x" in data["position"] and "y" in data["position"]: position = data["position"] del data["position"] else: position = None select_after_slicing = False if "select" in data.keys() and data["select"] in valid_boolean_trues: if not printer.isOperational(): return make_response( "Printer is not operational, cannot directly select for printing", 409) select_after_slicing = True print_after_slicing = False if "print" in data.keys() and data["print"] in valid_boolean_trues: if not printer.isOperational(): return make_response( "Printer is not operational, cannot directly start printing", 409) select_after_slicing = print_after_slicing = True override_keys = [ k for k in data if k.startswith("profile.") and data[k] is not None ] overrides = dict() for key in override_keys: overrides[key[len("profile."):]] = data[key] def slicing_done(target, gcode_name, select_after_slicing, print_after_slicing): if select_after_slicing or print_after_slicing: sd = False if target == FileDestinations.SDCARD: filenameToSelect = gcode_name sd = True else: filenameToSelect = fileManager.get_absolute_path( target, gcode_name) printer.selectFile(filenameToSelect, sd, print_after_slicing) ok, result = fileManager.slice(slicer, target, filename, target, gcode_name, profile=profile, printer_profile_id=printerProfile, position=position, overrides=overrides, callback=slicing_done, callback_args=(target, gcode_name, select_after_slicing, print_after_slicing)) if ok: files = {} location = url_for(".readGcodeFile", target=target, filename=gcode_name, _external=True) result = { "name": gcode_name, "origin": FileDestinations.LOCAL, "refs": { "resource": location, "download": url_for("index", _external=True) + "downloads/files/" + target + "/" + gcode_name } } r = make_response(jsonify(result), 202) r.headers["Location"] = location return r else: return make_response( "Could not slice: {result}".format(result=result), 500) return NO_CONTENT
def jog(): if not printer.isOperational() or printer.isPrinting(): # do not jog when a print job is running or we don't have a connection return jsonify(SUCCESS) (movementSpeedX, movementSpeedY, movementSpeedZ, movementSpeedE) = settings().get(["printerParameters", "movementSpeed", ["x", "y", "z", "e"]]) if "x" in request.values.keys(): # jog x x = request.values["x"] printer.commands(["G91", "G1 X%s F%d" % (x, movementSpeedX), "G90"]) if "y" in request.values.keys(): # jog y y = request.values["y"] printer.commands(["G91", "G1 Y%s F%d" % (y, movementSpeedY), "G90"]) if "z" in request.values.keys(): # jog z z = request.values["z"] printer.commands(["G91", "G1 Z%s F%d" % (z, movementSpeedZ), "G90"]) if "homeXY" in request.values.keys(): # home x/y printer.command("G28 X0 Y0") if "homeZ" in request.values.keys(): # home z printer.command("G28 Z0") if "extrude" in request.values.keys(): # extrude/retract length = request.values["extrude"] printer.commands(["G91", "G1 E%s F%d" % (length, movementSpeedE), "G90"]) movementSpeed = settings().get(["printerParameters", "movementSpeed", ["x", "y", "z"]], asdict=True) valid_axes = ["x", "y", "z"] ##~~ jog command if command == "jog": # validate all jog instructions, make sure that the values are numbers validated_values = {} for axis in valid_axes: if axis in data: value = data[axis] if not isinstance(value, (int, long, float)): return make_response("Not a number for axis %s: %r" % (axis, value), 400) validated_values[axis] = value # execute the jog commands for axis, value in validated_values.iteritems(): # TODO make this a generic method call (printer.jog(axis, value)) to get rid of gcode here printer.commands(["G91", "G1 %s%.4f F%d" % (axis.upper(), value, movementSpeed[axis]), "G90"]) ##~~ home command elif command == "home": validated_values = [] axes = data["axes"] for axis in axes: if not axis in valid_axes: return make_response("Invalid axis: %s" % axis, 400) validated_values.append(axis) # execute the home command # TODO make this a generic method call (printer.home(axis, ...)) to get rid of gcode here printer.commands(["G91", "G28 %s" % " ".join(map(lambda x: "%s0" % x.upper(), validated_values)), "G90"]) return jsonify(SUCCESS)
def saveToEEPROM(self): from octoprint.server import printer if printer.isOperational(): cmd_eeprom = GcodeCommand("M500") printer.command(cmd_eeprom)
def uploadGcodeFile(target): if not target in [FileDestinations.LOCAL, FileDestinations.SDCARD]: return make_response("Unknown target: %s" % target, 404) if not "file" in request.files.keys(): return make_response("No file included", 400) if target == FileDestinations.SDCARD and not settings().getBoolean( ["feature", "sdSupport"]): return make_response("SD card support is disabled", 404) file = request.files["file"] sd = target == FileDestinations.SDCARD selectAfterUpload = "select" in request.values.keys( ) and request.values["select"] in valid_boolean_trues printAfterSelect = "print" in request.values.keys( ) and request.values["print"] in valid_boolean_trues if sd: # validate that all preconditions for SD upload are met before attempting it if not (printer.isOperational() and not (printer.isPrinting() or printer.isPaused())): return make_response( "Can not upload to SD card, printer is either not operational or already busy", 409) if not printer.isSdReady(): return make_response( "Can not upload to SD card, not yet initialized", 409) # determine current job currentFilename = None currentSd = None currentJob = printer.getCurrentJob() if currentJob is not None and "filename" in currentJob.keys( ) and "sd" in currentJob.keys(): currentFilename = currentJob["filename"] currentSd = currentJob["sd"] # determine future filename of file to be uploaded, abort if it can't be uploaded futureFilename = gcodeManager.getFutureFilename(file) if futureFilename is None or ( not settings().getBoolean(["cura", "enabled"]) and not gcodefiles.isGcodeFileName(futureFilename)): return make_response( "Can not upload file %s, wrong format?" % file.filename, 415) # prohibit overwriting currently selected file while it's being printed if futureFilename == currentFilename and sd == currentSd and printer.isPrinting( ) or printer.isPaused(): return make_response( "Trying to overwrite file that is currently being printed: %s" % currentFilename, 409) filename = None def fileProcessingFinished(filename, absFilename, destination): """ Callback for when the file processing (upload, optional slicing, addition to analysis queue) has finished. Depending on the file's destination triggers either streaming to SD card or directly calls selectAndOrPrint. """ if destination == FileDestinations.SDCARD: return filename, printer.addSdFile(filename, absFilename, selectAndOrPrint) else: selectAndOrPrint(absFilename, destination) return filename def selectAndOrPrint(nameToSelect, destination): """ Callback for when the file is ready to be selected and optionally printed. For SD file uploads this is only the case after they have finished streaming to the printer, which is why this callback is also used for the corresponding call to addSdFile. Selects the just uploaded file if either selectAfterUpload or printAfterSelect are True, or if the exact file is already selected, such reloading it. """ sd = destination == FileDestinations.SDCARD if selectAfterUpload or printAfterSelect or ( currentFilename == filename and currentSd == sd): printer.selectFile(nameToSelect, sd, printAfterSelect) destination = FileDestinations.SDCARD if sd else FileDestinations.LOCAL filename, done = gcodeManager.addFile(file, destination, fileProcessingFinished) if filename is None: return make_response("Could not upload the file %s" % file.filename, 500) sdFilename = None if isinstance(filename, tuple): filename, sdFilename = filename eventManager.fire(Events.UPLOAD, {"file": filename, "target": target}) files = {} if done: files.update({ FileDestinations.LOCAL: { "name": filename, "origin": FileDestinations.LOCAL, "refs": { "resource": url_for(".readGcodeFile", target=FileDestinations.LOCAL, filename=filename, _external=True), "download": url_for("index", _external=True) + "downloads/files/" + FileDestinations.LOCAL + "/" + filename } } }) if sd and sdFilename: files.update({ FileDestinations.SDCARD: { "name": sdFilename, "origin": FileDestinations.SDCARD, "refs": { "resource": url_for(".readGcodeFile", target=FileDestinations.SDCARD, filename=sdFilename, _external=True) } } }) return make_response(jsonify(files=files, done=done), 201)
def printerToolCommand(): if not printer.isOperational(): return make_response("Printer is not operational", 409) valid_commands = { "select": ["tool"], "target": ["targets"], "offset": ["offsets"], "extrude": ["amount"] } command, data, response = util.getJsonCommandFromRequest(request, valid_commands) if response is not None: return response validation_regex = re.compile("tool\d+") ##~~ tool selection if command == "select": tool = data["tool"] if re.match(validation_regex, tool) is None: return make_response("Invalid tool: %s" % tool, 400) if not tool.startswith("tool"): return make_response("Invalid tool for selection: %s" % tool, 400) printer.changeTool(tool) ##~~ temperature elif command == "target": targets = data["targets"] # make sure the targets are valid and the values are numbers validated_values = {} for tool, value in targets.iteritems(): if re.match(validation_regex, tool) is None: return make_response("Invalid target for setting temperature: %s" % tool, 400) if not isinstance(value, (int, long, float)): return make_response("Not a number for %s: %r" % (tool, value), 400) validated_values[tool] = value # perform the actual temperature commands for tool in validated_values.keys(): printer.setTemperature(tool, validated_values[tool]) ##~~ temperature offset elif command == "offset": offsets = data["offsets"] # make sure the targets are valid, the values are numbers and in the range [-50, 50] validated_values = {} for tool, value in offsets.iteritems(): if re.match(validation_regex, tool) is None: return make_response("Invalid target for setting temperature: %s" % tool, 400) if not isinstance(value, (int, long, float)): return make_response("Not a number for %s: %r" % (tool, value), 400) if not -50 <= value <= 50: return make_response("Offset %s not in range [-50, 50]: %f" % (tool, value), 400) validated_values[tool] = value # set the offsets printer.setTemperatureOffset(validated_values) ##~~ extrusion elif command == "extrude": if printer.isPrinting(): # do not extrude when a print job is running return make_response("Printer is currently printing", 409) amount = data["amount"] speedTarget = data["speed"] if not isinstance(amount, (int, long, float)): return make_response("Not a number for extrusion amount: %r" % amount, 400) #printer.extrude(amount) print("speed=%s" % str(speedTarget)) printer.extrudeSpeed(amount, speedTarget) return NO_CONTENT
def uploadGcodeFile(target): if not target in [FileDestinations.LOCAL, FileDestinations.SDCARD]: return make_response("Unknown target: %s" % target, 404) if not "file" in request.files.keys(): return make_response("No file included", 400) if target == FileDestinations.SDCARD and not settings().getBoolean(["feature", "sdSupport"]): return make_response("SD card support is disabled", 404) file = request.files["file"] sd = target == FileDestinations.SDCARD selectAfterUpload = "select" in request.values.keys() and request.values["select"] in valid_boolean_trues printAfterSelect = "print" in request.values.keys() and request.values["print"] in valid_boolean_trues if sd: # validate that all preconditions for SD upload are met before attempting it if not (printer.isOperational() and not (printer.isPrinting() or printer.isPaused())): return make_response("Can not upload to SD card, printer is either not operational or already busy", 409) if not printer.isSdReady(): return make_response("Can not upload to SD card, not yet initialized", 409) # determine current job currentFilename = None currentSd = None currentJob = printer.getCurrentJob() if currentJob is not None and "filename" in currentJob.keys() and "sd" in currentJob.keys(): currentFilename = currentJob["filename"] currentSd = currentJob["sd"] # determine future filename of file to be uploaded, abort if it can't be uploaded futureFilename = gcodeManager.getFutureFilename(file) if futureFilename is None or (not settings().getBoolean(["cura", "enabled"]) and not gcodefiles.isGcodeFileName(futureFilename)): return make_response("Can not upload file %s, wrong format?" % file.filename, 415) # prohibit overwriting currently selected file while it's being printed if futureFilename == currentFilename and sd == currentSd and printer.isPrinting() or printer.isPaused(): return make_response("Trying to overwrite file that is currently being printed: %s" % currentFilename, 409) filename = None def fileProcessingFinished(filename, absFilename, destination): """ Callback for when the file processing (upload, optional slicing, addition to analysis queue) has finished. Depending on the file's destination triggers either streaming to SD card or directly calls selectAndOrPrint. """ if destination == FileDestinations.SDCARD: return filename, printer.addSdFile(filename, absFilename, selectAndOrPrint) else: selectAndOrPrint(absFilename, destination) return filename def selectAndOrPrint(nameToSelect, destination): """ Callback for when the file is ready to be selected and optionally printed. For SD file uploads this is only the case after they have finished streaming to the printer, which is why this callback is also used for the corresponding call to addSdFile. Selects the just uploaded file if either selectAfterUpload or printAfterSelect are True, or if the exact file is already selected, such reloading it. """ sd = destination == FileDestinations.SDCARD if selectAfterUpload or printAfterSelect or (currentFilename == filename and currentSd == sd): printer.selectFile(nameToSelect, sd, printAfterSelect) destination = FileDestinations.SDCARD if sd else FileDestinations.LOCAL filename, done = gcodeManager.addFile(file, destination, fileProcessingFinished) if filename is None: return make_response("Could not upload the file %s" % file.filename, 500) sdFilename = None if isinstance(filename, tuple): filename, sdFilename = filename eventManager.fire(Events.UPLOAD, {"file": filename, "target": target}) files = {} if done: files.update({ FileDestinations.LOCAL: { "name": filename, "origin": FileDestinations.LOCAL, "refs": { "resource": url_for(".readGcodeFile", target=FileDestinations.LOCAL, filename=filename, _external=True), "download": url_for("index", _external=True) + "downloads/files/" + FileDestinations.LOCAL + "/" + filename } } }) if sd and sdFilename: files.update({ FileDestinations.SDCARD: { "name": sdFilename, "origin": FileDestinations.SDCARD, "refs": { "resource": url_for(".readGcodeFile", target=FileDestinations.SDCARD, filename=sdFilename, _external=True) } } }) return make_response(jsonify(files=files, done=done), 201)
def add_firmware_file(self, destination, path, file_object, links=None, allow_overwrite=False, printer_profile=None, analysis=None): if printer_profile is None: printer_profile = self._printer_profile_manager.get_current_or_default( ) _, ext = os.path.splitext(path) print("lkj ext:%s" % ext) if ext != ".fbot": return None file_path = self._storage(destination).add_file( path, file_object, links=links, printer_profile=printer_profile, allow_overwrite=allow_overwrite) absolute_path = self._storage(destination).get_absolute_path(file_path) firmware = firmwareFastBot.FirmwareFastbot() print("firmware absolute path:%s" % absolute_path) firmware_version = firmware.loadFirmware(absolute_path, "/tmp/") print("firmware_version :%s" % firmware_version) from octoprint.settings import settings settings().set(["fastbotArgs", "firmwareVersion"], firmware_version) settings().save() print("firmware_version 22 :%s" % firmware_version) from octoprint.server import printer from octoprint.comm.protocol.reprap.util import GcodeCommand if printer.isOperational(): try: cmd = GcodeCommand("M504", s=firmware_version.strip("\x00")) #print("cmd=%s" % str(cmd)) printer.command(cmd) except: exceptionString = getExceptionString() print("ex:%s" % str(exceptionString)) print("firmware_version 333 :%s" % firmware_version) firmware.rebootSystem(5) print("firmware absolute path:%s" % absolute_path) ''' if analysis is None: file_type = get_file_type(absolute_path) if file_type: queue_entry = QueueEntry(file_path, file_type[-1], destination, absolute_path, _profile) self._analysis_queue.enqueue(queue_entry, high_priority=True) else: self._add_analysis_result(destination, path, analysis) eventManager().fire(Events.UPDATED_FILES, dict(type="printables")) ''' return absolute_path