From c487088d5672fffc055b6c2ed8767e652f2a12d7 Mon Sep 17 00:00:00 2001 From: GHAFHA Date: Fri, 9 Feb 2024 23:41:13 -0600 Subject: [PATCH 01/18] wifi client changes --- wifi_client/wifi_client.py | 109 +++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 46 deletions(-) diff --git a/wifi_client/wifi_client.py b/wifi_client/wifi_client.py index 838aef7..e82b9b5 100644 --- a/wifi_client/wifi_client.py +++ b/wifi_client/wifi_client.py @@ -1,46 +1,63 @@ -import wifi -from wifi import Cell, Scheme -import traceback - -# home network will be the network you want to connect to -# password is the password for that network - - -class Client: - def __init__(self, device_id: str, home_network: str, password: str): - self.device_id = device_id - self.home_network = home_network - self.password = password - - def get_wifi_networks(self) -> list: - try: - return Cell.all(self.device_id) - except: - traceback.print_exc() - return [] - - def connect(self) -> bool: - - try: - networks = self.get_wifi_networks() - - for network in networks: - if network.ssid == self.home_network: - scheme = wifi.Scheme.for_cell(self.device_id, network.ssid, network, self.password) - scheme.save() - scheme.activate() - - print(f"Connected to wifi: {network.ssid}") - return True - except: - traceback.print_exc() - return False - - -def main(): - client = Client('wlan0', 'home_network_ssid', 'home_network_password') - client.connect() - - -if __name__ == '__main__': - main() +import wifi +from wifi import Cell, Scheme +import traceback +import os + +# home network will be the network you want to connect to +# password is the password for that network + + +class Client: + def __init__(self, device_id: str, home_network: str, password: str): + self.device_id = device_id + self.home_network = home_network + self.password = password + + def get_wifi_networks(self) -> list: + try: + return Cell.all("d8:3a:dd:19:9f:a4") + except: + traceback.print_exc() + return [] + + def connect(self) -> bool: + + connected = False + + networks = self.get_wifi_networks() + + for network in networks: + print(network) + + for network in networks: + print(network.ssid) + if network.ssid == self.home_network: + print("Found home network") + scheme = wifi.Scheme.for_cell(self.device_id, network.ssid, network, self.password) + + try: + scheme.save() + scheme.activate() + + print(f"Connected to wifi: {network.ssid}") + connected = True + + except wifi.exceptions.ConnectionError as err: + traceback.print_exc(err) + connected = False + + return connected + + +def main(): + device_id = os.getenv("DEVICE_ID") + home_network = os.getenv("NETWORK_SSID") + password = os.getenv("NETWORK_PASSWORD") + client = Client(device_id, home_network, password) + + # client.get_wifi_networks() + print(client.connect()) + + +if __name__ == '__main__': + main() From 8ab6512dead2ab8b2b57e27541a42c0c02719ee2 Mon Sep 17 00:00:00 2001 From: kdiaz03 Date: Mon, 12 Feb 2024 18:07:49 -0600 Subject: [PATCH 02/18] env changes and example --- .env.example | 4 ++++ handler.py | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..1985ff3 --- /dev/null +++ b/.env.example @@ -0,0 +1,4 @@ +WIFI_PASSWORD = +DISCORD_WEBHOOK = +DEVICE_ID = +NETWORK_NAME = \ No newline at end of file diff --git a/handler.py b/handler.py index e439130..556833f 100644 --- a/handler.py +++ b/handler.py @@ -14,15 +14,17 @@ def handler(): load_dotenv() WIFI_PASSWORD = os.getenv('WIFI_PASSWORD') WEBHOOK_URL = os.getenv('DISCORD_WEBHOOK') + DEVICE_ID = os.getenv('DEVICE_ID') + NETWORK_NAME = os.getenv('NETWORK_NAME') - wifi_client = WifiClient('device_id', 'home_network', WIFI_PASSWORD) + wifi_client = WifiClient(DEVICE_ID, NETWORK_NAME, WIFI_PASSWORD) discord_client = DiscordClient(WEBHOOK_URL) box_client = BoxClient() mongo_client = MongoClient('cluster0', 'dfr_sensor_data') wifi_networks = wifi_client.get_wifi_networks() - if 'NETGEAR76' in wifi_networks: + if NETWORK_NAME in wifi_networks: try: wifi_client.connect() From 79e6560d7597b9838ad1be079f709491b39ec881 Mon Sep 17 00:00:00 2001 From: Sahan Wick Date: Thu, 15 Feb 2024 21:36:42 -0600 Subject: [PATCH 03/18] Working wifi client wooo --- wifi_client/wifi_client.py | 79 ++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 28 deletions(-) diff --git a/wifi_client/wifi_client.py b/wifi_client/wifi_client.py index 838aef7..c22fd47 100644 --- a/wifi_client/wifi_client.py +++ b/wifi_client/wifi_client.py @@ -1,46 +1,69 @@ -import wifi -from wifi import Cell, Scheme +import os +import subprocess +import re import traceback -# home network will be the network you want to connect to -# password is the password for that network +''' +This script removes all saved networks to ensure that the specific +network passed is chosen when restarting the wifi adapter + +We could try testing if appending to the wpa_supplicant.conf file +works so saved wifi networks can remain +''' class Client: - def __init__(self, device_id: str, home_network: str, password: str): - self.device_id = device_id - self.home_network = home_network - self.password = password + def __init__(self, network_name: str, network_password: str): + self.network_name = network_name + self.network_password = network_password - def get_wifi_networks(self) -> list: + def scan_for_networks(self) -> list: try: - return Cell.all(self.device_id) - except: - traceback.print_exc() + devices = subprocess.check_output(['sudo', 'iwlist', 'wlan0', 'scan']) + network_names = devices.decode('utf-8') + network_names = re.findall(r'ESSID:"(.*?)"', devices) + + return network_names + + except subprocess.CalledProcessError as err: + traceback.print_exc(err.returncode) return [] - def connect(self) -> bool: + def connect_to_network(self): + found = False - try: - networks = self.get_wifi_networks() + for network_ssid in self.scan_for_networks(): + print(self.network_name, network_ssid) + if network_ssid == self.network_name: + print("Found home network") + found = True + break - for network in networks: - if network.ssid == self.home_network: - scheme = wifi.Scheme.for_cell(self.device_id, network.ssid, network, self.password) - scheme.save() - scheme.activate() + if found: + try: + network = subprocess.check_output(['wpa_passphrase', self.network_name, self.network_password], stderr=subprocess.STDOUT) + with open("/etc/wpa_supplicant/wpa_supplicant.conf", "w+") as fp: + fp.write("ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\nupdate_config=1\ncountry=US\n") + fp.write(network.decode("utf8")) - print(f"Connected to wifi: {network.ssid}") - return True - except: - traceback.print_exc() - return False + subprocess.check_output(["wpa_cli", "-i", "wlan0", "reconfigure"]) + + except subprocess.CalledProcessError as err: + found = False + traceback.print_exc(err.returncode) + else: + print("Could not find network") + + return found def main(): - client = Client('wlan0', 'home_network_ssid', 'home_network_password') - client.connect() + name = os.getenv('NETWORK_NAME') + password = os.getenv('NETWORK_PASSWORD') + + client = Client(name, password) + print(client.connect_to_network()) -if __name__ == '__main__': +if __name__ == "__main__": main() From 259d43326c3b1b6bc85248810aa48a7cbb5056fe Mon Sep 17 00:00:00 2001 From: Sahan Wick Date: Thu, 15 Feb 2024 21:55:46 -0600 Subject: [PATCH 04/18] Added todos from Arjun --- handler.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/handler.py b/handler.py index 556833f..9db05fb 100644 --- a/handler.py +++ b/handler.py @@ -6,6 +6,22 @@ import os from dotenv import load_dotenv +''' +TODO: wifi code has been rewritten, needs updating here + +TODO: only upload new files, not all files + - mark files that have been uploaded + (append "_dash_uploaded" or something similar to the end of the uploaded files) + - Whenever handler is called again after testing days, ignore files that have already renamed + +TODO: add more descriptive discord webhook message(s) + - files that are uploaded + - size of individual files + - size of all files uploaded + +TODO: Need to pass a list of file paths to upload into mongo/box + +''' class Handler: From b9319e76e94cc3125414764c5af1da9260e1b99a Mon Sep 17 00:00:00 2001 From: Sahan Wick Date: Thu, 15 Feb 2024 23:11:16 -0600 Subject: [PATCH 05/18] Separated find_network method --- wifi_client/wifi_client.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/wifi_client/wifi_client.py b/wifi_client/wifi_client.py index c22fd47..0fcd1c9 100644 --- a/wifi_client/wifi_client.py +++ b/wifi_client/wifi_client.py @@ -8,7 +8,7 @@ network passed is chosen when restarting the wifi adapter We could try testing if appending to the wpa_supplicant.conf file -works so saved wifi networks can remain +works so other saved wifi networks can remain ''' @@ -29,7 +29,7 @@ def scan_for_networks(self) -> list: traceback.print_exc(err.returncode) return [] - def connect_to_network(self): + def find_network(self) -> bool: found = False for network_ssid in self.scan_for_networks(): @@ -39,7 +39,10 @@ def connect_to_network(self): found = True break - if found: + return found + + def connect_to_network(self) -> bool: + if self.find_network(): try: network = subprocess.check_output(['wpa_passphrase', self.network_name, self.network_password], stderr=subprocess.STDOUT) with open("/etc/wpa_supplicant/wpa_supplicant.conf", "w+") as fp: @@ -47,14 +50,14 @@ def connect_to_network(self): fp.write(network.decode("utf8")) subprocess.check_output(["wpa_cli", "-i", "wlan0", "reconfigure"]) + return True except subprocess.CalledProcessError as err: - found = False traceback.print_exc(err.returncode) + return False else: print("Could not find network") - - return found + return False def main(): From 171a248eb308fbf86c9ce5ce35dc4b86ed31f1c7 Mon Sep 17 00:00:00 2001 From: Noel Johnson <45609663+GHAFHA@users.noreply.github.com> Date: Sat, 17 Feb 2024 23:50:36 -0600 Subject: [PATCH 06/18] Update configuration and file upload settings --- .flake8 | 2 +- .gitignore | 3 ++- box_client/box_client.py | 40 ++++++++++++++++++------------------- handler.py | 9 ++++++++- network/identify_network.py | 25 ----------------------- test12.txt | 2 ++ tests/conftest.py | 17 ++++++++++++---- tests/test_handler.py | 4 ++-- 8 files changed, 47 insertions(+), 55 deletions(-) delete mode 100644 network/identify_network.py create mode 100644 test12.txt diff --git a/.flake8 b/.flake8 index 3550ac1..3ae0aa4 100644 --- a/.flake8 +++ b/.flake8 @@ -1,6 +1,6 @@ [flake8] -max-line-length = 140 +max-line-length = 180 # Exclude certain file patterns from checking exclude = diff --git a/.gitignore b/.gitignore index b602694..0a51b30 100644 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,5 @@ Thumbs.db data/ .ecu_data -512311_xk3jq6ao_config.json \ No newline at end of file +512311_xk3jq6ao_config.json +512311__config.json \ No newline at end of file diff --git a/box_client/box_client.py b/box_client/box_client.py index a3585af..46da8d3 100644 --- a/box_client/box_client.py +++ b/box_client/box_client.py @@ -6,29 +6,29 @@ import secrets from cryptography.hazmat.primitives.serialization import load_pem_private_key from cryptography.hazmat.backends import default_backend +import traceback -TOKEN_URL = "https://api.box.com/oauth2/token" -UPLOAD_URL = "https://upload.box.com/api/2.0/files/content" -USER_INFO_URL = "https://api.box.com/2.0/users/me" - -config = json.load( - open('512311_xk3jq6ao_config.json')) - -key_id = config['boxAppSettings']['appAuth']['publicKeyID'] +TOKEN_URL = os.getenv("TOKEN_URL") +UPLOAD_URL = os.getenv("UPLOAD_URL") +USER_INFO_URL = os.getenv("USER_INFO_URL") class Client: - def __init__(self, client_id: str, client_secret: str, file_path: str, folder_id: int): - self.client_id = config['boxAppSettings']['clientID'] - self.client_secret = config['boxAppSettings']['clientSecret'] + def __init__(self, client_id: str, client_secret: str, enterprise_id: str, key_id: str, private_key: str, password: str, file_path: str, folder_id: int): + self.client_id = client_id + self.client_secret = client_secret + self.enterprise_id = enterprise_id + self.key_id = key_id + self.private_key = private_key + self.password = password self.file_path = file_path self.folder_id = folder_id def retrieve_access_token(self) -> str: - appAuth = config['boxAppSettings']['appAuth'] - private_key = appAuth['privateKey'] - password = appAuth['passphrase'] + key_id = self.key_id + private_key = self.private_key + password = self.password key = load_pem_private_key( data=private_key.encode('utf8'), @@ -37,8 +37,8 @@ def retrieve_access_token(self) -> str: ) claims = { - 'iss': config['boxAppSettings']['clientID'], - 'sub': config['enterpriseID'], + 'iss': self.client_id, + 'sub': self.enterprise_id, 'box_sub_type': 'enterprise', 'aud': TOKEN_URL, 'jti': secrets.token_hex(64), @@ -79,6 +79,7 @@ def retrieve_access_token(self) -> str: def send_files(self) -> bool: access_token = self.retrieve_access_token() + file_name = os.path.basename(self.file_path) headers = { @@ -89,14 +90,11 @@ def send_files(self) -> bool: files = { 'file': (file_name, file_to_upload), - 'attributes': (None, json.dumps({'parent': {'id': '217403389478'}})), + 'attributes': (None, json.dumps({'parent': {'id': str(self.folder_id)}, 'name': file_name}), 'application/json'), } - # print(json.dumps({'parent': {'id': '217403389478'}})) - try: - response = requests.post( - UPLOAD_URL, headers=headers, files=files) + response = requests.post(UPLOAD_URL, headers=headers, files=files) if response.status_code == 201: return True diff --git a/handler.py b/handler.py index 9db05fb..0c5b15d 100644 --- a/handler.py +++ b/handler.py @@ -5,6 +5,7 @@ import os from dotenv import load_dotenv +import json ''' TODO: wifi code has been rewritten, needs updating here @@ -23,6 +24,11 @@ ''' +config = json.load( + open('512311_xk3jq6ao_config.json') + ) + + class Handler: def handler(): @@ -35,7 +41,8 @@ def handler(): wifi_client = WifiClient(DEVICE_ID, NETWORK_NAME, WIFI_PASSWORD) discord_client = DiscordClient(WEBHOOK_URL) - box_client = BoxClient() + box_client = BoxClient(config['boxAppSettings']['clientID'], config['boxAppSettings']['clientSecret'], config['enterpriseID'], config['appAuth']['publicKeyID'], + config['appAuth']['privateKey'], config['boxAppSettings']['appAuth']['passphrase'], config['file_path'], config['folder_id']) mongo_client = MongoClient('cluster0', 'dfr_sensor_data') wifi_networks = wifi_client.get_wifi_networks() diff --git a/network/identify_network.py b/network/identify_network.py deleted file mode 100644 index 89a1b19..0000000 --- a/network/identify_network.py +++ /dev/null @@ -1,25 +0,0 @@ -import platform -import subprocess - - -class Client: - - # make this return a boolean - def get_wifi_networks() -> bool: - try: - if platform.system().lower() == 'windows': - networks = subprocess.check_output( - ['netsh', 'wlan', 'show', 'network']) - elif platform.system().lower() == 'darwin': - networks = subprocess.check_output( - ['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport', '-s']) - else: - print("This script is intended for Windows or macOS systems.") - return - - decoded_nw = networks.decode('ascii') - print(decoded_nw) - return decoded_nw - - except subprocess.CalledProcessError as e: - print(f"Error: {e}") diff --git a/test12.txt b/test12.txt new file mode 100644 index 0000000..c79e0b3 --- /dev/null +++ b/test12.txt @@ -0,0 +1,2 @@ +'{"type":"error","status":400,"code":"bad_request","context_info":{"errors": +[{"reason":"missing_parameter","name":"file_data","message":"\'file_data\' is required"}]},"help_url":"http:\\/\\/developers.box.com\\/docs\\/#errors","message":"Bad Request","request_id":"tsnp8whmxjd6gk1a"}' \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py index f859a91..2f73796 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,17 +2,26 @@ from mongo_client.mongo_client import Client as Mongo_Client import pytest import os +import json uri = f"mongodb+srv://noel:${os.getenv('MONGO_PASSWORD')}@cluster0.gw7z3sn.mongodb.net/?retryWrites=true&w=majority" +config = json.load( + open('512311_xk3jq6ao_config.json') + ) + @pytest.fixture def box_client(): return Box_Client( - client_id=os.getenv("CLIENT_ID"), - client_secret=os.getenv("CLIENT_SECRET"), - file_path="/Users/noeljohnson/Documents/file.txt", - folder_id="217403389478" + client_id=config['boxAppSettings']['clientID'], + client_secret=config['boxAppSettings']['clientSecret'], + enterprise_id=config['enterpriseID'], + key_id=config['boxAppSettings']['appAuth']['publicKeyID'], + private_key=config['boxAppSettings']['appAuth']['privateKey'], + password=config['boxAppSettings']['appAuth']['passphrase'], + file_path=config['file_path'], + folder_id=config['folder_id'] ) diff --git a/tests/test_handler.py b/tests/test_handler.py index e6bfb50..f65626c 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -3,8 +3,8 @@ def test_access_token(box_client): assert isinstance(request, str) -def test_send_file(box_client): - request = box_client.send_file() +def test_send_files(box_client): + request = box_client.send_files() assert request is True From 3d00903fad990fee16e5e25e28663bc799e1a80f Mon Sep 17 00:00:00 2001 From: kdiaz03 Date: Mon, 19 Feb 2024 17:56:05 -0600 Subject: [PATCH 07/18] Updated handler to reflect new wifi client, still needs work --- handler.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/handler.py b/handler.py index 0c5b15d..2289f05 100644 --- a/handler.py +++ b/handler.py @@ -34,23 +34,20 @@ class Handler: def handler(): load_dotenv() - WIFI_PASSWORD = os.getenv('WIFI_PASSWORD') + WIFI_PASSWORD = os.getenv('NETWORK_PASSWORD') WEBHOOK_URL = os.getenv('DISCORD_WEBHOOK') - DEVICE_ID = os.getenv('DEVICE_ID') NETWORK_NAME = os.getenv('NETWORK_NAME') - wifi_client = WifiClient(DEVICE_ID, NETWORK_NAME, WIFI_PASSWORD) + wifi_client = WifiClient(NETWORK_NAME, WIFI_PASSWORD) discord_client = DiscordClient(WEBHOOK_URL) box_client = BoxClient(config['boxAppSettings']['clientID'], config['boxAppSettings']['clientSecret'], config['enterpriseID'], config['appAuth']['publicKeyID'], config['appAuth']['privateKey'], config['boxAppSettings']['appAuth']['passphrase'], config['file_path'], config['folder_id']) mongo_client = MongoClient('cluster0', 'dfr_sensor_data') - wifi_networks = wifi_client.get_wifi_networks() - if NETWORK_NAME in wifi_networks: + if wifi_client.connect_to_network(): try: - wifi_client.connect() box_client.send_files() discord_client.post_message("File uploaded to Box") mongo_client.check_connection() From 38c994d1fc26a45803a4e400f3dd35e038e1d36c Mon Sep 17 00:00:00 2001 From: kdiaz03 Date: Mon, 19 Feb 2024 18:13:36 -0600 Subject: [PATCH 08/18] moved line --- handler.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/handler.py b/handler.py index 2289f05..75e505f 100644 --- a/handler.py +++ b/handler.py @@ -34,9 +34,9 @@ class Handler: def handler(): load_dotenv() + NETWORK_NAME = os.getenv('NETWORK_NAME') WIFI_PASSWORD = os.getenv('NETWORK_PASSWORD') WEBHOOK_URL = os.getenv('DISCORD_WEBHOOK') - NETWORK_NAME = os.getenv('NETWORK_NAME') wifi_client = WifiClient(NETWORK_NAME, WIFI_PASSWORD) discord_client = DiscordClient(WEBHOOK_URL) @@ -44,7 +44,6 @@ def handler(): config['appAuth']['privateKey'], config['boxAppSettings']['appAuth']['passphrase'], config['file_path'], config['folder_id']) mongo_client = MongoClient('cluster0', 'dfr_sensor_data') - if wifi_client.connect_to_network(): try: From d47c05f4dfaf45abea9b59b30dfa434cba0d752d Mon Sep 17 00:00:00 2001 From: Noel Johnson <45609663+GHAFHA@users.noreply.github.com> Date: Mon, 19 Feb 2024 22:47:07 -0600 Subject: [PATCH 09/18] successfully uploading files from a list to box --- box_client/box_client.py | 63 ++++++++++++++++++++++++++-------------- tests/conftest.py | 2 +- tests/test_handler.py | 13 ++++++++- 3 files changed, 54 insertions(+), 24 deletions(-) diff --git a/box_client/box_client.py b/box_client/box_client.py index 46da8d3..ba04d4d 100644 --- a/box_client/box_client.py +++ b/box_client/box_client.py @@ -15,14 +15,14 @@ class Client: - def __init__(self, client_id: str, client_secret: str, enterprise_id: str, key_id: str, private_key: str, password: str, file_path: str, folder_id: int): + def __init__(self, client_id: str, client_secret: str, enterprise_id: str, key_id: str, private_key: str, password: str, folder_path: str, folder_id: int): self.client_id = client_id self.client_secret = client_secret self.enterprise_id = enterprise_id self.key_id = key_id self.private_key = private_key self.password = password - self.file_path = file_path + self.folder_path = folder_path self.folder_id = folder_id def retrieve_access_token(self) -> str: @@ -77,35 +77,39 @@ def retrieve_access_token(self) -> str: print(response.text) return None - def send_files(self) -> bool: + def send_files(self, filenames: list) -> bool: access_token = self.retrieve_access_token() - file_name = os.path.basename(self.file_path) + for filename in filenames: - headers = { - "Authorization": f"Bearer {access_token}", - } + file_name = os.path.basename(filename) - with open(self.file_path, 'rb') as file_to_upload: - - files = { - 'file': (file_name, file_to_upload), - 'attributes': (None, json.dumps({'parent': {'id': str(self.folder_id)}, 'name': file_name}), 'application/json'), + headers = { + "Authorization": f"Bearer {access_token}", } - try: - response = requests.post(UPLOAD_URL, headers=headers, files=files) + with open(filename, 'rb') as file_to_upload: + + files = { + 'file': (filename, file_to_upload), + 'attributes': (None, json.dumps({'parent': {'id': str(self.folder_id)}, 'name': file_name}), 'application/json'), + } - if response.status_code == 201: - return True - else: - print(f"Error: {response.status_code}") - print(response.json()) + try: + response = requests.post(UPLOAD_URL, headers=headers, files=files) + + if response.status_code == 201: + continue + else: + print(f"Error: {response.status_code}") + print(response.json()) + return False + + except requests.exceptions.RequestException as error: + print(f'Request Exception: {error}') return False - except requests.exceptions.RequestException as error: - print(f'Request Exception: {error}') - return False + return True def get_user_info(self): @@ -125,3 +129,18 @@ def get_user_info(self): except requests.exceptions.RequestException as error: print(f'Request Exception: {error}') return False + + def discover_files(self) -> list: + + list_of_files = [] + + if os.path.exists(self.folder_path) and os.path.isdir(self.folder_path): + files = os.listdir(self.folder_path) + + for file in files: + list_of_files.append("C:\\Users\\sajip\\OneDrive\\Desktop\\" + file) + print(file) + + return True + + return False diff --git a/tests/conftest.py b/tests/conftest.py index 2f73796..adc2e22 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -20,7 +20,7 @@ def box_client(): key_id=config['boxAppSettings']['appAuth']['publicKeyID'], private_key=config['boxAppSettings']['appAuth']['privateKey'], password=config['boxAppSettings']['appAuth']['passphrase'], - file_path=config['file_path'], + folder_path=config['folder_path'], folder_id=config['folder_id'] ) diff --git a/tests/test_handler.py b/tests/test_handler.py index f65626c..4c53977 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -4,7 +4,13 @@ def test_access_token(box_client): def test_send_files(box_client): - request = box_client.send_files() + + list_of_files = [ + "C:\\Users\\sajip\\Documents\\GitHub\\PowertrainLib\\files\\ecu4.csv", + "C:\\Users\\sajip\\Documents\\GitHub\\PowertrainLib\\files\\ecu2.csv" + ] + + request = box_client.send_files(list_of_files) assert request is True @@ -13,6 +19,11 @@ def test_get_user_info(box_client): assert isinstance(request, dict) +def test_discover_file(box_client): + file_exist = box_client.discover_files() + assert file_exist is True + + def test_mongo_connection(mongo_client): request = mongo_client.establish_connection() assert request is None From 39af5c9e25f079fd7eae5fba2fb7a13ec5dc8215 Mon Sep 17 00:00:00 2001 From: Noel Johnson <45609663+GHAFHA@users.noreply.github.com> Date: Tue, 20 Feb 2024 01:22:50 -0600 Subject: [PATCH 10/18] updated return type of discover_files method --- box_client/box_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/box_client/box_client.py b/box_client/box_client.py index ba04d4d..1037b12 100644 --- a/box_client/box_client.py +++ b/box_client/box_client.py @@ -141,6 +141,6 @@ def discover_files(self) -> list: list_of_files.append("C:\\Users\\sajip\\OneDrive\\Desktop\\" + file) print(file) - return True + return list_of_files - return False + return None From 6ef8bb5c0df0907128a99a3ab445908abf66c347 Mon Sep 17 00:00:00 2001 From: Sahan Wick Date: Tue, 20 Feb 2024 17:25:59 -0600 Subject: [PATCH 11/18] Disconnect ethernet connection before restarting wifi adapter --- wifi_client/wifi_client.py | 1 + 1 file changed, 1 insertion(+) diff --git a/wifi_client/wifi_client.py b/wifi_client/wifi_client.py index 0fcd1c9..f97dd02 100644 --- a/wifi_client/wifi_client.py +++ b/wifi_client/wifi_client.py @@ -49,6 +49,7 @@ def connect_to_network(self) -> bool: fp.write("ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\nupdate_config=1\ncountry=US\n") fp.write(network.decode("utf8")) + subprocess.check_output(["sudo", "ifconfig", "eth0", "down"]) subprocess.check_output(["wpa_cli", "-i", "wlan0", "reconfigure"]) return True From 71b7b41ab61fff704ad9ed391c1413b97f04d432 Mon Sep 17 00:00:00 2001 From: GHAFHA <45609663+GHAFHA@users.noreply.github.com> Date: Fri, 23 Feb 2024 01:27:14 -0600 Subject: [PATCH 12/18] created messages class, updated handler --- box_client/box_client.py | 1 - discord_client/messages.py | 11 +++++++++++ handler.py | 37 +++++++++++++++++-------------------- 3 files changed, 28 insertions(+), 21 deletions(-) create mode 100644 discord_client/messages.py diff --git a/box_client/box_client.py b/box_client/box_client.py index 1037b12..8b51eb4 100644 --- a/box_client/box_client.py +++ b/box_client/box_client.py @@ -6,7 +6,6 @@ import secrets from cryptography.hazmat.primitives.serialization import load_pem_private_key from cryptography.hazmat.backends import default_backend -import traceback TOKEN_URL = os.getenv("TOKEN_URL") UPLOAD_URL = os.getenv("UPLOAD_URL") diff --git a/discord_client/messages.py b/discord_client/messages.py new file mode 100644 index 0000000..2a209fd --- /dev/null +++ b/discord_client/messages.py @@ -0,0 +1,11 @@ +from enum import Enum + + +class Messages(str, Enum): + MONGO_SUCCESS_MESSAGE = "Docuemnts inserted Successfully" + MONGO_ERROR_MESSAGE = "An error occurred while trying to connect to MongoDB: {e}" + MONGO_CLOSE_MESSAGE = "MongoDB connection closed." + BOX_SUCCESS_MESSAGE = "Files uploaded successfully" + BOX_ERROR_MESSAGE = "Error occurred while trying to upload files to Box" + WIFI_SUCCESS_MESSAGE = "Connected to network successfully" + WIFI_ERROR_MESSAGE = "Error occurred while trying to connect to network" diff --git a/handler.py b/handler.py index 75e505f..345cd92 100644 --- a/handler.py +++ b/handler.py @@ -1,32 +1,18 @@ from box_client.box_client import Client as BoxClient from discord_client.discord_client import Client as DiscordClient +from discord_client.messages import Messages as discord_messages from mongo_client.mongo_client import Client as MongoClient from wifi_client.wifi_client import Client as WifiClient + import os from dotenv import load_dotenv import json -''' -TODO: wifi code has been rewritten, needs updating here - -TODO: only upload new files, not all files - - mark files that have been uploaded - (append "_dash_uploaded" or something similar to the end of the uploaded files) - - Whenever handler is called again after testing days, ignore files that have already renamed - -TODO: add more descriptive discord webhook message(s) - - files that are uploaded - - size of individual files - - size of all files uploaded - -TODO: Need to pass a list of file paths to upload into mongo/box - -''' config = json.load( open('512311_xk3jq6ao_config.json') - ) +) class Handler: @@ -44,14 +30,23 @@ def handler(): config['appAuth']['privateKey'], config['boxAppSettings']['appAuth']['passphrase'], config['file_path'], config['folder_id']) mongo_client = MongoClient('cluster0', 'dfr_sensor_data') + files_for_upload = [] + if wifi_client.connect_to_network(): + discord_client.post_message(discord_messages.WIFI_SUCCESS_MESSAGE) + try: - box_client.send_files() - discord_client.post_message("File uploaded to Box") + files_for_upload = box_client.discover_files() + box_client.send_files(files_for_upload) + + discord_client.post_message(discord_messages.BOX_SUCCESS_MESSAGE) + mongo_client.check_connection() - mongo_client.insert_documents() + mongo_client.insert_documents(files_for_upload) + discord_client.post_message(discord_messages.MONGO_SUCCESS_MESSAGE) mongo_client.close_connection() + discord_client.post_message(discord_messages.MONGO_CLOSE_MESSAGE) discord_client.post_message("Documents inserted into MongoDB") except Exception as e: @@ -59,6 +54,8 @@ def handler(): discord_client.post_message(f"An error occurred: {e}") return + discord_client.post_message(discord_messages.WIFI_ERROR_MESSAGE) + return None From acb10edf155a3b9201ca40823ab83520a16c5f72 Mon Sep 17 00:00:00 2001 From: GHAFHA <45609663+GHAFHA@users.noreply.github.com> Date: Fri, 23 Feb 2024 01:30:27 -0600 Subject: [PATCH 13/18] updated structure --- {box_client => data_uploader/box_client}/__init__.py | 0 {box_client => data_uploader/box_client}/box_client.py | 0 .../discord_client}/__init__.py | 0 .../discord_client}/discord_client.py | 0 .../discord_client}/messages.py | 0 .../mongo_client}/__init__.py | 0 .../mongo_client}/mongo_client.py | 0 {wifi_client => data_uploader/wifi_client}/__init__.py | 0 .../wifi_client}/wifi_client.py | 0 handler.py | 10 +++++----- 10 files changed, 5 insertions(+), 5 deletions(-) rename {box_client => data_uploader/box_client}/__init__.py (100%) rename {box_client => data_uploader/box_client}/box_client.py (100%) rename {discord_client => data_uploader/discord_client}/__init__.py (100%) rename {discord_client => data_uploader/discord_client}/discord_client.py (100%) rename {discord_client => data_uploader/discord_client}/messages.py (100%) rename {mongo_client => data_uploader/mongo_client}/__init__.py (100%) rename {mongo_client => data_uploader/mongo_client}/mongo_client.py (100%) rename {wifi_client => data_uploader/wifi_client}/__init__.py (100%) rename {wifi_client => data_uploader/wifi_client}/wifi_client.py (100%) diff --git a/box_client/__init__.py b/data_uploader/box_client/__init__.py similarity index 100% rename from box_client/__init__.py rename to data_uploader/box_client/__init__.py diff --git a/box_client/box_client.py b/data_uploader/box_client/box_client.py similarity index 100% rename from box_client/box_client.py rename to data_uploader/box_client/box_client.py diff --git a/discord_client/__init__.py b/data_uploader/discord_client/__init__.py similarity index 100% rename from discord_client/__init__.py rename to data_uploader/discord_client/__init__.py diff --git a/discord_client/discord_client.py b/data_uploader/discord_client/discord_client.py similarity index 100% rename from discord_client/discord_client.py rename to data_uploader/discord_client/discord_client.py diff --git a/discord_client/messages.py b/data_uploader/discord_client/messages.py similarity index 100% rename from discord_client/messages.py rename to data_uploader/discord_client/messages.py diff --git a/mongo_client/__init__.py b/data_uploader/mongo_client/__init__.py similarity index 100% rename from mongo_client/__init__.py rename to data_uploader/mongo_client/__init__.py diff --git a/mongo_client/mongo_client.py b/data_uploader/mongo_client/mongo_client.py similarity index 100% rename from mongo_client/mongo_client.py rename to data_uploader/mongo_client/mongo_client.py diff --git a/wifi_client/__init__.py b/data_uploader/wifi_client/__init__.py similarity index 100% rename from wifi_client/__init__.py rename to data_uploader/wifi_client/__init__.py diff --git a/wifi_client/wifi_client.py b/data_uploader/wifi_client/wifi_client.py similarity index 100% rename from wifi_client/wifi_client.py rename to data_uploader/wifi_client/wifi_client.py diff --git a/handler.py b/handler.py index 345cd92..68180d6 100644 --- a/handler.py +++ b/handler.py @@ -1,8 +1,8 @@ -from box_client.box_client import Client as BoxClient -from discord_client.discord_client import Client as DiscordClient -from discord_client.messages import Messages as discord_messages -from mongo_client.mongo_client import Client as MongoClient -from wifi_client.wifi_client import Client as WifiClient +from data_uploader.box_client.box_client import Client as BoxClient +from data_uploader.discord_client.discord_client import Client as DiscordClient +from data_uploader.discord_client.messages import Messages as discord_messages +from data_uploader.mongo_client.mongo_client import Client as MongoClient +from data_uploader.wifi_client.wifi_client import Client as WifiClient import os From 16452fb86cd4f49baad707e0d0c0ff779d7d2be1 Mon Sep 17 00:00:00 2001 From: GHAFHA <45609663+GHAFHA@users.noreply.github.com> Date: Fri, 23 Feb 2024 01:33:25 -0600 Subject: [PATCH 14/18] more reorg --- data_manipulation/__init__.py | 0 data_deserializer.py => data_manipulation/data_deserializer.py | 0 {decrepicated => deprecated}/oauth_method.txt | 0 test12.txt | 2 -- 4 files changed, 2 deletions(-) create mode 100644 data_manipulation/__init__.py rename data_deserializer.py => data_manipulation/data_deserializer.py (100%) rename {decrepicated => deprecated}/oauth_method.txt (100%) delete mode 100644 test12.txt diff --git a/data_manipulation/__init__.py b/data_manipulation/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/data_deserializer.py b/data_manipulation/data_deserializer.py similarity index 100% rename from data_deserializer.py rename to data_manipulation/data_deserializer.py diff --git a/decrepicated/oauth_method.txt b/deprecated/oauth_method.txt similarity index 100% rename from decrepicated/oauth_method.txt rename to deprecated/oauth_method.txt diff --git a/test12.txt b/test12.txt deleted file mode 100644 index c79e0b3..0000000 --- a/test12.txt +++ /dev/null @@ -1,2 +0,0 @@ -'{"type":"error","status":400,"code":"bad_request","context_info":{"errors": -[{"reason":"missing_parameter","name":"file_data","message":"\'file_data\' is required"}]},"help_url":"http:\\/\\/developers.box.com\\/docs\\/#errors","message":"Bad Request","request_id":"tsnp8whmxjd6gk1a"}' \ No newline at end of file From 58c3b733edd0789b2422d11e84b1edbf10b85a1c Mon Sep 17 00:00:00 2001 From: GHAFHA <45609663+GHAFHA@users.noreply.github.com> Date: Fri, 23 Feb 2024 16:35:53 -0600 Subject: [PATCH 15/18] moved handler --- data_uploader/__init__.py | 0 handler.py => data_uploader/handler.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 data_uploader/__init__.py rename handler.py => data_uploader/handler.py (100%) diff --git a/data_uploader/__init__.py b/data_uploader/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/handler.py b/data_uploader/handler.py similarity index 100% rename from handler.py rename to data_uploader/handler.py From e83d6aabbc523ca0b78f238586f4a7b26693b94c Mon Sep 17 00:00:00 2001 From: GHAFHA <45609663+GHAFHA@users.noreply.github.com> Date: Tue, 27 Feb 2024 12:08:32 -0600 Subject: [PATCH 16/18] removed absolute file path from code --- data_uploader/box_client/box_client.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/data_uploader/box_client/box_client.py b/data_uploader/box_client/box_client.py index 8b51eb4..d0076d4 100644 --- a/data_uploader/box_client/box_client.py +++ b/data_uploader/box_client/box_client.py @@ -10,6 +10,7 @@ TOKEN_URL = os.getenv("TOKEN_URL") UPLOAD_URL = os.getenv("UPLOAD_URL") USER_INFO_URL = os.getenv("USER_INFO_URL") +ROOT_FILE_PATH = os.getenv("ROOT_FILE_PATH") class Client: @@ -137,7 +138,7 @@ def discover_files(self) -> list: files = os.listdir(self.folder_path) for file in files: - list_of_files.append("C:\\Users\\sajip\\OneDrive\\Desktop\\" + file) + list_of_files.append(ROOT_FILE_PATH + file) print(file) return list_of_files From 0157e8f583b15c8e7ee708096cffa637427fa43a Mon Sep 17 00:00:00 2001 From: GHAFHA <45609663+GHAFHA@users.noreply.github.com> Date: Tue, 27 Feb 2024 12:12:55 -0600 Subject: [PATCH 17/18] updated pytest --- tests/conftest.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index adc2e22..49a79a8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,5 @@ -from box_client.box_client import Client as Box_Client -from mongo_client.mongo_client import Client as Mongo_Client +from data_uploader.box_client.box_client import Client as Box_Client +from data_uploader.mongo_client.mongo_client import Client as Mongo_Client import pytest import os import json @@ -8,7 +8,7 @@ config = json.load( open('512311_xk3jq6ao_config.json') - ) +) @pytest.fixture From 8ea7599d2c80183b69aa588e67f8522f0846a3ff Mon Sep 17 00:00:00 2001 From: GHAFHA <45609663+GHAFHA@users.noreply.github.com> Date: Tue, 27 Feb 2024 12:40:47 -0600 Subject: [PATCH 18/18] updated test_handler --- tests/test_handler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_handler.py b/tests/test_handler.py index 4c53977..9b78560 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -6,8 +6,8 @@ def test_access_token(box_client): def test_send_files(box_client): list_of_files = [ - "C:\\Users\\sajip\\Documents\\GitHub\\PowertrainLib\\files\\ecu4.csv", - "C:\\Users\\sajip\\Documents\\GitHub\\PowertrainLib\\files\\ecu2.csv" + r"data\ecu4.csv", + r"data\ecu2.csv" ] request = box_client.send_files(list_of_files)