Merge pull request #7 from Arduinum/arduinum/service-orange-pi-mvp-1
Merge for master
This commit was merged in pull request #7.
This commit is contained in:
@@ -9,6 +9,11 @@ LEFT=влево
|
|||||||
RIGHT=вправо
|
RIGHT=вправо
|
||||||
STOP=стоп
|
STOP=стоп
|
||||||
|
|
||||||
|
# Команды Linux
|
||||||
|
VIDEOSTREAM=команда видеострима
|
||||||
|
ON_VIDEOSTREAM=включить видеострим
|
||||||
|
ON_STREAM_DEBUG=включить дебаг видеострима
|
||||||
|
|
||||||
# Настройки для линий GPIO
|
# Настройки для линий GPIO
|
||||||
GPIO_PATH=путь до gpio
|
GPIO_PATH=путь до gpio
|
||||||
LED_LINE=номер линии
|
LED_LINE=номер линии
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from gpiod import exception
|
from gpiod import exception
|
||||||
|
from subprocess import Popen, DEVNULL
|
||||||
|
from os import setpgrp, killpg, getpgid
|
||||||
|
from signal import SIGTERM
|
||||||
from socket import gethostbyname
|
from socket import gethostbyname
|
||||||
from websockets import serve, exceptions
|
from websockets import serve, exceptions
|
||||||
from websockets.legacy.server import WebSocketServerProtocol
|
from websockets.legacy.server import WebSocketServerProtocol
|
||||||
from json import loads, dumps, JSONDecodeError
|
from json import loads, dumps, JSONDecodeError
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
from settings import settings
|
from settings import settings
|
||||||
from gpio_control import RobotControl
|
from gpio_control import RobotControl
|
||||||
@@ -57,7 +61,7 @@ async def robot_control_gpio(
|
|||||||
data.update(commands_status.OK_COMMAND.response)
|
data.update(commands_status.OK_COMMAND.response)
|
||||||
await websocket.send(message=dumps(data))
|
await websocket.send(message=dumps(data))
|
||||||
except (exceptions.ConnectionClosed, exception.RequestReleasedError):
|
except (exceptions.ConnectionClosed, exception.RequestReleasedError):
|
||||||
pass
|
return
|
||||||
except (exceptions.ConnectionClosedOK, exceptions.InvalidMessage, exceptions.InvalidState, OSError) as err:
|
except (exceptions.ConnectionClosedOK, exceptions.InvalidMessage, exceptions.InvalidState, OSError) as err:
|
||||||
message_err = f'{err.__class__.__name__}: {err}'
|
message_err = f'{err.__class__.__name__}: {err}'
|
||||||
print(message_err)
|
print(message_err)
|
||||||
@@ -86,21 +90,42 @@ async def start() -> None:
|
|||||||
):
|
):
|
||||||
# бесконечный цикл
|
# бесконечный цикл
|
||||||
await asyncio.Future()
|
await asyncio.Future()
|
||||||
except asyncio.CancelledError:
|
except (asyncio.CancelledError, OSError, exception.RequestReleasedError) as err:
|
||||||
|
# пробрасываем ошибку, чтобы asyncio.run() всё корректно закрыл
|
||||||
|
message_err = f'{err.__class__.__name__}: {err}'
|
||||||
|
print(message_err)
|
||||||
|
raise
|
||||||
|
finally:
|
||||||
if robot_control is not None:
|
if robot_control is not None:
|
||||||
robot_control.blinking_off()
|
robot_control.blinking_off()
|
||||||
robot_control.close()
|
robot_control.close()
|
||||||
|
|
||||||
# пробрасываем CancelledError, чтобы asyncio.run() всё корректно закрыл
|
|
||||||
raise
|
|
||||||
|
|
||||||
|
|
||||||
def run_app() -> None:
|
def run_app() -> None:
|
||||||
"""Функция старата приложения"""
|
"""Функция старата приложения"""
|
||||||
|
|
||||||
|
command_stream = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
if settings.commands_linux.on_videostream:
|
||||||
|
command_stream = Popen(
|
||||||
|
settings.commands_linux.videostream,
|
||||||
|
shell=True,
|
||||||
|
preexec_fn=setpgrp,
|
||||||
|
stderr=None if settings.commands_linux.on_stream_debug else DEVNULL
|
||||||
|
)
|
||||||
|
sleep(0.1)
|
||||||
|
print('Видеострим запущен.')
|
||||||
|
|
||||||
asyncio.run(start())
|
asyncio.run(start())
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
try:
|
||||||
|
if settings.commands_linux.on_videostream and command_stream:
|
||||||
|
killpg(getpgid(command_stream.pid), SIGTERM)
|
||||||
|
except ProcessLookupError:
|
||||||
|
# Если процесс был уже завершён
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
print('Выключение сервиса робота для приёма команд.')
|
print('Выключение сервиса робота для приёма команд.')
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -40,12 +40,21 @@ class GpioLines(ModelConfig):
|
|||||||
right_motor_consumer: str
|
right_motor_consumer: str
|
||||||
|
|
||||||
|
|
||||||
|
class CommandsLinux(ModelConfig):
|
||||||
|
"""Класс с командами для линукса"""
|
||||||
|
|
||||||
|
videostream: str
|
||||||
|
on_videostream: bool
|
||||||
|
on_stream_debug: bool
|
||||||
|
|
||||||
|
|
||||||
class Settings(ModelConfig):
|
class Settings(ModelConfig):
|
||||||
"""Класс для данных конфига"""
|
"""Класс для данных конфига"""
|
||||||
|
|
||||||
websocket_host: str
|
websocket_host: str
|
||||||
websocket_port: int
|
websocket_port: int
|
||||||
commands_robot: CommandsRobot = CommandsRobot()
|
commands_robot: CommandsRobot = CommandsRobot()
|
||||||
|
commands_linux: CommandsLinux = CommandsLinux()
|
||||||
gpio_lines: GpioLines = GpioLines()
|
gpio_lines: GpioLines = GpioLines()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user