Skip to content

Commit

Permalink
Merge pull request #146 from mindsuru/master
Browse files Browse the repository at this point in the history
	geändert:       platformio_upload.py
  • Loading branch information
ayushsharma82 committed Feb 3, 2024
2 parents 0577ec5 + ef920a1 commit 234461a
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions platformio_upload.py
Expand Up @@ -16,6 +16,7 @@
import hashlib
from urllib.parse import urlparse
import time
from requests.auth import HTTPDigestAuth
Import("env")

try:
Expand All @@ -29,7 +30,9 @@

def on_upload(source, target, env):
firmware_path = str(source[0])
upload_url_compatibility = env.GetProjectOption('upload_url')

auth = None
upload_url_compatibility = env.GetProjectOption('custom_upload_url')
upload_url = upload_url_compatibility.replace("/update", "")

with open(firmware_path, 'rb') as firmware:
Expand All @@ -50,12 +53,37 @@ def on_upload(source, target, env):
'Referer': f'{upload_url}/update',
'Connection': 'keep-alive'
}

start_response = requests.get(start_url, headers=start_headers)

if start_response.status_code != 200:
print("start-request faild " + str(start_response.status_code))
return
checkAuthResponse = requests.get(f"{upload_url_compatibility}/update")

if checkAuthResponse.status_code == 401:
try:
username = env.GetProjectOption('custom_username')
password = env.GetProjectOption('custom_password')
except:
username = None
password = None
print("No authentication values specified.")
print('Please, add some Options in your .ini file like: \n\ncustom_username=username\ncustom_password=password\n')
if username is None or password is None:
print("Authentication required, but no credentials provided.")
return
print("Serverconfiguration: authentication needed.")
auth = HTTPDigestAuth(username, password)
doUpdateAuth = requests.get(start_url, headers=start_headers, auth=auth)

if doUpdateAuth.status_code != 200:
print("authentication faild " + str(doUpdateAuth.status_code))
return
print("Authentication successfull")
else:
auth = None
print("Serverconfiguration: autentication not needed.")
doUpdate = requests.get(start_url, headers=start_headers)

if doUpdate.status_code != 200:
print("start-request faild " + str(doUpdate.status_code))
return

firmware.seek(0)
encoder = MultipartEncoder(fields={
Expand Down Expand Up @@ -87,7 +115,7 @@ def on_upload(source, target, env):
}


response = requests.post(f"{upload_url}/ota/upload", data=monitor, headers=post_headers)
response = requests.post(f"{upload_url}/ota/upload", data=monitor, headers=post_headers, auth=auth)

bar.close()
time.sleep(0.1)
Expand All @@ -100,4 +128,4 @@ def on_upload(source, target, env):
tqdm.write(message)


env.Replace(UPLOADCMD=on_upload)
env.Replace(UPLOADCMD=on_upload)

0 comments on commit 234461a

Please sign in to comment.