real-time icons and buttons state

This commit is contained in:
Devaev Maxim
2018-07-17 00:44:38 +03:00
parent caf7f4c99b
commit 514aac8d82
7 changed files with 72 additions and 21 deletions

View File

@@ -12,6 +12,7 @@
<script src="js/keyboard.js"></script>
<script src="js/mouse.js"></script>
<script src="js/hid.js"></script>
<script src="js/msd.js"></script>
<script src="js/session.js"></script>
<script src="js/main.js"></script>

View File

@@ -1,10 +1,22 @@
var atx = new function() {
this.setLedsState = function(leds) {
$("atx-power-led").className = (leds.power ? "led-on" : "led-off");
$("atx-hdd-led").className = (leds.hdd ? "led-hdd-busy" : "led-off");
this.loadInitialState = function() {
var http = tools.makeRequest("GET", "/kvmd/atx", function() {
if (http.readyState === 4) {
if (http.status === 200) {
atx.setButtonsBusy(JSON.parse(http.responseText).result.busy);
} else {
setTimeout(atx.loadInitialState, 1000);
}
}
});
};
this.clearLeds = function() {
this.setState = function(state) {
$("atx-power-led").className = (state.leds.power ? "led-on" : "led-off");
$("atx-hdd-led").className = (state.leds.hdd ? "led-hdd-busy" : "led-off");
};
this.clearState = function() {
[
"atx-power-led",
"atx-hdd-led",
@@ -35,7 +47,7 @@ var atx = new function() {
}
if (button && confirm(confirm_msg)) {
__setButtonsBusy(true);
// atx.setButtonsBusy(true);
var http = tools.makeRequest("POST", "/kvmd/atx/click?button=" + button, function() {
if (http.readyState === 4) {
if (http.status === 409) {
@@ -43,13 +55,13 @@ var atx = new function() {
} else if (http.status !== 200) {
alert("Click error:", http.responseText);
}
__setButtonsBusy(false);
// atx.setButtonsBusy(false);
}
}, timeout);
}
};
var __setButtonsBusy = function(busy) {
this.setButtonsBusy = function(busy) {
[
"atx-power-button",
"atx-power-button-long",

24
kvmd/web/js/msd.js Normal file
View File

@@ -0,0 +1,24 @@
var msd = new function() {
this.loadInitialState = function() {
var http = tools.makeRequest("GET", "/kvmd/msd", function() {
if (http.readyState === 4) {
if (http.status === 200) {
msd.setState(JSON.parse(http.responseText).result);
} else {
setTimeout(msd.loadInitialState, 1000);
}
}
});
};
this.setState = function(state) {
if (state.connected_to == "server") {
cls = "led-on";
} else if (state.busy) {
cls = "led-msd-writing";
} else {
cls = "led-off";
}
$("msd-led").className = cls;
};
};

View File

@@ -13,6 +13,8 @@ var session = new function() {
var __wsOpenHandler = function(event) {
tools.debug("WebSocket opened:", event);
atx.loadInitialState();
msd.loadInitialState();
hid.installCapture(__ws);
__missed_heartbeats = 0;
__ping_timer = setInterval(__pingServer, 1000);
@@ -25,7 +27,11 @@ var session = new function() {
__missed_heartbeats = 0;
} else if (event.msg_type === "event") {
if (event.msg.event === "atx_state") {
atx.setLedsState(event.msg.event_attrs.leds);
atx.setState(event.msg.event_attrs);
// } else if (event.msg.event === "atx_click") {
// atx.setButtonsBusy(event.msg.event_attrs.button);
} else if (event.msg.event === "msd_state") {
msd.setState(event.msg.event_attrs);
}
}
};
@@ -46,7 +52,7 @@ var session = new function() {
__ping_timer = null;
}
hid.clearCapture();
atx.clearLeds();
atx.clearState();
__ws = null;
setTimeout(session.startPoller, 1000);
};