Compare commits
29 Commits
5b3f9bfaba
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b65bea7856 | ||
|
|
bd8deeb44e | ||
| 7f455f427e | |||
|
|
ddfa9fc2a3 | ||
|
|
fb9f8a680f | ||
|
|
a26dd619b8 | ||
|
|
133f29635e | ||
|
|
cbc2a3fc86 | ||
|
|
b3a5dbb626 | ||
|
|
44684ed020 | ||
|
|
5ff1d6273f | ||
|
|
6324f82789 | ||
|
|
3086da0e2b | ||
|
|
021f27d62e | ||
|
|
d3e7e780f3 | ||
|
|
872bbf2965 | ||
|
|
e7956577d7 | ||
|
|
c31984e2d8 | ||
|
|
5191433e5d | ||
|
|
9dfee88789 | ||
|
|
46ec27c359 | ||
|
|
75b2d0007d | ||
| c616acd6af | |||
|
|
7e44e0e803 | ||
| 2418ac2701 | |||
| f9d30b0c8b | |||
| a5ff9fa1be | |||
|
|
9f10914b8b | ||
|
|
2c3c372da1 |
57
.gitea/workflows/build-nightly.yml
Normal file
57
.gitea/workflows/build-nightly.yml
Normal file
@@ -0,0 +1,57 @@
|
||||
name: release-tag
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- dev
|
||||
|
||||
jobs:
|
||||
release-image:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
DOCKER_LATEST: stable
|
||||
CONTAINER_NAME: drb-client-discord-bot
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up QEMU
|
||||
uses: docker/setup-qemu-action@v3
|
||||
|
||||
- name: Set up Docker BuildX
|
||||
uses: docker/setup-buildx-action@v3
|
||||
with: # replace it with your local IP
|
||||
config-inline: |
|
||||
[registry."git.vpn.cusano.net"]
|
||||
http = false
|
||||
insecure = false
|
||||
|
||||
- name: Login to DockerHub
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: git.vpn.cusano.net # replace it with your local IP
|
||||
username: ${{ secrets.GIT_REPO_USERNAME }}
|
||||
password: ${{ secrets.GIT_REPO_PASSWORD }}
|
||||
|
||||
- name: Get Meta
|
||||
id: meta
|
||||
run: |
|
||||
echo REPO_NAME=$(echo ${GITHUB_REPOSITORY} | awk -F"/" '{print $2}') >> $GITHUB_OUTPUT
|
||||
echo REPO_VERSION=$(git describe --tags --always | sed 's/^v//') >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Validate build configuration
|
||||
uses: docker/build-push-action@v6
|
||||
with:
|
||||
call: check
|
||||
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@v6
|
||||
with:
|
||||
context: .
|
||||
file: ./Dockerfile
|
||||
platforms: |
|
||||
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 }}
|
||||
git.vpn.cusano.net/${{ vars.DOCKER_ORG }}/${{ steps.meta.outputs.REPO_NAME }}/${{ env.CONTAINER_NAME }}:${{ env.DOCKER_LATEST }}
|
||||
@@ -8,10 +8,8 @@ on:
|
||||
jobs:
|
||||
release-image:
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: catthehacker/ubuntu:act-latest
|
||||
env:
|
||||
DOCKER_LATEST: nightly
|
||||
DOCKER_LATEST: stable
|
||||
CONTAINER_NAME: drb-client-discord-bot
|
||||
steps:
|
||||
- name: Checkout
|
||||
@@ -52,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 }}
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,4 +2,5 @@ __pycache__*
|
||||
bot-poc.py
|
||||
configs*
|
||||
.env
|
||||
*.log
|
||||
*.log*
|
||||
.venv
|
||||
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)
|
||||
|
||||
@@ -30,15 +30,15 @@ class AudioStream:
|
||||
if _input:
|
||||
self.paInstance_kwargs['input_device_index'] = _input_device_index
|
||||
else:
|
||||
LOGGER.warning(f"[AudioStream.__init__]:\tInput was not enabled."
|
||||
f" Reinitialize with '_input=True'")
|
||||
LOGGER.warning("[AudioStream.__init__]:\tInput was not enabled."
|
||||
" Reinitialize with '_input=True'")
|
||||
|
||||
if _output_device_index:
|
||||
if _output:
|
||||
self.paInstance_kwargs['output_device_index'] = _output_device_index
|
||||
else:
|
||||
LOGGER.warning(f"[AudioStream.__init__]:\tOutput was not enabled."
|
||||
f" Reinitialize with '_output=True'")
|
||||
LOGGER.warning("[AudioStream.__init__]:\tOutput was not enabled."
|
||||
" Reinitialize with '_output=True'")
|
||||
|
||||
if _init_on_startup:
|
||||
# Init PyAudio instance
|
||||
@@ -59,15 +59,15 @@ class AudioStream:
|
||||
if self.paInstance_kwargs['input']:
|
||||
self.paInstance_kwargs['input_device_index'] = _new_input_device_index
|
||||
else:
|
||||
LOGGER.warning(f"[AudioStream.init_stream]:\tInput was not enabled when initialized."
|
||||
f" Reinitialize with '_input=True'")
|
||||
LOGGER.warning("[AudioStream.init_stream]:\tInput was not enabled when initialized."
|
||||
" Reinitialize with '_input=True'")
|
||||
|
||||
if _new_output_device_index:
|
||||
if self.paInstance_kwargs['output']:
|
||||
self.paInstance_kwargs['output_device_index'] = _new_output_device_index
|
||||
else:
|
||||
LOGGER.warning(f"[AudioStream.init_stream]:\tOutput was not enabled when initialized."
|
||||
f" Reinitialize with '_output=True'")
|
||||
LOGGER.warning("[AudioStream.init_stream]:\tOutput was not enabled when initialized."
|
||||
" Reinitialize with '_output=True'")
|
||||
|
||||
self.close_if_open()
|
||||
|
||||
@@ -80,7 +80,7 @@ class AudioStream:
|
||||
if self.stream.is_active():
|
||||
self.stream.stop_stream()
|
||||
self.stream.close()
|
||||
LOGGER.debug(f"[ReopenStream.close_if_open]:\t Stream was open; It was closed.")
|
||||
LOGGER.debug("[ReopenStream.close_if_open]:\t Stream was open; It was closed.")
|
||||
|
||||
def list_devices(self, _display_input_devices: bool = True, _display_output_devices: bool = True):
|
||||
LOGGER.info('Getting a list of the devices connected')
|
||||
@@ -126,7 +126,7 @@ class NoiseGate(AudioStream):
|
||||
def run(self) -> None:
|
||||
global voice_connection
|
||||
# Start the audio stream
|
||||
LOGGER.debug(f"Starting stream")
|
||||
LOGGER.debug("Starting stream")
|
||||
self.stream.start_stream()
|
||||
# Start the stream to discord
|
||||
self.core()
|
||||
@@ -139,15 +139,15 @@ class NoiseGate(AudioStream):
|
||||
time.sleep(.2)
|
||||
|
||||
if not voice_connection.is_playing():
|
||||
LOGGER.debug(f"Playing stream to discord")
|
||||
LOGGER.debug("Playing stream to discord")
|
||||
voice_connection.play(self.NGStream, after=self.core)
|
||||
|
||||
async def close(self):
|
||||
LOGGER.debug(f"Closing")
|
||||
LOGGER.debug("Closing")
|
||||
await voice_connection.disconnect()
|
||||
if self.stream.is_active:
|
||||
self.stream.stop_stream()
|
||||
LOGGER.debug(f"Stopping stream")
|
||||
LOGGER.debug("Stopping stream")
|
||||
|
||||
|
||||
# noinspection PyUnresolvedReferences
|
||||
@@ -155,7 +155,7 @@ class NoiseGateStream(discord.AudioSource):
|
||||
def __init__(self, _stream):
|
||||
super(NoiseGateStream, self).__init__()
|
||||
self.stream = _stream # The actual audio stream object
|
||||
self.NG_fadeout = 240/20 # Fadeout value used to hold the noisegate after de-triggering
|
||||
self.NG_fadeout = 240 / 20 # Fadeout value used to hold the noisegate after de-triggering
|
||||
self.NG_fadeout_count = 0 # A count set when the noisegate is triggered and was de-triggered
|
||||
self.process_set_count = 0 # Counts how many processes have been made
|
||||
|
||||
|
||||
@@ -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):
|
||||
@@ -87,18 +112,27 @@ class DiscordBotManager:
|
||||
|
||||
try:
|
||||
voice_client = await channel.connect(timeout=60.0, reconnect=True)
|
||||
LOGGER.debug(f"Voice Connected.")
|
||||
LOGGER.debug("Voice Connected.")
|
||||
streamHandler = NoiseGate(
|
||||
_input_device_index=device_id,
|
||||
_voice_connection=voice_client,
|
||||
_noise_gate_threshold=ng_threshold)
|
||||
streamHandler.run()
|
||||
LOGGER.debug(f"Stream is running.")
|
||||
LOGGER.debug("Stream is running.")
|
||||
self.voice_clients[guild_id] = voice_client
|
||||
LOGGER.info(f"Joined guild {guild_id} voice channel {channel_id} and stream is running.")
|
||||
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.")
|
||||
@@ -119,21 +153,27 @@ class DiscordBotManager:
|
||||
if os.name == 'nt':
|
||||
if processor == "AMD64":
|
||||
opus.load_opus(os.path.join(script_dir, './opus/libopus_amd64.dll'))
|
||||
LOGGER.info(f"Loaded OPUS library for AMD64")
|
||||
LOGGER.info("Loaded OPUS library for AMD64")
|
||||
return "AMD64"
|
||||
else:
|
||||
if processor == "aarch64":
|
||||
opus.load_opus(os.path.join(script_dir, './opus/libopus_aarcch64.so'))
|
||||
LOGGER.info(f"Loaded OPUS library for aarch64")
|
||||
LOGGER.info("Loaded OPUS library for aarch64")
|
||||
return "aarch64"
|
||||
elif processor == "armv7l":
|
||||
opus.load_opus(os.path.join(script_dir, './opus/libopus_armv7l.so'))
|
||||
LOGGER.info(f"Loaded OPUS library for armv7l")
|
||||
LOGGER.info("Loaded OPUS library for armv7l")
|
||||
return "armv7l"
|
||||
|
||||
async def set_presence(self, presence: str):
|
||||
""" Set the presense (activity) of the bot """
|
||||
async def set_presence(self, system_name: str):
|
||||
""" Set the presence (activity) of the bot """
|
||||
if not self.bot:
|
||||
LOGGER.warning("Bot is not running, cannot set presence.")
|
||||
return
|
||||
|
||||
try:
|
||||
await self.bot.change_presence(activity=Activity(type=ActivityType.listening, name=presence))
|
||||
activity = Activity(type=ActivityType.listening, name=system_name)
|
||||
await self.bot.change_presence(activity=activity)
|
||||
LOGGER.info(f"Bot presence set to 'Listening to {system_name}'")
|
||||
except Exception as pe:
|
||||
LOGGER.error(f"Unable to set presence: '{pe}'")
|
||||
70
app/internal/op25_config_utls.py
Normal file
70
app/internal/op25_config_utls.py
Normal file
@@ -0,0 +1,70 @@
|
||||
import csv
|
||||
import json
|
||||
from models import TalkgroupTag
|
||||
from typing import List
|
||||
from internal.logger import create_logger
|
||||
|
||||
LOGGER = create_logger(__name__)
|
||||
|
||||
def save_talkgroup_tags(talkgroup_tags: List[TalkgroupTag]) -> None:
|
||||
"""
|
||||
Writes a list of tags to the tags file.
|
||||
|
||||
Args:
|
||||
talkgroup_tags (List[TalkgroupTag]): The list of TalkgroupTag instances.
|
||||
"""
|
||||
with open("/configs/active.cfg.tags.tsv", 'w', newline='', encoding='utf-8') as file:
|
||||
writer = csv.writer(file, delimiter='\t', lineterminator='\n')
|
||||
# Write rows
|
||||
for tag in talkgroup_tags:
|
||||
writer.writerow([tag.talkgroup, tag.tagDec])
|
||||
|
||||
def save_whitelist(talkgroup_tags: List[int]) -> None:
|
||||
"""
|
||||
Writes a list of talkgroups to the whitelists file.
|
||||
|
||||
Args:
|
||||
talkgroup_tags (List[int]): The list of decimals to whitelist.
|
||||
"""
|
||||
with open("/configs/active.cfg.whitelist.tsv", 'w', newline='', encoding='utf-8') as file:
|
||||
writer = csv.writer(file, delimiter='\t', lineterminator='\n')
|
||||
# Write rows
|
||||
for tag in talkgroup_tags:
|
||||
writer.writerow([tag])
|
||||
|
||||
def del_none_in_dict(d):
|
||||
"""
|
||||
Delete keys with the value ``None`` in a dictionary, recursively.
|
||||
|
||||
This alters the input so you may wish to ``copy`` the dict first.
|
||||
"""
|
||||
for key, value in list(d.items()):
|
||||
LOGGER.info(f"Key: '{key}'\nValue: '{value}'")
|
||||
if value is None:
|
||||
del d[key]
|
||||
elif isinstance(value, dict):
|
||||
del_none_in_dict(value)
|
||||
elif isinstance(value, list):
|
||||
for iterative_value in value:
|
||||
del_none_in_dict(iterative_value)
|
||||
return d # For convenience
|
||||
|
||||
def get_current_system_from_config() -> str:
|
||||
# Get the current config
|
||||
with open('/configs/active.cfg.json', 'r') as f:
|
||||
json_data = f.read()
|
||||
if isinstance(json_data, str):
|
||||
try:
|
||||
data = json.loads(json_data)
|
||||
except json.JSONDecodeError:
|
||||
return None
|
||||
elif isinstance(json_data, dict):
|
||||
data = json_data
|
||||
else:
|
||||
return None
|
||||
|
||||
if "channels" in data and isinstance(data["channels"], list) and len(data["channels"]) > 0:
|
||||
first_channel = data["channels"][0]
|
||||
if "name" in first_channel:
|
||||
return first_channel["name"]
|
||||
return None
|
||||
15
app/main.py
15
app/main.py
@@ -1,13 +1,9 @@
|
||||
import asyncio
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional, Dict
|
||||
from fastapi import FastAPI
|
||||
import routers.op25_controller as op25_controller
|
||||
import routers.pulse as pulse
|
||||
import routers.bot as bot
|
||||
from internal.logger import create_logger
|
||||
from internal.bot_manager import DiscordBotManager
|
||||
|
||||
# Initialize logging
|
||||
LOGGER = create_logger(__name__)
|
||||
@@ -15,6 +11,9 @@ LOGGER = create_logger(__name__)
|
||||
# Define FastAPI app
|
||||
app = FastAPI()
|
||||
|
||||
app.include_router(op25_controller.router, prefix="/op25")
|
||||
# Initialize Discord Bot Manager
|
||||
bot_manager_instance = DiscordBotManager()
|
||||
|
||||
app.include_router(op25_controller.create_op25_router(bot_manager=bot_manager_instance), prefix="/op25")
|
||||
app.include_router(pulse.router, prefix="/pulse")
|
||||
app.include_router(bot.router, prefix="/bot")
|
||||
app.include_router(bot.create_bot_router(bot_manager=bot_manager_instance), prefix="/bot")
|
||||
|
||||
@@ -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,24 +1,17 @@
|
||||
import asyncio
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional, Dict
|
||||
from models import BotConfig, VoiceChannelRequest
|
||||
from models import BotConfig, VoiceChannelJoinRequest, VoiceChannelLeaveRequest
|
||||
from internal.bot_manager import DiscordBotManager
|
||||
from internal.logger import create_logger
|
||||
|
||||
LOGGER = create_logger(__name__)
|
||||
|
||||
# Define FastAPI app
|
||||
router = APIRouter()
|
||||
# Function to create router
|
||||
def create_bot_router(bot_manager: DiscordBotManager):
|
||||
router = APIRouter()
|
||||
|
||||
# Initialize Discord Bot Manager
|
||||
bot_manager = DiscordBotManager()
|
||||
|
||||
# API Endpoints
|
||||
@router.post("/start_bot")
|
||||
async def start_bot(config: BotConfig):
|
||||
# API Endpoints
|
||||
@router.post("/start_bot")
|
||||
async def start_bot(config: BotConfig):
|
||||
try:
|
||||
await bot_manager.start_bot(config.token)
|
||||
return {"status": "Bot started successfully."}
|
||||
@@ -26,8 +19,8 @@ async def start_bot(config: BotConfig):
|
||||
LOGGER.error(f"Error starting bot: {e}")
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
@router.post("/stop_bot")
|
||||
async def stop_bot():
|
||||
@router.post("/stop_bot")
|
||||
async def stop_bot():
|
||||
try:
|
||||
await bot_manager.stop_bot()
|
||||
return {"status": "Bot stopped successfully."}
|
||||
@@ -35,8 +28,8 @@ async def stop_bot():
|
||||
LOGGER.error(f"Error stopping bot: {e}")
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
@router.post("/join_voice")
|
||||
async def join_voice_channel(request: VoiceChannelRequest):
|
||||
@router.post("/join_voice")
|
||||
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}."}
|
||||
@@ -44,8 +37,8 @@ async def join_voice_channel(request: VoiceChannelRequest):
|
||||
LOGGER.error(f"Error joining voice channel: {e}")
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
@router.post("/leave_voice")
|
||||
async def leave_voice_channel(request: VoiceChannelRequest):
|
||||
@router.post("/leave_voice")
|
||||
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."}
|
||||
@@ -53,10 +46,13 @@ async def leave_voice_channel(request: VoiceChannelRequest):
|
||||
LOGGER.error(f"Error leaving voice channel: {e}")
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
@router.get("/status")
|
||||
async def get_status():
|
||||
@router.get("/status")
|
||||
async def get_status():
|
||||
status = {
|
||||
"bot_running": bot_manager.bot is not None and not bot_manager.bot.is_closed(),
|
||||
"connected_guilds": list(bot_manager.voice_clients.keys())
|
||||
"connected_guilds": list(bot_manager.voice_clients.keys()),
|
||||
"active_token": bot_manager.token
|
||||
}
|
||||
return status
|
||||
|
||||
return router
|
||||
|
||||
@@ -1,22 +1,24 @@
|
||||
from fastapi import HTTPException, APIRouter
|
||||
from pydantic import BaseModel
|
||||
import subprocess
|
||||
import os
|
||||
import signal
|
||||
import json
|
||||
import csv
|
||||
from models import *
|
||||
from models import ConfigGenerator, DecodeMode, ChannelConfig, DeviceConfig, TrunkingConfig, TrunkingChannelConfig, AudioConfig, TerminalConfig
|
||||
from internal.logger import create_logger
|
||||
from internal.bot_manager import DiscordBotManager
|
||||
from internal.op25_config_utls import save_talkgroup_tags, save_whitelist, del_none_in_dict, get_current_system_from_config
|
||||
|
||||
router = APIRouter()
|
||||
LOGGER = create_logger(__name__)
|
||||
|
||||
op25_process = None
|
||||
OP25_PATH = "/op25/op25/gr-op25_repeater/apps/"
|
||||
OP25_SCRIPT = "run_multi-rx_service.sh"
|
||||
|
||||
@router.post("/start")
|
||||
async def start_op25():
|
||||
def create_op25_router(bot_manager: DiscordBotManager):
|
||||
router = APIRouter()
|
||||
|
||||
@router.post("/start")
|
||||
async def start_op25():
|
||||
global op25_process
|
||||
if op25_process is None:
|
||||
try:
|
||||
@@ -28,8 +30,8 @@ async def start_op25():
|
||||
else:
|
||||
return {"status": "OP25 already running"}
|
||||
|
||||
@router.post("/stop")
|
||||
async def stop_op25():
|
||||
@router.post("/stop")
|
||||
async def stop_op25():
|
||||
global op25_process
|
||||
if op25_process is not None:
|
||||
try:
|
||||
@@ -41,12 +43,12 @@ async def stop_op25():
|
||||
else:
|
||||
return {"status": "OP25 is not running"}
|
||||
|
||||
@router.get("/status")
|
||||
async def get_status():
|
||||
@router.get("/status")
|
||||
async def get_status():
|
||||
return {"status": "running" if op25_process else "stopped"}
|
||||
|
||||
@router.post("/generate-config")
|
||||
async def generate_config(generator: ConfigGenerator):
|
||||
@router.post("/generate-config")
|
||||
async def generate_config(generator: ConfigGenerator):
|
||||
try:
|
||||
if generator.type == DecodeMode.P25:
|
||||
channels = [ChannelConfig(
|
||||
@@ -105,49 +107,20 @@ async def generate_config(generator: ConfigGenerator):
|
||||
with open('/configs/active.cfg.json', 'w') as f:
|
||||
json.dump(del_none_in_dict(config_dict), f, indent=2)
|
||||
|
||||
# Set the presence of the bot (if it's online)
|
||||
await bot_manager.set_presence(generator.systemName)
|
||||
|
||||
return {"message": "Config exported to '/configs/active.cfg.json'"}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
def save_talkgroup_tags(talkgroup_tags: List[TalkgroupTag]) -> None:
|
||||
"""
|
||||
Writes a list of tags to the tags file.
|
||||
@router.post("/update-presence")
|
||||
async def update_presence():
|
||||
current_system = get_current_system_from_config()
|
||||
if not current_system:
|
||||
raise HTTPException(status_code=500, detail="Unable to get current system.")
|
||||
|
||||
Args:
|
||||
talkgroup_tags (List[TalkgroupTag]): The list of TalkgroupTag instances.
|
||||
"""
|
||||
with open("/configs/active.cfg.tags.tsv", 'w', newline='', encoding='utf-8') as file:
|
||||
writer = csv.writer(file, delimiter='\t', lineterminator='\n')
|
||||
# Write rows
|
||||
for tag in talkgroup_tags:
|
||||
writer.writerow([tag.talkgroup, tag.tagDec])
|
||||
await bot_manager.set_presence(current_system)
|
||||
return current_system
|
||||
|
||||
def save_whitelist(talkgroup_tags: List[int]) -> None:
|
||||
"""
|
||||
Writes a list of talkgroups to the whitelists file.
|
||||
|
||||
Args:
|
||||
talkgroup_tags (List[int]): The list of decimals to whitelist.
|
||||
"""
|
||||
with open("/configs/active.cfg.whitelist.tsv", 'w', newline='', encoding='utf-8') as file:
|
||||
writer = csv.writer(file, delimiter='\t', lineterminator='\n')
|
||||
# Write rows
|
||||
for tag in talkgroup_tags:
|
||||
writer.writerow([tag])
|
||||
|
||||
def del_none_in_dict(d):
|
||||
"""
|
||||
Delete keys with the value ``None`` in a dictionary, recursively.
|
||||
|
||||
This alters the input so you may wish to ``copy`` the dict first.
|
||||
"""
|
||||
for key, value in list(d.items()):
|
||||
LOGGER.info(f"Key: '{key}'\nValue: '{value}'")
|
||||
if value is None:
|
||||
del d[key]
|
||||
elif isinstance(value, dict):
|
||||
del_none_in_dict(value)
|
||||
elif isinstance(value, list):
|
||||
for iterative_value in value:
|
||||
del_none_in_dict(iterative_value)
|
||||
return d # For convenience
|
||||
return router
|
||||
|
||||
15
op25-liq.service
Normal file
15
op25-liq.service
Normal file
@@ -0,0 +1,15 @@
|
||||
[Unit]
|
||||
Description=op25-liq
|
||||
After=syslog.target network.target nss-lookup.target network-online.target
|
||||
Requires=network-online.target
|
||||
|
||||
[Service]
|
||||
User=1000
|
||||
Group=1000
|
||||
WorkingDirectory=/op25/op25/gr-op25_repeater/apps
|
||||
ExecStart=/usr/bin/liquidsoap op25.liq
|
||||
RestartSec=5
|
||||
Restart=on-failure
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -5,4 +5,3 @@ uvicorn
|
||||
fastapi
|
||||
pyaudio
|
||||
argparse
|
||||
pyaudio
|
||||
Reference in New Issue
Block a user