Skip to content

Commit

Permalink
Add support for sensing via system commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kantlivelong committed Sep 17, 2017
1 parent 41f78f8 commit fd4f99c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 21 deletions.
59 changes: 44 additions & 15 deletions octoprint_psucontrol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from octoprint.util import RepeatedTimer
import RPi.GPIO as GPIO
import time
import subprocess
import threading
import os
from flask import make_response, jsonify
Expand Down Expand Up @@ -46,11 +47,12 @@ def __init__(self):
self.idleIgnoreCommands = ''
self._idleIgnoreCommandsArray = []
self.idleTimeoutWaitTemp = 0
self.enableSensing = False
self.disconnectOnPowerOff = False
self.sensingMethod = ''
self.senseGPIOPin = 0
self.invertsenseGPIOPin = False
self.senseGPIOPinPUD = ''
self.senseSystemCommand = ''
self.isPSUOn = False
self._noSensing_isPSUOn = False
self._checkPSUTimer = None
Expand Down Expand Up @@ -100,12 +102,12 @@ def on_settings_initialized(self):
self.postOnDelay = self._settings.get_float(["postOnDelay"])
self._logger.debug("postOnDelay: %s" % self.postOnDelay)

self.enableSensing = self._settings.get_boolean(["enableSensing"])
self._logger.debug("enableSensing: %s" % self.enableSensing)

self.disconnectOnPowerOff = self._settings.get_boolean(["disconnectOnPowerOff"])
self._logger.debug("disconnectOnPowerOff: %s" % self.disconnectOnPowerOff)

self.sensingMethod = self._settings.get(["sensingMethod"])
self._logger.debug("sensingMethod: %s" % self.sensingMethod)

self.senseGPIOPin = self._settings.get_int(["senseGPIOPin"])
self._logger.debug("senseGPIOPin: %s" % self.senseGPIOPin)

Expand All @@ -115,6 +117,9 @@ def on_settings_initialized(self):
self.senseGPIOPinPUD = self._settings.get(["senseGPIOPinPUD"])
self._logger.debug("senseGPIOPinPUD: %s" % self.senseGPIOPinPUD)

self.senseSystemCommand = self._settings.get(["senseSystemCommand"])
self._logger.debug("senseSystemCommand: %s" % self.senseSystemCommand)

self.autoOn = self._settings.get_boolean(["autoOn"])
self._logger.debug("autoOn: %s" % self.autoOn)

Expand Down Expand Up @@ -198,8 +203,8 @@ def _configure_gpio(self):
else:
return

if self.enableSensing:
self._logger.info("Using sensing to determine PSU on/off state.")
if self.sensingMethod == 'GPIO':
self._logger.info("Using GPIO sensing to determine PSU on/off state.")
self._logger.info("Configuring GPIO for pin %s" % self.senseGPIOPin)

if self.senseGPIOPinPUD == 'PULL_UP':
Expand Down Expand Up @@ -235,9 +240,9 @@ def _configure_gpio(self):
def check_psu_state(self):
old_isPSUOn = self.isPSUOn

if self.enableSensing:
if self.sensingMethod == 'GPIO':
self._logger.debug("Polling PSU state...")
new_isPSUOn = False

r = 0
try:
r = GPIO.input(self._gpio_get_pin(self.senseGPIOPin))
Expand All @@ -253,6 +258,20 @@ def check_psu_state(self):
if self.invertsenseGPIOPin:
new_isPSUOn = not new_isPSUOn

self.isPSUOn = new_isPSUOn
elif self.sensingMethod == 'SYSTEM':
new_isPSUOn = False

p = subprocess.Popen(self.senseSystemCommand, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
output = p.communicate()[0]
r = p.returncode
self._logger.debug("System command returned: %s" % r)

if r==0:
new_isPSUOn = True
elif r==1:
new_isPSUOn = False

self.isPSUOn = new_isPSUOn
else:
self.isPSUOn = self._noSensing_isPSUOn
Expand Down Expand Up @@ -383,7 +402,7 @@ def turn_psu_on(self):
except (RuntimeError, ValueError) as e:
self._logger.error(e)

if not self.enableSensing:
if self.sensingMethod not in ('GPIO','SYSTEM'):
self._noSensing_isPSUOn = True

time.sleep(0.1 + self.postOnDelay)
Expand Down Expand Up @@ -414,7 +433,7 @@ def turn_psu_off(self):
if self.disconnectOnPowerOff:
self._printer.disconnect()

if not self.enableSensing:
if self.sensingMethod not in ('GPIO','SYSTEM'):
self._noSensing_isPSUOn = False

time.sleep(0.1)
Expand Down Expand Up @@ -458,11 +477,12 @@ def get_settings_defaults(self):
pseudoOnGCodeCommand = 'M80',
pseudoOffGCodeCommand = 'M81',
postOnDelay = 0.0,
enableSensing = False,
disconnectOnPowerOff = False,
sensingMethod = '',
senseGPIOPin = 0,
invertsenseGPIOPin = False,
senseGPIOPinPUD = '',
senseSystemCommand = '',
autoOn = False,
autoOnTriggerGCodeCommands = "G0,G1,G2,G3,G10,G11,G28,G29,G32,M104,M106,M109,M140,M190",
enablePowerOffWarningDialog = True,
Expand All @@ -475,7 +495,7 @@ def get_settings_defaults(self):
def on_settings_save(self, data):
old_GPIOMode = self.GPIOMode
old_onoffGPIOPin = self.onoffGPIOPin
old_enableSensing = self.enableSensing
old_sensingMethod = self.sensingMethod
old_senseGPIOPin = self.senseGPIOPin
old_invertsenseGPIOPin = self.invertsenseGPIOPin
old_senseGPIOPinPUD = self.senseGPIOPinPUD
Expand All @@ -495,11 +515,12 @@ 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.enableSensing = self._settings.get_boolean(["enableSensing"])
self.disconnectOnPowerOff = self._settings.get_boolean(["disconnectOnPowerOff"])
self.sensingMethod = self._settings.get(["sensingMethod"])
self.senseGPIOPin = self._settings.get_int(["senseGPIOPin"])
self.invertsenseGPIOPin = self._settings.get_boolean(["invertsenseGPIOPin"])
self.senseGPIOPinPUD = self._settings.get(["senseGPIOPinPUD"])
self.senseSystemCommand = self._settings.get(["senseSystemCommand"])
self.autoOn = self._settings.get_boolean(["autoOn"])
self.autoOnTriggerGCodeCommands = self._settings.get(["autoOnTriggerGCodeCommands"])
self._autoOnTriggerGCodeCommandsArray = self.autoOnTriggerGCodeCommands.split(',')
Expand All @@ -520,7 +541,7 @@ def on_settings_save(self, data):
if (old_GPIOMode != self.GPIOMode or
old_onoffGPIOPin != self.onoffGPIOPin or
old_senseGPIOPin != self.senseGPIOPin or
old_enableSensing != self.enableSensing or
old_sensingMethod != self.sensingMethod or
old_invertsenseGPIOPin != self.invertsenseGPIOPin or
old_senseGPIOPinPUD != self.senseGPIOPinPUD or
old_switchingMethod != self.switchingMethod):
Expand All @@ -529,7 +550,7 @@ def on_settings_save(self, data):
self._start_idle_timer()

def get_settings_version(self):
return 2
return 3

def on_settings_migrate(self, target, current=None):
if current is None or current < 2:
Expand Down Expand Up @@ -557,6 +578,14 @@ def on_settings_migrate(self, target, current=None):
self._settings.set(["autoOnTriggerGCodeCommands"], cur_autoOnCommands)
self._settings.remove(["autoOnCommands"])

if current < 3:
# v3 adds support for multiple sensing methods
cur_enableSensing = self._settings.get_boolean(["enableSensing"])
if cur_enableSensing is not None and cur_enableSensing:
self._logger.info("Migrating Setting: enableSensing=True -> sensingMethod=GPIO")
self._settings.set(["sensingMethod"], "GPIO")
self._settings.remove(["enableSensing"])

def get_template_configs(self):
return [
dict(type="settings", custom_bindings=False)
Expand Down
24 changes: 18 additions & 6 deletions octoprint_psucontrol/templates/psucontrol_settings.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</label>
</div>
</div>
<!-- ko if: settings.plugins.psucontrol.switchingMethod() === "GPIO" || settings.plugins.psucontrol.enableSensing() -->
<!-- ko if: settings.plugins.psucontrol.switchingMethod() === "GPIO" || settings.plugins.psucontrol.sensingMethod() === "GPIO" -->
<div class="control-group">
<label class="control-label">GPIO Mode</label>
<div class="controls">
Expand Down Expand Up @@ -96,15 +96,19 @@

<h4>Sensing</h4>
<div class="control-group">
<label class="control-label">Sensing Method</label>
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: settings.plugins.psucontrol.enableSensing"> Get PSU on/off state via GPIO (Recommended)
</label>
<select data-bind="value: settings.plugins.psucontrol.sensingMethod">
<option value="">Select Method...</option>
<option value="">Internal</option>
<option value="GPIO">GPIO Pin</option>
<option value="SYSTEM">System Command</option>
</select>
</div>
</div>
<!-- ko if: settings.plugins.psucontrol.enableSensing() -->
<!-- ko if: settings.plugins.psucontrol.sensingMethod() === "GPIO" -->
<div class="control-group">
<label class="control-label">Sense GPIO Pin</label>
<label class="control-label">Sensing GPIO Pin</label>
<div class="controls">
<input type="number" min="0" class="input-mini" data-bind="value: settings.plugins.psucontrol.senseGPIOPin">
<select data-bind="value: settings.plugins.psucontrol.senseGPIOPinPUD" class="input-medium">
Expand All @@ -116,6 +120,14 @@
</div>
</div>
<!-- /ko -->
<!-- ko if: settings.plugins.psucontrol.sensingMethod() === "SYSTEM" -->
<div class="control-group">
<label class="control-label">Sensing System Command</label>
<div class="controls">
<input type="text" class="input-block-level" data-bind="value: settings.plugins.psucontrol.senseSystemCommand">
</div>
</div>
<!-- /ko -->
<br />

<h4>Power On Options</h4>
Expand Down

0 comments on commit fd4f99c

Please sign in to comment.