def connectionCommand(request=None): valid_commands = {"connect": ["autoconnect"], "disconnect": []} command, data, response = util.getJsonCommandFromRequest( request, valid_commands) print "connectionCommand", command, data, response, "connectionCommand" if response is not None: return response if command == "connect": options = getConnectionOptions() port = None baudrate = None if "port" in data.keys(): port = data["port"] if port not in options["ports"]: return make_response("Invalid port: %s" % port, 400) if "baudrate" in data.keys(): baudrate = data["baudrate"] if baudrate not in options["baudrates"]: return make_response("Invalid baudrate: %d" % baudrate, 400) if "save" in data.keys() and data["save"]: settings().set(["serial", "port"], port) settings().setInt(["serial", "baudrate"], baudrate) if "autoconnect" in data.keys(): settings().setBoolean(["serial", "autoconnect"], data["autoconnect"]) settings().save() printer.connect(port=port, baudrate=baudrate) elif command == "disconnect": printer.disconnect() return NO_CONTENT
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 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 copyFile(data=None): if data is None: return make_response("Expected content type JSON", 400) target = None filename = None if "target" in data.keys(): target = data["target"] if "filename" in data.keys(): filename = data["filename"] if not _verifyFileExists(target, filename): return make_response("File not found on '%s': %s" % (target, filename), 404) else: gcodeManager.startThreadToCopyFile(filename) return NO_CONTENT
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 deleteGcodeFile(filename=None, target=None): 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) sd = target == FileDestinations.SDCARD currentJob = printer.getCurrentJob() currentFilename = None currentSd = None if currentJob is not None and "filename" in currentJob.keys( ) and "sd" in currentJob.keys(): currentFilename = currentJob["filename"] currentSd = currentJob["sd"] # prohibit deleting the file that is currently being printed if currentFilename == filename and currentSd == sd and ( printer.isPrinting() or printer.isPaused()): make_response( "Trying to delete file that is currently being printed: %s" % filename, 409) # deselect the file if it's currently selected if currentFilename is not None and filename == currentFilename: printer.unselectFile() # delete it if sd: printer.deleteSdFile(filename) else: gcodeManager.removeFile(filename) return NO_CONTENT
def printerBedCommand(request=None): 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 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 changeFilesPath(data=None): #modify by kevin, for use json format if data is None: return make_response("Expected content type JSON", 400) print "changeFilesPath", data, "changeFilesPath" if "filespath" in data.keys(): if "local" == data["filespath"]: gcodeManager._uploadFolder = gcodeManager._localpath elif "usb" == data["filespath"]: gcodeManager._uploadFolder = gcodeManager._usbpath #modify end if "returnFiles" in data.keys() and data.get("returnFiles") is True: files = _getFileList(FileDestinations.LOCAL) files.extend(_getFileList(FileDestinations.SDCARD)) return jsonify(files=files, free=util.getFreeBytes(gcodeManager._uploadFolder)) 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 printerToolCommand(request=None): 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) print "printerToolCommand", command, data, response, "printerToolCommand" 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) return NO_CONTENT