Handle geocordinates under active scanning
[wifihood] / wifiscanner / wifimap / wifiscan.py
index ed7f7cf..4035467 100644 (file)
@@ -1,32 +1,45 @@
 
-import osso
+try :
+    import osso
+except :
+    import osso_wrapper as osso
 
 import time
 
-import gps
+import config , db , gps
 
 import gobject
 
+import os
+
+conf = config.Configuration()
+
 class Scanner ( gps.GPSObject ) :
 
     def __init__ ( self , widget=None , ifname="wlan0" ) :
         gps.GPSObject.__init__( self , widget )
-        self.osso_context = osso.Context("wifi_scanner", "2.0", False)
-        osso_rpc = osso.Rpc(self.osso_context)
-        scan_out = osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "wakeup", wait_reply = True)
+        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
         self.nscan = 0
         self.nfp = 0
         self.scanlist = None
-        self.aplist = {}
+        self.newap = 0
+        self.db = db.database( os.path.join( conf.homedir , conf.dbname ) )
 
     def start ( self ) :
-        osso_rpc = osso.Rpc(self.osso_context)
-        scan_out = osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "start")
+        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 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()
 
     def scan ( self ) :
         osso_rpc = osso.Rpc(self.osso_context)
@@ -36,44 +49,78 @@ class Scanner ( gps.GPSObject ) :
         except Exception , ex :
             osso.SystemNote(self.osso_context).system_note_infoprint("Exception scanning %s" % ex )
             return True
-        out_str = ""
         if self.scanlist :
             start, end = self.scanlist.get_bounds()
             self.scanlist.delete( start , end )
+        tstamp = time.time()
+        latlon = None
+        if self.gps_state == "FIX" :
+            latlon = ( self.device.fix[4] , self.device.fix[5] , self.device.fix[7] )
+        out_str = self.store_scan( scan_out , tstamp , scan_out , latlon )
+        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 , gps_info ) :
+        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]) ) )
-            self.aplist[ items[0] ] = 1
-        self.refresh_infowin()
-        if self._debug :
-        # Use osso or hildon for notes ???
-            osso.SystemNote(self.osso_context).system_note_infoprint("Found %d APs" % len(scan_out) )
-        #    hildon.hildon_banner_show_information( self._parent , "icon_path" , "Found %d APs" % len(scan_out) )
-        else :
-            fd = open( "/home/user/MyDocs/wiscan_gui.info" , 'a' )
-            fd.write( "%s %s%s\n" % ( time.time() , self.gps_info , out_str ) )
+            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 , gps_info )
+            else :
+                self.newap += 1
+                self.db.add( items[0] , int(items[1]) , timestamp , gps_info )
+        return out_str
+
+    def store_logscan ( self , timestamp , scan_out , gps_info ) :
+        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 , gps_info )
+            else :
+                self.newap += 1
+                self.db.add( items[0] , int(items[1]) , timestamp , gps_info )
+
+    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( "/home/user/MyDocs/location.info" , 'a' )
+                loclist = open( os.path.join( conf.homedir , "location.info" ) , 'a' )
                 loclist.write ( "%s\n" % ( self.satellites ,) )
                 loclist.close()
             if self.cell_info :
-                celllist = open( "/home/user/MyDocs/cell.info" , 'a' )
+                celllist = open( os.path.join( conf.homedir , "cell.info" ) , 'a' )
                 celllist.write ( "%s\n" % ( self.cell_info ,) )
                 celllist.close()
 
-        return True
-
     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_label( "%d gps\t%d scan\t%d fp\t%d ap" % ( self.ngps , self.nscan , self.nfp , len(self.aplist.keys()) ) )
+            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)