Compare commits
13 Commits
2418ac2701
...
#5/impleme
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6324f82789 | ||
|
|
3086da0e2b | ||
|
|
021f27d62e | ||
|
|
d3e7e780f3 | ||
|
|
872bbf2965 | ||
|
|
e7956577d7 | ||
|
|
c31984e2d8 | ||
|
|
5191433e5d | ||
|
|
9dfee88789 | ||
|
|
46ec27c359 | ||
|
|
75b2d0007d | ||
| c616acd6af | |||
|
|
7e44e0e803 |
@@ -50,7 +50,7 @@ jobs:
|
||||
context: .
|
||||
file: ./Dockerfile
|
||||
platforms: |
|
||||
linux/arm4
|
||||
linux/arm64
|
||||
push: true
|
||||
tags: | # replace it with your local IP and tags
|
||||
git.vpn.cusano.net/${{ vars.DOCKER_ORG }}/${{ steps.meta.outputs.REPO_NAME }}/${{ env.CONTAINER_NAME }}:${{ steps.meta.outputs.REPO_VERSION }}
|
||||
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -2,4 +2,4 @@ __pycache__*
|
||||
bot-poc.py
|
||||
configs*
|
||||
.env
|
||||
*.log
|
||||
*.log*
|
||||
14
Dockerfile
14
Dockerfile
@@ -26,7 +26,9 @@ RUN apt-get update && \
|
||||
libportaudio2 \
|
||||
libpulse-dev \
|
||||
apulse \
|
||||
ffmpeg
|
||||
ffmpeg \
|
||||
liquidsoap \
|
||||
supervisor
|
||||
|
||||
# Clone the boatbod op25 repository
|
||||
RUN git clone -b gr310 https://github.com/boatbod/op25 /op25
|
||||
@@ -37,6 +39,9 @@ WORKDIR /op25
|
||||
# Run the install script to set up op25
|
||||
RUN ./install.sh -f
|
||||
|
||||
# Update the liquid file
|
||||
COPY op25.liq /op25/op25.liq
|
||||
|
||||
# Install Python dependencies
|
||||
COPY requirements.txt /tmp/requirements.txt
|
||||
RUN pip3 install --no-cache-dir -r /tmp/requirements.txt
|
||||
@@ -66,5 +71,8 @@ COPY ./app/internal/opus /app/internal/opus
|
||||
# Copy the rest of the directory contents into the container at /app
|
||||
COPY ./app /app
|
||||
|
||||
# Run the node script
|
||||
ENTRYPOINT ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8001", "--reload"]
|
||||
# Add Supervisord configuration
|
||||
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
|
||||
|
||||
# Modify the ENTRYPOINT to run Supervisord
|
||||
ENTRYPOINT ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
|
||||
10
Makefile
10
Makefile
@@ -23,6 +23,16 @@ run: build
|
||||
--network=host \
|
||||
$(IMAGE_NAME)
|
||||
|
||||
# Deploy docker
|
||||
deploy: build
|
||||
docker run --rm -d \
|
||||
--privileged \
|
||||
-v /dev:/dev \
|
||||
-v $(shell pwd)/configs:/configs \
|
||||
--name $(CONTAINER_NAME) \
|
||||
--network=host \
|
||||
$(IMAGE_NAME)
|
||||
|
||||
# Stop the Docker container
|
||||
stop:
|
||||
docker stop $(CONTAINER_NAME)
|
||||
|
||||
@@ -22,6 +22,8 @@ class DiscordBotManager:
|
||||
self.token: Optional[str] = None
|
||||
self.loop = asyncio.get_event_loop()
|
||||
self.lock = asyncio.Lock()
|
||||
self._ready_event = asyncio.Event()
|
||||
self._voice_ready_event = asyncio.Event()
|
||||
|
||||
async def start_bot(self, token: str):
|
||||
async with self.lock:
|
||||
@@ -36,12 +38,17 @@ class DiscordBotManager:
|
||||
@self.bot.event
|
||||
async def on_ready():
|
||||
LOGGER.info(f'Logged in as {self.bot.user}')
|
||||
# Set the event when on_ready is called
|
||||
self._ready_event.set()
|
||||
|
||||
@self.bot.event
|
||||
async def on_voice_state_update(member, before, after):
|
||||
# Check if the bot was disconnected
|
||||
if member == self.bot.user and after.channel is None:
|
||||
guild_id = before.channel.guild.id
|
||||
if not self.voice_clients.get(guild_id):
|
||||
LOGGER.info("Bot has left channel, reconnection ignored.")
|
||||
return
|
||||
LOGGER.info(f"Bot was disconnected from channel in guild {guild_id}. Attempting to reconnect...")
|
||||
try:
|
||||
await self.leave_voice_channel(guild_id)
|
||||
@@ -51,11 +58,28 @@ class DiscordBotManager:
|
||||
await asyncio.sleep(2)
|
||||
await self.join_voice_channel(guild_id, before.channel.id)
|
||||
|
||||
if member == self.bot.user and before.channel is None and after.channel is not None:
|
||||
print(f"{member.name} joined voice channel {after.channel.name}")
|
||||
self._voice_ready_event.set()
|
||||
|
||||
# Load Opus for the current CPU
|
||||
await self.load_opus()
|
||||
|
||||
# Create the task to run the bot in the background
|
||||
self.bot_task = self.loop.create_task(self.bot.start(token))
|
||||
|
||||
# Wait for the on_ready event to be set by the bot task
|
||||
LOGGER.info("Waiting for bot to become ready...")
|
||||
try:
|
||||
await asyncio.wait_for(self._ready_event.wait(), timeout=60.0)
|
||||
LOGGER.info("Bot is ready, start_bot returning.")
|
||||
return
|
||||
except asyncio.TimeoutError:
|
||||
LOGGER.error("Timeout waiting for bot to become ready. Bot might have failed to start.")
|
||||
if self.bot_task and not self.bot_task.done():
|
||||
self.bot_task.cancel()
|
||||
raise RuntimeError("Bot failed to become ready within timeout.")
|
||||
|
||||
async def stop_bot(self):
|
||||
async with self.lock:
|
||||
if self.bot:
|
||||
@@ -65,6 +89,7 @@ class DiscordBotManager:
|
||||
await self.bot_task
|
||||
self.bot_task = None
|
||||
self.voice_clients.clear()
|
||||
self._ready_event.clear()
|
||||
LOGGER.info("Bot has been stopped.")
|
||||
|
||||
async def join_voice_channel(self, guild_id: int, channel_id: int, ng_threshold: int = 50, device_id: int = 4):
|
||||
@@ -99,6 +124,15 @@ class DiscordBotManager:
|
||||
except Exception as e:
|
||||
LOGGER.error(f"Failed to connect to voice channel: {e}")
|
||||
|
||||
LOGGER.info("Waiting for bot to join voice...")
|
||||
try:
|
||||
await asyncio.wait_for(self._voice_ready_event.wait(), timeout=60.0)
|
||||
LOGGER.info("Bot joined voice, returning.")
|
||||
return
|
||||
except asyncio.TimeoutError:
|
||||
LOGGER.error("Timeout waiting for bot to join voice.")
|
||||
raise RuntimeError("Bot failed to join voice within timeout.")
|
||||
|
||||
async def leave_voice_channel(self, guild_id: int):
|
||||
if not self.bot:
|
||||
raise RuntimeError("Bot is not running.")
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
from pydantic import BaseModel
|
||||
from typing import List, Optional
|
||||
from typing import List, Optional, Union
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class BotConfig(BaseModel):
|
||||
token: str
|
||||
|
||||
class VoiceChannelRequest(BaseModel):
|
||||
class VoiceChannelJoinRequest(BaseModel):
|
||||
guild_id: int
|
||||
channel_id: int
|
||||
|
||||
class VoiceChannelLeaveRequest(BaseModel):
|
||||
guild_id: int
|
||||
|
||||
class DecodeMode(str, Enum):
|
||||
P25 = "P25"
|
||||
DMR = "DMR"
|
||||
@@ -22,7 +25,7 @@ class TalkgroupTag(BaseModel):
|
||||
class ConfigGenerator(BaseModel):
|
||||
type: DecodeMode
|
||||
systemName: str
|
||||
channels: List[str]
|
||||
channels: List[Union[int, str]]
|
||||
tags: Optional[List[TalkgroupTag]]
|
||||
whitelist: Optional[List[int]]
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from models import BotConfig, VoiceChannelRequest
|
||||
from models import BotConfig, VoiceChannelJoinRequest, VoiceChannelLeaveRequest
|
||||
from internal.bot_manager import DiscordBotManager
|
||||
from internal.logger import create_logger
|
||||
|
||||
@@ -31,7 +31,7 @@ async def stop_bot():
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
@router.post("/join_voice")
|
||||
async def join_voice_channel(request: VoiceChannelRequest):
|
||||
async def join_voice_channel(request: VoiceChannelJoinRequest):
|
||||
try:
|
||||
await bot_manager.join_voice_channel(request.guild_id, request.channel_id)
|
||||
return {"status": f"Joined guild {request.guild_id} voice channel {request.channel_id}."}
|
||||
@@ -40,7 +40,7 @@ async def join_voice_channel(request: VoiceChannelRequest):
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
@router.post("/leave_voice")
|
||||
async def leave_voice_channel(request: VoiceChannelRequest):
|
||||
async def leave_voice_channel(request: VoiceChannelLeaveRequest):
|
||||
try:
|
||||
await bot_manager.leave_voice_channel(request.guild_id)
|
||||
return {"status": f"Left guild {request.guild_id} voice channel."}
|
||||
|
||||
54
op25.liq
Normal file
54
op25.liq
Normal file
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/liquidsoap
|
||||
|
||||
# Example liquidsoap streaming from op25 to icecast
|
||||
# (c) 2019-2021 gnorbury@bondcar.com, wllmbecks@gmail.com
|
||||
#
|
||||
|
||||
set("log.stdout", true)
|
||||
set("log.file", false)
|
||||
set("log.level", 1)
|
||||
|
||||
# Make the native sample rate compatible with op25
|
||||
set("frame.audio.samplerate", 8000)
|
||||
|
||||
input = mksafe(input.external(buffer=0.25, channels=2, samplerate=8000, restart_on_error=false, "./audio.py -x 1.5 -s"))
|
||||
# Consider increasing the buffer value on slow systems such as RPi3. e.g. buffer=0.25
|
||||
# Longer buffer results in less choppy audio but at the expense of increased latency.
|
||||
|
||||
|
||||
# OPTIONAL AUDIO SIGNAL PROCESSING BLOCKS
|
||||
# Uncomment to enable
|
||||
#
|
||||
# High pass filter
|
||||
#input = filter.iir.butterworth.high(frequency = 200.0, order = 4, input)
|
||||
|
||||
# Low pass filter
|
||||
#input = filter.iir.butterworth.low(frequency = 3250.0, order = 4, input)
|
||||
|
||||
# Compression
|
||||
input = compress(input, attack = 2.0, gain = 0.0, knee = 13.0, ratio = 2.0, release = 12.3, threshold = -18.0)
|
||||
|
||||
# Normalization
|
||||
input = normalize(input, gain_max = 6.0, gain_min = -6.0, target = -16.0, threshold = -65.0)
|
||||
|
||||
|
||||
# LOCAL AUDIO OUTPUT
|
||||
# Uncomment the appropriate line below to enable local sound
|
||||
#
|
||||
# Default audio subsystem
|
||||
#out (input)
|
||||
#
|
||||
# PulseAudio
|
||||
#output.pulseaudio(input)
|
||||
#
|
||||
# ALSA
|
||||
#output.alsa(input)
|
||||
|
||||
|
||||
|
||||
# ICECAST STREAMING
|
||||
# Uncomment to enable output to an icecast server
|
||||
# Change the "host", "password", and "mount" strings appropriately first!
|
||||
# For metadata to work properly, the host address given here MUST MATCH the address in op25's meta.json file
|
||||
#
|
||||
output.icecast(%mp3(bitrate=16, samplerate=22050, stereo=false), description="op25", genre="Public Safety", url="", fallible=false, host="localhost", port=8000, mount="mountpoint", password="hackme", mean(input))
|
||||
20
supervisord.conf
Normal file
20
supervisord.conf
Normal file
@@ -0,0 +1,20 @@
|
||||
[supervisord]
|
||||
nodaemon=true
|
||||
|
||||
[program:op25-liq]
|
||||
command=/usr/bin/liquidsoap /op25/op25.liq
|
||||
directory=/op25
|
||||
autostart=true
|
||||
autorestart=true
|
||||
stderr_logfile=/var/log/supervisor/op25-liq.err.log
|
||||
stdout_logfile=/var/log/supervisor/op25-liq.out.log
|
||||
user=root
|
||||
|
||||
[program:drb-client-discord]
|
||||
command=uvicorn main:app --host 0.0.0.0 --port 8001 --reload
|
||||
directory=/app
|
||||
autostart=true
|
||||
autorestart=true
|
||||
stderr_logfile=/var/log/supervisor/main_app.err.log
|
||||
stdout_logfile=/var/log/supervisor/main_app.out.log
|
||||
user=root
|
||||
Reference in New Issue
Block a user