REFACTORING : modify Scanner object to actually become a combination of GPS and WifiS...
[wifihood] / wifimap / wifiscan.py
index efb4d9b..d9f7d17 100644 (file)
@@ -17,32 +17,37 @@ class WifiScanner ( gobject.GObject ) :
         self.osso_context = osso.Context("wifi_scanner", "2.0", False)
         osso_rpc = osso.Rpc(self.osso_context)
         osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "wakeup", wait_reply = True)
+        self.scan_timeout = 0
 
         # Values to be set by wireless scans
-        self.scanlist = []
+        self.scanlist = {}
         self.tstamp = 0
         self.nscan = 0
         self.nfp = 0
 
-    def start ( self ) :
+    def start ( self , timeout=5000 ) :
         osso_rpc = osso.Rpc(self.osso_context)
         osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "start")
+        self.scan_timeout = timeout
 
     def stop ( self ) :
         osso_rpc = osso.Rpc(self.osso_context)
         osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "stop")
+        self.scan_timeout = 0
 
     def scan ( self ) :
         osso_rpc = osso.Rpc(self.osso_context)
         try :
             scan_out = osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "scan", wait_reply = True)
+            if self.scan_timeout :
+                gobject.timeout_add( self.scan_timeout , WifiScanner.scan , self )
             self.nscan +=1
-            del self.scanlist[:]
+            self.scanlist.clear()
             self.tstamp = time.time()
             for net in scan_out.split() :
                 self.nfp += 1
                 items = net.rsplit(":", 1)
-                self.scanlist.append( "%s %5d\n" % ( items[0] , int(items[1]) ) )
+                self.scanlist[ items[0] ] = int(items[1])
         except Exception , ex :
             osso.SystemNote(self.osso_context).system_note_infoprint("Exception scanning %s" % ex )
 
@@ -56,10 +61,20 @@ if __name__ == "__main__" :
     loop = gobject.MainLoop()
     sample = WifiScanner()
     sample.start()                                                        
-    for i in range(10) :
-        sample.scan()
+    def show_scan(sample):
+        gobject.timeout_add( 5000 , show_scan , sample )
         print "scan results : %s" % sample.report()
-        print "  tsamp %s\n    %s" % ( sample.tstamp , "    ".join( sample.scanlist ) )
+        print "  tstamp %s" % sample.tstamp
+        c = 0
+        for k,v in sample.scanlist.iteritems() :
+           c += 1
+           print "    %s %s" % ( k , v )
+           if c > 5 :
+               print "    ..."
+               break
         print
+    sample.scan()                                                        
+    gobject.timeout_add( 5100 , show_scan , sample )
+    loop.run()                                       
     sample.stop()