-
Notifications
You must be signed in to change notification settings - Fork 2
4 rasberry pi wifi upload script #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GHAFHA
wants to merge
24
commits into
main
Choose a base branch
from
4-rasberry-pi-wifi-upload-script
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
c487088
wifi client changes
GHAFHA 8ccec99
Merge branch '4-rasberry-pi-wifi-upload-script' of https://github.com…
GHAFHA 32455ac
Merge pull request #35 from DallasFormulaRacing/main
GHAFHA 7211f41
Merge pull request #36 from DallasFormulaRacing/main
GHAFHA 8ab6512
env changes and example
kdiaz03 79e6560
Working wifi client wooo
259d433
Added todos from Arjun
b9319e7
Separated find_network method
171a248
Update configuration and file upload settings
GHAFHA 3d00903
Updated handler to reflect new wifi client, still needs work
kdiaz03 38c994d
moved line
kdiaz03 d47c05f
successfully uploading files from a list to box
GHAFHA 39af5c9
updated return type of discover_files method
GHAFHA eec3cec
Merge branch '4-rasberry-pi-wifi-upload-script' of https://github.com…
GHAFHA 6ef8bb5
Disconnect ethernet connection before restarting wifi adapter
71b7b41
created messages class, updated handler
GHAFHA acb10ed
updated structure
GHAFHA 16452fb
more reorg
GHAFHA 58c3b73
moved handler
GHAFHA 23ed749
Merge branch 'main' into 4-rasberry-pi-wifi-upload-script
GHAFHA e83d6aa
removed absolute file path from code
GHAFHA 0157e8f
updated pytest
GHAFHA 8ea7599
updated test_handler
GHAFHA 8eec535
Merge branch '4-rasberry-pi-wifi-upload-script' of https://github.com…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| WIFI_PASSWORD = | ||
| DISCORD_WEBHOOK = | ||
| DEVICE_ID = | ||
| NETWORK_NAME = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| [flake8] | ||
|
|
||
| max-line-length = 140 | ||
| max-line-length = 180 | ||
|
|
||
| # Exclude certain file patterns from checking | ||
| exclude = | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,4 +30,5 @@ Thumbs.db | |
|
|
||
| data/ | ||
| .ecu_data | ||
| 512311_xk3jq6ao_config.json | ||
| 512311_xk3jq6ao_config.json | ||
| 512311__config.json | ||
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| import requests | ||
| import jwt | ||
| import time | ||
| import json | ||
| import os | ||
| import secrets | ||
| from cryptography.hazmat.primitives.serialization import load_pem_private_key | ||
| from cryptography.hazmat.backends import default_backend | ||
|
|
||
| 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: | ||
|
|
||
| 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.folder_path = folder_path | ||
| self.folder_id = folder_id | ||
|
|
||
| def retrieve_access_token(self) -> str: | ||
| key_id = self.key_id | ||
| private_key = self.private_key | ||
| password = self.password | ||
|
|
||
| key = load_pem_private_key( | ||
| data=private_key.encode('utf8'), | ||
| password=password.encode('utf8'), | ||
| backend=default_backend() | ||
| ) | ||
|
|
||
| claims = { | ||
| 'iss': self.client_id, | ||
| 'sub': self.enterprise_id, | ||
| 'box_sub_type': 'enterprise', | ||
| 'aud': TOKEN_URL, | ||
| 'jti': secrets.token_hex(64), | ||
| 'exp': int(time.time()) + 60 | ||
| } | ||
|
|
||
| assertion = jwt.encode( | ||
| claims, | ||
| key, | ||
| algorithm='RS512', | ||
| headers={ | ||
| 'kid': key_id | ||
| } | ||
| ) | ||
|
|
||
| params = { | ||
| 'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer', | ||
| 'assertion': assertion, | ||
| 'client_id': self.client_id, | ||
| 'client_secret': self.client_secret | ||
| } | ||
|
|
||
| try: | ||
| response = requests.post(TOKEN_URL, data=params) | ||
|
|
||
| if response.status_code == 200: | ||
|
|
||
| access_token = response.json().get("access_token") | ||
|
|
||
| return access_token | ||
|
|
||
| except requests.exceptions.RequestException as error: | ||
| print(f'Request Exception: {error}') | ||
| else: | ||
| print(f"Error: {response.status_code}") | ||
| print(response.text) | ||
| return None | ||
|
|
||
| def send_files(self, filenames: list) -> bool: | ||
| access_token = self.retrieve_access_token() | ||
|
|
||
| for filename in filenames: | ||
|
|
||
| file_name = os.path.basename(filename) | ||
|
|
||
| headers = { | ||
| "Authorization": f"Bearer {access_token}", | ||
| } | ||
|
|
||
| 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'), | ||
| } | ||
|
|
||
| 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 | ||
|
|
||
| return True | ||
|
|
||
| def get_user_info(self): | ||
|
|
||
| headers = { | ||
| "Authorization": f"Bearer {self.retrieve_access_token()}", | ||
| } | ||
|
|
||
| try: | ||
| response = requests.get(USER_INFO_URL, headers=headers) | ||
|
|
||
| if response.status_code == 200: | ||
| return response.json() | ||
| 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 | ||
|
|
||
| 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(ROOT_FILE_PATH + file) | ||
| print(file) | ||
|
|
||
| return list_of_files | ||
|
|
||
| return None |
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| 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 dotenv import load_dotenv | ||
| import json | ||
|
|
||
|
|
||
| config = json.load( | ||
| open('512311_xk3jq6ao_config.json') | ||
| ) | ||
|
|
||
|
|
||
| class Handler: | ||
|
|
||
| def handler(): | ||
|
|
||
| load_dotenv() | ||
| NETWORK_NAME = os.getenv('NETWORK_NAME') | ||
| WIFI_PASSWORD = os.getenv('NETWORK_PASSWORD') | ||
| WEBHOOK_URL = os.getenv('DISCORD_WEBHOOK') | ||
|
|
||
| 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') | ||
|
|
||
| files_for_upload = [] | ||
|
|
||
| if wifi_client.connect_to_network(): | ||
|
|
||
| discord_client.post_message(discord_messages.WIFI_SUCCESS_MESSAGE) | ||
|
|
||
| try: | ||
| 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(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: | ||
| print.traceback(e) | ||
| discord_client.post_message(f"An error occurred: {e}") | ||
| return | ||
|
|
||
| discord_client.post_message(discord_messages.WIFI_ERROR_MESSAGE) | ||
|
|
||
| return None | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| handler = Handler() | ||
| handler.handler() |
Empty file.
File renamed without changes.
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.