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 17 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be a wildcard *_config.json?

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
File renamed without changes.
File renamed without changes.
File renamed without changes.
92 changes: 54 additions & 38 deletions box_client/box_client.py → data_uploader/box_client/box_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,27 @@
from cryptography.hazmat.primitives.serialization import load_pem_private_key
from cryptography.hazmat.backends import default_backend

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']
self.file_path = file_path
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:
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'),
Expand All @@ -37,8 +36,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),
Expand Down Expand Up @@ -77,37 +76,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)

headers = {
"Authorization": f"Bearer {access_token}",
}
for filename in filenames:

with open(self.file_path, 'rb') as file_to_upload:
file_name = os.path.basename(filename)

files = {
'file': (file_name, file_to_upload),
'attributes': (None, json.dumps({'parent': {'id': '217403389478'}})),
headers = {
"Authorization": f"Bearer {access_token}",
}

# print(json.dumps({'parent': {'id': '217403389478'}}))
with open(filename, 'rb') as file_to_upload:

try:
response = requests.post(
UPLOAD_URL, headers=headers, files=files)
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):

Expand All @@ -127,3 +128,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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just testing code or does this need to be fixed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved this

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"
File renamed without changes.
Empty file.
72 changes: 72 additions & 0 deletions data_uploader/wifi_client/wifi_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import os
import subprocess
import re
import traceback

'''
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 other saved wifi networks can remain
'''


class Client:
def __init__(self, network_name: str, network_password: str):
self.network_name = network_name
self.network_password = network_password

def scan_for_networks(self) -> list:
try:
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 find_network(self) -> bool:
found = False

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

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:
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(["wpa_cli", "-i", "wlan0", "reconfigure"])
return True

except subprocess.CalledProcessError as err:
traceback.print_exc(err.returncode)
return False
else:
print("Could not find network")
return False


def main():
name = os.getenv('NETWORK_NAME')
password = os.getenv('NETWORK_PASSWORD')

client = Client(name, password)
print(client.connect_to_network())


if __name__ == "__main__":
main()
File renamed without changes.
44 changes: 31 additions & 13 deletions handler.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,61 @@
from box_client.box_client import Client as BoxClient
from discord_client.discord_client import Client as DiscordClient
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 dotenv import load_dotenv
import json


config = json.load(
open('512311_xk3jq6ao_config.json')
GHAFHA marked this conversation as resolved.
Show resolved Hide resolved
)


class Handler:

def handler():

load_dotenv()
WIFI_PASSWORD = os.getenv('WIFI_PASSWORD')
NETWORK_NAME = os.getenv('NETWORK_NAME')
WIFI_PASSWORD = os.getenv('NETWORK_PASSWORD')
WEBHOOK_URL = os.getenv('DISCORD_WEBHOOK')

wifi_client = WifiClient('device_id', 'home_network', WIFI_PASSWORD)
wifi_client = WifiClient(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()
files_for_upload = []

if wifi_client.connect_to_network():

if 'NETGEAR76' in wifi_networks:
discord_client.post_message(discord_messages.WIFI_SUCCESS_MESSAGE)

try:
wifi_client.connect()
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:
print.traceback(e)
discord_client.post_message(f"An error occurred: {e}")
return

discord_client.post_message(discord_messages.WIFI_ERROR_MESSAGE)

return None


Expand Down
25 changes: 0 additions & 25 deletions network/identify_network.py

This file was deleted.

17 changes: 13 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
folder_path=config['folder_path'],
folder_id=config['folder_id']
)


Expand Down
Loading