Skip to content

Commit

Permalink
Merge branch 'devel'
Browse files Browse the repository at this point in the history
  • Loading branch information
kantlivelong committed Dec 13, 2020
2 parents b74c2fb + 66d736e commit e437a46
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 8 deletions.
49 changes: 44 additions & 5 deletions octoprint_psucontrol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import octoprint.plugin
from octoprint.server import user_permission
from octoprint.events import Events
import time
import subprocess
import threading
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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=[],
Expand Down Expand Up @@ -623,6 +652,7 @@ def get_settings_defaults(self):
pseudoOnGCodeCommand = 'M80',
pseudoOffGCodeCommand = 'M81',
postOnDelay = 0.0,
connectOnPowerOn = False,
disconnectOnPowerOff = False,
sensingMethod = 'INTERNAL',
senseGPIOPin = 0,
Expand Down Expand Up @@ -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"])
Expand All @@ -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
Expand Down
38 changes: 35 additions & 3 deletions octoprint_psucontrol/static/js/psucontrol.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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() {
Expand Down
19 changes: 19 additions & 0 deletions octoprint_psucontrol/templates/psucontrol_settings.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: settings.plugins.psucontrol.connectOnPowerOn"> Connect when powered on.
</label>
</div>
</div>
<div class="control-group">
<label class="control-label">Post On GCode Script<br/><font color="red">experimental</font></label>
<div class="controls">
<textarea rows="5" class="block" data-bind="value: scripts_gcode_psucontrol_post_on"></textarea>
</div>
</div>
<br />

<h4>Power Off Options</h4>
Expand Down Expand Up @@ -201,6 +214,12 @@
</div>
</div>
</div>
<div class="control-group">
<label class="control-label">Pre Off GCode Script<br/><font color="red">experimental</font></label>
<div class="controls">
<textarea rows="5" class="block" data-bind="value: scripts_gcode_psucontrol_pre_off"></textarea>
</div>
</div>
<!-- /ko -->
<div class="control-group">
<div class="controls">
Expand Down

0 comments on commit e437a46

Please sign in to comment.