gpio view and refactoring

This commit is contained in:
Devaev Maxim
2020-08-31 09:01:40 +03:00
parent 9feb353150
commit 7782668944
6 changed files with 39 additions and 15 deletions

View File

@@ -26,7 +26,7 @@ from aiohttp.web import Response
from ....validators.basic import valid_bool
from ....validators.basic import valid_float_f0
from ....validators.hw import valid_gpio_channel
from ....validators.kvm import valid_ugpio_channel
from ..ugpio import UserGpio
@@ -45,19 +45,20 @@ class UserGpioApi:
async def __state_handler(self, _: Request) -> Response:
return make_json_response({
"scheme": (await self.__user_gpio.get_scheme()),
"view": (await self.__user_gpio.get_view()),
"state": (await self.__user_gpio.get_state()),
})
@exposed_http("POST", "/gpio/switch")
async def __switch_handler(self, request: Request) -> Response:
channel = valid_gpio_channel(request.query.get("channel"))
channel = valid_ugpio_channel(request.query.get("channel"))
state = valid_bool(request.query.get("state"))
done = await self.__user_gpio.switch(channel, state)
return make_json_response({"done": done})
@exposed_http("POST", "/gpio/pulse")
async def __pulse_handler(self, request: Request) -> Response:
channel = valid_gpio_channel(request.query.get("channel"))
channel = valid_ugpio_channel(request.query.get("channel"))
delay = valid_float_f0(request.query.get("delay", "0"))
await self.__user_gpio.pulse(channel, delay)
return make_json_response()

View File

@@ -244,6 +244,7 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins
await self.__register_ws_client(client)
try:
await self.__broadcast_event("gpio_scheme_state", await self.__user_gpio.get_scheme())
await self.__broadcast_event("gpio_view_state", await self.__user_gpio.get_view())
await asyncio.gather(*[
self.__broadcast_event(component.event_type, await component.get_state())
for component in self.__components

View File

@@ -167,6 +167,8 @@ class _GpioOutput: # pylint: disable=too-many-instance-attributes
# =====
class UserGpio:
def __init__(self, config: Section) -> None:
self.__view = config.view
self.__state_notifier = aiotools.AioNotifier()
self.__reader = gpio.BatchReader(
pins=[gpio.set_input(ch_config.pin) for ch_config in config.scheme.values()],
@@ -189,6 +191,9 @@ class UserGpio:
"outputs": {channel: gout.get_scheme() for (channel, gout) in self.__outputs.items()},
}
async def get_view(self) -> Dict:
return self.__view
async def get_state(self) -> Dict:
return {
"inputs": {channel: gin.get_state() for (channel, gin) in self.__inputs.items()},