31
31
except :
32
32
from _pybytes_pymesh_config import PybytesPymeshConfig
33
33
34
+ try :
35
+ from pybytes_machine_learning import MlFeatures
36
+ except :
37
+ from _pybytes_machine_learning import MlFeatures
38
+
34
39
try :
35
40
from pybytes_config_reader import PybytesConfigReader
36
41
except :
@@ -281,10 +286,10 @@ def __process_recv_message(self, message):
281
286
splittedBody = bodyString .split (',' )
282
287
if (len (splittedBody ) >= 2 ):
283
288
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' ):
286
291
self .send_fcota_ping ('updating file...' )
287
- newContent = bodyString [len (path )+ 1 :len (body )]
292
+ newContent = bodyString [len (path ) + 1 :len (body )]
288
293
if (self .__FCOTA .update_file_content (path , newContent ) is True ): # noqa
289
294
size = self .__FCOTA .get_file_size (path )
290
295
self .send_fcota_file (newContent , path , size )
@@ -319,7 +324,18 @@ def __process_recv_message(self, message):
319
324
if (len (body ) > 3 ):
320
325
value = body [2 ] << 8 | body [3 ]
321
326
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 ):
323
339
pass
324
340
325
341
elif (command == constants .__COMMAND_DIGITAL_READ ):
@@ -633,16 +649,11 @@ def write_firmware(self, customManifest=None):
633
649
def get_application_details (self , body ):
634
650
application = self .__conf .get ('application' )
635
651
if application is not None :
636
- if 'id' in application and application ['id' ]:
637
- applicationID = application ['id' ]
638
- else :
639
- applicationID = body ['applicationId' ]
640
652
if 'release' in application and 'codeFilename' in application ['release' ]:
641
653
currentReleaseID = application ['release' ]['codeFilename' ]
642
654
else :
643
655
currentReleaseID = None
644
656
else :
645
- applicationID = body ['applicationId' ]
646
657
currentReleaseID = None
647
658
self .__conf ['application' ] = {
648
659
"id" : "" ,
@@ -652,6 +663,7 @@ def get_application_details(self, body):
652
663
"version" : 0
653
664
}
654
665
}
666
+ applicationID = body ['applicationId' ]
655
667
return (applicationID , currentReleaseID )
656
668
657
669
def get_update_manifest (self , applicationID , newReleaseID , currentReleaseID ):
@@ -755,21 +767,46 @@ def update_network_config(self, letResp):
755
767
except Exception as e :
756
768
print_debug (1 , "error while updating network config pybytes_config.json! {}" .format (e ))
757
769
758
- def update_firmware (self , body ):
770
+ def update_firmware (self , body , applicationID , fw_type = 'pybytes' ):
759
771
if "firmware" not in body :
760
772
print_debug (0 , "no firmware to update" )
761
773
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
+ }
770
787
}
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 )
773
810
774
811
def deploy_new_release (self , body ):
775
812
try :
@@ -783,12 +820,15 @@ def deploy_new_release(self, body):
783
820
applicationID , currentReleaseID = self .get_application_details (body )
784
821
785
822
letResp = self .get_update_manifest (applicationID , newReleaseID , currentReleaseID )
823
+
786
824
if not letResp :
787
825
return
788
826
827
+ fwtype = 'pygate' if hasattr (os .uname (), 'pygate' ) else 'pybytes'
828
+ fwtype = 'pymesh' if hasattr (os .uname (), 'pymesh' ) else fwtype
789
829
self .update_files (letResp , applicationID , newReleaseID )
790
830
self .delete_files (letResp )
791
831
self .update_application_config (letResp , applicationID )
792
832
self .update_network_config (letResp )
793
- self .update_firmware (letResp )
833
+ self .update_firmware (letResp , applicationID , fw_type = fwtype )
794
834
machine .reset ()
0 commit comments