示例#1
0
def printerToolCommand():
	if not printer.is_operational():
		return make_response("Printer is not operational", 409)

	valid_commands = {
		"select": ["tool"],
		"target": ["targets"],
		"offset": ["offsets"],
		"extrude": ["amount"],
		"flowrate": ["factor"]
	}
	command, data, response = get_json_command_from_request(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.change_tool(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.set_temperature(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.set_temperature_offset(validated_values)

	##~~ extrusion
	elif command == "extrude":
		if printer.is_printing():
			# 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)

	elif command == "flowrate":
		factor = data["factor"]
		if not isinstance(factor, (int, long, float)):
			return make_response("Not a number for flow rate: %r" % factor, 400)
		try:
			printer.flow_rate(factor)
		except ValueError as e:
			return make_response("Invalid value for flow rate: %s" % str(e), 400)

	return NO_CONTENT
示例#2
0
def printerToolCommand():
    if not printer.is_operational():
        abort(409, description="Printer is not operational")

    valid_commands = {
        "select": ["tool"],
        "target": ["targets"],
        "offset": ["offsets"],
        "extrude": ["amount"],
        "flowrate": ["factor"],
    }
    command, data, response = get_json_command_from_request(request, valid_commands)
    if response is not None:
        return response

    validation_regex = re.compile(r"tool\d+")

    tags = {"source:api", "api:printer.tool"}

    ##~~ tool selection
    if command == "select":
        tool = data["tool"]
        if not isinstance(tool, basestring) or re.match(validation_regex, tool) is None:
            abort(400, description="tool is invalid")

        printer.change_tool(tool, tags=tags)

    ##~~ 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.items():
            if re.match(validation_regex, tool) is None:
                abort(400, description="targets contains invalid tool")
            if not isinstance(value, (int, long, float)):
                abort(400, description="targets contains invalid value")
            validated_values[tool] = value

        # perform the actual temperature commands
        for tool in validated_values.keys():
            printer.set_temperature(tool, validated_values[tool], tags=tags)

    ##~~ 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.items():
            if re.match(validation_regex, tool) is None:
                abort(400, description="offsets contains invalid tool")
            if not isinstance(value, (int, long, float)) or not -50 <= value <= 50:
                abort(400, description="offsets contains invalid value")
            validated_values[tool] = value

        # set the offsets
        printer.set_temperature_offset(validated_values)

    ##~~ extrusion
    elif command == "extrude":
        if printer.is_printing():
            # do not extrude when a print job is running
            abort(409, description="Printer is currently printing")

        amount = data["amount"]
        speed = data.get("speed", None)
        if not isinstance(amount, (int, long, float)):
            abort(400, description="amount is invalid")
        printer.extrude(amount, speed=speed, tags=tags)

    elif command == "flowrate":
        factor = data["factor"]
        if not isinstance(factor, (int, long, float)):
            abort(400, description="factor is invalid")
        try:
            printer.flow_rate(factor, tags=tags)
        except ValueError:
            abort(400, description="factor is invalid")

    return NO_CONTENT