Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
bb9795b6df | |||
81b5f2e5c3 | |||
13e4540053 | |||
d97c000dd7 | |||
93ca39abdf | |||
29c2858dc5 | |||
c76712159f | |||
576a53fad1 | |||
0cab5c3bc6 | |||
8421bd2339 | |||
7a4d53d221 | |||
4bd7df899c |
@ -12,11 +12,14 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
|
||||
octoprint.plugin.AssetPlugin,
|
||||
octoprint.plugin.TemplatePlugin,
|
||||
octoprint.plugin.StartupPlugin,
|
||||
octoprint.plugin.SimpleApiPlugin,
|
||||
octoprint.plugin.ShutdownPlugin):
|
||||
|
||||
def __init__(self):
|
||||
self.ip = None
|
||||
self.intervall = 1
|
||||
self.onOffButtonEnabled = False
|
||||
|
||||
self._timer = None
|
||||
|
||||
self.ctx = ssl.create_default_context()
|
||||
@ -29,6 +32,9 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
|
||||
|
||||
self.intervall = self._settings.get_int(["intervall"])
|
||||
self._logger.debug("intervall: %s" % self.intervall)
|
||||
|
||||
self.onOffButtonEnabled = self._settings.get_boolean(["onOffButtonEnabled"])
|
||||
self._logger.info("onOffButtonEnabled: %s" % self.onOffButtonEnabled)
|
||||
self._timer_start()
|
||||
|
||||
def get_assets(self):
|
||||
@ -56,13 +62,53 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
|
||||
try:
|
||||
request = requests.get(
|
||||
'http://{}/report'.format(self.ip), timeout=1)
|
||||
self._plugin_manager.send_plugin_message(self._identifier,
|
||||
request.json())
|
||||
data = request.json()
|
||||
data["onOffButtonEnabled"] = self.onOffButtonEnabled
|
||||
self._plugin_manager.send_plugin_message(self._identifier, data)
|
||||
except (requests.exceptions.ConnectionError, ValueError):
|
||||
self._logger.info('Connection Error Host: {}'.format(self.ip))
|
||||
else:
|
||||
self._logger.info("Ip is None")
|
||||
|
||||
def _setRelaisState(self, newState):
|
||||
try:
|
||||
value = '0'
|
||||
if (newState == True):
|
||||
value = '1'
|
||||
request = requests.get(
|
||||
'http://{}/relay'.format(self.ip), params={'state': value}, timeout=1)
|
||||
if not request.status_code == 200:
|
||||
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")
|
||||
|
||||
def _toggleRelay(self):
|
||||
try:
|
||||
request = requests.get(
|
||||
'http://{}/toggle'.format(self.ip), timeout=1)
|
||||
if not request.status_code == 200:
|
||||
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")
|
||||
|
||||
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):
|
||||
pass
|
||||
|
||||
@ -70,15 +116,19 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
|
||||
pass
|
||||
|
||||
def on_settings_migrate(self, target, current):
|
||||
if target > current:
|
||||
if current <= 1:
|
||||
self.onOffButtonEnabled = False
|
||||
pass
|
||||
|
||||
def get_settings_version(self):
|
||||
return 1
|
||||
return 2
|
||||
|
||||
def get_settings_defaults(self):
|
||||
return dict(
|
||||
ip=None,
|
||||
intervall=1
|
||||
intervall=1,
|
||||
onOffButtonEnabled=False
|
||||
)
|
||||
|
||||
def on_settings_save(self, data):
|
||||
|
@ -6,44 +6,36 @@ $(function() {
|
||||
self.settings = parameters[1];
|
||||
self.printer = parameters[2];
|
||||
|
||||
self.mystromswitchEnabled = ko.observable();
|
||||
self.onOffButtonEnabled = ko.observable();
|
||||
self.mystromswitchPowerValue = document.getElementById("mystromswitchPowerValue")
|
||||
|
||||
self.onmystromswitchEvent = function() {
|
||||
if (self.mystromswitchEnabled()) {
|
||||
self.onToggleRelayEvent = function(){
|
||||
$.ajax({
|
||||
url: API_BASEURL + "plugin/mystromswitch",
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
data: JSON.stringify({
|
||||
command: "enable",
|
||||
eventView : false
|
||||
command: "toggleRelais",
|
||||
}),
|
||||
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) {
|
||||
if (plugin != "mystromswitch" && plugin != "octoprint_mystromswitch") {
|
||||
return;
|
||||
}
|
||||
self.mystromswitchEnabled(data.mystromswitchEnabled);
|
||||
if (data.power != null) {
|
||||
self.mystromswitchPowerValue.innerHTML = "Power Consumption "+data.power.toFixed(2)+"W"
|
||||
self.onOffButtonEnabled(data.onOffButtonEnabled);
|
||||
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,12 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="control-group">
|
||||
<div class="controls">
|
||||
<label class="checkbox">
|
||||
<input type="checkbox" id="mystromswitchShowToggleButton" data-bind="checked: settings.plugins.mystromswitch.onOffButtonEnabled">Toggle Button Enabled
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
@ -1,3 +1,4 @@
|
||||
<div class="sidebar_plugin_mystromswitch">
|
||||
<label class="control-label" id="mystromswitchPowerValue">Powerconsumption 0.0W</label>
|
||||
<button class="btn btn-primary" data-bind="click: onToggleRelayEvent, visible : onOffButtonEnabled">{{ _('Toggle Relais') }}</button>
|
||||
</div>
|
||||
|
2
setup.py
2
setup.py
@ -14,7 +14,7 @@ plugin_package = "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
|
||||
plugin_version = "0.1.0"
|
||||
plugin_version = "0.2.0"
|
||||
|
||||
# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
|
||||
# module
|
||||
|
Reference in New Issue
Block a user