99import mpos .config
1010from mpos .net .wifi_service import WifiService
1111
12- have_network = True
13- try :
14- import network
15- except Exception as e :
16- have_network = False
17-
18- # Global variables because they're used by multiple Activities:
19- access_points = {}
20- last_tried_ssid = ""
21- last_tried_result = ""
22-
2312class WiFi (Activity ):
2413
2514 prefs = None
15+ access_points = {}
16+ last_tried_ssid = ""
17+ last_tried_result = ""
18+ have_network = True
19+ try :
20+ import network
21+ except Exception as e :
22+ have_network = False
2623
2724 scan_button_scan_text = "Rescan"
2825 scan_button_scanning_text = "Scanning..."
@@ -93,15 +90,14 @@ def hide_error(self, timer):
9390 self .update_ui_threadsafe_if_foreground (self .error_label .add_flag ,lv .obj .FLAG .HIDDEN )
9491
9592 def scan_networks_thread (self ):
96- global have_network
9793 print ("scan_networks: Scanning for Wi-Fi networks" )
98- if have_network :
94+ if self . have_network :
9995 wlan = network .WLAN (network .STA_IF )
10096 if not wlan .isconnected (): # restart WiFi hardware in case it's in a bad state
10197 wlan .active (False )
10298 wlan .active (True )
10399 try :
104- if have_network :
100+ if self . have_network :
105101 networks = wlan .scan ()
106102 self .ssids = list (set (n [0 ].decode () for n in networks ))
107103 else :
@@ -129,7 +125,6 @@ def start_scan_networks(self):
129125 _thread .start_new_thread (self .scan_networks_thread , ())
130126
131127 def refresh_list (self ):
132- global have_network
133128 print ("refresh_list: Clearing current list" )
134129 self .aplist .clean () # this causes an issue with lost taps if an ssid is clicked that has been removed
135130 print ("refresh_list: Populating list with scanned networks" )
@@ -141,13 +136,13 @@ def refresh_list(self):
141136 button = self .aplist .add_button (None ,ssid )
142137 button .add_event_cb (lambda e , s = ssid : self .select_ssid_cb (s ),lv .EVENT .CLICKED ,None )
143138 status = ""
144- if have_network :
139+ if self . have_network :
145140 wlan = network .WLAN (network .STA_IF )
146141 if wlan .isconnected () and wlan .config ('essid' )== ssid :
147142 status = "connected"
148143 if status != "connected" :
149- if last_tried_ssid == ssid : # implies not connected because not wlan.isconnected()
150- status = last_tried_result
144+ if self . last_tried_ssid == ssid : # implies not connected because not wlan.isconnected()
145+ status = self . last_tried_result
151146 elif ssid in access_points :
152147 status = "saved"
153148 label = lv .label (button )
@@ -177,19 +172,21 @@ def edit_network_result_callback(self, result):
177172 data = result .get ("data" )
178173 if data :
179174 ssid = data .get ("ssid" )
180- global access_points
181175 editor = self .prefs .edit ()
182176 forget = data .get ("forget" )
183177 if forget :
184- del access_points [ssid ]
185- editor .put_dict ("access_points" , access_points )
186- editor .commit ()
187- self .refresh_list ()
178+ try :
179+ del access_points [ssid ]
180+ editor .put_dict ("access_points" , self .access_points )
181+ editor .commit ()
182+ self .refresh_list ()
183+ except Exception as e :
184+ print (f"Error when trying to forget access point, it might not have been remembered in the first place: { e } " )
188185 else : # save or update
189186 password = data .get ("password" )
190187 hidden = data .get ("hidden" )
191188 self .setPassword (ssid , password , hidden )
192- editor .put_dict ("access_points" , access_points )
189+ editor .put_dict ("access_points" , self . access_points )
193190 editor .commit ()
194191 self .start_attempt_connecting (ssid , password )
195192
@@ -205,11 +202,10 @@ def start_attempt_connecting(self, ssid, password):
205202 _thread .start_new_thread (self .attempt_connecting_thread , (ssid ,password ))
206203
207204 def attempt_connecting_thread (self , ssid , password ):
208- global last_tried_ssid , last_tried_result , have_network
209205 print (f"attempt_connecting_thread: Attempting to connect to SSID '{ ssid } ' with password '{ password } '" )
210206 result = "connected"
211207 try :
212- if have_network :
208+ if self . have_network :
213209 wlan = network .WLAN (network .STA_IF )
214210 wlan .disconnect ()
215211 wlan .connect (ssid ,password )
@@ -222,43 +218,38 @@ def attempt_connecting_thread(self, ssid, password):
222218 if not wlan .isconnected ():
223219 result = "timeout"
224220 else :
225- print ("Warning: not trying to connect because not have_network, just waiting a bit..." )
221+ print ("Warning: not trying to connect because not self. have_network, just waiting a bit..." )
226222 time .sleep (5 )
227223 except Exception as e :
228224 print (f"attempt_connecting: Connection error: { e } " )
229225 result = f"{ e } "
230226 self .show_error ("Connecting to {ssid} failed!" )
231227 print (f"Connecting to { ssid } got result: { result } " )
232- last_tried_ssid = ssid
233- last_tried_result = result
228+ self . last_tried_ssid = ssid
229+ self . last_tried_result = result
234230 # also do a time sync, otherwise some apps (Nostr Wallet Connect) won't work:
235- if have_network and wlan .isconnected ():
231+ if self . have_network and wlan .isconnected ():
236232 mpos .time .sync_time ()
237233 self .busy_connecting = False
238234 self .update_ui_threadsafe_if_foreground (self .scan_button_label .set_text , self .scan_button_scan_text )
239235 self .update_ui_threadsafe_if_foreground (self .scan_button .remove_state , lv .STATE .DISABLED )
240236 self .update_ui_threadsafe_if_foreground (self .refresh_list )
241237
242- @staticmethod
243- def findSavedPassword (ssid ):
244- if not access_points :
245- return None
246- ap = access_points .get (ssid )
238+ def findSavedPassword (self , ssid ):
239+ ap = self .access_points .get (ssid )
247240 if ap :
248241 return ap .get ("password" )
249242 return None
250243
251- @staticmethod
252- def setPassword (ssid , password , hidden = False ):
253- global access_points
254- ap = access_points .get (ssid )
244+ def setPassword (self , ssid , password , hidden = False ):
245+ ap = self .access_points .get (ssid )
255246 if ap :
256247 ap ["password" ] = password
257248 if hidden is True :
258249 ap ["hidden" ] = True
259250 return
260251 # if not found, then add it:
261- access_points [ssid ] = { "password" : password , "hidden" : hidden }
252+ self . access_points [ssid ] = { "password" : password , "hidden" : hidden }
262253
263254
264255class EditNetwork (Activity ):
0 commit comments