8000 Merge pull request #62 from pycom/pyconfig-url-fix · daq-tools/pycom-micropython@9c76d59 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c76d59

Browse files
author
Islam Wahdan
authored
Merge pull request pycom#62 from pycom/pyconfig-url-fix
Pyconfig url fix (using production URL)
2 parents b6dd4ad + babc738 commit 9c76d59

File tree

2 files changed

+91
-83
lines changed

2 files changed

+91
-83
lines changed

esp32/frozen/Pybytes/_pybytes_constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,6 @@ class constants:
125125
except:
126126
__DEFAULT_SW_HOST = 'software.pycom.io'
127127
try:
128-
__DEFAULT_PYCONFIG_DOMAIN = pycom.nvs_get('pyconfig_host', 'staging-pyconfig.eu-central-1.elasticbeanstalk.com')
128+
__DEFAULT_PYCONFIG_DOMAIN = pycom.nvs_get('pyconfig_host', 'pyconfig.eu-central-1.elasticbeanstalk.com')
129129
except:
130-
__DEFAULT_PYCONFIG_DOMAIN = 'staging-pyconfig.eu-central-1.elasticbeanstalk.com'
130+
__DEFAULT_PYCONFIG_DOMAIN = 'pyconfig.eu-central-1.elasticbeanstalk.com'

esp32/frozen/Pybytes/_pybytes_protocol.py

Lines changed: 89 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -524,93 +524,101 @@ def set_battery_level(self, battery_level):
524524

525525
def deploy_new_release(self, body):
526526
application = self.__conf.get('application')
527+
try:
528+
body = ujson.loads(body.decode())
529+
except Exception as e:
530+
print_debug(0, "error while loading body {}".format(e))
531+
return
532+
527533
if application is not None:
528-
baseWebConfigUrl = 'https://{}'.format(constants.__DEFAULT_PYCONFIG_DOMAIN)
529-
manifestURL = '{}/manifest.json?'.format(baseWebConfigUrl)
530-
fileUrl = '{}/files?'.format(baseWebConfigUrl)
534+
if 'id' in application and application['id']:
535+
applicationID = application['id']
536+
else:
537+
applicationID = body['applicationId']
538+
if 'release' in application and 'codeFilename' in application['release']:
539+
currentReleaseID = application['release']['codeFilename']
540+
else:
541+
currentReleaseID = None
542+
else:
543+
applicationID = body['applicationId']
544+
currentReleaseID = None
545+
546+
baseWebConfigUrl = 'https://{}'.format(constants.__DEFAULT_PYCONFIG_DOMAIN)
547+
manifestURL = '{}/manifest.json?'.format(baseWebConfigUrl)
548+
fileUrl = '{}/files?'.format(baseWebConfigUrl)
549+
newReleaseID = body["releaseId"]
550+
targetURL = '{}app_id={}&target_ver={}&current_ver={}'.format(
551+
manifestURL,
552+
applicationID,
553+
newReleaseID,
554+
currentReleaseID
555+
)
556+
print_debug(6, "manifest URL: {}".format(targetURL))
557+
try:
558+
pybytes_activation = urequest.get(targetURL, headers={'content-type': 'application/json'})
559+
letResp = pybytes_activation.json()
560+
pybytes_activation.close()
561+
print_debug(6, "letResp: {}".format(letResp))
562+
except Exception as ex:
563+
print_debug(1, "error while calling {}!: {}".format(targetURL, ex))
564+
return
531565

532-
applicationID = application['id']
533-
currentReleaseID = application['release']['codeFilename']
534-
newReleaseID = body.decode()
566+
if 'errorMessage' in letResp:
567+
print_debug(1, letResp['errorMessage'])
568+
return
535569

570+
try:
571+
newFiles = letResp['newFiles']
572+
updatedFiles = letResp['updatedFiles']
573+
newFiles.extend(updatedFiles)
574+
except Exception as e:
575+
print_debug(1, "error getting files {}".format(e))
576+
newFiles = []
577+
578+
for file in newFiles:
579+
targetFileLocation = '{}application_id={}&target_ver={}&target_path={}'.format(
580+
fileUrl,
581+
applicationID,
582+
newReleaseID,
583+
file['fileName']
584+
)
536585
try:
537-
targetURL = '{}app_id={}&target_ver={}&current_ver={}'.format(
538-
manifestURL,
539-
applicationID,
540-
newReleaseID,
541-
currentReleaseID
542-
)
543-
544-
pybytes_activation = urequest.get(
545-
targetURL,
546-
headers={'content-type': 'application/json'})
586+
getFile = urequest.get(targetFileLocation, headers={'content-type': 'text/plain'})
587+
except Exception as e:
588+
print_debug(1, "error getting {}! {}".format(targetFileLocation, e))
589+
continue
547590

548-
letResp = pybytes_activation.json()
549-
pybytes_activation.close()
591+
fileContent = getFile.content
592+
self.__FCOTA.update_file_content(file['fileName'], fileContent)
550593

551-
try:
552-
errorMessage = letResp['errorMessage']
553-
print_debug(1, errorMessage)
554-
return
555-
except:
556-
# manifest does not contain an error message
557-
pass
594+
if 'deletedFiles' in letResp:
595+
deletedFiles = letResp['deletedFiles']
596+
for file in deletedFiles:
597+
self.__FCOTA.delete_file(file['fileName'])
558598

559-
newFiles = letResp['newFiles']
560-
updatedFiles = letResp['updatedFiles']
561-
newFiles.extend(updatedFiles)
599+
try:
600+
if application is None:
601+
self.__conf['application'] = {
602+
"id": "",
603+
"release": {
604+
"id": "",
605+
"codeFilename": "",
606+
"version": 0
607+
}
608+
}
609+
610+
self.__conf['application']["id"] = applicationID
611+
self.__conf['application']['release']['id'] = letResp['target_version']['id']
612+
self.__conf['application']['release']['codeFilename'] = letResp['target_version']['codeFileName']
613+
try:
614+
self.__conf['application']['release']['version'] = int(letResp['target_version']['version'])
615+
except Exception as e:
616+
print_debug(1, "error while converting version: {}".format(e))
562617

563-
for file in newFiles:
564-
targetFileLocation = '{}application_id={}&target_ver={}&target_path={}'.format(
565-
fileUrl,
566-
applicationID,
567-
newReleaseID,
568-
file['fileName']
569-
)
618+
json_string = ujson.dumps(self.__conf)
619+
print_debug(1, "json_string: {}".format(json_string))
620+
self.__FCOTA.update_file_content('/flash/pybytes_config.json', json_string)
621+
except Exception as e:
622+
print_debug(1, "error while updating pybytes_config.json! {}".format(e))
570623

571-
getFile = urequest.get(
572-
targetFileLocation,
573-
headers={'content-type': 'text/plain'})
574-
575-
fileContent = getFile.content
576-
self.__FCOTA.update_file_content(file['fileName'], fileContent)
577-
578-
deletedFiles = letResp['deletedFiles']
579-
for file in deletedFiles:
580-
self.__FCOTA.delete_file(file['fileName'])
581-
582-
self.__conf['application']['release']['idea'] = letResp['target_version']['id']
583-
self.__conf['application']['release']['codeFilename'] = letResp['target_version']['codeFileName']
584-
self.__conf['application']['release']['version'] = letResp['target_version']['version']
585-
json_string = ujson.dumps(self.__conf)
586-
self.__FCOTA.update_file_content('/flash/pybytes_config.json', json_string)
587-
588-
newFiles = letResp['newFiles']
589-
updatedFiles = letResp['updatedFiles']
590-
newFiles.extend(updatedFiles)
591-
592-
for file in newFiles:
593-
getFile = urequest.get(
594-
'{}application_id={}&target_ver={}&target_path={}'.format(
595-
fileUrl,
596-
applicationID,
597-
newReleaseID,
598-
file['fileName']
599-
),
600-
headers={'content-type': 'text/plain'})
601-
fileContent = getFile.content
602-
self.__FCOTA.update_file_content(file['fileName'], fileContent)
603-
604-
deletedFiles = letResp['deletedFiles']
605-
for file in deletedFiles:
606-
self.__FCOTA.delete_file(file['fileName'])
607-
608-
self.__conf['application']['release']['idea'] = letResp['target_version']['id']
609-
self.__conf['application']['release']['codeFilename'] = letResp['target_version']['codeFileName']
610-
self.__conf['application']['release']['version'] = letResp['target_version']['version']
611-
json_string = ujson.dumps(self.__conf)
612-
self.__FCOTA.update_file_content('/flash/pybytes_config.json', json_string)
613-
614-
machine.reset()
615-
except Exception as ex:
616-
print_debug(1, "an error has occurred while deploying changes!: {}".format(ex))
624+
machine.reset()

0 commit comments

Comments
 (0)
0