Skip to content

Commit

Permalink
add sending gcode commands before off and after on, #160, #170
Browse files Browse the repository at this point in the history
  • Loading branch information
jneilliii committed Feb 28, 2021
1 parent 0aa3f35 commit b006a6b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
29 changes: 27 additions & 2 deletions octoprint_tplinksmartplug/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion octoprint_tplinksmartplug/static/js/tplinksmartplug.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@
<td><div class="controls"><label class="control-label">{{ _('GCODE Off Delay') }}</label><div class="input-append" data-toggle="tooltip" data-bind="tooltip: {container: '#TPLinkPlugEditor'}" title="{{ _('Amount of time to wait after receiving GCODE command for powering off this plug.') }}"><input type="number" data-bind="value: gcodeOffDelay, enable: gcodeEnabled()" class="input input-mini" disabled /><span class="add-on">{{ _('secs') }}</span></div></div></td>
<td></td>
</tr>
<tr>
<td colspan="3" style="vertical-align: bottom"><div class="controls"><label class="checkbox"><input type="checkbox" title="{{ _('Run gcode command after powering on this plug.') }}" data-toggle="tooltip" data-bind="checked: gcodeCmdOn, tooltip: {container: '#TPLinkPlugEditor'}"/> {{ _('Run GCODE Commands After On') }}</label></div></td>
</tr>
<tr>
<td colspan="2" style="vertical-align: bottom"><textarea type="text" title="{{ _('GCODE command to run after powering on this plug. Separate multiple commands using a ,.') }}" data-toggle="tooltip" data-bind="value: gcodeRunCmdOn, enable: gcodeCmdOn(), tooltip: {container: '#TPLinkPlugEditor'}" class="input-block-level" disabled></textarea></td>
</tr>
<tr>
<td colspan="3" style="vertical-align: bottom"><div class="controls"><label class="checkbox"><input type="checkbox" title="{{ _('Run gcode command before powering off this plug.') }}" data-toggle="tooltip" data-bind="checked: gcodeCmdOff, tooltip: {container: '#TPLinkPlugEditor'}"/> {{ _('Run GCODE Commands Before Off') }}</label></div></td>
</tr>
<tr>
<td colspan="2" style="vertical-align: bottom"><textarea type="text" title="{{ _('GCODE command to run before powering off this plug.') }}" data-toggle="tooltip" data-bind="value: gcodeRunCmdOff, enable: gcodeCmdOff(), tooltip: {container: '#TPLinkPlugEditor'}" class="input-block-level" disabled></textarea></td>
</tr>
<tr>
<td colspan="2" style="vertical-align: bottom"><div class="controls"><label class="checkbox"><input type="checkbox" title="{{ _('Run system command after powering on this plug and waiting configured delay.') }}" data-toggle="tooltip" data-bind="checked: sysCmdOn, tooltip: {container: '#TPLinkPlugEditor'}"/> {{ _('Run System Command After On') }}</label><input type="text" title="{{ _('System command to run after powering on this plug.') }}" data-toggle="tooltip" data-bind="value: sysRunCmdOn, enable: sysCmdOn(), tooltip: {container: '#TPLinkPlugEditor'}" class="input-block-level" disabled /></div></td>
<td style="vertical-align: bottom"><div class="controls" data-toggle="tooltip" data-bind="visible: sysCmdOn"><label class="control-label">{{ _('Delay') }}</label><div class="input-append" data-toggle="tooltip" data-bind="tooltip: {container: '#TPLinkPlugEditor'}" title="{{ _('Amount of time to wait before running configured system command after power on.') }}"><input type="number" data-bind="value: sysCmdOnDelay, enable: sysCmdOn()" class="input input-mini" disabled /><span class="add-on">{{ _('secs') }}</span></div></div></td>
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 = "OctoPrint-TPLinkSmartplug"

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

# 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 b006a6b

Please sign in to comment.