diff --git a/jarvis/config/config.json b/jarvis/config/config.json new file mode 100644 index 0000000..6eba7de --- /dev/null +++ b/jarvis/config/config.json @@ -0,0 +1,3 @@ +{ + "SERVER_PORT": 5000 +} diff --git a/jarvis/main.py b/jarvis/main.py index 400a7dc..a8687fa 100644 --- a/jarvis/main.py +++ b/jarvis/main.py @@ -4,7 +4,7 @@ import pvporcupine import pyaudio import speech_recognition as sr -from jarvis.utils import server_utils +from jarvis.utils import server_utils, config_utils wake_word_handler = pvporcupine.create(keywords=['jarvis']) @@ -43,4 +43,9 @@ def record(): if __name__ == '__main__': + + if config_utils.get_in_config('SERVER_IP') is None: + print("No server IP specified in config, looking trough the entire network... (might take a few seconds)") + server_utils.find_server_on_network() + wake_word_listening() diff --git a/jarvis/utils/config_utils.py b/jarvis/utils/config_utils.py new file mode 100644 index 0000000..f65f507 --- /dev/null +++ b/jarvis/utils/config_utils.py @@ -0,0 +1,29 @@ +import json +import os + +path = os.getcwd() + + +def get_in_config(name): + config_json = json.load(open(path + "/config/config.json", encoding='utf-8', mode='r')) + if name in config_json: + if isinstance(config_json.get(name), str): + if "!secret" in config_json.get(name): + # secret_name = config_json.get(name).removeprefix('!secret ') + secret_name = config_json.get(name).replace('!secret ', '') + return get_in_secret(secret_name) + else: + return config_json.get(name) + else: + return config_json.get(name) + else: + return None + + +def get_in_secret(secret_name): + secrets_json = json.load(open(path + "/config/secrets.json", encoding='utf-8', mode='r')) + + if secret_name in secrets_json: + return secrets_json.get(secret_name) + else: + return None diff --git a/jarvis/utils/server_utils.py b/jarvis/utils/server_utils.py index 87a1e66..05528df 100644 --- a/jarvis/utils/server_utils.py +++ b/jarvis/utils/server_utils.py @@ -1,10 +1,18 @@ +import ipaddress +import socket +import sys + import requests from requests.structures import CaseInsensitiveDict +from jarvis.utils import config_utils + +server_ip = None + def send_record_to_server(frame_data): # TODO: use config or even ping to find the server on local network ? - url_service = "http://192.168.1.12:5000" + "/process_audio_request" + url_service = "http://" + str(get_server_ip()) + ":" + str(get_server_port()) + "/process_audio_request" headers = CaseInsensitiveDict() headers["Content-Type"] = "text/xml; charset=utf8" @@ -15,3 +23,66 @@ def send_record_to_server(frame_data): data=frame_data) print(response.content) + + +def get_server_port(): + return config_utils.get_in_config('SERVER_PORT') if not None else 5000 + + +def get_server_ip(): + global server_ip + + if server_ip is not None: + return server_ip + + if config_utils.get_in_config('SERVER_IP') is None: + print("No server IP specified in config, looking trough the entire network... (might take a few seconds)") + result = find_server_on_network() + if result is not None: + print("Found server on : " + result) + server_ip = result + return result + else: + sys.exit("Server not found!") + else: + return config_utils.get_in_config('SERVER_IP') + + +def find_server_on_network(): + global server_ip + + # first try on the most common mask 192.168.1.0/24 + server_ip = find_device_on_network_with_opened_port(ipaddress.ip_network("192.168.1.0/24"), + config_utils.get_in_config('SERVER_PORT')) + if server_ip is None: + # then 172.16.0.0/12 + server_ip = find_device_on_network_with_opened_port(ipaddress.ip_network("172.16.0.0/12"), + config_utils.get_in_config('SERVER_PORT')) + if server_ip is None: + # and last but not least the 10.0.0.0/8 (damn that's gonna be long to scan) + server_ip = find_device_on_network_with_opened_port(ipaddress.ip_network("10.0.0.0/8"), + config_utils.get_in_config('SERVER_PORT')) + + print("Found server on : " + str(server_ip)) + server_ip = server_ip.compressed + return server_ip + + +def find_device_on_network_with_opened_port(hosts, port, timeout=0.02): + found_ip = None + + for ip in hosts: + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.settimeout(timeout) + result = sock.connect_ex((socket.gethostbyname(ip.compressed), port)) + if result == 0: + sock.close() + found_ip = ip + break + + sock.close() + except Exception: + pass + + return found_ip