3131except :
3232 from _pybytes_pymesh_config import PybytesPymeshConfig
3333
34+ try :
35+ from pybytes_machine_learning import MlFeatures
36+ except :
37+ from _pybytes_machine_learning import MlFeatures
38+
3439try :
3540 from pybytes_config_reader import PybytesConfigReader
3641except :
@@ -281,10 +286,10 @@ def __process_recv_message(self, message):
281286 splittedBody = bodyString .split (',' )
282287 if (len (splittedBody ) >= 2 ):
283288 path = splittedBody [0 ]
284- print_debug (2 , path [len (path )- 7 :len (path )])
285- if (path [len (path )- 7 :len (path )] != '.pymakr' ):
289+ print_debug (2 , path [len (path ) - 7 :len (path )])
290+ if (path [len (path ) - 7 :len (path )] != '.pymakr' ):
286291 self .send_fcota_ping ('updating file...' )
287- newContent = bodyString [len (path )+ 1 :len (body )]
292+ newContent = bodyString [len (path ) + 1 :len (body )]
288293 if (self .__FCOTA .update_file_content (path , newContent ) is True ): # noqa
289294 size = self .__FCOTA .get_file_size (path )
290295 self .send_fcota_file (newContent , path , size )
@@ -319,7 +324,18 @@ def __process_recv_message(self, message):
319324 if (len (body ) > 3 ):
320325 value = body [2 ] << 8 | body [3 ]
321326
322- if (command == constants .__COMMAND_PIN_MODE ):
327+ if (command == constants .__COMMAND_START_SAMPLE ):
328+ parameters = ujson .loads (body [2 : len (body )].decode ("utf-8" ))
329+ sampling = MlFeatures (self , parameters = parameters )
330+ sampling .start_sampling (pin = parameters ["pin" ])
331+ self .send_ota_response (result = 2 , topic = 'sample' )
332+ elif (command == constants .__COMMAND_DEPLOY_MODEL ):
333+ parameters = ujson .loads (body [2 : len (body )].decode ("utf-8" ))
334+ sampling = MlFeatures ()
335+ sampling .deploy_model (modelId = parameters ["modelId" ])
336+ self .send_ota_response (result = 2 , topic = 'deploymlmodel' )
337+
338+ elif (command == constants .__COMMAND_PIN_MODE ):
323339 pass
324340
325341 elif (command == constants .__COMMAND_DIGITAL_READ ):
@@ -633,16 +649,11 @@ def write_firmware(self, customManifest=None):
633649 def get_application_details (self , body ):
634650 application = self .__conf .get ('application' )
635651 if application is not None :
636- if 'id' in application and application ['id' ]:
637- applicationID = application ['id' ]
638- else :
639- applicationID = body ['applicationId' ]
640652 if 'release' in application and 'codeFilename' in application ['release' ]:
641653 currentReleaseID = application ['release' ]['codeFilename' ]
642654 else :
643655 currentReleaseID = None
644656 else :
645- applicationID = body ['applicationId' ]
646657 currentReleaseID = None
647658 self .__conf ['application' ] = {
648659 "id" : "" ,
@@ -652,6 +663,7 @@ def get_application_details(self, body):
652663 "version" : 0
653664 }
654665 }
666+ applicationID = body ['applicationId' ]
655667 return (applicationID , currentReleaseID )
656668
657669 def get_update_manifest (self , applicationID , newReleaseID , currentReleaseID ):
@@ -755,21 +767,46 @@ def update_network_config(self, letResp):
755767 except Exception as e :
756768 print_debug (1 , "error while updating network config pybytes_config.json! {}" .format (e ))
757769
758- def update_firmware (self , body ):
770+ def update_firmware (self , body , applicationID , fw_type = 'pybytes' ):
759771 if "firmware" not in body :
760772 print_debug (0 , "no firmware to update" )
761773 return
762- version = body ['firmware' ]["version" ]
763- print_debug (0 , "updating firmware to {}" .format (version ))
764- customManifest = {
765- "firmware" : {
766- "URL" : "https://{}/downloads/appimg/firmware_{}_{}.bin" .format (
767- constants .__DEFAULT_SW_HOST ,
768- os .uname ().sysname ,
769- version ),
774+
775+ if "version" in body ['firmware' ]:
776+ version = body ['firmware' ]["version" ]
777+ print_debug (0 , "updating firmware to {}" .format (version ))
778+
779+ customManifest = {
780+ "firmware" : {
781+ "URL" : "https://{}/findupgrade?redirect=true&strict=true&type={}&model={}&version={}&download=true" .format (
782+ constants .__DEFAULT_SW_HOST ,
783+ fw_type ,
784+ os .uname ().sysname ,
785+ version ),
786+ }
770787 }
771- }
772- self .write_firmware (customManifest )
788+ self .write_firmware (customManifest )
789+ else :
790+ fileUrl = '{}://{}/firmware?' .format (constants .__DEFAULT_PYCONFIG_PROTOCOL , constants .__DEFAULT_PYCONFIG_DOMAIN )
791+ customFirmwares = body ['firmware' ]["customFirmwares" ]
792+ firmwareFilename = ''
793+ for firmware in customFirmwares :
794+ print_debug (1 , "firmware['firmwareType']={} and os.uname().sysname.lower()={}" .format (firmware ['firmwareType' ], os .uname ().sysname .lower ()))
795+ print_debug (1 , "firmware={}" .format (firmware ))
796+ if (firmware ['firmwareType' ] == os .uname ().sysname .lower ()):
797+ firmwareFilename = firmware ['firmwareFilename' ]
798+ targetFileLocation = '{}application_id={}&target_ver={}&target_path={}' .format (
799+ fileUrl ,
800+ applicationID ,
801+ firmwareFilename ,
802+ '/{}.bin' .format (firmwareFilename )
803+ )
804+ customManifest = {
805+ "firmware" : {
806+ "URL" : targetFileLocation ,
807+ }
808+ }
809+ self .write_firmware (customManifest )
773810
774811 def deploy_new_release (self , body ):
775812 try :
@@ -783,12 +820,15 @@ def deploy_new_release(self, body):
783820 applicationID , currentReleaseID = self .get_application_details (body )
784821
785822 letResp = self .get_update_manifest (applicationID , newReleaseID , currentReleaseID )
823+
786824 if not letResp :
787825 return
788826
827+ fwtype = 'pygate' if hasattr (os .uname (), 'pygate' ) else 'pybytes'
828+ fwtype = 'pymesh' if hasattr (os .uname (), 'pymesh' ) else fwtype
789829 self .update_files (letResp , applicationID , newReleaseID )
790830 self .delete_files (letResp )
791831 self .update_application_config (letResp , applicationID )
792832 self .update_network_config (letResp )
793- self .update_firmware (letResp )
833+ self .update_firmware (letResp , applicationID , fw_type = fwtype )
794834 machine .reset ()
0 commit comments