diff --git a/bot.py b/bot.py index e92f657..c71541b 100644 --- a/bot.py +++ b/bot.py @@ -5,6 +5,7 @@ import sound import configparser from discord.ext import commands from gqrxHandler import GQRXHandler +from op25Handler import OP25Handler # Init class for bot @@ -43,9 +44,8 @@ class Bot(commands.Bot): self.system_os_type = None self.sdr_started = False - if self.Handler == "gqrx": - print("Starting gqrx handler") - self.GQRXHandler = GQRXHandler() + # Check the handler being used + self.check_handler() # Set linux or windows self.check_os_type() @@ -56,6 +56,16 @@ class Bot(commands.Bot): # Check the ./modules folder for any modules (cog.py) self.check_for_modules() + # Check the handler being used during init + def check_handler(self): + if self.Handler == "gqrx": + print("Starting GQRX handler") + self.GQRXHandler = GQRXHandler() + + elif self.Handler == 'op25': + print("Starting OP25 handler") + self.OP25Handler = OP25Handler() + # Start the bot def start_bot(self): self.run(self.BOT_TOKEN) @@ -126,14 +136,22 @@ class Bot(commands.Bot): # 'Unlock' the bot self.Bot_Connected = False - if self.Handler == 'gqrx': + # Add commands for GQRX and OP25 + if self.Handler == 'gqrx' or self.Handler == 'op25': @self.command(name='chfreq', help="Use this command to change the frequency the bot is listening to. " "\nExample command: '@ chfreq wfm 104700000\n" - "Example command: '@ chfreq fm 154785000", + "Example command: '@ chfreq p25 154.785", brief="Changes radio frequency") async def chfreq(ctx, mode: str, freq: str, member: discord.Member = None): # Possible band-types that can be used - possible_modes = ['wfm', 'fm'] + possible_modes = [] + + if self.Handler == 'gqrx': + possible_modes = ['wfm', 'fm'] + + elif self.Handler == 'op25': + possible_modes = ['d', 'p25'] + member = member or ctx.author.display_name # Check to make sure the frequency input matches the syntax needed @@ -154,22 +172,26 @@ class Bot(commands.Bot): self.start_sdr() await self.set_activity() else: - await ctx.send(f"{str(member).capitalize()}, {mode} is not valid. You may only enter 'fm' or 'wbfm'") + await ctx.send(f"{str(member).capitalize()}, {mode} is not valid." + f" You may only enter {possible_modes}") else: - await ctx.send(f"{str(member).capitalize()}, {freq} is not valid. please refer to the help page '@ help chfreq'") + await ctx.send(f"{str(member).capitalize()}, {freq} is not valid. " + f"Please refer to the help page '@ help chfreq'") - @self.command(name='chsquelch', help="Use this command to change the squelch for the frequency" - "the bot is listening to", - brief="Changes radio squelch") - async def chsquelch(ctx, squelch: float, member: discord.Member = None): - member = member or ctx.author.display_name + # GQRX Specific commands + if self.Handler == 'gqrx': + @self.command(name='chsquelch', help="Use this command to change the squelch for the frequency" + "the bot is listening to", + brief="Changes radio squelch") + async def chsquelch(ctx, squelch: float, member: discord.Member = None): + member = member or ctx.author.display_name - self.squelch = squelch - await ctx.send(f"Ok {str(member).capitalize()}, I'm changing the squelch to {self.squelch}") + self.squelch = squelch + await ctx.send(f"Ok {str(member).capitalize()}, I'm changing the squelch to {self.squelch}") - # If the SDR is started, restart it with the updates - if self.sdr_started: - self.start_sdr() + # If the SDR is started, restart it with the updates + if self.sdr_started: + self.start_sdr() # Hidden admin commands @self.command(name='saveprofile', hidden=True) @@ -205,6 +227,7 @@ class Bot(commands.Bot): async def _stopsdr(ctx, member: discord.Member = None): self.stop_sdr() + # Load the proper OPUS library for the device being used def load_opus(self): # Check the system type and load the correct library if self.system_os_type == 'Linux_32': @@ -263,7 +286,7 @@ class Bot(commands.Bot): # Check to see if there is only one frequency def start_sdr(self): - if self.Handler == 'gqrx': + if self.Handler in ['gqrx', 'op25']: if type(self.freq) == str: # Single freq sent # Stop the SDR if it is running @@ -272,8 +295,12 @@ class Bot(commands.Bot): # Start the radio print(f"Starting freq: {self.freq}") - # Set the settings in GQRX - self.GQRXHandler.set_all_settings(self.mode, self.squelch, self.freq) + if self.Handler == 'gqrx': + # Set the settings in GQRX + self.GQRXHandler.set_all_settings(self.mode, self.squelch, self.freq) + + elif self.Handler == 'op25': + self.OP25Handler.set_op25_parameters(self.freq) # Set the started variable for later checks self.sdr_started = True @@ -282,6 +309,8 @@ class Bot(commands.Bot): def stop_sdr(self): if self.sdr_started: # Wait for the running processes to close + if self.Handler == 'op25': + self.OP25Handler.close_op25() # Need a way to 'close' GQRX self.sdr_started = False @@ -348,3 +377,5 @@ class Bot(commands.Bot): return False else: return False + + diff --git a/op25Handler.py b/op25Handler.py new file mode 100644 index 0000000..f01aad7 --- /dev/null +++ b/op25Handler.py @@ -0,0 +1,35 @@ +import threading +import subprocess +import os + + +class OP25Handler(threading.Thread): + def __init__(self): + super().__init__() + self.OP25Dir: str = "/home/pi/op25/op25/gr-op25_repeater/apps" + self.OP25Proc = None + + self.Frequency = None + + def run(self) -> None: + self.open_op25() + + def set_op25_parameters(self, _frequency): + self.Frequency = _frequency + + def open_op25(self): + if self.OP25Proc is not None: + self.close_op25() + + print(f"Starting OBS") + os.chdir(self.OP25Dir) + + self.OP25Proc = subprocess.Popen([f"./rx.py", "--args", "'rtl'", "-N", "'LNA:49'", "-s", "200000", "-o", + "25600", "-U", "-f", f"{self.Frequency}e6", "-X", "-2"]) + + def close_op25(self): + print(f"Closing OP25") + try: + self.OP25Proc.kill() + except Exception as e: + print(e)