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

@ -14,6 +14,9 @@ class AioExclusiveRegion:
self.__exc_type = exc_type self.__exc_type = exc_type
self.__busy = False self.__busy = False
def is_busy(self) -> bool:
return self.__busy
def enter(self) -> None: def enter(self) -> None:
if not self.__busy: if not self.__busy:
self.__busy = True self.__busy = True

View File

@ -37,6 +37,7 @@ class Atx:
def get_state(self) -> Dict: def get_state(self) -> Dict:
return { return {
"busy": self.__region.is_busy(),
"leds": { "leds": {
"power": (not gpio.read(self.__power_led)), "power": (not gpio.read(self.__power_led)),
"hdd": (not gpio.read(self.__hdd_led)), "hdd": (not gpio.read(self.__hdd_led)),

View File

@ -174,14 +174,16 @@ class Server: # pylint: disable=too-many-instance-attributes
@_wrap_exceptions_for_web("Click error") @_wrap_exceptions_for_web("Click error")
async def __atx_click_handler(self, request: aiohttp.web.Request) -> aiohttp.web.Response: async def __atx_click_handler(self, request: aiohttp.web.Request) -> aiohttp.web.Response:
button = request.query.get("button") button = request.query.get("button")
if button == "power": clicker = {
await self.__atx.click_power() "power": self.__atx.click_power,
elif button == "power_long": "power_long": self.__atx.click_power_long,
await self.__atx.click_power_long() "reset": self.__atx.click_reset,
elif button == "reset": }.get(button)
await self.__atx.click_reset() if not clicker:
else:
raise BadRequest("Missing or invalid 'button=%s'" % (button)) 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}) return _json({"clicked": button})
async def __msd_state_handler(self, _: aiohttp.web.Request) -> aiohttp.web.Response: 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") to = request.query.get("to")
if to == "kvm": if to == "kvm":
await self.__msd.connect_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": elif to == "server":
await self.__msd.connect_to_pc() 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: else:
raise BadRequest("Missing or invalid 'to=%s'" % (to)) 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") @_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: 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") raise BadRequest("Missing 'image_data' field")
async with self.__msd: 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) logger.info("Writing image %r to mass-storage device ...", image_name)
await self.__msd.write_image_info(image_name, False) await self.__msd.write_image_info(image_name, False)
while True: while True:
@ -225,7 +229,7 @@ class Server: # pylint: disable=too-many-instance-attributes
break break
written = await self.__msd.write_image_chunk(chunk) written = await self.__msd.write_image_chunk(chunk)
await self.__msd.write_image_info(image_name, True) 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: finally:
if written != 0: if written != 0:
logger.info("Written %d bytes to mass-storage device", written) logger.info("Written %d bytes to mass-storage device", written)

View File

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

View File

@ -1,10 +1,22 @@
var atx = new function() { var atx = new function() {
this.setLedsState = function(leds) { this.loadInitialState = function() {
$("atx-power-led").className = (leds.power ? "led-on" : "led-off"); var http = tools.makeRequest("GET", "/kvmd/atx", function() {
$("atx-hdd-led").className = (leds.hdd ? "led-hdd-busy" : "led-off"); 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-power-led",
"atx-hdd-led", "atx-hdd-led",
@ -35,7 +47,7 @@ var atx = new function() {
} }
if (button && confirm(confirm_msg)) { if (button && confirm(confirm_msg)) {
__setButtonsBusy(true); // atx.setButtonsBusy(true);
var http = tools.makeRequest("POST", "/kvmd/atx/click?button=" + button, function() { var http = tools.makeRequest("POST", "/kvmd/atx/click?button=" + button, function() {
if (http.readyState === 4) { if (http.readyState === 4) {
if (http.status === 409) { if (http.status === 409) {
@ -43,13 +55,13 @@ var atx = new function() {
} else if (http.status !== 200) { } else if (http.status !== 200) {
alert("Click error:", http.responseText); alert("Click error:", http.responseText);
} }
__setButtonsBusy(false); // atx.setButtonsBusy(false);
} }
}, timeout); }, timeout);
} }
}; };
var __setButtonsBusy = function(busy) { this.setButtonsBusy = function(busy) {
[ [
"atx-power-button", "atx-power-button",
"atx-power-button-long", "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) { var __wsOpenHandler = function(event) {
tools.debug("WebSocket opened:", event); tools.debug("WebSocket opened:", event);
atx.loadInitialState();
msd.loadInitialState();
hid.installCapture(__ws); hid.installCapture(__ws);
__missed_heartbeats = 0; __missed_heartbeats = 0;
__ping_timer = setInterval(__pingServer, 1000); __ping_timer = setInterval(__pingServer, 1000);
@ -25,7 +27,11 @@ var session = new function() {
__missed_heartbeats = 0; __missed_heartbeats = 0;
} else if (event.msg_type === "event") { } else if (event.msg_type === "event") {
if (event.msg.event === "atx_state") { 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; __ping_timer = null;
} }
hid.clearCapture(); hid.clearCapture();
atx.clearLeds(); atx.clearState();
__ws = null; __ws = null;
setTimeout(session.startPoller, 1000); setTimeout(session.startPoller, 1000);
}; };