Skip to content

Commit

Permalink
Use fallback temperatures when printing from the SD card
Browse files Browse the repository at this point in the history
fixes #22
  • Loading branch information
marian42 committed Feb 17, 2019
1 parent 88dacc5 commit 00fd566
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 40 deletions.
85 changes: 46 additions & 39 deletions octoprint_preheat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,44 +66,11 @@ def parse_line(self, line):

return tool, temperature


def get_fallback_temperature(self):
enable_bed = self._settings.get_boolean(["enable_bed"])
enable_tool = self._settings.get_boolean(["enable_tool"])
fallback_tool = self._settings.get_float(["fallback_tool"])
fallback_bed = self._settings.get_float(["fallback_bed"])

printer_profile = self._printer._printerProfileManager.get_current_or_default()

result = dict()

if enable_bed and fallback_bed > 0 and printer_profile["heatedBed"]:
result["bed"] = fallback_bed

if enable_tool and fallback_tool > 0:
extruder_count = printer_profile["extruder"]["count"]
for i in range(extruder_count):
tool = "tool" + str(i)
result[tool] = fallback_tool

return result


def get_temperatures(self):
printer = self._printer

def read_temperatures_from_file(self, path_on_disk):
enable_bed = self._settings.get_boolean(["enable_bed"])
enable_tool = self._settings.get_boolean(["enable_tool"])

if (printer.get_current_job()["file"]["path"] == None):
raise PreheatError("No gcode file loaded.")

file_name = printer.get_current_job()["file"]["path"]

if printer.get_current_job()["file"]["origin"] != octoprint.filemanager.FileDestinations.LOCAL:
raise PreheatError("Can't read the temperature from a gcode file stored on the SD card.")
path_on_disk = octoprint.server.fileManager.path_on_disk(octoprint.filemanager.FileDestinations.LOCAL, file_name)


file = open(path_on_disk, 'r')
line = file.readline()
max_lines = 1000
Expand All @@ -114,7 +81,7 @@ def get_temperatures(self):
line = file.readline()
if line == "":
break
if enable_tool and (line.startswith("M104") or line.startswith("M109")): # Set tool temperature
if enable_tool and (line.startswith("M104") or line.startswith("M109")): # Set tool temperature
tool, temperature = self.parse_line(line)
if temperature != None and tool not in temperatures:
temperatures[tool] = temperature
Expand All @@ -127,13 +94,53 @@ def get_temperatures(self):
except:
self._logger.exception("Something went wrong while trying to read the preheat temperature from {}".format(path_on_disk))

if len(temperatures) == 0:
temperatures = self.get_fallback_temperature()
return temperatures


def get_fallback_temperatures(self):
enable_bed = self._settings.get_boolean(["enable_bed"])
enable_tool = self._settings.get_boolean(["enable_tool"])
fallback_tool = self._settings.get_float(["fallback_tool"])
fallback_bed = self._settings.get_float(["fallback_bed"])

printer_profile = self._printer._printerProfileManager.get_current_or_default()

result = dict()

if enable_bed and fallback_bed > 0 and printer_profile["heatedBed"]:
result["bed"] = fallback_bed

if enable_tool and fallback_tool > 0:
extruder_count = printer_profile["extruder"]["count"]
for i in range(extruder_count):
tool = "tool" + str(i)
result[tool] = fallback_tool

return result


def get_temperatures(self):
if (self._printer.get_current_job()["file"]["path"] == None):
raise PreheatError("No gcode file loaded.")

if self._printer.get_current_job()["file"]["origin"] == octoprint.filemanager.FileDestinations.SDCARD:
temperatures = self.get_fallback_temperatures()

if len(temperatures) == 0:
raise PreheatError("Can't read the temperature from a gcode file stored on the SD card.")
else:
self._logger.info("Can't read the temperatures from the SD card, using fallback temperatures.")
else:
file_name = self._printer.get_current_job()["file"]["path"]
path_on_disk = octoprint.server.fileManager.path_on_disk(octoprint.filemanager.FileDestinations.LOCAL, file_name)
temperatures = self.read_temperatures_from_file(path_on_disk)

if len(temperatures) == 0:
temperatures = self.get_fallback_temperatures()
if len(temperatures) == 0:
raise PreheatError("Could not find any preheat commands in the gcode file. You can configure fallback temperatures for this case.")
else:
self._logger.info("Could not find any preheat commands in the gcode file, using fallback temperatures.")
self._logger.info("Could not find any preheat commands in the gcode file, using fallback temperatures.")

offsets = self._printer.get_current_data()["offsets"]
for tool in temperatures:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
plugin_name = "Preheat"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "0.3.0"
plugin_version = "0.3.1"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
Expand Down

0 comments on commit 00fd566

Please sign in to comment.