Skip to content

Commit

Permalink
0.9.7
Browse files Browse the repository at this point in the history
  • Loading branch information
jneilliii committed Nov 3, 2018
1 parent 01a69e5 commit dc4bd55
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 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.9.7] - 2018-11-03
### Added
- Added energy monitoring support for the HS-110 devices. Plugs' statuses will be checked on startup, on toggle of on/off, on print progress, and on polling interval configured in settings.

## [0.9.6] - 2018-08-05
### Added
- Added countdown timer option to plug settings to allow using the plug's built in functions for delayed power on/off.
Expand Down Expand Up @@ -135,6 +139,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- Initial release.

[0.9.7]: https://github.com/jneilliii/OctoPrint-TPLinkSmartplug/tree/0.9.7
[0.9.6]: https://github.com/jneilliii/OctoPrint-TPLinkSmartplug/tree/0.9.6
[0.9.5]: https://github.com/jneilliii/OctoPrint-TPLinkSmartplug/tree/0.9.5
[0.9.4]: https://github.com/jneilliii/OctoPrint-TPLinkSmartplug/tree/0.9.4
Expand Down
34 changes: 22 additions & 12 deletions octoprint_tplinksmartplug/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class tplinksmartplugPlugin(octoprint.plugin.SettingsPlugin,
octoprint.plugin.AssetPlugin,
octoprint.plugin.TemplatePlugin,
octoprint.plugin.SimpleApiPlugin,
octoprint.plugin.StartupPlugin):
octoprint.plugin.StartupPlugin,
octoprint.plugin.ProgressPlugin):

def __init__(self):
self._logger = logging.getLogger("octoprint.plugins.tplinksmartplug")
Expand Down Expand Up @@ -73,7 +74,7 @@ def on_settings_migrate(self, target, current=None):

def get_assets(self):
return dict(
js=["js/tplinksmartplug.js"],
js=["js/tplinksmartplug.js","js/knockout-bootstrap.min.js"],
css=["css/tplinksmartplug.css"]
)

Expand All @@ -85,6 +86,12 @@ def get_template_configs(self):
dict(type="settings", custom_bindings=True)
]

def on_print_progress(self, storage, path, progress):
self._tplinksmartplug_logger.debug("Checking statuses during print progress (%s)." % progress)
for plug in self._settings.get(["arrSmartplugs"]):
if plug["emeter"] and plug["emeter"] != {}:
self.check_status(plug["ip"])

##~~ SimpleApiPlugin mixin

def turn_on(self, plugip):
Expand Down Expand Up @@ -131,16 +138,14 @@ def check_status(self, plugip):
self._tplinksmartplug_logger.debug("Checking status of %s." % plugip)
if plugip != "":
today = datetime.today()
response = self.sendCommand('{"system":{"get_sysinfo":{}},"emeter":{"get_realtime":{},"get_daystat":{"month":%d,"year":%d}}}' % (today.month, today.year),plugip)
self._tplinksmartplug_logger.info("%s %s" % (response["emeter"], plugip))

if response["emeter"]["err_code"] != 0:
self._tplinksmartplug_logger.info("energy error: %s" % response["emeter"]["err_msg"])
self._plugin_manager.send_plugin_message(self._identifier, dict(emeter=dict(),ip=plugip))
else:
check_status_cmnd = '{"system":{"get_sysinfo":{}},"emeter":{"get_realtime":{},"get_daystat":{"month":%d,"year":%d}}}' % (today.month, today.year)
self._tplinksmartplug_logger.info(check_status_cmnd)
response = self.sendCommand(check_status_cmnd, plugip)
self._tplinksmartplug_logger.info(response)
if not self.lookup(response, *["emeter","err_code"]):
self._plugin_manager.send_plugin_message(self._identifier, dict(emeter=response["emeter"],ip=plugip))

chk = response["system"]["get_sysinfo"]["relay_state"]
chk = self.lookup(response,*["system","get_sysinfo","relay_state"])
if chk == 1:
self._plugin_manager.send_plugin_message(self._identifier, dict(currentState="on",ip=plugip))
elif chk == 0:
Expand All @@ -166,6 +171,11 @@ def on_api_command(self, command, data):

##~~ Utilities

def lookup(self, dic, key, *keys):
if keys:
return self.lookup(dic.get(key, {}), *keys)
return dic.get(key)

def plug_search(self, list, key, value):
for item in list:
if item[key] == value:
Expand Down Expand Up @@ -216,7 +226,7 @@ def sendCommand(self, cmd, plugip):
self._tplinksmartplug_logger.debug("Hostname %s is valid." % plugip)
except (socket.herror, socket.gaierror):
self._tplinksmartplug_logger.debug("Invalid hostname %s." % plugip)
return {"system":{"get_sysinfo":{"relay_state":3}}}
return {"system":{"get_sysinfo":{"relay_state":3}},"emeter":{"err_code": True}}

try:
sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Expand All @@ -230,7 +240,7 @@ def sendCommand(self, cmd, plugip):
return json.loads(self.decrypt(data[4:]))
except socket.error:
self._tplinksmartplug_logger.debug("Could not connect to %s." % plugip)
return {"system":{"get_sysinfo":{"relay_state":3}}}
return {"system":{"get_sysinfo":{"relay_state":3}},"emeter":{"err_code": True}}

##~~ Gcode processing hook

Expand Down
7 changes: 7 additions & 0 deletions octoprint_tplinksmartplug/static/js/knockout-bootstrap.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions octoprint_tplinksmartplug/static/js/tplinksmartplug.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ $(function() {
self.isPrinting = ko.observable(false);
self.selectedPlug = ko.observable();
self.processing = ko.observableArray([]);
self.energy_data = function(data){
var output = data.label() + '\n';
/* self.energy_data = function(data){
var output = data.label() + '<br/>';
var energy_data = ko.toJS(data.emeter);
for (x in energy_data){output += x + ': ' + energy_data[x] + '\n'};
for (x in energy_data){output += x + ': ' + energy_data[x] + '<br/>'};
return output;
}
} */

self.onStartup = function() {
var element = $("#state").find(".accordion-inner .progress");
if (element.length) {
element.before('<div class="row-fluid" data-bind="foreach: settings.settings.plugins.tplinksmartplug.arrSmartplugs"><!-- ko if: emeter.get_realtime --><div class="row-fluid"><hr>Plug: <strong data-bind="text: label"></strong><br>Current Power: <strong data-bind="text: emeter.get_realtime.power"></strong> W<br>Total Consumption: <strong data-bind="text: emeter.get_realtime.total"></strong> kWh</div><!-- /ko --></div>');
}
};

self.onBeforeBinding = function() {
self.arrSmartplugs(self.settings.settings.plugins.tplinksmartplug.arrSmartplugs());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- ko foreach: settings.settings.plugins.tplinksmartplug.arrSmartplugs -->
<a class="pull-right" href=#" data-toggle="tooltip" data-bind="click: $root.toggleRelay,visible: $root.loginState.loggedIn(),attr: {title: $root.energy_data($data)}" style="display: none;float: left;"><i class="icon" data-bind="css: [currentState(), icon(),(($root.processing().indexOf(ip()) > -1) ? 'icon-spin' : '')].join(' ')"></i></a>
<a class="pull-right" href=#" data-toggle="tooltip" data-html="true" data-bind="click: $root.toggleRelay, visible: $root.loginState.loggedIn(), tooltip: {title: label, placement: 'bottom'}" style="display: none;float: left;"><i class="icon" data-bind="css: [currentState(), icon(),(($root.processing().indexOf(ip()) > -1) ? 'icon-spin' : '')].join(' ')"></i></a>
<!-- /ko -->

<div id="TPLinkSmartPlugWarning" data-bind="with: selectedPlug" class="modal hide fade">
Expand Down

0 comments on commit dc4bd55

Please sign in to comment.