summaryrefslogtreecommitdiff
path: root/kvmd
diff options
context:
space:
mode:
Diffstat (limited to 'kvmd')
-rw-r--r--kvmd/kvmd/aioregion.py3
-rw-r--r--kvmd/kvmd/atx.py1
-rw-r--r--kvmd/kvmd/server.py28
-rw-r--r--kvmd/web/index.html1
-rw-r--r--kvmd/web/js/atx.js26
-rw-r--r--kvmd/web/js/msd.js24
-rw-r--r--kvmd/web/js/session.js10
7 files changed, 72 insertions, 21 deletions
diff --git a/kvmd/kvmd/aioregion.py b/kvmd/kvmd/aioregion.py
index c822216b..c2b335f4 100644
--- a/kvmd/kvmd/aioregion.py
+++ b/kvmd/kvmd/aioregion.py
@@ -14,6 +14,9 @@ class AioExclusiveRegion:
self.__exc_type = exc_type
self.__busy = False
+ def is_busy(self) -> bool:
+ return self.__busy
+
def enter(self) -> None:
if not self.__busy:
self.__busy = True
diff --git a/kvmd/kvmd/atx.py b/kvmd/kvmd/atx.py
index 23e50d1c..9ce97386 100644
--- a/kvmd/kvmd/atx.py
+++ b/kvmd/kvmd/atx.py
@@ -37,6 +37,7 @@ class Atx:
def get_state(self) -> Dict:
return {
+ "busy": self.__region.is_busy(),
"leds": {
"power": (not gpio.read(self.__power_led)),
"hdd": (not gpio.read(self.__hdd_led)),
diff --git a/kvmd/kvmd/server.py b/kvmd/kvmd/server.py
index e8d69cc7..e9536f05 100644
--- a/kvmd/kvmd/server.py
+++ b/kvmd/kvmd/server.py
@@ -174,14 +174,16 @@ class Server: # pylint: disable=too-many-instance-attributes
@_wrap_exceptions_for_web("Click error")
async def __atx_click_handler(self, request: aiohttp.web.Request) -> aiohttp.web.Response:
button = request.query.get("button")
- if button == "power":
- await self.__atx.click_power()
- elif button == "power_long":
- await self.__atx.click_power_long()
- elif button == "reset":
- await self.__atx.click_reset()
- else:
+ clicker = {
+ "power": self.__atx.click_power,
+ "power_long": self.__atx.click_power_long,
+ "reset": self.__atx.click_reset,
+ }.get(button)
+ if not clicker:
raise BadRequest("Missing or invalid 'button=%s'" % (button))
+ await self.__broadcast_event("atx_click", button=button) # type: ignore
+ await clicker()
+ await self.__broadcast_event("atx_click", button=None) # type: ignore
return _json({"clicked": button})
async def __msd_state_handler(self, _: aiohttp.web.Request) -> aiohttp.web.Response:
@@ -192,13 +194,15 @@ class Server: # pylint: disable=too-many-instance-attributes
to = request.query.get("to")
if to == "kvm":
await self.__msd.connect_to_kvm()
- await self.__broadcast_event("msd_state", state="connected_to_kvm") # type: ignore
+ state = self.__msd.get_state()
+ await self.__broadcast_event("msd_state", **state)
elif to == "server":
await self.__msd.connect_to_pc()
- await self.__broadcast_event("msd_state", state="connected_to_server") # type: ignore
+ state = self.__msd.get_state()
+ await self.__broadcast_event("msd_state", **state)
else:
raise BadRequest("Missing or invalid 'to=%s'" % (to))
- return _json(self.__msd.get_state())
+ return _json(state)
@_wrap_exceptions_for_web("Can't write data to mass-storage device")
async def __msd_write_handler(self, request: aiohttp.web.Request) -> aiohttp.web.Response:
@@ -216,7 +220,7 @@ class Server: # pylint: disable=too-many-instance-attributes
raise BadRequest("Missing 'image_data' field")
async with self.__msd:
- await self.__broadcast_event("msd_state", state="busy") # type: ignore
+ await self.__broadcast_event("msd_state", **self.__msd.get_state())
logger.info("Writing image %r to mass-storage device ...", image_name)
await self.__msd.write_image_info(image_name, False)
while True:
@@ -225,7 +229,7 @@ class Server: # pylint: disable=too-many-instance-attributes
break
written = await self.__msd.write_image_chunk(chunk)
await self.__msd.write_image_info(image_name, True)
- await self.__broadcast_event("msd_state", state="free") # type: ignore
+ await self.__broadcast_event("msd_state", **self.__msd.get_state())
finally:
if written != 0:
logger.info("Written %d bytes to mass-storage device", written)
diff --git a/kvmd/web/index.html b/kvmd/web/index.html
index abd232c9..935ea77d 100644
--- a/kvmd/web/index.html
+++ b/kvmd/web/index.html
@@ -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>
diff --git a/kvmd/web/js/atx.js b/kvmd/web/js/atx.js
index 0779f584..0167ae6f 100644
--- a/kvmd/web/js/atx.js
+++ b/kvmd/web/js/atx.js
@@ -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.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.clearLeds = function() {
+ 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",
diff --git a/kvmd/web/js/msd.js b/kvmd/web/js/msd.js
new file mode 100644
index 00000000..5ae851ea
--- /dev/null
+++ b/kvmd/web/js/msd.js
@@ -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;
+ };
+};
diff --git a/kvmd/web/js/session.js b/kvmd/web/js/session.js
index b4642b62..33c8af60 100644
--- a/kvmd/web/js/session.js
+++ b/kvmd/web/js/session.js
@@ -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);
};