Skip to content
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

4 rasberry pi wifi upload script #41

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c487088
wifi client changes
GHAFHA Feb 10, 2024
8ccec99
Merge branch '4-rasberry-pi-wifi-upload-script' of https://github.com…
GHAFHA Feb 10, 2024
32455ac
Merge pull request #35 from DallasFormulaRacing/main
GHAFHA Feb 11, 2024
7211f41
Merge pull request #36 from DallasFormulaRacing/main
GHAFHA Feb 11, 2024
8ab6512
env changes and example
kdiaz03 Feb 13, 2024
79e6560
Working wifi client wooo
SahanYW Feb 16, 2024
259d433
Added todos from Arjun
SahanYW Feb 16, 2024
b9319e7
Separated find_network method
SahanYW Feb 16, 2024
171a248
Update configuration and file upload settings
GHAFHA Feb 18, 2024
3d00903
Updated handler to reflect new wifi client, still needs work
kdiaz03 Feb 19, 2024
38c994d
moved line
kdiaz03 Feb 20, 2024
d47c05f
successfully uploading files from a list to box
GHAFHA Feb 20, 2024
39af5c9
updated return type of discover_files method
GHAFHA Feb 20, 2024
eec3cec
Merge branch '4-rasberry-pi-wifi-upload-script' of https://github.com…
GHAFHA Feb 20, 2024
6ef8bb5
Disconnect ethernet connection before restarting wifi adapter
SahanYW Feb 20, 2024
71b7b41
created messages class, updated handler
GHAFHA Feb 23, 2024
acb10ed
updated structure
GHAFHA Feb 23, 2024
16452fb
more reorg
GHAFHA Feb 23, 2024
58c3b73
moved handler
GHAFHA Feb 23, 2024
23ed749
Merge branch 'main' into 4-rasberry-pi-wifi-upload-script
GHAFHA Feb 27, 2024
e83d6aa
removed absolute file path from code
GHAFHA Feb 27, 2024
0157e8f
updated pytest
GHAFHA Feb 27, 2024
8ea7599
updated test_handler
GHAFHA Feb 27, 2024
8eec535
Merge branch '4-rasberry-pi-wifi-upload-script' of https://github.com…
SahanYW Apr 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
WIFI_PASSWORD =
DISCORD_WEBHOOK =
DEVICE_ID =
NETWORK_NAME =
2 changes: 1 addition & 1 deletion .flake8
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 =
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ Thumbs.db

data/
.ecu_data
512311_xk3jq6ao_config.json
512311_xk3jq6ao_config.json
512311__config.json
139 changes: 0 additions & 139 deletions box_client/box_client.py

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
146 changes: 146 additions & 0 deletions data_uploader/box_client/box_client.py
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.
11 changes: 11 additions & 0 deletions data_uploader/discord_client/messages.py
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"
GHAFHA marked this conversation as resolved.
Show resolved Hide resolved
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"
64 changes: 64 additions & 0 deletions data_uploader/handler.py
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.
Empty file.
Loading