Implemented a program able to feed a stored logfile like a live scan
authorjaviplx <javiplx@gmail.com>
Mon, 11 Oct 2010 16:34:50 +0000 (16:34 +0000)
committerjaviplx <javiplx@gmail.com>
Mon, 11 Oct 2010 16:34:50 +0000 (16:34 +0000)
git-svn-id: file:///svnroot/wifihood/trunk/wifiscanner@48 c51dfc6a-5949-4919-9c8e-f207a149c383

logscanner [new file with mode: 0755]
wifimap/wifiscan.py

diff --git a/logscanner b/logscanner
new file mode 100755 (executable)
index 0000000..022b50c
--- /dev/null
@@ -0,0 +1,26 @@
+#!/usr/bin/python
+
+import wifimap
+
+import sys
+
+def main ( scanner , filename ) :
+    scanner.start()
+    fd = open( filename )
+    for line in fd.readlines() :
+        gps_info = line[:-1].split(None,11)
+        tstamp = float(gps_info.pop(0))
+        scan_out = gps_info.pop()
+        scanner.nscan +=1
+        scanner.store_logscan( tstamp , scan_out )
+        print "%d gps\t%d scan\t%d fp\t%d ap\t%d total ap" % ( scanner.ngps , scanner.nscan , scanner.nfp , scanner.newap , scanner.db.nrows() )
+    fd.close()
+
+if __name__ == "__main__" :
+
+    if len(sys.argv) != 2 :
+        print "Usage : logscanner wifiscanner.log"
+        sys.exit(1)
+    scanner = wifimap.Scanner()
+    main( scanner , sys.argv[1] )
+
index d87f624..b35d7f0 100644 (file)
@@ -18,9 +18,11 @@ 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
@@ -29,8 +31,9 @@ class Scanner ( gps.GPSObject ) :
         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 ) :
@@ -39,7 +42,6 @@ class Scanner ( gps.GPSObject ) :
         self.db.close()
 
     def scan ( self ) :
-        timestamp = time.time()
         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)
@@ -47,10 +49,21 @@ 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()
+        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)
@@ -66,10 +79,26 @@ class Scanner ( gps.GPSObject ) :
             else :
                 self.newap += 1
                 self.db.add( items[0] , int(items[1]) , timestamp )
-        self.refresh_infowin()
-        if self._debug :
-            osso.SystemNote(self.osso_context).system_note_infoprint("Found %d APs" % len(scan_out) )
-        else :
+        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()
@@ -82,8 +111,6 @@ class Scanner ( gps.GPSObject ) :
                 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