@@ -193,8 +193,12 @@ def traverseConfig(config_root, config, interface_config) -> bool:
193193 return True
194194
195195
196- def setPref (config , comp_name , raw_val ) -> bool :
197- """Set a channel or preferences value"""
196+ def setPref (config , comp_name : str , raw_val ) -> bool :
197+ """Set a channel or preferences value
198+ config: LocalConfig, ModuleConfig structures from protobuf
199+ comp_name: dotted name of configuration entry
200+ raw_val: value to set
201+ """
198202
199203 name = splitCompoundName (comp_name )
200204
@@ -209,7 +213,7 @@ def setPref(config, comp_name, raw_val) -> bool:
209213 config_type = objDesc .fields_by_name .get (name [0 ])
210214 if config_type and config_type .message_type is not None :
211215 for name_part in name [1 :- 1 ]:
212- part_snake_name = meshtastic .util .camel_to_snake (( name_part ) )
216+ part_snake_name = meshtastic .util .camel_to_snake (name_part )
213217 config_part = getattr (config , config_type .name )
214218 config_type = config_type .message_type .fields_by_name .get (part_snake_name )
215219 pref = None
@@ -234,7 +238,7 @@ def setPref(config, comp_name, raw_val) -> bool:
234238
235239 enumType = pref .enum_type
236240 # pylint: disable=C0123
237- if enumType and type (val ) == str :
241+ if enumType and isinstance (val , str ) :
238242 # We've failed so far to convert this string into an enum, try to find it by reflection
239243 e = enumType .values_by_name .get (val )
240244 if e :
@@ -628,12 +632,11 @@ def onConnected(interface):
628632 closeNow = True
629633 waitForAckNak = True
630634 node = interface .getNode (args .dest , False , ** getNode_kwargs )
631-
632635 # Handle the int/float/bool arguments
633- pref = None
634- fields = set ()
636+ fields = []
637+ allSettingOK = True
635638 for pref in args .set :
636- found = False
639+ actSettingOK = False
637640 field = splitCompoundName (pref [0 ].lower ())[0 ]
638641 for config in [node .localConfig , node .moduleConfig ]:
639642 config_type = config .DESCRIPTOR .fields_by_name .get (field )
@@ -642,30 +645,29 @@ def onConnected(interface):
642645 node .requestConfig (
643646 config .DESCRIPTOR .fields_by_name .get (field )
644647 )
645- found = setPref (config , pref [0 ], pref [1 ])
646- if found :
647- fields .add (field )
648+ if actSettingOK := setPref (config , pref [0 ], pref [1 ]):
648649 break
650+ fields .append ((field , actSettingOK , pref [0 ]),)
651+ allSettingOK = allSettingOK and actSettingOK
649652
650- if found :
653+ # only write to radio when all settings can be processed. If one setting is wrong, drop everything.
654+ # This shall ensure consistency of settings in the radio
655+ if allSettingOK :
656+ fieldsToWrite = set ([field [0 ] for field in fields ]) # keep only one occurence of a field
651657 print ("Writing modified preferences to device" )
652- if len (fields ) > 1 :
658+ if len (fieldsToWrite ) > 1 :
653659 print ("Using a configuration transaction" )
654660 node .beginSettingsTransaction ()
655- for field in fields :
661+ for field in fieldsToWrite :
656662 print (f"Writing { field } configuration to device" )
657663 node .writeConfig (field )
658- if len (fields ) > 1 :
664+ if len (fieldsToWrite ) > 1 :
659665 node .commitSettingsTransaction ()
660666 else :
661- if mt_config .camel_case :
662- print (
663- f"{ node .localConfig .__class__ .__name__ } and { node .moduleConfig .__class__ .__name__ } do not have an attribute { pref [0 ]} ."
664- )
665- else :
666- print (
667- f"{ node .localConfig .__class__ .__name__ } and { node .moduleConfig .__class__ .__name__ } do not have attribute { pref [0 ]} ."
668- )
667+ print ("Cannot process command due to errors in parameters:" )
668+ for field , flag , settingName in fields :
669+ if not flag :
670+ print (f"{ node .localConfig .__class__ .__name__ } and { node .moduleConfig .__class__ .__name__ } do not have an attribute { settingName } in category { field } ." )
669671 print ("Choices are..." )
670672 printConfig (node .localConfig )
671673 printConfig (node .moduleConfig )
@@ -927,11 +929,11 @@ def setSimpleConfig(modem_preset):
927929 # Handle the channel settings
928930 for pref in args .ch_set or []:
929931 if pref [0 ] == "psk" :
930- found = True
932+ actSettingOK = True
931933 ch .settings .psk = meshtastic .util .fromPSK (pref [1 ])
932934 else :
933- found = setPref (ch .settings , pref [0 ], pref [1 ])
934- if not found :
935+ actSettingOK = setPref (ch .settings , pref [0 ], pref [1 ])
936+ if not actSettingOK :
935937 category_settings = ["module_settings" ]
936938 print (
937939 f"{ ch .settings .__class__ .__name__ } does not have an attribute { pref [0 ]} ."
@@ -1003,9 +1005,9 @@ def setSimpleConfig(modem_preset):
10031005 closeNow = True
10041006 node = interface .getNode (args .dest , False , ** getNode_kwargs )
10051007 for pref in args .get :
1006- found = getPref (node , pref [0 ])
1008+ actSettingOK = getPref (node , pref [0 ])
10071009
1008- if found :
1010+ if actSettingOK :
10091011 print ("Completed getting preferences" )
10101012
10111013 if args .nodes :
0 commit comments