diff --git a/octoprint_psucontrol/__init__.py b/octoprint_psucontrol/__init__.py index b01f4ea..887648c 100644 --- a/octoprint_psucontrol/__init__.py +++ b/octoprint_psucontrol/__init__.py @@ -7,6 +7,7 @@ import octoprint.plugin from octoprint.server import user_permission +from octoprint.events import Events import time import subprocess import threading @@ -68,10 +69,11 @@ def reset(self, interval=None): class PSUControl(octoprint.plugin.StartupPlugin, - octoprint.plugin.TemplatePlugin, - octoprint.plugin.AssetPlugin, - octoprint.plugin.SettingsPlugin, - octoprint.plugin.SimpleApiPlugin): + octoprint.plugin.TemplatePlugin, + octoprint.plugin.AssetPlugin, + octoprint.plugin.SettingsPlugin, + octoprint.plugin.SimpleApiPlugin, + octoprint.plugin.EventHandlerPlugin): def __init__(self): try: @@ -164,6 +166,9 @@ def on_settings_initialized(self): self.postOnDelay = self._settings.get_float(["postOnDelay"]) self._logger.debug("postOnDelay: %s" % self.postOnDelay) + self.connectOnPowerOn = self._settings.get_boolean(["connectOnPowerOn"]) + self._logger.debug("connectOnPowerOn: %s" % self.connectOnPowerOn) + self.disconnectOnPowerOff = self._settings.get_boolean(["disconnectOnPowerOff"]) self._logger.debug("disconnectOnPowerOff: %s" % self.disconnectOnPowerOff) @@ -208,6 +213,14 @@ def on_settings_initialized(self): self.idleTimeoutWaitTemp = self._settings.get_int(["idleTimeoutWaitTemp"]) self._logger.debug("idleTimeoutWaitTemp: %s" % self.idleTimeoutWaitTemp) + scripts = self._settings.listScripts("gcode") + if not "psucontrol_post_on" in scripts: + self._settings.saveScript("gcode", "psucontrol_post_on", u'') + + scripts = self._settings.listScripts("gcode") + if not "psucontrol_pre_off" in scripts: + self._settings.saveScript("gcode", "psucontrol_pre_off", u'') + if self.switchingMethod == 'GCODE': self._logger.info("Using G-Code Commands for On/Off") elif self.switchingMethod == 'GPIO': @@ -375,7 +388,7 @@ def _check_psu_state(self): elif (old_isPSUOn != self.isPSUOn) and not self.isPSUOn: self._stop_idle_timer() - self._plugin_manager.send_plugin_message(self._identifier, dict(hasGPIO=self._hasGPIO, isPSUOn=self.isPSUOn)) + self._plugin_manager.send_plugin_message(self._identifier, dict(isPSUOn=self.isPSUOn)) self._check_psu_state_event.wait(self.sensePollingInterval) self._check_psu_state_event.clear() @@ -540,10 +553,21 @@ def turn_psu_on(self): self._noSensing_isPSUOn = True time.sleep(0.1 + self.postOnDelay) + self.check_psu_state() + + if self.connectOnPowerOn and self._printer.is_closed_or_error(): + self._printer.connect() + time.sleep(0.1) + + if not self._printer.is_closed_or_error(): + self._printer.script("psucontrol_post_on", must_be_set=False) def turn_psu_off(self): if self.switchingMethod == 'GCODE' or self.switchingMethod == 'GPIO' or self.switchingMethod == 'SYSTEM': + if not self._printer.is_closed_or_error(): + self._printer.script("psucontrol_pre_off", must_be_set=False) + self._logger.info("Switching PSU Off") if self.switchingMethod == 'GCODE': self._logger.debug("Switching PSU Off Using GCODE: %s" % self.offGCodeCommand) @@ -582,6 +606,11 @@ def turn_psu_off(self): time.sleep(0.1) self.check_psu_state() + def on_event(self, event, payload): + if event == Events.CLIENT_OPENED: + self._plugin_manager.send_plugin_message(self._identifier, dict(hasGPIO=self._hasGPIO, isPSUOn=self.isPSUOn)) + return + def get_api_commands(self): return dict( turnPSUOn=[], @@ -623,6 +652,7 @@ def get_settings_defaults(self): pseudoOnGCodeCommand = 'M80', pseudoOffGCodeCommand = 'M81', postOnDelay = 0.0, + connectOnPowerOn = False, disconnectOnPowerOff = False, sensingMethod = 'INTERNAL', senseGPIOPin = 0, @@ -662,6 +692,7 @@ def on_settings_save(self, data): self.pseudoOnGCodeCommand = self._settings.get(["pseudoOnGCodeCommand"]) self.pseudoOffGCodeCommand = self._settings.get(["pseudoOffGCodeCommand"]) self.postOnDelay = self._settings.get_float(["postOnDelay"]) + self.connectOnPowerOn = self._settings.get_boolean(["connectOnPowerOn"]) self.disconnectOnPowerOff = self._settings.get_boolean(["disconnectOnPowerOff"]) self.sensingMethod = self._settings.get(["sensingMethod"]) self.senseGPIOPin = self._settings.get_int(["senseGPIOPin"]) @@ -679,6 +710,14 @@ def on_settings_save(self, data): self._idleIgnoreCommandsArray = self.idleIgnoreCommands.split(',') self.idleTimeoutWaitTemp = self._settings.get_int(["idleTimeoutWaitTemp"]) + if 'scripts_gcode_psucontrol_post_on' in data: + script = data["scripts_gcode_psucontrol_post_on"] + self._settings.saveScript("gcode", "psucontrol_post_on", u'' + script.replace("\r\n", "\n").replace("\r", "\n")) + + if 'scripts_gcode_psucontrol_pre_off' in data: + script = data["scripts_gcode_psucontrol_pre_off"] + self._settings.saveScript("gcode", "psucontrol_pre_off", u'' + script.replace("\r\n", "\n").replace("\r", "\n")) + #GCode switching and PseudoOnOff are not compatible. if self.switchingMethod == 'GCODE' and self.enablePseudoOnOff: self.enablePseudoOnOff = False diff --git a/octoprint_psucontrol/static/js/psucontrol.js b/octoprint_psucontrol/static/js/psucontrol.js index ac28ee9..74ccc8c 100644 --- a/octoprint_psucontrol/static/js/psucontrol.js +++ b/octoprint_psucontrol/static/js/psucontrol.js @@ -4,15 +4,42 @@ $(function() { self.settingsViewModel = parameters[0] self.loginState = parameters[1]; + self.settings = undefined; - self.hasGPIO = ko.observable(undefined); + self.scripts_gcode_psucontrol_post_on = ko.observable(undefined); + self.scripts_gcode_psucontrol_pre_off = ko.observable(undefined); + + self.hasGPIO = ko.observable(true); self.isPSUOn = ko.observable(undefined); + self.psu_indicator = $("#psucontrol_indicator"); self.onBeforeBinding = function() { self.settings = self.settingsViewModel.settings; }; + self.onSettingsShown = function () { + self.scripts_gcode_psucontrol_post_on(self.settings.scripts.gcode["psucontrol_post_on"]()); + self.scripts_gcode_psucontrol_pre_off(self.settings.scripts.gcode["psucontrol_pre_off"]()); + }; + + self.onSettingsHidden = function () { + self.settings.plugins.psucontrol.scripts_gcode_psucontrol_post_on = null; + self.settings.plugins.psucontrol.scripts_gcode_psucontrol_pre_off = null; + }; + + self.onSettingsBeforeSave = function () { + if (self.scripts_gcode_psucontrol_post_on() != self.settings.scripts.gcode["psucontrol_post_on"]()) { + self.settings.plugins.psucontrol.scripts_gcode_psucontrol_post_on = self.scripts_gcode_psucontrol_post_on; + self.settings.scripts.gcode["psucontrol_post_on"](self.scripts_gcode_psucontrol_post_on()); + } + + if (self.scripts_gcode_psucontrol_pre_off() != self.settings.scripts.gcode["psucontrol_pre_off"]()) { + self.settings.plugins.psucontrol.scripts_gcode_psucontrol_pre_off = self.scripts_gcode_psucontrol_pre_off; + self.settings.scripts.gcode["psucontrol_pre_off"](self.scripts_gcode_psucontrol_pre_off()); + } + }; + self.onStartup = function () { self.isPSUOn.subscribe(function() { if (self.isPSUOn()) { @@ -40,8 +67,13 @@ $(function() { return; } - self.hasGPIO(data.hasGPIO); - self.isPSUOn(data.isPSUOn); + if (data.hasGPIO !== undefined) { + self.hasGPIO(data.hasGPIO); + } + + if (data.isPSUOn !== undefined) { + self.isPSUOn(data.isPSUOn); + } }; self.togglePSU = function() { diff --git a/octoprint_psucontrol/templates/psucontrol_settings.jinja2 b/octoprint_psucontrol/templates/psucontrol_settings.jinja2 index cfcb06d..0c06c18 100644 --- a/octoprint_psucontrol/templates/psucontrol_settings.jinja2 +++ b/octoprint_psucontrol/templates/psucontrol_settings.jinja2 @@ -166,6 +166,19 @@ +