From 9237bb020d098f62877622f4926aef9f5f72153b Mon Sep 17 00:00:00 2001 From: Devaev Maxim Date: Thu, 28 Jun 2018 23:18:53 +0300 Subject: refactoring --- kvmd/kvmd.yaml | 14 ++++---------- kvmd/kvmd/__init__.py | 37 ++++++++++++++++++------------------ kvmd/kvmd/extras/cleanup/__init__.py | 5 +++-- kvmd/kvmd/streamer.py | 35 +++++++++++++++------------------- 4 files changed, 41 insertions(+), 50 deletions(-) diff --git a/kvmd/kvmd.yaml b/kvmd/kvmd.yaml index cfeabf00..738a752b 100644 --- a/kvmd/kvmd.yaml +++ b/kvmd/kvmd.yaml @@ -27,18 +27,12 @@ kvmd: video: pinout: cap: 21 - vga: 25 + conv: 25 sync_delay: 1.0 + shutdown_delay: 10.0 - mjpg_streamer: - prog: /usr/bin/mjpg_streamer - device: /dev/video0 - width: 720 - height: 576 - every: 2 - host: localhost - port: 8082 + cmd: "/usr/bin/mjpg_streamer -i 'input_uvc.so -d /dev/video0 -e 2 -y -n -r 720x576' -o 'output_http.so -l localhost -p 8082'" logging: version: 1 @@ -46,7 +40,7 @@ logging: formatters: console: - (): contextlog.SmartFormatter + (): logging.Formatter style: "{" datefmt: "%H:%M:%S" format: "[{asctime}] {name:20.20} {levelname:>7} --- {message}" diff --git a/kvmd/kvmd/__init__.py b/kvmd/kvmd/__init__.py index 324b09fb..dd615298 100644 --- a/kvmd/kvmd/__init__.py +++ b/kvmd/kvmd/__init__.py @@ -18,38 +18,39 @@ def main() -> None: loop = asyncio.get_event_loop() atx = Atx( - power_led=config["atx"]["leds"]["pinout"]["power"], - hdd_led=config["atx"]["leds"]["pinout"]["hdd"], - power_switch=config["atx"]["switches"]["pinout"]["power"], - reset_switch=config["atx"]["switches"]["pinout"]["reset"], - click_delay=config["atx"]["switches"]["click_delay"], - long_click_delay=config["atx"]["switches"]["long_click_delay"], + power_led=int(config["atx"]["leds"]["pinout"]["power"]), + hdd_led=int(config["atx"]["leds"]["pinout"]["hdd"]), + power_switch=int(config["atx"]["switches"]["pinout"]["power"]), + reset_switch=int(config["atx"]["switches"]["pinout"]["reset"]), + click_delay=float(config["atx"]["switches"]["click_delay"]), + long_click_delay=float(config["atx"]["switches"]["long_click_delay"]), ) streamer = Streamer( - cap_power=config["video"]["pinout"]["cap"], - vga_power=config["video"]["pinout"]["vga"], - sync_delay=config["video"]["sync_delay"], - mjpg_streamer=config["video"]["mjpg_streamer"], + cap_power=int(config["video"]["pinout"]["cap"]), + conv_power=int(config["video"]["pinout"]["conv"]), + sync_delay=float(config["video"]["sync_delay"]), + cmd=str(config["video"]["cmd"]), loop=loop, ) keyboard = Ps2Keyboard( - clock=config["keyboard"]["pinout"]["clock"], - data=config["keyboard"]["pinout"]["data"], - pulse=config["keyboard"]["pulse"], + clock=int(config["keyboard"]["pinout"]["clock"]), + data=int(config["keyboard"]["pinout"]["data"]), + pulse=float(config["keyboard"]["pulse"]), ) Server( atx=atx, streamer=streamer, keyboard=keyboard, - heartbeat=config["server"]["heartbeat"], - atx_leds_poll=config["atx"]["leds"]["poll"], - video_shutdown_delay=config["video"]["shutdown_delay"], + heartbeat=float(config["server"]["heartbeat"]), + atx_leds_poll=float(config["atx"]["leds"]["poll"]), + video_shutdown_delay=float(config["video"]["shutdown_delay"]), loop=loop, ).run( - host=config["server"]["host"], - port=config["server"]["port"], + host=str(config["server"]["host"]), + port=int(config["server"]["port"]), ) + get_logger().info("Bye-bye") diff --git a/kvmd/kvmd/extras/cleanup/__init__.py b/kvmd/kvmd/extras/cleanup/__init__.py index 13d27d78..040accdf 100644 --- a/kvmd/kvmd/extras/cleanup/__init__.py +++ b/kvmd/kvmd/extras/cleanup/__init__.py @@ -18,8 +18,9 @@ def main() -> None: *config["atx"]["switches"]["pinout"].items(), *config["video"]["pinout"].items(), ]: - logger.info("Writing value=0 to pin=%d (%s)", pin, key) - gpio.write(pin, False) + if pin > 0: + logger.info("Writing value=0 to pin=%d (%s)", pin, key) + gpio.write(pin, False) logger.info("Trying to find and kill mjpg_streamer ...") try: diff --git a/kvmd/kvmd/streamer.py b/kvmd/kvmd/streamer.py index 2b47925f..5ee16871 100644 --- a/kvmd/kvmd/streamer.py +++ b/kvmd/kvmd/streamer.py @@ -1,7 +1,6 @@ import asyncio import asyncio.subprocess -from typing import Dict from typing import Optional from .logging import get_logger @@ -14,21 +13,16 @@ class Streamer: # pylint: disable=too-many-instance-attributes def __init__( self, cap_power: int, - vga_power: int, + conv_power: int, sync_delay: float, - mjpg_streamer: Dict, + cmd: str, loop: asyncio.AbstractEventLoop, ) -> None: self.__cap_power = gpio.set_output(cap_power) - self.__vga_power = gpio.set_output(vga_power) + self.__conv_power = (gpio.set_output(conv_power) if conv_power > 0 else conv_power) self.__sync_delay = sync_delay - - self.__cmd = ( - "%(prog)s" - " -i 'input_uvc.so -d %(device)s -e %(every)s -y -n -r %(width)sx%(height)s'" - " -o 'output_http.so -p -l %(host)s %(port)s'" - ) % (mjpg_streamer) + self.__cmd = cmd self.__loop = loop @@ -36,13 +30,13 @@ class Streamer: # pylint: disable=too-many-instance-attributes async def start(self) -> None: assert not self.__proc_task - get_logger().info("Starting mjpg_streamer ...") + get_logger().info("Starting streamer ...") await self.__set_hw_enabled(True) self.__proc_task = self.__loop.create_task(self.__process()) async def stop(self) -> None: assert self.__proc_task - get_logger().info("Stopping mjpg_streamer ...") + get_logger().info("Stopping streamer ...") self.__proc_task.cancel() await asyncio.gather(self.__proc_task, return_exceptions=True) await self.__set_hw_enabled(False) @@ -52,11 +46,12 @@ class Streamer: # pylint: disable=too-many-instance-attributes return bool(self.__proc_task) async def __set_hw_enabled(self, enabled: bool) -> None: - # XXX: This sequence is very important for enable + # XXX: This sequence is very important to enable converter and cap board gpio.write(self.__cap_power, enabled) - if enabled: - await asyncio.sleep(self.__sync_delay) - gpio.write(self.__vga_power, enabled) + if self.__conv_power > 0: + if enabled: + await asyncio.sleep(self.__sync_delay) + gpio.write(self.__conv_power, enabled) await asyncio.sleep(self.__sync_delay) async def __process(self) -> None: @@ -70,13 +65,13 @@ class Streamer: # pylint: disable=too-many-instance-attributes stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT, ) - logger.info("Started mjpg_streamer pid=%d: %s", proc.pid, self.__cmd) + logger.info("Started streamer pid=%d: %s", proc.pid, self.__cmd) empty = 0 while proc.returncode is None: line = (await proc.stdout.readline()).decode(errors="ignore").strip() if line: - logger.info("mjpg_streamer: %s", line) + logger.info("streamer: %s", line) empty = 0 else: empty += 1 @@ -90,9 +85,9 @@ class Streamer: # pylint: disable=too-many-instance-attributes break except Exception as err: if proc: - logger.exception("Unexpected finished mjpg_streamer pid=%d with retcode=%d", proc.pid, proc.returncode) + logger.exception("Unexpected finished streamer pid=%d with retcode=%d", proc.pid, proc.returncode) else: - logger.exception("Can't start mjpg_streamer: %s", err) + logger.exception("Can't start streamer: %s", err) await asyncio.sleep(1) if proc: -- cgit v1.2.3