Skip to content

Commit

Permalink
Add ping
Browse files Browse the repository at this point in the history
  • Loading branch information
synesthesiam committed Jan 16, 2024
1 parent db5918e commit 33e6d80
Show file tree
Hide file tree
Showing 14 changed files with 224 additions and 24 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,7 @@ Satellites can respond to events from the server by running commands:
* `--tts-start-command` - text-to-speech response started (no stdin)
* `--tts-stop-command` - text-to-speech response stopped (no stdin)
* `--error-command` - an error was sent from the server (text on stdin)
* `--connected-command` - satellite connected to server
* `--disconnected-command` - satellite disconnected from server

For more advanced scenarios, use an event service (`--event-uri`). See `wyoming_satellite/example_event_client.py` for a basic client that just logs events.
16 changes: 15 additions & 1 deletion examples/2mic_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
import spidev
from wyoming.asr import Transcript
from wyoming.event import Event
from wyoming.satellite import RunSatellite, StreamingStarted, StreamingStopped
from wyoming.satellite import (
RunSatellite,
StreamingStarted,
StreamingStopped,
SatelliteConnected,
SatelliteDisconnected,
)
from wyoming.server import AsyncEventHandler, AsyncServer
from wyoming.vad import VoiceStarted
from wyoming.wake import Detection
Expand Down Expand Up @@ -107,6 +113,14 @@ async def handle_event(self, event: Event) -> bool:
self.color(_BLACK)
elif RunSatellite.is_type(event.type):
self.color(_BLACK)
elif SatelliteConnected.is_type(event.type):
# Flash
for _ in range(5):
self.color(_GREEN)
await asyncio.sleep(0.3)
self.color(_BLACK)
elif SatelliteDisconnected.is_type(event.type):
self.color(_RED)

return True

Expand Down
16 changes: 15 additions & 1 deletion examples/4mic_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
import spidev
from wyoming.asr import Transcript
from wyoming.event import Event
from wyoming.satellite import RunSatellite, StreamingStarted, StreamingStopped
from wyoming.satellite import (
RunSatellite,
StreamingStarted,
StreamingStopped,
SatelliteConnected,
SatelliteDisconnected,
)
from wyoming.server import AsyncEventHandler, AsyncServer
from wyoming.vad import VoiceStarted
from wyoming.wake import Detection
Expand Down Expand Up @@ -107,6 +113,14 @@ async def handle_event(self, event: Event) -> bool:
self.color(_BLACK)
elif RunSatellite.is_type(event.type):
self.color(_BLACK)
elif SatelliteConnected.is_type(event.type):
# Flash
for _ in range(5):
self.color(_GREEN)
await asyncio.sleep(0.3)
self.color(_BLACK)
elif SatelliteDisconnected.is_type(event.type):
self.color(_RED)

return True

Expand Down
1 change: 1 addition & 0 deletions installer/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class SatelliteSettings(DataClassJsonMixin):
name: str = "Wyoming Satellite"
type: SatelliteType = SatelliteType.ALWAYS_STREAMING
debug: bool = False
event_service_command: Optional[List[str]] = None


@dataclass
Expand Down
48 changes: 38 additions & 10 deletions installer/satellite.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Satellite settings."""
from typing import Optional

from .const import SatelliteType, Settings
from .const import SatelliteType, Settings, PROGRAM_DIR
from .whiptail import error, inputbox, menu, passwordbox, radiolist, run_with_gauge


Expand Down Expand Up @@ -29,20 +29,22 @@ def configure_satellite(settings: Settings) -> None:
if satellite_type is not None:
settings.satellite.type = SatelliteType(satellite_type)
settings.save()
elif choice in ("stop", "start"):
elif choice == "feedback":
configure_feedback(settings)
elif choice in ("restart", "stop", "start"):
password = passwordbox("sudo password:")
if not password:
continue

command = ["sudo", "-S", "systemctl", choice, "wyoming-satellite.service"]
text = (
"Stopping satellite..." if choice == "stop" else "Starting satellite..."
text = {"restart": "Restarting", "stop": "Stopping", "start": "Starting"}[
choice
]
success = run_with_gauge(
f"{text} satellite...", [command], sudo_password=password
)
success = run_with_gauge(text, [command], sudo_password=password)
if not success:
error(
"stopping satellite" if choice == "stop" else "starting satellite"
)
error(f"{text.lower()} satellite")
elif choice == "debug":
debug = radiolist(
"Debug Mode:",
Expand All @@ -66,10 +68,36 @@ def satellite_menu(last_choice: Optional[str]) -> Optional[str]:
[
("name", "Satellite Name"),
("type", "Satellite Type"),
("stop", "Stop Service"),
("start", "Start Service"),
("feedback", "Feedback"),
("restart", "Restart Services"),
("stop", "Stop Services"),
("start", "Start Services"),
("debug", "Set Debug Mode"),
],
selected_item=last_choice,
menu_args=["--ok-button", "Select", "--cancel-button", "Back"],
)


def configure_feedback(settings: Settings) -> None:
choice: Optional[str] = None
while True:
choice = menu("Main > Satellite > Feedback", [("respeaker", "ReSpeaker")])
if choice == "respeaker":
event_service = radiolist(
"Event Service:",
[
("2mic", "2mic LEDs"),
("4mic", "4mic LEDs"),
],
)

if event_service is not None:
settings.satellite.event_service_command = [
str(PROGRAM_DIR / "script" / "run_{event_service}"),
"--uri",
"tcp://127.0.0.1:10500",
]
settings.save()
else:
break
23 changes: 23 additions & 0 deletions installer/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,29 @@ def generate_services(settings: Settings) -> None:
)
satellite_requires.append(f"{wake_word_service}.service")

if settings.satellite.event_service_command:
event_service = "wyoming-event"
event_command_str = shlex.join(settings.satellite.event_service_command)
with open(
SERVICES_DIR / f"{event_service}.service", "w", encoding="utf-8"
) as service_file:
print("[Unit]", file=service_file)
print(f"Description=Event service", file=service_file)
print("", file=service_file)
print("[Service]", file=service_file)
print("Type=simple", file=service_file)
print(f"User={user_name}", file=service_file)
print(f"ExecStart={event_command_str}", file=service_file)
print(f"WorkingDirectory={PROGRAM_DIR}", file=service_file)
print("Restart=always", file=service_file)
print("RestartSec=1", file=service_file)
print("", file=service_file)
print("[Install]", file=service_file)
print("WantedBy=default.target", file=service_file)

satellite_command.extend(["--event-uri", "tcp://127.0.0.1:10500"])
satellite_requires.append(f"{event_service}.service")

if settings.satellite.debug:
satellite_command.extend(
["--debug", "--debug-recording-dir", str(LOCAL_DIR / "debug-recording")]
Expand Down
3 changes: 2 additions & 1 deletion script/format
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ _VENV_DIR = _PROGRAM_DIR / ".venv"
_MODULE_DIR = _PROGRAM_DIR / "wyoming_satellite"
_TESTS_DIR = _PROGRAM_DIR / "tests"
_INSTALLER_DIR = _PROGRAM_DIR / "installer"
_EXAMPLES_DIR = _PROGRAM_DIR / "examples"

_FORMAT_DIRS = [_MODULE_DIR, _TESTS_DIR, _INSTALLER_DIR]
_FORMAT_DIRS = [_MODULE_DIR, _TESTS_DIR, _INSTALLER_DIR, _EXAMPLES_DIR]

context = venv.EnvBuilder().ensure_directories(_VENV_DIR)
subprocess.check_call([context.env_exe, "-m", "black"] + _FORMAT_DIRS)
Expand Down
15 changes: 15 additions & 0 deletions script/run_2mic
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python3
import sys
import subprocess
import venv
from pathlib import Path

_DIR = Path(__file__).parent
_PROGRAM_DIR = _DIR.parent
_VENV_DIR = _PROGRAM_DIR / ".venv"

context = venv.EnvBuilder().ensure_directories(_VENV_DIR)
subprocess.check_call(
[context.env_exe, str(_PROGRAM_DIR / "examples" / "2mic_service.py")]
+ sys.argv[1:]
)
15 changes: 15 additions & 0 deletions script/run_4mic
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python3
import sys
import subprocess
import venv
from pathlib import Path

_DIR = Path(__file__).parent
_PROGRAM_DIR = _DIR.parent
_VENV_DIR = _PROGRAM_DIR / ".venv"

context = venv.EnvBuilder().ensure_directories(_VENV_DIR)
subprocess.check_call(
[context.env_exe, str(_PROGRAM_DIR / "examples" / "4mic_service.py")]
+ sys.argv[1:]
)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_requirements(req_path: Path) -> List[str]:

setup(
name="wyoming_satellite",
version="1.0.1",
version="1.1.0",
description="Wyoming server for remote voice satellite",
url="http://github.com/rhasspy/wyoming-satellite",
author="Michael Hansen",
Expand Down
12 changes: 11 additions & 1 deletion wyoming_satellite/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ async def main() -> None:
"--error-command",
help="Command to run when an error occurs",
)
parser.add_argument(
"--connected-command",
help="Command to run when connected to the server",
)
parser.add_argument(
"--disconnected-command",
help="Command to run when disconnected from the server",
)

# Sounds
parser.add_argument(
Expand Down Expand Up @@ -326,6 +334,8 @@ async def main() -> None:
tts_start=split_command(args.tts_start_command),
tts_stop=split_command(args.tts_stop_command),
error=split_command(args.error_command),
connected=split_command(args.connected_command),
disconnected=split_command(args.disconnected_command),
),
debug_recording_dir=args.debug_recording_dir,
)
Expand Down Expand Up @@ -368,7 +378,7 @@ async def main() -> None:
args.zeroconf_host,
)

satellite_task = asyncio.create_task(satellite.run())
satellite_task = asyncio.create_task(satellite.run(), name="satellite run")

try:
await server.run(partial(SatelliteEventHandler, wyoming_info, satellite, args))
Expand Down
4 changes: 2 additions & 2 deletions wyoming_satellite/event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async def handle_event(self, event: Event) -> bool:

if self.satellite.server_id is None:
# Take over after a problem occurred
self.satellite.set_server(self.client_id, self.writer)
await self.satellite.set_server(self.client_id, self.writer)
elif self.satellite.server_id != self.client_id:
# New connection
_LOGGER.debug("Connection cancelled: %s", self.client_id)
Expand All @@ -53,4 +53,4 @@ async def handle_event(self, event: Event) -> bool:
async def disconnect(self) -> None:
"""Server disconnect."""
if self.satellite.server_id == self.client_id:
self.satellite.clear_server()
await self.satellite.clear_server()
Loading

0 comments on commit 33e6d80

Please sign in to comment.