summaryrefslogtreecommitdiff
path: root/kvmd/apps
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2018-11-16 06:54:27 +0300
committerDevaev Maxim <[email protected]>2018-11-16 06:54:27 +0300
commit17c6b9e31f9910c30ac660476e7bed8103f3935e (patch)
treea944ac26154930acb31a7d2e725181aaf744dc09 /kvmd/apps
parent4294eed14e94fc63a1eb1fb664de0690b1a16bf5 (diff)
meta, refactoring
Diffstat (limited to 'kvmd/apps')
-rw-r--r--kvmd/apps/kvmd/__init__.py5
-rw-r--r--kvmd/apps/kvmd/atx.py13
-rw-r--r--kvmd/apps/kvmd/server.py21
-rw-r--r--kvmd/apps/kvmd/streamer.py12
4 files changed, 35 insertions, 16 deletions
diff --git a/kvmd/apps/kvmd/__init__.py b/kvmd/apps/kvmd/__init__.py
index 099fd192..103d8be8 100644
--- a/kvmd/apps/kvmd/__init__.py
+++ b/kvmd/apps/kvmd/__init__.py
@@ -42,6 +42,7 @@ def main() -> None:
reset_switch=int(config["atx"]["pinout"]["reset_switch"]),
click_delay=float(config["atx"]["click_delay"]),
long_click_delay=float(config["atx"]["long_click_delay"]),
+ state_poll=float(config["atx"]["state_poll"]),
)
msd = MassStorageDevice(
@@ -62,6 +63,7 @@ def main() -> None:
sync_delay=float(config["streamer"]["sync_delay"]),
init_delay=float(config["streamer"]["init_delay"]),
init_restart_after=float(config["streamer"]["init_restart_after"]),
+ state_poll=float(config["streamer"]["state_poll"]),
quality=int(config["streamer"]["quality"]),
desired_fps=int(config["streamer"]["desired_fps"]),
@@ -83,9 +85,8 @@ def main() -> None:
msd=msd,
streamer=streamer,
+ meta_path=str(config["info"]["meta"]),
heartbeat=float(config["server"]["heartbeat"]),
- atx_state_poll=float(config["atx"]["state_poll"]),
- streamer_state_poll=float(config["streamer"]["state_poll"]),
streamer_shutdown_delay=float(config["streamer"]["shutdown_delay"]),
msd_chunk_size=int(config["msd"]["chunk_size"]),
diff --git a/kvmd/apps/kvmd/atx.py b/kvmd/apps/kvmd/atx.py
index d49708e0..8444c097 100644
--- a/kvmd/apps/kvmd/atx.py
+++ b/kvmd/apps/kvmd/atx.py
@@ -1,6 +1,7 @@
import asyncio
from typing import Dict
+from typing import AsyncGenerator
from ...logging import get_logger
@@ -13,16 +14,17 @@ class AtxIsBusy(aioregion.RegionIsBusyError):
pass
-class Atx:
+class Atx: # pylint: disable=too-many-instance-attributes
def __init__(
self,
power_led: int,
hdd_led: int,
-
power_switch: int,
reset_switch: int,
+
click_delay: float,
long_click_delay: float,
+ state_poll: float,
) -> None:
self.__power_led = gpio.set_input(power_led)
@@ -33,6 +35,8 @@ class Atx:
self.__click_delay = click_delay
self.__long_click_delay = long_click_delay
+ self.__state_poll = state_poll
+
self.__region = aioregion.AioExclusiveRegion(AtxIsBusy)
def get_state(self) -> Dict:
@@ -44,6 +48,11 @@ class Atx:
},
}
+ async def poll_state(self) -> AsyncGenerator[Dict, None]:
+ while True:
+ yield self.get_state()
+ await asyncio.sleep(self.__state_poll)
+
async def click_power(self) -> None:
await self.__click(self.__power_switch, self.__click_delay)
get_logger().info("Clicked power")
diff --git a/kvmd/apps/kvmd/server.py b/kvmd/apps/kvmd/server.py
index 04ce140f..ae66cbc4 100644
--- a/kvmd/apps/kvmd/server.py
+++ b/kvmd/apps/kvmd/server.py
@@ -18,6 +18,8 @@ from ...logging import Log
from ...aioregion import RegionIsBusyError
+from ...yaml import load_yaml_file
+
from ... import __version__
from .hid import Hid
@@ -111,9 +113,8 @@ class Server: # pylint: disable=too-many-instance-attributes
msd: MassStorageDevice,
streamer: Streamer,
+ meta_path: str,
heartbeat: float,
- atx_state_poll: float,
- streamer_state_poll: float,
streamer_shutdown_delay: float,
msd_chunk_size: int,
@@ -126,9 +127,8 @@ class Server: # pylint: disable=too-many-instance-attributes
self.__msd = msd
self.__streamer = streamer
+ self.__meta_path = meta_path
self.__heartbeat = heartbeat
- self.__atx_state_poll = atx_state_poll
- self.__streamer_state_poll = streamer_state_poll
self.__streamer_shutdown_delay = streamer_shutdown_delay
self.__msd_chunk_size = msd_chunk_size
@@ -190,6 +190,7 @@ class Server: # pylint: disable=too-many-instance-attributes
"streamer": await self.__streamer.get_version(),
},
"streamer": self.__streamer.get_app(),
+ "meta": load_yaml_file(self.__meta_path),
})
@_wrap_exceptions_for_web("Log error")
@@ -198,7 +199,7 @@ class Server: # pylint: disable=too-many-instance-attributes
follow = _valid_bool("follow", request.query.get("follow", "false"))
response = aiohttp.web.StreamResponse(status=200, reason="OK", headers={"Content-Type": "text/plain"})
await response.prepare(request)
- async for record in self.__log.log(seek, follow):
+ async for record in self.__log.poll_log(seek, follow):
await response.write(("[%s %s] --- %s" % (
record["dt"].strftime("%Y-%m-%d %H:%M:%S"),
record["service"],
@@ -431,17 +432,15 @@ class Server: # pylint: disable=too-many-instance-attributes
@_system_task
async def __poll_atx_state(self) -> None:
- while True:
+ async for state in self.__atx.poll_state():
if self.__sockets:
- await self.__broadcast_event("atx_state", **self.__atx.get_state())
- await asyncio.sleep(self.__atx_state_poll)
+ await self.__broadcast_event("atx_state", **state)
@_system_task
async def __poll_streamer_state(self) -> None:
- while True:
+ async for state in self.__streamer.poll_state():
if self.__sockets:
- await self.__broadcast_event("streamer_state", **(await self.__streamer.get_state()))
- await asyncio.sleep(self.__streamer_state_poll)
+ await self.__broadcast_event("streamer_state", **state)
async def __broadcast_event(self, event: str, **kwargs: Dict) -> None:
await asyncio.gather(*[
diff --git a/kvmd/apps/kvmd/streamer.py b/kvmd/apps/kvmd/streamer.py
index da82ade1..25fe7527 100644
--- a/kvmd/apps/kvmd/streamer.py
+++ b/kvmd/apps/kvmd/streamer.py
@@ -4,6 +4,7 @@ import asyncio.subprocess
from typing import List
from typing import Dict
+from typing import AsyncGenerator
from typing import Optional
import aiohttp
@@ -15,13 +16,15 @@ from ... import gpio
# =====
class Streamer: # pylint: disable=too-many-instance-attributes
- def __init__( # pylint: disable=too-many-arguments
+ def __init__( # pylint: disable=too-many-arguments,too-many-locals
self,
cap_power: int,
conv_power: int,
+
sync_delay: float,
init_delay: float,
init_restart_after: float,
+ state_poll: float,
quality: int,
desired_fps: int,
@@ -38,9 +41,11 @@ class Streamer: # pylint: disable=too-many-instance-attributes
self.__cap_power = (gpio.set_output(cap_power) if cap_power > 0 else cap_power)
self.__conv_power = (gpio.set_output(conv_power) if conv_power > 0 else conv_power)
+
self.__sync_delay = sync_delay
self.__init_delay = init_delay
self.__init_restart_after = init_restart_after
+ self.__state_poll = state_poll
self.__params = {
"quality": quality,
@@ -99,6 +104,11 @@ class Streamer: # pylint: disable=too-many-instance-attributes
"state": state,
}
+ async def poll_state(self) -> AsyncGenerator[Dict, None]:
+ while True:
+ yield (await self.get_state())
+ await asyncio.sleep(self.__state_poll)
+
def get_app(self) -> str:
return os.path.basename(self.__cmd[0])