From 78d9b9e30733b64b84ee80345ffe8ffd1cff080b Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sun, 27 Feb 2022 21:43:28 -0500 Subject: [PATCH 01/27] Cleanup --- bot.py | 1 - main.py | 34 +++++++++++++++++++--------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/bot.py b/bot.py index 24e9e25..e35d144 100644 --- a/bot.py +++ b/bot.py @@ -2,7 +2,6 @@ import asyncio import os import platform import discord - import BotResources import sound import configparser diff --git a/main.py b/main.py index f401c0e..8887442 100644 --- a/main.py +++ b/main.py @@ -27,18 +27,21 @@ class BotDeviceNotFound(Exception): def main(**passed_config): - print('Checking config file...') - if not check_if_config_exists(): - print("No config file exists, please enter this information now") - write_config_file(init=True) + # Don't create a config if the user passed parameters + if len(passed_config.keys()) == 0: + print('Checking config file...') + if not check_if_config_exists(): + print("No config file exists, please enter this information now") + write_config_file(init=True) config = read_config_file() # Overwrite config options if they were passed - for sub in config: - # checking if key present in other dictionary - if sub in passed_config and passed_config[sub]: - config[sub] = passed_config[sub] + if len(passed_config.keys()) == 0: + for sub in config: + # checking if key present in other dictionary + if sub in passed_config and passed_config[sub]: + config[sub] = passed_config[sub] print('Starting Bot...') @@ -106,20 +109,21 @@ def cmd_arguments(): return args -if __name__ == '__main__': - args = cmd_arguments() - if not all(args.values()): +if __name__ == '__main__': + cmd_args = cmd_arguments() + + if not all(cmd_args.values()): print("Passed arguments:") - for arg in args: - if args[arg]: - print(f"\t{arg}:\t{args[arg]}") + for arg in cmd_args: + if cmd_args[arg]: + print(f"\t{arg}:\t{cmd_args[arg]}") try: print('Starting...') while True: try: - main(**args) + main(**cmd_args) except BotDeviceNotFound: print("Restarting...") time.sleep(2) From 5cc8a8c1189d648eb7c3137b9c9bf572c3d8a9e6 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Wed, 16 Mar 2022 01:15:22 -0400 Subject: [PATCH 02/27] Changed to sync sleep --- op25Handler.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/op25Handler.py b/op25Handler.py index b35f8dd..a1ab84b 100644 --- a/op25Handler.py +++ b/op25Handler.py @@ -2,6 +2,7 @@ import threading import subprocess import asyncio import os +import time class OP25Handler: #(threading.Thread): @@ -12,12 +13,16 @@ class OP25Handler: #(threading.Thread): self.Frequency = None + self.HTTP_ENABLED = False + def start(self) -> None: self.open_op25() - def set_op25_parameters(self, _frequency): + def set_op25_parameters(self, _frequency, _http_enabled: bool = False): self.Frequency = _frequency + self.HTTP_ENABLED = _http_enabled + def open_op25(self): if self.OP25Proc is not None: self.close_op25() @@ -25,15 +30,17 @@ class OP25Handler: #(threading.Thread): print(f"Starting OP25") cwd = os.getcwd() - print(cwd) os.chdir(self.OP25Dir) - print(os.getcwd()) + if self.HTTP_ENABLED: + self.OP25Proc = subprocess.Popen([f"./rx.py", "--args", "rtl", "-N", "LNA:49", "-s", + "200000", "-o", "25600", "-U", "-f", f"{self.Frequency}e6", "-X", "-2", + "-l" "http:0.0.0.0:8080"]) - self.OP25Proc = subprocess.Popen([f"./rx.py", "--args", "rtl", "-N", "LNA:49", "-s", - "200000", "-o", "25600", "-U", "-f", f"{self.Frequency}e6", "-X", "-2", - "-l" "http:0.0.0.0:8080"]) + else: + self.OP25Proc = subprocess.Popen([f"./rx.py", "--args", "rtl", "-N", "LNA:49", "-s", + "200000", "-o", "25600", "-U", "-f", f"{self.Frequency}e6", "-X", "-2"]) os.chdir(cwd) @@ -48,7 +55,7 @@ class OP25Handler: #(threading.Thread): if seconds_waited % 5 == 0: print("Terminating OP25") self.OP25Proc.terminate() - asyncio.sleep(1000) + time.sleep(1) print(f"Waited {seconds_waited} seconds") seconds_waited += 1 From 1f30e05e464de5cbf0dd05137c39a2e55e95ecd5 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Wed, 16 Mar 2022 01:24:23 -0400 Subject: [PATCH 03/27] Revert "Cleanup" This reverts commit 78d9b9e30733b64b84ee80345ffe8ffd1cff080b. --- bot.py | 1 + main.py | 32 ++++++++++++++------------------ 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/bot.py b/bot.py index e35d144..24e9e25 100644 --- a/bot.py +++ b/bot.py @@ -2,6 +2,7 @@ import asyncio import os import platform import discord + import BotResources import sound import configparser diff --git a/main.py b/main.py index 8887442..f401c0e 100644 --- a/main.py +++ b/main.py @@ -27,21 +27,18 @@ class BotDeviceNotFound(Exception): def main(**passed_config): - # Don't create a config if the user passed parameters - if len(passed_config.keys()) == 0: - print('Checking config file...') - if not check_if_config_exists(): - print("No config file exists, please enter this information now") - write_config_file(init=True) + print('Checking config file...') + if not check_if_config_exists(): + print("No config file exists, please enter this information now") + write_config_file(init=True) config = read_config_file() # Overwrite config options if they were passed - if len(passed_config.keys()) == 0: - for sub in config: - # checking if key present in other dictionary - if sub in passed_config and passed_config[sub]: - config[sub] = passed_config[sub] + for sub in config: + # checking if key present in other dictionary + if sub in passed_config and passed_config[sub]: + config[sub] = passed_config[sub] print('Starting Bot...') @@ -109,21 +106,20 @@ def cmd_arguments(): return args - if __name__ == '__main__': - cmd_args = cmd_arguments() + args = cmd_arguments() - if not all(cmd_args.values()): + if not all(args.values()): print("Passed arguments:") - for arg in cmd_args: - if cmd_args[arg]: - print(f"\t{arg}:\t{cmd_args[arg]}") + for arg in args: + if args[arg]: + print(f"\t{arg}:\t{args[arg]}") try: print('Starting...') while True: try: - main(**cmd_args) + main(**args) except BotDeviceNotFound: print("Restarting...") time.sleep(2) From 73fa5600405dce8aa04c97cb091d5a08ba8aff21 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Wed, 16 Mar 2022 01:29:09 -0400 Subject: [PATCH 04/27] Removed noisegate (again) --- NoiseGate-v2.py | 130 ------------------------------------------------ NoiseGate.py | 67 ------------------------- 2 files changed, 197 deletions(-) delete mode 100644 NoiseGate-v2.py delete mode 100644 NoiseGate.py diff --git a/NoiseGate-v2.py b/NoiseGate-v2.py deleted file mode 100644 index a06e0c5..0000000 --- a/NoiseGate-v2.py +++ /dev/null @@ -1,130 +0,0 @@ -import pyaudio -import struct -import numpy -import time -from threading import Thread - -sound_buffer = [] - - -class AudioSteam(Thread): - def __init__(self, _channels: int = 1, _sample_rate: int = 48000, _frames_per_buffer: int = 2048, - _input_device_index: int = None, _output_device_index: int = None, _input: bool = True, - _output: bool = True): - super(AudioSteam, self).__init__() - self.paInstance_kwargs = { - 'format': pyaudio.paFloat32, - 'channels': _channels, - 'rate': _sample_rate, - 'input': _input, - 'output': _output, - 'frames_per_buffer': _frames_per_buffer, - 'stream_callback': callback - } - - if _input_device_index: - if _input: - self.paInstance_kwargs['input_device_index'] = _input_device_index - else: - print(f"[AudioStream.__init__]:\tInput was not enabled." - f" Reinitialize with '_input=True'") - - if _output_device_index: - if _output: - self.paInstance_kwargs['output_device_index'] = _output_device_index - else: - print(f"[AudioStream.__init__]:\tOutput was not enabled." - f" Reinitialize with '_output=True'") - - # Init PyAudio instance - self.paInstance = pyaudio.PyAudio() - - # Define and initialize stream object if we have been passed a device ID (pyaudio.open) - self.stream = None - if _output_device_index and _input_device_index: - self.init_stream() - - # temp section - - def init_stream(self, _new_output_device_index: int = None, _new_input_device_index: int = None): - # Check what device was asked to be changed (or set) - if _new_input_device_index: - if self.paInstance_kwargs['input']: - self.paInstance_kwargs['input_device_index'] = _new_input_device_index - else: - print(f"[AudioStream.init_stream]:\tInput was not enabled when initialized." - f" 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: - print(f"[AudioStream.init_stream]:\tOutput was not enabled when initialized." - f" Reinitialize with '_output=True'") - - self.close_if_open() - - # Reopen the stream - self.stream = self.paInstance.open(**self.paInstance_kwargs) - - def close_if_open(self): - # Stop the stream if it is started - if self.stream: - if self.stream.is_active(): - self.stream.stop_stream() - self.stream.close() - print(f"[ReopenStream.close_if_open]:\t Stream was open; It was closed.") - - def list_devices(self, _show_input_devices: bool = True, _show_output_devices: bool = True): - info = self.paInstance.get_host_api_info_by_index(0) - numdevices = info.get('deviceCount') - - for i in range(0, numdevices): - if (self.paInstance.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0: - if _show_input_devices: - print("Input Device id ", i, " - ", - self.paInstance.get_device_info_by_host_api_device_index(0, i).get('name')) - - if (self.paInstance.get_device_info_by_host_api_device_index(0, i).get('maxOutputChannels')) > 0: - if _show_output_devices: - print("Output Device id ", i, " - ", - self.paInstance.get_device_info_by_host_api_device_index(0, i).get('name')) - - -class NoiseGate(AudioSteam): - def __init__(self, **kwargs): - super().__init__(**kwargs) - - def run(self) -> None: - # Start the audio stream - self.stream.start_stream() - - global sound_buffer - # While the stream is running, display the stream buffer in floats? - while self.stream.is_active(): - if len(sound_buffer) > 0: - for buffer in sound_buffer: - volume_normalization = numpy.linalg.norm(numpy.fromstring(buffer)) * 10 - print(str(float(volume_normalization))) - - self.stream.stop_stream() - self.stream.close() - - self.paInstance.terminate() - - -def callback(in_data, frame_count, time_info, status): - global sound_buffer - - sound_buffer.append(in_data) - - return in_data, pyaudio.paContinue - - -if __name__ == '__main__': - input_index = int(input("Input:\t")) - output_index = int(input("Output:\t")) - - ng = NoiseGate(_input_device_index=input_index, _output_device_index=output_index) - - ng.start() diff --git a/NoiseGate.py b/NoiseGate.py deleted file mode 100644 index 70e8171..0000000 --- a/NoiseGate.py +++ /dev/null @@ -1,67 +0,0 @@ -import threading -import time - -import numpy as np -import discord -import sounddevice as sd -from threading import Thread, Event - -noise_gate_trigger = 0 # Set this value for the trigger on the noise-gate -voice_connection = None -audio_stream = None -last_callback_value = 0 - - -class NoiseGate(Thread): - def __init__(self, trigger_value: int = 700): - global noise_gate_trigger - super(NoiseGate, self).__init__() - self.stream = None - self.stop_NG = threading.Event() - self.NG_Started = threading.Event() - noise_gate_trigger = trigger_value - - def init_stream(self, num, _voice_connection, _audio_stream): - global voice_connection, audio_stream - self.stream = sd.InputStream(device=num, callback=stream_callback) - voice_connection = _voice_connection - audio_stream = _audio_stream - - def run(self) -> None: - self.NG_Started.set() - self.stream.start() - while not self.stop_NG.is_set(): - #print("Main loop start") - if last_callback_value >= noise_gate_trigger: - trigger_noise_gate(True) - while last_callback_value >= noise_gate_trigger: - time.sleep(6) - trigger_noise_gate(False) - #print("Main loop end") - - self.stream.stop() - self.stream.close() - self.NG_Started.clear() - voice_connection.stop() - print('Thread #%s stopped' % self.ident) - - -def stream_callback(indata, *args): - global last_callback_value - volume_normalization = np.linalg.norm(indata) * 10 - last_callback_value = int(volume_normalization) - #print(f"Callback Value: {last_callback_value}") - - -def trigger_noise_gate(triggered: bool): - if triggered: - if not voice_connection.is_playing(): - voice_connection.play(discord.PCMAudio(audio_stream)) - # print("|" * int(volume_normalization / 4)) - print("Noise Gate was Triggered") - #time.sleep(10) - else: - if voice_connection.is_playing(): - print("Noise Gate stopped") - voice_connection.pause() - # try disconnecting and reconnecting From a1f9869e8e8d563cc27179b4f3dc9383049f5b70 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Wed, 16 Mar 2022 01:34:02 -0400 Subject: [PATCH 05/27] Undo revert --- bot.py | 1 - main.py | 34 +++++++++++++++++++--------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/bot.py b/bot.py index 24e9e25..e35d144 100644 --- a/bot.py +++ b/bot.py @@ -2,7 +2,6 @@ import asyncio import os import platform import discord - import BotResources import sound import configparser diff --git a/main.py b/main.py index f401c0e..8887442 100644 --- a/main.py +++ b/main.py @@ -27,18 +27,21 @@ class BotDeviceNotFound(Exception): def main(**passed_config): - print('Checking config file...') - if not check_if_config_exists(): - print("No config file exists, please enter this information now") - write_config_file(init=True) + # Don't create a config if the user passed parameters + if len(passed_config.keys()) == 0: + print('Checking config file...') + if not check_if_config_exists(): + print("No config file exists, please enter this information now") + write_config_file(init=True) config = read_config_file() # Overwrite config options if they were passed - for sub in config: - # checking if key present in other dictionary - if sub in passed_config and passed_config[sub]: - config[sub] = passed_config[sub] + if len(passed_config.keys()) == 0: + for sub in config: + # checking if key present in other dictionary + if sub in passed_config and passed_config[sub]: + config[sub] = passed_config[sub] print('Starting Bot...') @@ -106,20 +109,21 @@ def cmd_arguments(): return args -if __name__ == '__main__': - args = cmd_arguments() - if not all(args.values()): +if __name__ == '__main__': + cmd_args = cmd_arguments() + + if not all(cmd_args.values()): print("Passed arguments:") - for arg in args: - if args[arg]: - print(f"\t{arg}:\t{args[arg]}") + for arg in cmd_args: + if cmd_args[arg]: + print(f"\t{arg}:\t{cmd_args[arg]}") try: print('Starting...') while True: try: - main(**args) + main(**cmd_args) except BotDeviceNotFound: print("Restarting...") time.sleep(2) From 897e53d4c04af311be4c7bd8b9bd9e2de181622c Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Wed, 16 Mar 2022 02:20:35 -0400 Subject: [PATCH 06/27] Changed the order of operations on boot so squelch is changed first --- gqrxHandler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gqrxHandler.py b/gqrxHandler.py index d230516..b5cf96c 100644 --- a/gqrxHandler.py +++ b/gqrxHandler.py @@ -43,6 +43,6 @@ class GQRXHandler(): tel_conn.close() def set_all_settings(self, mode, squelch, freq): + self.change_squelch(squelch) self.change_mode(mode) self.change_freq(freq) - self.change_squelch(squelch) From db0922bf68744ca1a692f41d9fe28e31af12a1a0 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Wed, 16 Mar 2022 02:20:47 -0400 Subject: [PATCH 07/27] Updated TODO --- TODO.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TODO.md b/TODO.md index 679fd44..3c1fd84 100644 --- a/TODO.md +++ b/TODO.md @@ -4,8 +4,8 @@ - *May* have been fixed with the noise gate? - [ ] Fix bug that shows different index number for audio device selection on linux - [x] Add more proper help text with real examples for discord commands -- [ ] Add command line args with argparse for main bot - - [ ] Add method for user to change audio device without redoing entire config file +- [x] Add command line args with argparse for main bot + - [x] Add method for user to change audio device without redoing entire config file - [ ] Clean up code - [ ] Transcode radio transmissions to text ### Modules From c564a145b5214bfcdf3af67ed1d1b2d42d14778a Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Wed, 16 Mar 2022 23:52:17 -0400 Subject: [PATCH 08/27] Moved the startup ping response to a module, so it will only reply if the modules are enabled --- BotResources.py | 11 +++++++++++ bot.py | 16 +--------------- modules/LinkCop/cog.py | 4 ++-- modules/ModuleProbe/cog.py | 24 ++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 17 deletions(-) create mode 100644 modules/ModuleProbe/cog.py diff --git a/BotResources.py b/BotResources.py index b0d9ec6..bcfab85 100644 --- a/BotResources.py +++ b/BotResources.py @@ -191,3 +191,14 @@ def check_negative(s): return False except ValueError: return False + + +# Check if message is a ping request and respond even if it is a bot +async def check_and_reply_to_ping(bot, message): + if "check_modules" in message.content: + ctx = await bot.get_context(message) + await bot.invoke(ctx) + return True + else: + await bot.process_commands(message) + return False diff --git a/bot.py b/bot.py index e35d144..82b2ae2 100644 --- a/bot.py +++ b/bot.py @@ -253,10 +253,6 @@ class Bot(commands.Bot): await self.check_for_modules() print("Bot started!") - @self.event - async def on_message(message): - await self.check_and_reply_to_ping(message) - # Check to see if other bots are online async def check_other_bots_online(self): print('Checking if other bots are online') @@ -272,7 +268,7 @@ class Bot(commands.Bot): await self.wait_until_ready() # Send the ping command with the prefix the current bot is using - await channel.send(f"{self.Command_Prefix}ping") + await channel.send(f"{self.Command_Prefix}check_modules") seconds_waited = 0 while seconds_waited < 3: @@ -480,13 +476,3 @@ class Bot(commands.Bot): message_body += f"Squelch: {self.squelch}" return message_body - - # Check if message is a ping request and respond even if it is a bot - async def check_and_reply_to_ping(self, message): - if "ping" in message.content: - ctx = await self.get_context(message) - await self.invoke(ctx) - return True - else: - await self.process_commands(message) - return False diff --git a/modules/LinkCop/cog.py b/modules/LinkCop/cog.py index 7556d3c..c180511 100644 --- a/modules/LinkCop/cog.py +++ b/modules/LinkCop/cog.py @@ -2,7 +2,7 @@ import re import discord from discord.ext import commands import random -from BotResources import PDB_KNOWN_BOT_IDS +from BotResources import PDB_KNOWN_BOT_IDS, check_and_reply_to_ping regex_link = re.compile('(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)') @@ -71,7 +71,7 @@ class LinkCop(commands.Cog): print(f"Link Cop Error: '{err}'") print(f"Bot or other non-user that has not been whitelisted sent a message") - await self.bot.check_and_reply_to_ping(ctx) + await check_and_reply_to_ping(self.bot, ctx) async def send_message(self, message): send_channel = self.bot.get_channel(id=self.reply_channel_id) diff --git a/modules/ModuleProbe/cog.py b/modules/ModuleProbe/cog.py new file mode 100644 index 0000000..b653bc9 --- /dev/null +++ b/modules/ModuleProbe/cog.py @@ -0,0 +1,24 @@ +from discord.ext import commands +from BotResources import check_and_reply_to_ping + + +class ModuleProbe(commands.Cog): + def __init__(self, bot): + self.bot = bot + self.channel_id = 767303243285790721 + + self.add_events() + + def add_events(self): + @self.bot.event + async def on_message(message): + await check_and_reply_to_ping(self.bot, message) + + @commands.command(help="This command is used by other bots to test if there are any bots online with modules.") + async def check_modules(self, ctx): + if ctx.author.id != self.bot.user.id: + await ctx.send('pong') + + +def setup(bot: commands.Bot): + bot.add_cog(ModuleProbe(bot)) From 760ebd7e43cf39b4cbec8ddcc67215679e71d2de Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Wed, 16 Mar 2022 23:59:42 -0400 Subject: [PATCH 09/27] Added a call to set squelch to 0 as all settings are being changed, so there is no sudden burst of noise --- gqrxHandler.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gqrxHandler.py b/gqrxHandler.py index b5cf96c..342d66f 100644 --- a/gqrxHandler.py +++ b/gqrxHandler.py @@ -43,6 +43,7 @@ class GQRXHandler(): tel_conn.close() def set_all_settings(self, mode, squelch, freq): - self.change_squelch(squelch) + self.change_squelch(0) self.change_mode(mode) self.change_freq(freq) + self.change_squelch(squelch) From 830d299a017297bc6aeba154e85ffeb09b30b1ab Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Thu, 17 Mar 2022 00:09:39 -0400 Subject: [PATCH 10/27] Update gitignore to ignore all pycache folders --- .gitignore | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index a9c4648..4b7f14d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,7 @@ /.idea/ /Releases/ /Old/ -/__pycache__/ -*/__pycache__/ +**/__pycache__/ /venv/ *.7z *.bat From 56a3bdd0eabf17e4097e0f1282a6bef7c4750542 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Thu, 17 Mar 2022 00:12:15 -0400 Subject: [PATCH 11/27] Default the OP25 HTTP server to enabled, caused issues with the shell with it disabled. Need to look into perhaps using the `shell=True` parameter --- op25Handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/op25Handler.py b/op25Handler.py index a1ab84b..8c70866 100644 --- a/op25Handler.py +++ b/op25Handler.py @@ -18,7 +18,7 @@ class OP25Handler: #(threading.Thread): def start(self) -> None: self.open_op25() - def set_op25_parameters(self, _frequency, _http_enabled: bool = False): + def set_op25_parameters(self, _frequency, _http_enabled: bool = True): self.Frequency = _frequency self.HTTP_ENABLED = _http_enabled From 9456b91d09dde81e9639394ef18a99b6128dbbb9 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Thu, 17 Mar 2022 23:10:13 -0400 Subject: [PATCH 12/27] Added WYA command to display where the bot is --- bot.py | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/bot.py b/bot.py index 82b2ae2..7c88e69 100644 --- a/bot.py +++ b/bot.py @@ -69,6 +69,17 @@ class Bot(commands.Bot): if ctx.author.id != self.user.id: await ctx.send('pong') + # Command to display what channel the bot is in + @self.command(help="Use this command to display what channel the bot is in", + brief="Where ya at?") + async def wya(ctx, member: discord.Member = None): + member = member or ctx.author.display_name + if self.Bot_Connected: + await ctx.send(f"Hey {str(member).capitalize()}, I'm in {ctx.voice_client.channel}") + + else: + await ctx.send(f"{str(member).capitalize()}, I am not in any channel.") + # Command to join the bot the voice channel the user who called the command is in @self.command(help="Use this command to join the bot to your channel", brief="Joins the voice channel that the caller is in") @@ -146,16 +157,27 @@ class Bot(commands.Bot): # Add commands for GQRX and OP25 if self.Handler in BotResources.PDB_ACCEPTABLE_HANDLERS.keys(): # Command to display the current config - @self.command(name='displayprofile', + @self.command(name='displaycurprofile', help="Use this command to display the current configuration of the bot.\n" "Example command:\n" - "\t@ displayprofile", + "\t@ displaycurprofile", breif="Display current bot config") - async def _displayprofile(ctx, member: discord.Member = None): + async def _displaycurprofile(ctx, member: discord.Member = None): member = member or ctx.author.display_name message = self.display_current_radio_config() await ctx.send(f"Ok {str(member).capitalize()},\n{message}") + # Command to display the current config + @self.command(name='displayprofiles', + help="Use this command to display the saved profiles.\n" + "Example command:\n" + "\t@ displayprofiles", + breif="Display current bot config") + async def _displayprofiles(ctx, member: discord.Member = None): + member = member or ctx.author.display_name + message = self.display_saved_radio_configs() + await ctx.send(f"Ok {str(member).capitalize()},\n{message}") + # Command to change the current frequency and mode @self.command(name='chfreq', help="Use this command to change the frequency the bot is listening to.\n" "Example GQRX command:\n" @@ -476,3 +498,13 @@ class Bot(commands.Bot): message_body += f"Squelch: {self.squelch}" return message_body + + def display_saved_radio_configs(self): + message_body = f"Saved configs" + config = configparser.ConfigParser() + if os.path.exists('./profiles.ini'): + config.read('./profiles.ini') + for section in config.sections(): + message_body += f"{section}\n" + + return message_body From ff26cbc0b17276d3861f605e553f2a63ecbdf721 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Thu, 17 Mar 2022 23:13:07 -0400 Subject: [PATCH 13/27] Removed `ping` in favor of 'ModuleProbe' module --- bot.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/bot.py b/bot.py index 7c88e69..4339b23 100644 --- a/bot.py +++ b/bot.py @@ -63,12 +63,6 @@ class Bot(commands.Bot): # Add discord commands to the bot def add_commands(self): - # Test command to see if the bot is on (help command can also be used) - @self.command(help="Use this to test if the bot is alive", brief="Sends a 'pong' in response") - async def ping(ctx): - if ctx.author.id != self.user.id: - await ctx.send('pong') - # Command to display what channel the bot is in @self.command(help="Use this command to display what channel the bot is in", brief="Where ya at?") From 0d51b47146eaf5b2e1bba4412c99e082596a4597 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Thu, 17 Mar 2022 23:15:02 -0400 Subject: [PATCH 14/27] BUGFIX in display all profiles --- bot.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/bot.py b/bot.py index 4339b23..ba4f7ed 100644 --- a/bot.py +++ b/bot.py @@ -161,16 +161,16 @@ class Bot(commands.Bot): message = self.display_current_radio_config() await ctx.send(f"Ok {str(member).capitalize()},\n{message}") - # Command to display the current config - @self.command(name='displayprofiles', - help="Use this command to display the saved profiles.\n" - "Example command:\n" - "\t@ displayprofiles", - breif="Display current bot config") - async def _displayprofiles(ctx, member: discord.Member = None): - member = member or ctx.author.display_name - message = self.display_saved_radio_configs() - await ctx.send(f"Ok {str(member).capitalize()},\n{message}") + # Command to display the current config + @self.command(name='displayprofiles', + help="Use this command to display the saved profiles.\n" + "Example command:\n" + "\t@ displayprofiles", + breif="Display current bot config") + async def _displayprofiles(ctx, member: discord.Member = None): + member = member or ctx.author.display_name + message = self.display_saved_radio_configs() + await ctx.send(f"Ok {str(member).capitalize()},\n{message}") # Command to change the current frequency and mode @self.command(name='chfreq', help="Use this command to change the frequency the bot is listening to.\n" From 759f05eb0cc7f0c0242bf14f377ba807e7d6ca6f Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Thu, 17 Mar 2022 23:18:17 -0400 Subject: [PATCH 15/27] Improved the display of profiles --- bot.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bot.py b/bot.py index ba4f7ed..1edc187 100644 --- a/bot.py +++ b/bot.py @@ -494,11 +494,14 @@ class Bot(commands.Bot): return message_body def display_saved_radio_configs(self): - message_body = f"Saved configs" + message_body = f"Saved configs\n" config = configparser.ConfigParser() if os.path.exists('./profiles.ini'): config.read('./profiles.ini') for section in config.sections(): - message_body += f"{section}\n" + message_body += f"{section}\n" \ + f"Frequency:\t\t{config[section]['Frequency']}\n" \ + f"Mode:\t\t{config[section]['Mode']}\n" \ + f"Squelch:\t\t{config[section]['Squelch']}\n" return message_body From 9f4473ffcc0d16f1468600d72193166b9785cf04 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Thu, 17 Mar 2022 23:19:30 -0400 Subject: [PATCH 16/27] Improved the display of profiles --- bot.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bot.py b/bot.py index 1edc187..f01c1d3 100644 --- a/bot.py +++ b/bot.py @@ -499,9 +499,9 @@ class Bot(commands.Bot): if os.path.exists('./profiles.ini'): config.read('./profiles.ini') for section in config.sections(): - message_body += f"{section}\n" \ - f"Frequency:\t\t{config[section]['Frequency']}\n" \ - f"Mode:\t\t{config[section]['Mode']}\n" \ - f"Squelch:\t\t{config[section]['Squelch']}\n" + message_body += f"\n{section}:\n" \ + f"\tMode:\t\t\t\t{config[section]['Mode']}\n" \ + f"\tFrequency:\t\t{config[section]['Frequency']}\n" \ + f"\tSquelch:\t\t\t{config[section]['Squelch']}\n" return message_body From efe8eda66f94580ac548562016c3950b73a27d10 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Thu, 17 Mar 2022 23:28:59 -0400 Subject: [PATCH 17/27] Improved GQRX handler speed --- gqrxHandler.py | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/gqrxHandler.py b/gqrxHandler.py index 342d66f..28ce568 100644 --- a/gqrxHandler.py +++ b/gqrxHandler.py @@ -7,40 +7,29 @@ class GQRXHandler(): self.hostname = hostname self.port = port - self.telnet_connection = None + self.tel_conn = None def create_telnet_connection(self): print("Creating connection") - tel_conn = Telnet(self.hostname, self.port) - tel_conn.open(self.hostname, self.port) - - return tel_conn + self.tel_conn = Telnet(self.hostname, self.port) + self.tel_conn.open(self.hostname, self.port) def change_freq(self, freq): - tel_conn = self.create_telnet_connection() print(f"Changing freq to {freq}") - tel_conn.write(bytes(f"F {int(freq)}", 'utf-8')) - sleep(1) - - tel_conn.close() + self.tel_conn.write(bytes(f"F {int(freq)}", 'utf-8')) + sleep(.25) def change_squelch(self, squelch): - tel_conn = self.create_telnet_connection() if not check_negative(squelch): squelch = float(-abs(squelch)) print(f"Changing squelch to {squelch}") - tel_conn.write(bytes(f"L SQL {float(squelch)}", 'utf-8')) - sleep(1) - - tel_conn.close() + self.tel_conn.write(bytes(f"L SQL {float(squelch)}", 'utf-8')) + sleep(.25) def change_mode(self, mode): - tel_conn = self.create_telnet_connection() print(f"Changing mode to {mode}") - tel_conn.write(bytes(f"M {str(mode)}", 'utf-8')) - sleep(1) - - tel_conn.close() + self.tel_conn.write(bytes(f"M {str(mode)}", 'utf-8')) + sleep(.25) def set_all_settings(self, mode, squelch, freq): self.change_squelch(0) From 5ebbaca54bd80332cbf87bd6c134e87a25f98fb2 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Thu, 17 Mar 2022 23:30:40 -0400 Subject: [PATCH 18/27] GQRX bug; Never created connection --- gqrxHandler.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gqrxHandler.py b/gqrxHandler.py index 28ce568..4853638 100644 --- a/gqrxHandler.py +++ b/gqrxHandler.py @@ -9,6 +9,8 @@ class GQRXHandler(): self.tel_conn = None + self.create_telnet_connection() + def create_telnet_connection(self): print("Creating connection") self.tel_conn = Telnet(self.hostname, self.port) From c3fb20f12e7335f527d995bc15d5a2ce4e336f18 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 19 Mar 2022 00:07:06 -0400 Subject: [PATCH 19/27] Increased sleep time --- gqrxHandler.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gqrxHandler.py b/gqrxHandler.py index 4853638..3c85547 100644 --- a/gqrxHandler.py +++ b/gqrxHandler.py @@ -19,19 +19,19 @@ class GQRXHandler(): def change_freq(self, freq): print(f"Changing freq to {freq}") self.tel_conn.write(bytes(f"F {int(freq)}", 'utf-8')) - sleep(.25) + sleep(.5) def change_squelch(self, squelch): if not check_negative(squelch): squelch = float(-abs(squelch)) print(f"Changing squelch to {squelch}") self.tel_conn.write(bytes(f"L SQL {float(squelch)}", 'utf-8')) - sleep(.25) + sleep(.5) def change_mode(self, mode): print(f"Changing mode to {mode}") self.tel_conn.write(bytes(f"M {str(mode)}", 'utf-8')) - sleep(.25) + sleep(.5) def set_all_settings(self, mode, squelch, freq): self.change_squelch(0) From 8355b98da59eb514f60d3fbd18a9d69f0d2522ad Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 19 Mar 2022 00:16:24 -0400 Subject: [PATCH 20/27] Replaced arbitrary sleep with a wait until response --- gqrxHandler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gqrxHandler.py b/gqrxHandler.py index 3c85547..0a9ee86 100644 --- a/gqrxHandler.py +++ b/gqrxHandler.py @@ -19,7 +19,7 @@ class GQRXHandler(): def change_freq(self, freq): print(f"Changing freq to {freq}") self.tel_conn.write(bytes(f"F {int(freq)}", 'utf-8')) - sleep(.5) + self.tel_conn.read_until('RPRT 0') def change_squelch(self, squelch): if not check_negative(squelch): From a205bd2d8aa7efc3153837925800d4afa2da2efd Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 19 Mar 2022 00:17:36 -0400 Subject: [PATCH 21/27] BUG; Used string, not byte string --- gqrxHandler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gqrxHandler.py b/gqrxHandler.py index 0a9ee86..5b81844 100644 --- a/gqrxHandler.py +++ b/gqrxHandler.py @@ -19,7 +19,7 @@ class GQRXHandler(): def change_freq(self, freq): print(f"Changing freq to {freq}") self.tel_conn.write(bytes(f"F {int(freq)}", 'utf-8')) - self.tel_conn.read_until('RPRT 0') + self.tel_conn.read_until(b'RPRT 0') def change_squelch(self, squelch): if not check_negative(squelch): From e4cb8647ee0b2d1d13243a69e32f6636b7f05915 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 19 Mar 2022 00:19:02 -0400 Subject: [PATCH 22/27] Replaced arbitrary sleep with a wait until response on all functions --- gqrxHandler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gqrxHandler.py b/gqrxHandler.py index 5b81844..9e5eafb 100644 --- a/gqrxHandler.py +++ b/gqrxHandler.py @@ -26,12 +26,12 @@ class GQRXHandler(): squelch = float(-abs(squelch)) print(f"Changing squelch to {squelch}") self.tel_conn.write(bytes(f"L SQL {float(squelch)}", 'utf-8')) - sleep(.5) + self.tel_conn.read_until(b'RPRT 0') def change_mode(self, mode): print(f"Changing mode to {mode}") self.tel_conn.write(bytes(f"M {str(mode)}", 'utf-8')) - sleep(.5) + self.tel_conn.read_until(b'RPRT 0') def set_all_settings(self, mode, squelch, freq): self.change_squelch(0) From ec7880ed1e2fcf4b9050193e93d3f1ad70c38f9f Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 19 Mar 2022 00:27:55 -0400 Subject: [PATCH 23/27] Testing OP25 Improvements --- op25Handler.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/op25Handler.py b/op25Handler.py index 8c70866..57e2468 100644 --- a/op25Handler.py +++ b/op25Handler.py @@ -36,11 +36,12 @@ class OP25Handler: #(threading.Thread): if self.HTTP_ENABLED: self.OP25Proc = subprocess.Popen([f"./rx.py", "--args", "rtl", "-N", "LNA:49", "-s", "200000", "-o", "25600", "-U", "-f", f"{self.Frequency}e6", "-X", "-2", - "-l" "http:0.0.0.0:8080"]) + "-l" "http:0.0.0.0:8080"], shell=True) else: self.OP25Proc = subprocess.Popen([f"./rx.py", "--args", "rtl", "-N", "LNA:49", "-s", - "200000", "-o", "25600", "-U", "-f", f"{self.Frequency}e6", "-X", "-2"]) + "200000", "-o", "25600", "-U", "-f", f"{self.Frequency}e6", "-X", "-2"], + shell=True) os.chdir(cwd) From 4f7ec14807c8347e04548d8196e08c10fb9b087f Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 19 Mar 2022 00:32:03 -0400 Subject: [PATCH 24/27] Testing OP25 Improvements - Changed output to be ignored --- op25Handler.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/op25Handler.py b/op25Handler.py index 57e2468..0f54491 100644 --- a/op25Handler.py +++ b/op25Handler.py @@ -36,12 +36,13 @@ class OP25Handler: #(threading.Thread): if self.HTTP_ENABLED: self.OP25Proc = subprocess.Popen([f"./rx.py", "--args", "rtl", "-N", "LNA:49", "-s", "200000", "-o", "25600", "-U", "-f", f"{self.Frequency}e6", "-X", "-2", - "-l" "http:0.0.0.0:8080"], shell=True) + "-l" "http:0.0.0.0:8080"], shell=True, stdout=subprocess.DEVNULL, + stderr=subprocess.STDOUT) else: self.OP25Proc = subprocess.Popen([f"./rx.py", "--args", "rtl", "-N", "LNA:49", "-s", "200000", "-o", "25600", "-U", "-f", f"{self.Frequency}e6", "-X", "-2"], - shell=True) + shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) os.chdir(cwd) From 1257b713fc49f3eba35a89c0e39c25fc060a8d7e Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 19 Mar 2022 00:39:10 -0400 Subject: [PATCH 25/27] Testing OP25 Improvements - Disabled shell --- op25Handler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/op25Handler.py b/op25Handler.py index 0f54491..f9717a1 100644 --- a/op25Handler.py +++ b/op25Handler.py @@ -36,13 +36,13 @@ class OP25Handler: #(threading.Thread): if self.HTTP_ENABLED: self.OP25Proc = subprocess.Popen([f"./rx.py", "--args", "rtl", "-N", "LNA:49", "-s", "200000", "-o", "25600", "-U", "-f", f"{self.Frequency}e6", "-X", "-2", - "-l" "http:0.0.0.0:8080"], shell=True, stdout=subprocess.DEVNULL, + "-l" "http:0.0.0.0:8080"], shell=False, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) else: self.OP25Proc = subprocess.Popen([f"./rx.py", "--args", "rtl", "-N", "LNA:49", "-s", "200000", "-o", "25600", "-U", "-f", f"{self.Frequency}e6", "-X", "-2"], - shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) + shell=False, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) os.chdir(cwd) From 4f230f8d331b6cd9bcd8917c16d5660f58541dfd Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 19 Mar 2022 00:46:13 -0400 Subject: [PATCH 26/27] Testing OP25 Improvements - Introduced threading (again..) --- op25Handler.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/op25Handler.py b/op25Handler.py index f9717a1..a9cdc26 100644 --- a/op25Handler.py +++ b/op25Handler.py @@ -5,7 +5,7 @@ import os import time -class OP25Handler: #(threading.Thread): +class OP25Handler(threading.Thread): def __init__(self): super().__init__() self.OP25Dir: str = "/home/pi/op25/op25/gr-op25_repeater/apps" @@ -15,7 +15,8 @@ class OP25Handler: #(threading.Thread): self.HTTP_ENABLED = False - def start(self) -> None: + #def start(self) -> None: + def run(self) -> None: self.open_op25() def set_op25_parameters(self, _frequency, _http_enabled: bool = True): From 126ba907afc75d0a6eb626dab3824c9cf342523e Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 19 Mar 2022 01:18:45 -0400 Subject: [PATCH 27/27] Testing OP25 Improvements - New system of handling thread --- bot.py | 6 +++--- op25Handler.py | 33 ++++++++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/bot.py b/bot.py index f01c1d3..6d9576b 100644 --- a/bot.py +++ b/bot.py @@ -312,6 +312,7 @@ class Bot(commands.Bot): print("Starting OP25 handler") from op25Handler import OP25Handler self.OP25Handler = OP25Handler() + self.OP25Handler.start() self.possible_modes = BotResources.PDB_ACCEPTABLE_HANDLERS['op25']['Modes'] # Load the proper OPUS library for the device being used @@ -399,8 +400,7 @@ class Bot(commands.Bot): self.GQRXHandler.set_all_settings(self.mode, self.squelch, self.freq) elif self.Handler == 'op25': - self.OP25Handler.set_op25_parameters(self.freq) - self.OP25Handler.start() + self.OP25Handler.set_op25_parameters(self.freq, _start=True) # Set the started variable for later checks self.sdr_started = True @@ -411,7 +411,7 @@ class Bot(commands.Bot): # Wait for the running processes to close # Close the OP25 handler if self.Handler == 'op25': - self.OP25Handler.close_op25() + self.OP25Handler.set_op25_parameters(_stop=True) # self.OP25Handler.join() # Need a way to 'close' GQRX self.sdr_started = False diff --git a/op25Handler.py b/op25Handler.py index a9cdc26..0e39891 100644 --- a/op25Handler.py +++ b/op25Handler.py @@ -15,14 +15,37 @@ class OP25Handler(threading.Thread): self.HTTP_ENABLED = False - #def start(self) -> None: + self.Start_OP25 = False + + self.Stop_OP25 = False + def run(self) -> None: - self.open_op25() + while True: + if self.Start_OP25: + self.open_op25() - def set_op25_parameters(self, _frequency, _http_enabled: bool = True): - self.Frequency = _frequency + self.Start_OP25 = False + self.Stop_OP25 = False - self.HTTP_ENABLED = _http_enabled + while not self.Stop_OP25: + time.sleep(1) + + self.close_op25() + + time.sleep(.5) + + def set_op25_parameters(self, _frequency: str = False, _http_enabled: bool = True, _start: bool = False, _stop: bool = False): + if _frequency: + self.Frequency = _frequency + + if _start: + self.Start_OP25 = _start + + if _stop: + self.Stop_OP25 = _stop + + if _http_enabled: + self.HTTP_ENABLED = _http_enabled def open_op25(self): if self.OP25Proc is not None: