From 9a24d30b0a315f7dd3752093326fd78e5919b5ec Mon Sep 17 00:00:00 2001 From: Mathieu Date: Sun, 22 Aug 2021 18:49:20 +0200 Subject: [PATCH] Fix bug program would open outside pycharm --- jarvis/config/config.json | 3 ++- jarvis/main.py | 10 +++++----- jarvis/utils/config_utils.py | 2 +- jarvis/utils/server_utils.py | 5 +++++ run.py | 11 +++++++++++ 5 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 run.py diff --git a/jarvis/config/config.json b/jarvis/config/config.json index cd37757..b4abf05 100644 --- a/jarvis/config/config.json +++ b/jarvis/config/config.json @@ -1,4 +1,5 @@ { "PORT": 5001, - "SERVER_PORT": 5000 + "SERVER_PORT": 5000, + "SERVER_IP": "192.168.1.130" } diff --git a/jarvis/main.py b/jarvis/main.py index 361f70b..948ff84 100644 --- a/jarvis/main.py +++ b/jarvis/main.py @@ -33,7 +33,7 @@ def wake_word_listening(): if keyword_index >= 0: threading.Thread( - target=sa.WaveObject.from_wave_file(os.getcwd() + "/sounds/" + "listening.wav").play).start() + target=sa.WaveObject.from_wave_file(os.getcwd() + "/jarvis" + "/sounds/" + "listening.wav").play).start() record() @@ -44,7 +44,7 @@ def record(): r.adjust_for_ambient_noise(source=source, duration=0.7) audio = r.listen(source, timeout=2, phrase_time_limit=5) - threading.Thread(target=sa.WaveObject.from_wave_file(os.getcwd() + "/sounds/" + "listened.wav").play).start() + threading.Thread(target=sa.WaveObject.from_wave_file(os.getcwd() + "/jarvis" + "/sounds/" + "listened.wav").play).start() server_utils.send_record_to_server(audio.frame_data) @@ -55,14 +55,14 @@ def no_voice_input(): server_utils.send_sentence_to_server(sentence) -if __name__ == '__main__': +def start(): if server_utils.get_server_ip() is None: sys.exit(1) if '--no-voice' in sys.argv: print("[WARN] No voice mode enabled") - thread = threading.Thread(target=no_voice_input).start() + threading.Thread(target=no_voice_input).start() else: - thread = threading.Thread(target=wake_word_listening).start() + threading.Thread(target=wake_word_listening).start() flask_utils.start_server() diff --git a/jarvis/utils/config_utils.py b/jarvis/utils/config_utils.py index f65f507..729f948 100644 --- a/jarvis/utils/config_utils.py +++ b/jarvis/utils/config_utils.py @@ -1,7 +1,7 @@ import json import os -path = os.getcwd() +path = os.getcwd() + "/jarvis" def get_in_config(name): diff --git a/jarvis/utils/server_utils.py b/jarvis/utils/server_utils.py index be1d18d..447ee69 100644 --- a/jarvis/utils/server_utils.py +++ b/jarvis/utils/server_utils.py @@ -73,6 +73,11 @@ def find_server_on_network(): ip = socket.gethostbyname(socket.gethostname()) + # sometimes it might return the local ip (127.0.0.1) adding local should solves that + # TODO: see if we only use the ".local" ip + if ip.startswith("127.0"): + ip = socket.gethostbyname(socket.gethostname() + ".local") + if ip.startswith("192.168.1"): server_ip = find_device_on_network_with_opened_port(ipaddress.ip_network("192.168.1.0/24"), config_utils.get_in_config('SERVER_PORT')) diff --git a/run.py b/run.py new file mode 100644 index 0000000..8a75cc5 --- /dev/null +++ b/run.py @@ -0,0 +1,11 @@ +import os +import sys + +# TODO: see if something better could be done +if __name__ == '__main__': + sys.path.append(os.path.realpath('..')) + + # doing the import after editing the path otherwise cause issue between running in pycharm or the terminal + from jarvis.main import start + + start()