Skip to content

Commit

Permalink
0.9.16
Browse files Browse the repository at this point in the history
- Change socket receive to grab 1024 chunks until full response is received.
- Added strip support.
- Added energy data logging and graph support via new tab.
- Added temperature runaway protection.
  • Loading branch information
jneilliii authored May 3, 2019
1 parent 9a13944 commit aa9a379
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
20 changes: 9 additions & 11 deletions octoprint_tplinksmartplug/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def on_startup(self, host, port):
self._tplinksmartplug_logger.propagate = False

self.db_path = os.path.join(self.get_plugin_data_folder(),"energy_data.db")
if not os.path.exists(self.db_path):
self.db = sqlite3.connect(self.db_path)
cursor = self.db.cursor()
cursor.execute('''CREATE TABLE energy_data(id INTEGER PRIMARY KEY, ip TEXT, timestamp TEXT, current REAL, power REAL, total REAL, voltage REAL)''')
self.db.commit()
self.db.close()

def on_after_startup(self):
self._logger.info("TPLinkSmartplug loaded!")
Expand All @@ -49,7 +55,7 @@ def on_after_startup(self):
def get_settings_defaults(self):
return dict(
debug_logging = False,
arrSmartplugs = [{'ip':'','label':'','icon':'icon-bolt','displayWarning':True,'warnPrinting':False,'gcodeEnabled':False,'gcodeOnDelay':0,'gcodeOffDelay':0,'autoConnect':True,'autoConnectDelay':10.0,'autoDisconnect':True,'autoDisconnectDelay':0,'sysCmdOn':False,'sysRunCmdOn':'','sysCmdOnDelay':0,'sysCmdOff':False,'sysRunCmdOff':'','sysCmdOffDelay':0,'currentState':'unknown','btnColor':'#808080','useCountdownRules':False,'countdownOnDelay':0,'countdownOffDelay':0,'emeter':{'get_realtime':{}}}],
arrSmartplugs = [{'ip':'','label':'','icon':'icon-bolt','displayWarning':True,'warnPrinting':False,'thermal_runaway':False,'gcodeEnabled':False,'gcodeOnDelay':0,'gcodeOffDelay':0,'autoConnect':True,'autoConnectDelay':10.0,'autoDisconnect':True,'autoDisconnectDelay':0,'sysCmdOn':False,'sysRunCmdOn':'','sysCmdOnDelay':0,'sysCmdOff':False,'sysRunCmdOff':'','sysCmdOffDelay':0,'currentState':'unknown','btnColor':'#808080','useCountdownRules':False,'countdownOnDelay':0,'countdownOffDelay':0,'emeter':{'get_realtime':{}}}],
pollingInterval = 15,
pollingEnabled = False,
thermal_runaway_monitoring = False,
Expand All @@ -70,7 +76,7 @@ def on_settings_save(self, data):
self._tplinksmartplug_logger.setLevel(logging.INFO)

def get_settings_version(self):
return 10
return 9

def on_settings_migrate(self, target, current=None):
if current is None or current < 5:
Expand Down Expand Up @@ -102,15 +108,7 @@ def on_settings_migrate(self, target, current=None):
self._logger.info(arrSmartplugs_new)
self._settings.set(["arrSmartplugs"],arrSmartplugs_new)

if current is None or current < 9:
self.db_path = os.path.join(self.get_plugin_data_folder(),"energy_data.db")
self.db = sqlite3.connect(self.db_path)
cursor = self.db.cursor()
cursor.execute('''CREATE TABLE energy_data(id INTEGER PRIMARY KEY, ip TEXT, timestamp TEXT, current REAL, power REAL, total REAL, voltage REAL)''')
self.db.commit()
self.db.close()

if current is None or current < 10:
if current is not None and current < 9:
arrSmartplugs_new = []
for plug in self._settings.get(['arrSmartplugs']):
plug["thermal_runaway"] = False
Expand Down
6 changes: 3 additions & 3 deletions octoprint_tplinksmartplug/static/js/tplinksmartplug.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $(function() {
self.isPrinting = ko.observable(false);
self.selectedPlug = ko.observable();
self.processing = ko.observableArray([]);
self.plotted_graph_ip = ko.observable();
self.plotted_graph_ip = ko.observable(false);
self.plotted_graph_records = ko.observable(10);
self.plotted_graph_records_offset = ko.observable(0);
self.dictSmartplugs = ko.observableDictionary();
Expand Down Expand Up @@ -211,7 +211,7 @@ $(function() {
}).done(function(data){
console.log('Energy Data retrieved');
console.log(data);

//update plotly graph here.
var trace_current = {x:[],y:[],mode:'lines+markers',name:'Current (Amp)',xaxis: 'x2',yaxis: 'y2'};
var trace_power = {x:[],y:[],mode:'lines+markers',name:'Power (W)',xaxis: 'x3',yaxis: 'y3'};
Expand All @@ -228,7 +228,7 @@ $(function() {
//trace_voltage.x.push(row[0]);
//trace_voltage.y.push(row[4]);
});

var layout = {title:'TP-Link Smartplug Energy Data',
grid: {rows: 2, columns: 1, pattern: 'independent'},
xaxis: {
Expand Down
18 changes: 12 additions & 6 deletions octoprint_tplinksmartplug/templates/tplinksmartplug_tab.jinja2
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
<div class="row-fluid" id="tplinksmartplug_energy_graph" data-bind="visible: loginState.isUser()"></div>
<div class="row-fluid" data-bind="visible: loginState.isUser()">
<div class="span5">
<div class="span5">
<label class="control-label">{{ _('Plug to Graph') }}</label>
<div class="controls">
<select data-bind="options: filteredSmartplugs,optionsText: function(self){ return self.value()['label']; },optionsValue: function(self){ return self.value()['ip']; },value: plotted_graph_ip, event: { change: plotEnergyData}"></select>
<select data-bind="options: filteredSmartplugs,optionsText: function(self){ return self.value()['label']; },optionsValue: function(self){ return self.value()['ip']; },value: plotted_graph_ip"></select>
</div>
</div>
<div class="span3">
<div class="span2">
<label class="control-label">{{ _('Records') }}</label>
<div class="controls">
<input type="number" step="10" class="input-mini" data-bind="value: plotted_graph_records, event: { change: plotEnergyData}"/>
<input type="number" step="10" class="input-mini" data-bind="value: plotted_graph_records"/>
</div>
</div>
<div class="span3">
<div class="span2">
<label class="control-label">{{ _('Offset') }}</label>
<div class="controls">
<input type="number" step="10" class="input-mini" data-bind="value: plotted_graph_records_offset, event: { change: plotEnergyData}"/>
<input type="number" step="10" class="input-mini" data-bind="value: plotted_graph_records_offset"/>
</div>
</div>
<div class="span3">
<div class="controls">
<label class="control-label">&nbsp;</label>
<button class="btn btn-primary" data-bind="click: plotEnergyData">Update</button>
</div>
</div>
</div>

0 comments on commit aa9a379

Please sign in to comment.