Compare commits
21 Commits
Author | SHA1 | Date | |
---|---|---|---|
1ec0b46060 | |||
6b041d7cca | |||
d2e1b00b3f | |||
5d002ffc88 | |||
722c39604e | |||
70355683b2 | |||
37c09bd288 | |||
c9d706e603 | |||
9eca03c29c | |||
bb9795b6df | |||
81b5f2e5c3 | |||
13e4540053 | |||
d97c000dd7 | |||
93ca39abdf | |||
29c2858dc5 | |||
c76712159f | |||
576a53fad1 | |||
0cab5c3bc6 | |||
8421bd2339 | |||
7a4d53d221 | |||
4bd7df899c |
@ -2,6 +2,7 @@
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
import ssl
|
import ssl
|
||||||
|
import time
|
||||||
|
|
||||||
import octoprint.plugin
|
import octoprint.plugin
|
||||||
import requests
|
import requests
|
||||||
@ -12,13 +13,22 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
|
|||||||
octoprint.plugin.AssetPlugin,
|
octoprint.plugin.AssetPlugin,
|
||||||
octoprint.plugin.TemplatePlugin,
|
octoprint.plugin.TemplatePlugin,
|
||||||
octoprint.plugin.StartupPlugin,
|
octoprint.plugin.StartupPlugin,
|
||||||
|
octoprint.plugin.SimpleApiPlugin,
|
||||||
octoprint.plugin.ShutdownPlugin):
|
octoprint.plugin.ShutdownPlugin):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.ip = None
|
self.ip = None
|
||||||
self.intervall = 1
|
self.intervall = 1
|
||||||
|
self.onOffButtonEnabled = False
|
||||||
|
self.powerOnOnStart = False
|
||||||
|
self.powerOffOnShutdown = False
|
||||||
|
self.powerOffDelay = 0
|
||||||
|
|
||||||
self._timer = None
|
self._timer = None
|
||||||
|
|
||||||
|
self.energy = 0
|
||||||
|
self.lastTimeStamp = 0
|
||||||
|
|
||||||
self.ctx = ssl.create_default_context()
|
self.ctx = ssl.create_default_context()
|
||||||
self.ctx.check_hostname = False
|
self.ctx.check_hostname = False
|
||||||
self.ctx.verify_mode = ssl.CERT_NONE
|
self.ctx.verify_mode = ssl.CERT_NONE
|
||||||
@ -29,6 +39,19 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
|
|||||||
|
|
||||||
self.intervall = self._settings.get_int(["intervall"])
|
self.intervall = self._settings.get_int(["intervall"])
|
||||||
self._logger.debug("intervall: %s" % self.intervall)
|
self._logger.debug("intervall: %s" % self.intervall)
|
||||||
|
|
||||||
|
self.onOffButtonEnabled = self._settings.get_boolean(["onOffButtonEnabled"])
|
||||||
|
self._logger.debug("onOffButtonEnabled: %s" % self.onOffButtonEnabled)
|
||||||
|
|
||||||
|
self.powerOnOnStart = self._settings.get_boolean(["powerOnOnStart"])
|
||||||
|
self._logger.debug("powerOnOnStart: %s" % self.powerOnOnStart)
|
||||||
|
|
||||||
|
self.powerOffOnShutdown = self._settings.get_boolean(["powerOffOnShutdown"])
|
||||||
|
self._logger.debug("powerOffOnShutdown: %s" % self.powerOffOnShutdown)
|
||||||
|
|
||||||
|
self.powerOffDelay = self._settings.get_int(["powerOffDelay"])
|
||||||
|
self._logger.debug("powerOffDelay: %s" % self.powerOffDelay)
|
||||||
|
|
||||||
self._timer_start()
|
self._timer_start()
|
||||||
|
|
||||||
def get_assets(self):
|
def get_assets(self):
|
||||||
@ -56,29 +79,127 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
|
|||||||
try:
|
try:
|
||||||
request = requests.get(
|
request = requests.get(
|
||||||
'http://{}/report'.format(self.ip), timeout=1)
|
'http://{}/report'.format(self.ip), timeout=1)
|
||||||
self._plugin_manager.send_plugin_message(self._identifier,
|
if request.status_code == 200:
|
||||||
request.json())
|
timestamp = time.time()
|
||||||
|
data = request.json()
|
||||||
|
if not self.lastTimeStamp == 0:
|
||||||
|
intervall = timestamp - self.lastTimeStamp
|
||||||
|
# Energy in Wh
|
||||||
|
self.energy = self.energy + (intervall * data["power"] / 3600)
|
||||||
|
self.lastTimeStamp = timestamp
|
||||||
|
data["energy"] = self.energy
|
||||||
|
data["onOffButtonEnabled"] = self.onOffButtonEnabled
|
||||||
|
self._plugin_manager.send_plugin_message(self._identifier, data)
|
||||||
except (requests.exceptions.ConnectionError, ValueError):
|
except (requests.exceptions.ConnectionError, ValueError):
|
||||||
self._logger.info('Connection Error Host: {}'.format(self.ip))
|
self._logger.info('Connection Error Host: {}'.format(self.ip))
|
||||||
else:
|
else:
|
||||||
self._logger.info("Ip is None")
|
self._logger.info("Ip is None")
|
||||||
|
|
||||||
|
def _setRelaisState(self, newState):
|
||||||
|
nbRetry = 0
|
||||||
|
value = '0'
|
||||||
|
if (newState == True):
|
||||||
|
value = '1'
|
||||||
|
while nbRetry < 3:
|
||||||
|
try:
|
||||||
|
request = requests.get(
|
||||||
|
'http://{}/relay'.format(self.ip), params={'state': value}, timeout=1)
|
||||||
|
if request.status_code == 200:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
self._logger.info(
|
||||||
|
"Could not set new Relais State, Http Status Code: {}".format(request.status_code))
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
|
self._logger.info("Error during set Relais state")
|
||||||
|
nbRetry = nbRetry + 1
|
||||||
|
|
||||||
|
# Sets the switch to a specific inverse newState,
|
||||||
|
# waits for a specified amount of time (max 3h),
|
||||||
|
# then sets the the switch to the newState.
|
||||||
|
def _powerCycleRelais(self, newState, time):
|
||||||
|
nbRetry = 0
|
||||||
|
value = 'on'
|
||||||
|
if newState:
|
||||||
|
value = 'off'
|
||||||
|
while nbRetry < 3:
|
||||||
|
try:
|
||||||
|
request = requests.post(
|
||||||
|
'http://{}/timer'.format(self.ip), params={'mode': value, 'time': time}, timeout=1)
|
||||||
|
if request.status_code == 200:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
self._logger.info("Could not powerCycle Relais, Http Status Code: {}".format(request.status_code))
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
|
self._logger.info("Error during powerCycle Relais")
|
||||||
|
nbRetry = nbRetry + 1
|
||||||
|
|
||||||
|
def _toggleRelay(self):
|
||||||
|
nbRetry = 0
|
||||||
|
while nbRetry < 3:
|
||||||
|
try:
|
||||||
|
request = requests.get(
|
||||||
|
'http://{}/toggle'.format(self.ip), timeout=1)
|
||||||
|
if request.status_code == 200:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
self._logger.info("Could not toggle Relay State, Http Status Code: {}".format(request.status_code))
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
|
self._logger.info("Error during toggle Relais state")
|
||||||
|
nbRetry = nbRetry + 1
|
||||||
|
|
||||||
|
def on_api_command(self, command, data):
|
||||||
|
if command == "enableRelais":
|
||||||
|
self._logger.info("enableRelais")
|
||||||
|
self._setRelaisState(True)
|
||||||
|
elif command == "disableRelais":
|
||||||
|
self._logger.info("disableRelais")
|
||||||
|
self._setRelaisState(False)
|
||||||
|
elif command == "toggleRelais":
|
||||||
|
self._logger.info("toggleRelais")
|
||||||
|
self._toggleRelay()
|
||||||
|
|
||||||
|
def get_api_commands(self):
|
||||||
|
return dict(
|
||||||
|
enableRelais=[],
|
||||||
|
disableRelais=[],
|
||||||
|
toggleRelais=[]
|
||||||
|
)
|
||||||
|
|
||||||
def on_after_startup(self):
|
def on_after_startup(self):
|
||||||
pass
|
if self.powerOnOnStart:
|
||||||
|
self._logger.info("Turn on Relais on Start")
|
||||||
|
self._setRelaisState(True)
|
||||||
|
|
||||||
def on_shutdown(self):
|
def on_shutdown(self):
|
||||||
pass
|
if self.powerOffOnShutdown:
|
||||||
|
if self.powerOffDelay <= 0:
|
||||||
|
self._logger.info("Turn on Relais off Shutdown")
|
||||||
|
self._setRelaisState(False)
|
||||||
|
else:
|
||||||
|
self._logger.info("Turn off Relais on Shutdown Delayed")
|
||||||
|
self._powerCycleRelais(False, self.powerOffDelay)
|
||||||
|
|
||||||
def on_settings_migrate(self, target, current):
|
def on_settings_migrate(self, target, current):
|
||||||
pass
|
if target > current:
|
||||||
|
if current <= 1:
|
||||||
|
self.onOffButtonEnabled = False
|
||||||
|
pass
|
||||||
|
if current <= 2:
|
||||||
|
self.powerOnOnStart = False,
|
||||||
|
self.powerOffOnShutdown = False,
|
||||||
|
self.powerOffDelay = 0
|
||||||
|
|
||||||
def get_settings_version(self):
|
def get_settings_version(self):
|
||||||
return 1
|
return 3
|
||||||
|
|
||||||
def get_settings_defaults(self):
|
def get_settings_defaults(self):
|
||||||
return dict(
|
return dict(
|
||||||
ip=None,
|
ip=None,
|
||||||
intervall=1
|
intervall=1,
|
||||||
|
onOffButtonEnabled=False,
|
||||||
|
owerOnOnStart=False,
|
||||||
|
powerOffOnShutdown=False,
|
||||||
|
powerOffDelay=0
|
||||||
)
|
)
|
||||||
|
|
||||||
def on_settings_save(self, data):
|
def on_settings_save(self, data):
|
||||||
|
@ -6,45 +6,39 @@ $(function() {
|
|||||||
self.settings = parameters[1];
|
self.settings = parameters[1];
|
||||||
self.printer = parameters[2];
|
self.printer = parameters[2];
|
||||||
|
|
||||||
self.mystromswitchEnabled = ko.observable();
|
self.onOffButtonEnabled = ko.observable();
|
||||||
self.mystromswitchPowerValue = document.getElementById("mystromswitchPowerValue")
|
self.mystromswitchPowerValue = document.getElementById("mystromswitchPowerValue")
|
||||||
|
self.mystromswitchEnergyValue = document.getElementById("mystromswitchEnergyValue")
|
||||||
|
|
||||||
self.onmystromswitchEvent = function() {
|
self.onToggleRelayEvent = function(){
|
||||||
if (self.mystromswitchEnabled()) {
|
$.ajax({
|
||||||
$.ajax({
|
url: API_BASEURL + "plugin/mystromswitch",
|
||||||
url: API_BASEURL + "plugin/mystromswitch",
|
type: "POST",
|
||||||
type: "POST",
|
dataType: "json",
|
||||||
dataType: "json",
|
data: JSON.stringify({
|
||||||
data: JSON.stringify({
|
command: "toggleRelais",
|
||||||
command: "enable",
|
}),
|
||||||
eventView : false
|
contentType: "application/json; charset=UTF-8"
|
||||||
}),
|
})
|
||||||
contentType: "application/json; charset=UTF-8"
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
$.ajax({
|
|
||||||
url: API_BASEURL + "plugin/mystromswitch",
|
|
||||||
type: "POST",
|
|
||||||
dataType: "json",
|
|
||||||
data: JSON.stringify({
|
|
||||||
command: "disable",
|
|
||||||
eventView : false
|
|
||||||
}),
|
|
||||||
contentType: "application/json; charset=UTF-8"
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.mystromswitchEnabled.subscribe(self.onmystromswitchEvent, self);
|
self.onmystromswitchEvent = function() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
self.onOffButtonEnabled.subscribe(self.onmystromswitchEvent, self);
|
||||||
|
|
||||||
self.onDataUpdaterPluginMessage = function(plugin, data) {
|
self.onDataUpdaterPluginMessage = function(plugin, data) {
|
||||||
if (plugin != "mystromswitch" && plugin != "octoprint_mystromswitch") {
|
if (plugin != "mystromswitch" && plugin != "octoprint_mystromswitch") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.mystromswitchEnabled(data.mystromswitchEnabled);
|
self.onOffButtonEnabled(data.onOffButtonEnabled);
|
||||||
if (data.power != null) {
|
self.mystromswitchEnergyValue.innerHTML = "Energy: "+data.energy.toFixed(1)+"Wh"
|
||||||
self.mystromswitchPowerValue.innerHTML = "Power Consumption "+data.power.toFixed(2)+"W"
|
if(data.relay == false){
|
||||||
}
|
self.mystromswitchPowerValue.innerHTML = "Relay is off";
|
||||||
|
} else if (data.power != null) {
|
||||||
|
self.mystromswitchPowerValue.innerHTML = "Power Consumption "+data.power.toFixed(1)+"W";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,4 +17,35 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="control-group">
|
||||||
|
<div class="controls">
|
||||||
|
<label class="checkbox">
|
||||||
|
<input type="checkbox" data-bind="checked: settings.plugins.mystromswitch.onOffButtonEnabled">Toggle Button Enabled
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="control-group">
|
||||||
|
<div class="controls">
|
||||||
|
<label class="checkbox">
|
||||||
|
<input type="checkbox" data-bind="checked: settings.plugins.mystromswitch.powerOnOnStart">Turn relais ON on Octoprint start
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="control-group">
|
||||||
|
<div class="controls">
|
||||||
|
<label class="checkbox">
|
||||||
|
<input type="checkbox" data-bind="checked: settings.plugins.mystromswitch.powerOffOnShutdown">Turn relais OFF on Octoprint shutdown
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="control-group">
|
||||||
|
<label class="control-label">{{ _('Turn Relais Off Delay') }}</label>
|
||||||
|
<div class="controls">
|
||||||
|
<input type="number" class="input-block-level" data-bind="value: settings.plugins.mystromswitch.powerOffDelay">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
<div class="sidebar_plugin_mystromswitch">
|
<div class="sidebar_plugin_mystromswitch">
|
||||||
<label class="control-label" id="mystromswitchPowerValue">Powerconsumption 0.0W</label>
|
<label class="control-label" id="mystromswitchPowerValue">Powerconsumption 0.0W</label>
|
||||||
|
<label class="control-label" id="mystromswitchEnergyValue">Energy 0.0Wh</label>
|
||||||
|
<button class="btn btn-primary" data-bind="click: onToggleRelayEvent, visible : onOffButtonEnabled">{{ _('Toggle Relais') }}</button>
|
||||||
</div>
|
</div>
|
||||||
|
2
setup.py
2
setup.py
@ -14,7 +14,7 @@ plugin_package = "octoprint_mystromswitch"
|
|||||||
plugin_name = "OctoPrint-MyStromSwitch"
|
plugin_name = "OctoPrint-MyStromSwitch"
|
||||||
|
|
||||||
# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
|
# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
|
||||||
plugin_version = "0.1.0"
|
plugin_version = "0.4.0"
|
||||||
|
|
||||||
# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
|
# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
|
||||||
# module
|
# module
|
||||||
|
Reference in New Issue
Block a user