-
Notifications
You must be signed in to change notification settings - Fork 2
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
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 17 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
SahanYW 259d433
Added todos from Arjun
SahanYW b9319e7
Separated find_network method
SahanYW 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
SahanYW 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…
SahanYW 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 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 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 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 |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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 |
---|---|---|
|
@@ -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'), | ||
|
@@ -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), | ||
|
@@ -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): | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this just testing code or does this need to be fixed? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
File renamed without changes.
This file contains 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" | ||
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.
File renamed without changes.
Empty file.
This file contains 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,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.
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
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.
There was a problem hiding this comment.
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?