REFACTORING : move tstamp into real scanner object
[wifihood] / wifiscanner / wifimap / wifiscan.py
index b35d7f0..f2a59ba 100644 (file)
 try :
     import osso
 except :
-    import osso_wrapper as osso
+    import hildongtk.osso as osso
 
 import time
 
-import config , db , gps
-
 import gobject
 
 import os
 
-conf = config.Configuration()
+class WifiScanner ( gobject.GObject ) :
 
-class Scanner ( gps.GPSObject ) :
+    def __init__ ( self , ifname="wlan0" ) :
+        gobject.GObject.__init__( self )
+        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
 
-    def __init__ ( self , widget=None , ifname="wlan0" ) :
-        gps.GPSObject.__init__( self , widget )
-        self.osso_context = None
-        if widget :
-            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._timer = None
+        # Values to be set by wireless scans
+        self.scanlist = {}
         self.nscan = 0
         self.nfp = 0
-        self.scanlist = None
-        self.newap = 0
-        self.db = db.database( os.path.join( conf.homedir , conf.dbname ) )
 
-    def start ( self ) :
-        if self.osso_context :
-            osso_rpc = osso.Rpc(self.osso_context)
-            scan_out = osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "start")
-        self.db.open()
+    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)
-        scan_out = osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "stop")
-        self.db.close()
+        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 , self.scan )
+            # BUG : if scan is called after stop (maybe in other cases), 'ERROR' is returned and split raises exception
             self.nscan +=1
+            self.scanlist.clear()
+            for net in scan_out.split() :
+                self.nfp += 1
+                items = net.rsplit(":", 1)
+                self.scanlist[ items[0] ] = int(items[1])
         except Exception , ex :
             osso.SystemNote(self.osso_context).system_note_infoprint("Exception scanning %s" % ex )
-            return True
-        if self.scanlist :
-            start, end = self.scanlist.get_bounds()
-            self.scanlist.delete( start , end )
-        tstamp = time.time()
-        out_str = self.store_scan( scan_out , tstamp , scan_out )
-        self.refresh_infowin( tstamp )
-        if self._debug :
-            osso.SystemNote(self.osso_context).system_note_infoprint("Found %d APs" % len(scan_out) )
-        else :
-            self.write_logs( out_str )
-
-        return True
-
-    def store_scan ( self , timestamp , scan_out ) :
-        out_str = ""
-        for net in scan_out.split() :
-            self.nfp += 1
-            items = net.rsplit(":", 1)
-            out_str += " %s %s" % ( items[0] , items[1] )
-            if self.scanlist :
-                self.scanlist.insert_at_cursor( "%s %5d\n" % ( items[0] , int(items[1]) ) )
-            stored = self.db.get( items[0] )
-            if stored :
-                max_rss = int(items[1])
-                if stored[0] > max_rss :
-                    max_rss = stored[0]
-                self.db.update( items[0] , max_rss , timestamp )
-            else :
-                self.newap += 1
-                self.db.add( items[0] , int(items[1]) , timestamp )
-        return out_str
-
-    def store_logscan ( self , timestamp , scan_out ) :
-        nets = scan_out.split()
-        while nets :
-            self.nfp += 1
-            items = ( nets.pop(0) , nets.pop(0) )
-            if self.scanlist :
-                self.scanlist.insert_at_cursor( "%s %5d\n" % ( items[0] , int(items[1]) ) )
-            stored = self.db.get( items[0] )
-            if stored :
-                max_rss = int(items[1])
-                if stored[0] > max_rss :
-                    max_rss = stored[0]
-                self.db.update( items[0] , max_rss , timestamp )
-            else :
-                self.newap += 1
-                self.db.add( items[0] , int(items[1]) , timestamp )
-
-    def write_logs ( self , timestamp , out_str ) :
-            fd = open( os.path.join( conf.homedir , "wiscan_gui.info" ) , 'a' )
-            fd.write( "%s %s%s\n" % ( timestamp , self.gps_info , out_str ) )
-            fd.close()
-            if self.satellites :
-                loclist = open( os.path.join( conf.homedir , "location.info" ) , 'a' )
-                loclist.write ( "%s\n" % ( self.satellites ,) )
-                loclist.close()
-            if self.cell_info :
-                celllist = open( os.path.join( conf.homedir , "cell.info" ) , 'a' )
-                celllist.write ( "%s\n" % ( self.cell_info ,) )
-                celllist.close()
-
-    def set_infowin ( self , statuswin , listwin ) :
-        gps.GPSObject.set_infowin( self , statuswin )
-        self.scanlist = listwin
-
-    def refresh_infowin ( self ) :
-        if self.status :
-            self.status.set_text( "%d gps\t%d scan\t%d fp\t%d ap\t%d total ap" % ( self.ngps , self.nscan , self.nfp , self.newap , self.db.nrows() ) )
-
-
-gobject.type_register(Scanner)
+
+    def __str__ ( self ) :
+        output = map( lambda x : "%s %s" % x , self.scanlist.iteritems() )
+        return " ".join( output )
+
+    def report ( self ) :
+        return "%d scan\t%d fp" % ( self.nscan , self.nfp )
+
+
+gobject.type_register(WifiScanner)
+
+if __name__ == "__main__" :
+    loop = gobject.MainLoop()
+    sample = WifiScanner()
+    sample.start()                                                        
+    def show_scan(sample):
+        gobject.timeout_add( 5000 , show_scan , sample )
+        print "scan results : %s" % sample.report()
+        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()