api refactoring

This commit is contained in:
Devaev Maxim 2019-03-27 06:57:43 +03:00
parent 68f28c69f3
commit 2acec3f229
5 changed files with 23 additions and 18 deletions

View File

@ -141,7 +141,7 @@ class Hid(multiprocessing.Process): # pylint: disable=too-many-instance-attribu
self.__lock = asyncio.Lock()
self.__events_queue: multiprocessing.queues.Queue = multiprocessing.Queue()
self.__ok_shared = multiprocessing.Value("i", 1)
self.__online_shared = multiprocessing.Value("i", 1)
self.__stop_event = multiprocessing.Event()
@ -150,7 +150,7 @@ class Hid(multiprocessing.Process): # pylint: disable=too-many-instance-attribu
super().start()
def get_state(self) -> Dict:
return {"ok": bool(self.__ok_shared.value)}
return {"online": bool(self.__online_shared.value)}
async def poll_state(self) -> AsyncGenerator[Dict, None]:
while self.is_alive():
@ -290,23 +290,23 @@ class Hid(multiprocessing.Process): # pylint: disable=too-many-instance-attribu
logger.error("Got CRC error of request from HID: request=%r", request)
elif code == 0x45: # Unknown command
logger.error("HID did not recognize the request=%r", request)
self.__ok_shared.value = 1
self.__online_shared.value = 1
return
elif code == 0x24: # Rebooted?
logger.error("No previous command state inside HID, seems it was rebooted")
self.__ok_shared.value = 1
self.__online_shared.value = 1
return
elif code == 0x20: # Done
if error_occured:
logger.info("Success!")
self.__ok_shared.value = 1
self.__online_shared.value = 1
return
else:
logger.error("Invalid response from HID: request=%r; code=0x%x", request, code)
common_retries -= 1
error_occured = True
self.__ok_shared.value = 0
self.__online_shared.value = 0
if common_retries and read_retries:
logger.error("Retries left: common_retries=%d; read_retries=%d", common_retries, read_retries)

View File

@ -253,18 +253,19 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
logger.info("Mass-storage device is disabled")
def get_state(self) -> Dict:
online = (self._enabled and bool(self._device_path))
info = (self.__saved_device_info._asdict() if self.__saved_device_info else None)
if info:
info["hw"] = (info["hw"]._asdict() if info["hw"] else None)
info["image"] = (info["image"]._asdict() if info["image"] else None)
connected_to: Optional[str] = None
if self._enabled and self._device_path:
if online:
if info:
info["hw"] = (info["hw"]._asdict() if info["hw"] else None)
info["image"] = (info["image"]._asdict() if info["image"] else None)
connected_to = ("kvm" if self.__device_info else "server")
return {
"enabled": self._enabled,
"online": (self._enabled and bool(self._device_path)),
"online": online,
"connected_to": connected_to,
"busy": bool(self.__device_file),
"written": self.__written,

View File

@ -443,6 +443,10 @@ class Server: # pylint: disable=too-many-instance-attributes
# ===== HID
@_exposed("GET", "/hid")
async def __hid_state_handler(self, _: aiohttp.web.Request) -> aiohttp.web.Response:
return _json(self.__hid.get_state())
@_exposed("POST", "/hid/reset")
async def __hid_reset_handler(self, _: aiohttp.web.Request) -> aiohttp.web.Response:
await self.__hid.reset()

View File

@ -26,7 +26,7 @@ function Keyboard() {
/************************************************************************/
var __ws = null;
var __ok = true;
var __online = true;
var __keys = [].slice.call($$$("div#keyboard-desktop div.keyboard-block div.keyboard-row div.key"));
var __modifiers = [].slice.call($$$("div#keyboard-desktop div.keyboard-block div.keyboard-row div.modifier"));
@ -77,7 +77,7 @@ function Keyboard() {
};
self.setState = function(state) {
__ok = state.ok;
__online = state.online;
__updateLeds();
};
@ -102,7 +102,7 @@ function Keyboard() {
var title = "Keyboard free";
if (__ws) {
if (__ok) {
if (__online) {
if (is_captured) {
led = "led-green";
title = "Keyboard captured";

View File

@ -26,7 +26,7 @@ function Mouse() {
/************************************************************************/
var __ws = null;
var __ok = true;
var __online = true;
var __current_pos = {x: 0, y:0};
var __sent_pos = {x: 0, y:0};
@ -68,7 +68,7 @@ function Mouse() {
};
self.setState = function(state) {
__ok = state.ok;
__online = state.online;
__updateLeds();
};
@ -88,7 +88,7 @@ function Mouse() {
var title = "Mouse free";
if (__ws) {
if (__ok) {
if (__online) {
if (is_captured) {
led = "led-green";
title = "Mouse captured";