diff --git a/octoprint_tplinksmartplug/__init__.py b/octoprint_tplinksmartplug/__init__.py index 251e2fc..76e73f4 100644 --- a/octoprint_tplinksmartplug/__init__.py +++ b/octoprint_tplinksmartplug/__init__.py @@ -100,6 +100,7 @@ def __init__(self): self.db_path = None self.poll_status = None self.power_off_queue = [] + self._gcode_queued = False ##~~ StartupPlugin mixin @@ -228,7 +229,7 @@ def on_settings_save(self, data): self.poll_status.start() def get_settings_version(self): - return 14 + return 15 def on_settings_migrate(self, target, current=None): if current is None or current < 5: @@ -305,6 +306,16 @@ def on_settings_migrate(self, target, current=None): arrSmartplugs_new.append(plug) self._settings.set(["arrSmartplugs"], arrSmartplugs_new) + if current is not None and current < 15: + arrSmartplugs_new = [] + for plug in self._settings.get(['arrSmartplugs']): + plug["gcodeCmdOn"] = False + plug["gcodeCmdOff"] = False + plug["gcodeRunCmdOn"] = "" + plug["gcodeRunCmdOff"] = "" + arrSmartplugs_new.append(plug) + self._settings.set(["arrSmartplugs"], arrSmartplugs_new) + ##~~ AssetPlugin mixin def get_assets(self): @@ -380,6 +391,12 @@ def turn_on(self, plugip): c = threading.Timer(int(plug["autoConnectDelay"]), self._printer.connect) c.daemon = True c.start() + if plug["gcodeCmdOn"] and self._printer.is_closed_or_error(): + self._tplinksmartplug_logger.debug("queuing gcode on commands because printer isn't connected yet.") + self._gcode_queued = True + if plug["gcodeCmdOn"] and self._printer.is_ready() and plug["gcodeRunCmdOn"] != "": + self._tplinksmartplug_logger.debug("sending gcode commands to printer.") + self._printer.commands(plug["gcodeRunCmdOn"].split("\n")) if plug["sysCmdOn"]: t = threading.Timer(int(plug["sysCmdOnDelay"]), os.system, args=[plug["sysRunCmdOn"]]) t.daemon = True @@ -409,7 +426,9 @@ def turn_off(self, plugip): self._countdown_active = True c = threading.Timer(int(plug["countdownOffDelay"]) + 3, self._plugin_manager.send_plugin_message, [self._identifier, dict(check_status=True, ip=plugip)]) c.start() - + if plug["gcodeCmdOff"] and plug["gcodeRunCmdOff"] != "": + self._tplinksmartplug_logger.debug("sending gcode commands to printer.") + self._printer.commands(plug["gcodeRunCmdOff"].split("\n")) if plug["sysCmdOff"]: t = threading.Timer(int(plug["sysCmdOffDelay"]), os.system, args=[plug["sysRunCmdOff"]]) t.daemon = True @@ -693,6 +712,12 @@ def on_event(self, event, payload): self._timelapse_active = False # Printer Connected Event if event == Events.CONNECTED: + if self._gcode_queued: + for plug in self._settings.get(['arrSmartplugs']): + if plug["gcodeCmdOn"] and plug["gcodeRunCmdOn"] != "": + self._tplinksmartplug_logger.debug("sending gcode commands to printer.") + self._printer.commands(plug["gcodeRunCmdOn"].split("\n")) + self._gcode_queued = False if self._autostart_file: self._tplinksmartplug_logger.debug("printer connected starting print of %s" % self._autostart_file) self._printer.select_file(self._autostart_file, False, printAfterSelect=True) diff --git a/octoprint_tplinksmartplug/static/js/tplinksmartplug.js b/octoprint_tplinksmartplug/static/js/tplinksmartplug.js index 6910051..ef286a7 100644 --- a/octoprint_tplinksmartplug/static/js/tplinksmartplug.js +++ b/octoprint_tplinksmartplug/static/js/tplinksmartplug.js @@ -274,7 +274,12 @@ $(function() { 'event_on_disconnect':ko.observable(false), 'automaticShutdownEnabled':ko.observable(false), 'event_on_upload':ko.observable(false), - 'event_on_startup':ko.observable(false)}); + 'event_on_startup':ko.observable(false), + 'gcodeCmdOn': ko.observable(false), + 'gcodeCmdOff': ko.observable(false), + 'gcodeRunCmdOn': ko.observable(''), + 'gcodeRunCmdOff': ko.observable('') + }); self.settings.settings.plugins.tplinksmartplug.arrSmartplugs.push(self.selectedPlug()); $("#TPLinkPlugEditor").modal("show"); } diff --git a/octoprint_tplinksmartplug/templates/tplinksmartplug_settings.jinja2 b/octoprint_tplinksmartplug/templates/tplinksmartplug_settings.jinja2 index 2231934..2f48695 100644 --- a/octoprint_tplinksmartplug/templates/tplinksmartplug_settings.jinja2 +++ b/octoprint_tplinksmartplug/templates/tplinksmartplug_settings.jinja2 @@ -238,6 +238,18 @@