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

69 lines
2.3 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
2019-11-20 22:02:01 +01:00
self.mystromswitchEnabled = ko.observable();
2019-11-20 22:07:47 +01:00
self.mystromswitchPowerValue = document.getElementById("mystromswitchPowerValue")
2019-11-20 22:02:01 +01:00
self.onToggleRelayEvent = function(){
$.ajax({
url: API_BASEURL + "/api/plugin/mystromswitch",
type: "POST",
dataType: "json",
data: JSON.stringify({
command: "toggleRelais",
}),
contentType: "application/json; charset=UTF-8"
})
}
2019-11-19 21:55:31 +01:00
self.onmystromswitchEvent = function() {
if (self.mystromswitchEnabled()) {
2019-11-19 21:29:32 +01:00
$.ajax({
2019-11-19 21:55:31 +01:00
url: API_BASEURL + "plugin/mystromswitch",
2019-11-19 21:29:32 +01:00
type: "POST",
dataType: "json",
data: JSON.stringify({
command: "enableRelais",
2019-11-19 21:29:32 +01:00
}),
contentType: "application/json; charset=UTF-8"
})
} else {
$.ajax({
2019-11-19 21:55:31 +01:00
url: API_BASEURL + "plugin/mystromswitch",
2019-11-19 21:29:32 +01:00
type: "POST",
dataType: "json",
data: JSON.stringify({
command: "disableRelais",
2019-11-19 21:29:32 +01:00
}),
contentType: "application/json; charset=UTF-8"
})
}
}
2019-11-19 21:55:31 +01:00
self.mystromswitchEnabled.subscribe(self.onmystromswitchEvent, 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;
}
2019-11-20 22:12:45 +01:00
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";
}
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
]);
});