OctoPrint-MyStromSwitch/octoprint_mystromswitch/static/js/mystromswitch.js
2019-11-24 15:38:12 +01:00

69 lines
2.3 KiB
JavaScript

$(function() {
function mystromswitchViewModel(parameters) {
var self = this;
self.loginState = parameters[0];
self.settings = parameters[1];
self.printer = parameters[2];
self.mystromswitchEnabled = ko.observable();
self.mystromswitchPowerValue = document.getElementById("mystromswitchPowerValue")
self.onToggleRelayEvent = function(){
$.ajax({
url: API_BASEURL + "/plugin/mystromswitch",
type: "POST",
dataType: "json",
data: JSON.stringify({
command: "toggleRelais",
}),
contentType: "application/json; charset=UTF-8"
})
}
self.onmystromswitchEvent = function() {
if (self.mystromswitchEnabled()) {
$.ajax({
url: API_BASEURL + "plugin/mystromswitch",
type: "POST",
dataType: "json",
data: JSON.stringify({
command: "enableRelais",
}),
contentType: "application/json; charset=UTF-8"
})
} else {
$.ajax({
url: API_BASEURL + "plugin/mystromswitch",
type: "POST",
dataType: "json",
data: JSON.stringify({
command: "disableRelais",
}),
contentType: "application/json; charset=UTF-8"
})
}
}
self.mystromswitchEnabled.subscribe(self.onmystromswitchEvent, self);
self.onDataUpdaterPluginMessage = function(plugin, data) {
if (plugin != "mystromswitch" && plugin != "octoprint_mystromswitch") {
return;
}
self.mystromswitchEnabled(data.mystromswitchEnabled);
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";
}
}
}
OCTOPRINT_VIEWMODELS.push([
mystromswitchViewModel,
["loginStateViewModel", "settingsViewModel", "printerStateViewModel"],
$(".sidebar_plugin_mystromswitch").get(0)
]);
});