Skip to content

Commit

Permalink
Merge pull request #32 from jneilliii/python_timer
Browse files Browse the repository at this point in the history
Server Side Processing
  • Loading branch information
jneilliii authored Dec 22, 2017
2 parents ee4774e + cc376d8 commit c727bda
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 39 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [0.7.3] - 2017-12-21
### Changed
- Moved all command processing to server side to resolve gcode processing issues when web front-end wasn't loaded.

## [0.7.2] - 2017-10-24
### Fixed
- Thanks to Gina, really fix the issue with software upgrade where viewmodel binding was breaking.
Expand Down Expand Up @@ -84,6 +88,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- Initial release.

[0.7.3]: https://github.com/jneilliii/OctoPrint-TPLinkSmartplug/tree/0.7.3
[0.7.2]: https://github.com/jneilliii/OctoPrint-TPLinkSmartplug/tree/0.7.2
[0.7.1]: https://github.com/jneilliii/OctoPrint-TPLinkSmartplug/tree/0.7.1
[0.7.0]: https://github.com/jneilliii/OctoPrint-TPLinkSmartplug/tree/0.7.0
Expand Down
51 changes: 36 additions & 15 deletions octoprint_tplinksmartplug/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
from octoprint.server import user_permission
import socket
import json
import time
import logging
import os
import re
import threading
import time

class tplinksmartplugPlugin(octoprint.plugin.SettingsPlugin,
octoprint.plugin.AssetPlugin,
Expand Down Expand Up @@ -84,12 +85,28 @@ def get_template_configs(self):

def turn_on(self, plugip):
self._tplinksmartplug_logger.debug("Turning on %s." % plugip)
plug = self.plug_search(self._settings.get(["arrSmartplugs"]),"ip",plugip)
self._tplinksmartplug_logger.debug(plug)
chk = self.sendCommand("on",plugip)["system"]["set_relay_state"]["err_code"]
if chk == 0:
self.check_status(plugip)
if plug["autoConnect"]:
t = threading.Timer(int(plug["autoConnectDelay"]),self._printer.connect)
t.start()
if plug["sysCmdOn"]:
t = threading.Timer(int(plug["sysCmdOnDelay"]),os.system,args=[plug["sysRunCmdOn"]])
t.start()

def turn_off(self, plugip):
self._tplinksmartplug_logger.debug("Turning off %s." % plugip)
plug = self.plug_search(self._settings.get(["arrSmartplugs"]),"ip",plugip)
self._tplinksmartplug_logger.debug(plug)
if plug["sysCmdOff"]:
t = threading.Timer(int(plug["sysCmdOffDelay"]),os.system,args=[plug["sysRunCmdOff"]])
t.start()
if plug["autoDisconnect"]:
self._printer.disconnect()
time.sleep(int(plug["autoDisconnectDelay"]))
chk = self.sendCommand("off",plugip)["system"]["set_relay_state"]["err_code"]
if chk == 0:
self.check_status(plugip)
Expand Down Expand Up @@ -121,18 +138,14 @@ def on_api_command(self, command, data):
self.turn_off("{ip}".format(**data))
elif command == 'checkStatus':
self.check_status("{ip}".format(**data))
elif command == 'connectPrinter':
self._tplinksmartplug_logger.debug("Connecting printer.")
self._printer.connect()
elif command == 'disconnectPrinter':
self._tplinksmartplug_logger.debug("Disconnecting printer.")
self._printer.disconnect()
elif command == 'sysCommand':
self._tplinksmartplug_logger.debug("Running system command %s." % "{cmd}".format(**data))
os.system("{cmd}".format(**data))

##~~ Utilities

def plug_search(self, list, key, value):
for item in list:
if item[key] == value:
return item

def encrypt(self, string):
key = 171
result = "\0\0\0"+chr(len(string))
Expand Down Expand Up @@ -198,15 +211,23 @@ def sendCommand(self, cmd, plugip):

def processGCODE(self, comm_instance, phase, cmd, cmd_type, gcode, *args, **kwargs):
if gcode:
if cmd.startswith("M80"):
if cmd.startswith("M80"):
plugip = re.sub(r'^M80\s?', '', cmd)
self._plugin_manager.send_plugin_message(self._identifier, dict(currentState="unknown",gcodeon=True,ip=plugip))
self._tplinksmartplug_logger.debug("Received M80 command, attempting power on.")
self._tplinksmartplug_logger.debug("Received M80 command, attempting power on of %s." % plugip)
plug = self.plug_search(self._settings.get(["arrSmartplugs"]),"ip",plugip)
self._tplinksmartplug_logger.debug(plug)
if plug["gcodeEnabled"]:
t = threading.Timer(int(plug["gcodeOnDelay"]),self.turn_on,args=[plugip])
t.start()
return
elif cmd.startswith("M81"):
plugip = re.sub(r'^M81\s?', '', cmd)
self._plugin_manager.send_plugin_message(self._identifier, dict(currentState="unknown",gcodeoff=True,ip=plugip))
self._tplinksmartplug_logger.debug("Received M81 command, attempting power off.")
self._tplinksmartplug_logger.debug("Received M81 command, attempting power off of %s." % plugip)
plug = self.plug_search(self._settings.get(["arrSmartplugs"]),"ip",plugip)
self._tplinksmartplug_logger.debug(plug)
if plug["gcodeEnabled"]:
t = threading.Timer(int(plug["gcodeOffDelay"]),self.turn_off,args=[plugip])
t.start()
return
else:
return
Expand Down
24 changes: 2 additions & 22 deletions octoprint_tplinksmartplug/static/js/tplinksmartplug.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,6 @@ $(function() {
return item.ip() === data.ip;
}) || {'ip':data.ip,'currentState':'unknown','btnColor':'#808080'};

if (data.gcodeon && plug.gcodeEnabled()) {
setTimeout(function(){self.turnOn(plug)},plug.gcodeOnDelay()*1000);
return false;
}

if (data.gcodeoff && plug.gcodeEnabled()) {
setTimeout(function(){self.turnOff(plug)},plug.gcodeOffDelay()*1000);
return false;
}

if (plug.currentState != data.currentState) {
plug.currentState(data.currentState)
switch(data.currentState) {
Expand Down Expand Up @@ -118,12 +108,7 @@ $(function() {
if(data.sysCmdOn()){
setTimeout(function(){self.sysCommand(data.sysRunCmdOn())},data.sysCmdOnDelay()*1000);
}
if(data.autoConnect()){
self.sendTurnOn(data);
setTimeout(function(){self.connectPrinter()},data.autoConnectDelay()*1000);
} else {
self.sendTurnOn(data);
}
self.sendTurnOn(data);
}

self.sendTurnOn = function(data) {
Expand All @@ -148,12 +133,7 @@ $(function() {
if(data.sysCmdOff()){
setTimeout(function(){self.sysCommand(data.sysRunCmdOff())},data.sysCmdOffDelay()*1000);
}
if(data.autoDisconnect()){
self.disconnectPrinter();
setTimeout(function(){self.sendTurnOff(data);},data.autoDisconnectDelay()*1000);
} else {
self.sendTurnOff(data);
}
self.sendTurnOff(data);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
</tr>
</tbody>
</table>
<button data-bind='click: addPlug'>Add Plug</button>
<button data-bind='click: addPlug' class="btn btn-primary">Add Plug</button>
<div class="control-group">
<div class="controls">
<label class="checkbox">
Expand Down
Binary file added settings_general.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added settings_off.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added settings_on.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 = "0.7.2"
plugin_version = "0.7.3"

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

Please sign in to comment.