MCPcopy Create free account
hub / github.com/AstroPrint/AstroBox / printerToolCommand

Function printerToolCommand

src/astroprint/api/printer.py:59–124  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

57@api.route("/printer/tool", methods=["POST"])
58@restricted_access
59def printerToolCommand():
60 pm = printerManager()
61
62 if not pm.isOperational():
63 return make_response("Printer is not operational", 409)
64
65 valid_commands = {
66 "select": ["tool"],
67 "target": ["targets"],
68 "offset": ["offsets"],
69 "extrude": ["amount"]
70 }
71 command, data, response = util.getJsonCommandFromRequest(request, valid_commands)
72 if response is not None:
73 return response
74
75 validation_regex = re.compile("tool\d+")
76
77 ##~~ tool selection
78 if command == "select":
79 tool = data["tool"]
80 if re.match(validation_regex, tool) is None:
81 return make_response("Invalid tool: %s" % tool, 400)
82 if not tool.startswith("tool"):
83 return make_response("Invalid tool for selection: %s" % tool, 400)
84
85 pm.changeTool(int(tool[len("tool"):]))
86
87 ##~~ temperature
88 elif command == "target":
89 targets = data["targets"]
90
91 # make sure the targets are valid and the values are numbers
92 validated_values = {}
93 for tool, value in targets.iteritems():
94 if re.match(validation_regex, tool) is None:
95 return make_response("Invalid target for setting temperature: %s" % tool, 400)
96 if not isinstance(value, (int, long, float)):
97 return make_response("Not a number for %s: %r" % (tool, value), 400)
98 validated_values[tool] = value
99
100 # perform the actual temperature commands
101 for tool in validated_values.keys():
102 pm.setTemperature(tool, validated_values[tool])
103
104 ##~~ extrusion
105 elif command == "extrude":
106 if pm.isPrinting():
107 # do not extrude when a print job is running
108 return make_response("Printer is currently printing", 409)
109
110 amount = data["amount"]
111 speed = data.get("speed")
112 tool = data.get("tool")
113 if not isinstance(amount, (int, long, float)):
114 return make_response("Not a number for extrusion amount: %r" % amount, 400)
115
116 if tool is not None and re.match(validation_regex, tool) is None:

Callers

nothing calls this directly

Calls 7

printerManagerFunction · 0.90
isOperationalMethod · 0.45
changeToolMethod · 0.45
setTemperatureMethod · 0.45
isPrintingMethod · 0.45
getMethod · 0.45
extrudeMethod · 0.45

Tested by

no test coverage detected