@@ -496,7 +496,7 @@ def __send_terminal_message(self, data):
496496
497497 def enable_terminal (self ):
498498 self .__terminal_enabled = True
499- #os.dupterm(self.__terminal)
499+ # os.dupterm(self.__terminal)
500500
501501 def __send_pybytes_message (self , command , pin_number , value ):
502502 self .__send_message (
@@ -522,14 +522,28 @@ def __send_pybytes_message_variable(
522522 def set_battery_level (self , battery_level ):
523523 self .__battery_level = battery_level
524524
525- def deploy_new_release (self , body ):
526- 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
525+ def write_firmware (self , customManifest = None ):
526+ ota = WiFiOTA (
527+ self .__conf ['wifi' ]['ssid' ],
528+ self .__conf ['wifi' ]['password' ],
529+ self .__conf ['ota_server' ]['domain' ],
530+ self .__conf ['ota_server' ]['port' ]
531+ )
532+
533+ if (self .__pybytes_connection .__connection_status == constants .__CONNECTION_STATUS_DISCONNECTED ): # noqa
534+ print_debug (5 , 'Connecting to WiFi' )
535+ ota .connect ()
532536
537+ print_debug (5 , "Performing OTA" )
538+ result = ota .update (customManifest )
539+ self .send_ota_response (result )
540+ time .sleep (1.5 )
541+ if (result == 2 ):
542+ # Reboot the device to run the new decode
543+ machine .reset ()
544+
545+ def get_application_details (self , body ):
546+ application = self .__conf .get ('application' )
533547 if application is not None :
534548 if 'id' in application and application ['id' ]:
535549 applicationID = application ['id' ]
@@ -542,23 +556,32 @@ def deploy_new_release(self, body):
542556 else :
543557 applicationID = body ['applicationId' ]
544558 currentReleaseID = None
559+ self .__conf ['application' ] = {
560+ "id" : "" ,
561+ "release" : {
562+ "id" : "" ,
563+ "codeFilename" : "" ,
564+ "version" : 0
565+ }
566+ }
567+ return (applicationID , currentReleaseID )
545568
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={}¤t_ver={}' .format (
569+ def get_update_manifest (self , applicationID , newReleaseID , currentReleaseID ):
570+ manifestURL = '{}://{}/manifest.json?' .format (constants .__DEFAULT_PYCONFIG_PROTOCOL , constants .__DEFAULT_PYCONFIG_DOMAIN )
571+ targetURL = '{}app_id={}&target_ver={}¤t_ver={}&device_id={}' .format (
551572 manifestURL ,
552573 applicationID ,
553574 newReleaseID ,
554- currentReleaseID
575+ currentReleaseID ,
576+ self .__conf ['device_id' ]
555577 )
556578 print_debug (6 , "manifest URL: {}" .format (targetURL ))
557579 try :
558580 pybytes_activation = urequest .get (targetURL , headers = {'content-type' : 'application/json' })
559581 letResp = pybytes_activation .json ()
560582 pybytes_activation .close ()
561583 print_debug (6 , "letResp: {}" .format (letResp ))
584+ return letResp
562585 except Exception as ex :
563586 print_debug (1 , "error while calling {}!: {}" .format (targetURL , ex ))
564587 return
@@ -567,6 +590,8 @@ def deploy_new_release(self, body):
567590 print_debug (1 , letResp ['errorMessage' ])
568591 return
569592
593+ def update_files (self , letResp , applicationID , newReleaseID ):
594+ fileUrl = '{}://{}/files?' .format (constants .__DEFAULT_PYCONFIG_PROTOCOL , constants .__DEFAULT_PYCONFIG_DOMAIN )
570595 try :
571596 newFiles = letResp ['newFiles' ]
572597 updatedFiles = letResp ['updatedFiles' ]
@@ -591,22 +616,14 @@ def deploy_new_release(self, body):
591616 fileContent = getFile .content
592617 self .__FCOTA .update_file_content (file ['fileName' ], fileContent )
593618
619+ def delete_files (self , letResp ):
594620 if 'deletedFiles' in letResp :
595621 deletedFiles = letResp ['deletedFiles' ]
596622 for file in deletedFiles :
597623 self .__FCOTA .delete_file (file ['fileName' ])
598624
625+ def update_application_config (self , letResp , applicationID ):
599626 try :
600- if application is None :
601- self .__conf ['application' ] = {
602- "id" : "" ,
603- "release" : {
604- "id" : "" ,
605- "codeFilename" : "" ,
606- "version" : 0
607- }
608- }
609-
610627 self .__conf ['application' ]["id" ] = applicationID
611628 self .__conf ['application' ]['release' ]['id' ] = letResp ['target_version' ]['id' ]
612629 self .__conf ['application' ]['release' ]['codeFilename' ] = letResp ['target_version' ]['codeFileName' ]
@@ -621,4 +638,69 @@ def deploy_new_release(self, body):
621638 except Exception as e :
622639 print_debug (1 , "error while updating pybytes_config.json! {}" .format (e ))
623640
641+ def update_network_config (self , letResp ):
642+ try :
643+ if 'networkConfig' in letResp :
644+ netConf = letResp ['networkConfig' ]
645+ self .__conf ['network_preferences' ] = netConf ['networkPreferences' ]
646+ if 'wifi' in netConf :
647+ self .__conf ['wifi' ] = netConf ['wifi' ]
648+ elif 'wifi' in self .__conf :
649+ del self .__conf ['wifi' ]
650+
651+ if 'lte' in netConf :
652+ self .__conf ['lte' ] = netConf ['lte' ]
653+ elif 'lte' in self .__conf :
654+ del self .__conf ['lte' ]
655+
656+ if 'lora' in netConf :
657+ self .__conf ['lora' ] = {
658+ 'otaa' : netConf ['lora' ]['otaa' ],
659+ 'abp' : netConf ['lora' ]['abp' ]
660+ }
661+ elif 'lora' in self .__conf :
662+ del self .__conf ['lora' ]
663+
664+ json_string = ujson .dumps (self .__conf )
665+ print_debug (1 , "update_network_config : {}" .format (json_string ))
666+ self .__FCOTA .update_file_content ('/flash/pybytes_config.json' , json_string )
667+ except Exception as e :
668+ print_debug (1 , "error while updating network config pybytes_config.json! {}" .format (e ))
669+
670+ def update_firmware (self , body ):
671+ if "firmware" not in body :
672+ print_debug (0 , "no firmware to update" )
673+ return
674+ version = body ['firmware' ]["version" ]
675+ print_debug (0 , "updating firmware to {}" .format (version ))
676+ customManifest = {
677+ "firmware" : {
678+ "URL" : "https://{}/downloads/appimg/firmware_{}_{}.bin" .format (
679+ constants .__DEFAULT_SW_HOST ,
680+ os .uname ().sysname ,
681+ version ),
682+ }
683+ }
684+ self .write_firmware (customManifest )
685+
686+ def deploy_new_release (self , body ):
687+ try :
688+ body = ujson .loads (body .decode ())
689+ print_debug (6 , "body {}" .format (body ))
690+ except Exception as e :
691+ print_debug (0 , "error while loading body {}" .format (e ))
692+ return
693+
694+ newReleaseID = body ["releaseId" ]
695+ applicationID , currentReleaseID = self .get_application_details (body )
696+
697+ letResp = self .get_update_manifest (applicationID , newReleaseID , currentReleaseID )
698+ if not letResp :
699+ return
700+
701+ self .update_files (letResp , applicationID , newReleaseID )
702+ self .delete_files (letResp )
703+ self .update_application_config (letResp , applicationID )
704+ self .update_network_config (letResp )
705+ self .update_firmware (letResp )
624706 machine .reset ()
0 commit comments