OctoPrint-MyStromSwitch/octoprint_mystromswitch/static/js/mystromswitch.js

92 lines
3.4 KiB
JavaScript
Raw Normal View History

2019-11-19 21:29:32 +01:00
$(function() {
2019-11-19 21:55:31 +01:00
function mystromswitchViewModel(parameters) {
2019-11-19 21:29:32 +01:00
var self = this;
self.loginState = parameters[0];
self.settings = parameters[1];
self.printer = parameters[2];
2019-11-20 20:46:04 +01:00
self.onOffButtonEnabled = ko.observable();
self.showShutdownOctopiOption = ko.observable();
self.showPowerOffPrintFinishOption = ko.observable();
2019-11-20 22:07:47 +01:00
self.mystromswitchPowerValue = document.getElementById("mystromswitchPowerValue")
self.mystromswitchEnergyValue = document.getElementById("mystromswitchEnergyValue")
2019-11-20 22:02:01 +01:00
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() {
//}
//self.onOffButtonEnabled.subscribe(self.onmystromswitchEvent, self);
self.onAutomaticShutdownEnabledChanged = function(){
var cmd = "disableShutdownAfterFinish";
if (self.automaticShutdownEnabled()) {
var cmd = "enableShutdownAfterFinish";
}
$.ajax({
url: API_BASEURL + "plugin/mystromswitch",
type: "POST",
dataType: "json",
data: JSON.stringify({
command: cmd
}),
contentType: "application/json; charset=UTF-8"
})
}
self.onAutomaticPowerOffEnabledChanged = function(){
var cmd = "disablePowerOffAfterFinish";
if (self.automaticShutdownEnabled()) {
var cmd = "enablePowerOffAfterFinish";
}
$.ajax({
url: API_BASEURL + "plugin/mystromswitch",
type: "POST",
dataType: "json",
data: JSON.stringify({
command: cmd
}),
contentType: "application/json; charset=UTF-8"
})
2019-11-19 21:29:32 +01:00
}
//self.automaticShutdownEnabled.subscribe(self.onAutomaticShutdownEnabledChanged,self);
//self.automaticPowerOffEnabled.subscribe(self.onAutomaticPowerOffEnabledChanged,self);
2019-11-19 21:29:32 +01:00
self.onDataUpdaterPluginMessage = function(plugin, data) {
2019-11-19 21:55:31 +01:00
if (plugin != "mystromswitch" && plugin != "octoprint_mystromswitch") {
2019-11-19 21:29:32 +01:00
return;
}
self.onOffButtonEnabled(data.onOffButtonEnabled);
self.showShutdownOctopiOption(data.showShutdownOctopiOption);
self.showPowerOffPrintFinishOption(data.showPowerOffPrintFinishOption);
self.mystromswitchEnergyValue.innerHTML = "Energy: "+data.energy.toFixed(1)+"Wh"
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";
}else{
self.mystromswitchPowerValue.innerHTML = "myStrom switch not reachable"
}
2019-11-19 21:29:32 +01:00
}
}
OCTOPRINT_VIEWMODELS.push([
2019-11-19 21:55:31 +01:00
mystromswitchViewModel,
2019-11-19 21:29:32 +01:00
["loginStateViewModel", "settingsViewModel", "printerStateViewModel"],
2019-11-19 21:55:31 +01:00
$(".sidebar_plugin_mystromswitch").get(0)
2019-11-19 21:29:32 +01:00
]);
});