MERGE : incorporate changes on cleaning branch to completelly decouple wifiview and...
[wifihood] / wifimap / scanner.py
diff --git a/wifimap/scanner.py b/wifimap/scanner.py
new file mode 100644 (file)
index 0000000..03389ba
--- /dev/null
@@ -0,0 +1,94 @@
+
+try :
+    import osso
+except :
+    import osso_wrapper as osso
+
+import time
+
+import config , db
+import gps , wifiscan
+
+import gobject
+
+import os
+
+conf = config.Configuration()
+
+class Scanner ( gps.GPSObject , wifiscan.WifiScanner ) :
+
+    def __init__ ( self , ifname="wlan0" ) :
+        gps.GPSObject.__init__( self )
+        wifiscan.WifiScanner.__init__( self )
+        self.db = db.database( os.path.join( conf.homedir , conf.dbname ) )
+
+        # Values to be set by wireless scans
+        self.newap = 0
+
+    def start ( self , timeout=5000 ) :
+        gps.GPSObject.start( self )
+        wifiscan.WifiScanner.start( self , timeout )
+        self.db.open()
+
+    def stop ( self ) :
+        gps.GPSObject.stop( self )
+        wifiscan.WifiScanner.stop( self )
+        self.db.close()
+
+    def scan ( self ) :
+        wifiscan.WifiScanner.scan( self )
+        for mac,max_rss in self.scanlist.iteritems() :
+            stored = self.db.get( mac )
+            if stored :
+                if stored[0] > max_rss :
+                    max_rss = stored[0]
+                self.db.update( mac , max_rss , self.tstamp , self.info[4:] )
+            else :
+                self.newap += 1
+                self.db.add( mac , max_rss , self.tstamp , self.info[4:] )
+        self.write_logs()
+
+    def write_logs ( self ) :
+            fd = open( os.path.join( conf.homedir , "wiscan_gui.info" ) , 'a' )
+            fd.write( "%s %s %s\n" % ( self.tstamp , self.info , self.scanlist ) )
+            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.cells :
+                celllist = open( os.path.join( conf.homedir , "cell.info" ) , 'a' )
+                celllist.write ( "%s\n" % ( self.cells ,) )
+                celllist.close()
+
+    def report ( self ) :
+        # BUG : if report is called after close, db.nrows() will produce an exception
+        return "%s\t%s\t%d ap\t%d total ap" % ( gps.GPSObject.report(self) , wifiscan.WifiScanner.report(self) , self.newap , self.db.nrows() )
+
+
+gobject.type_register(Scanner)
+
+if __name__ == "__main__" :
+    loop = gobject.MainLoop()
+    sample = Scanner()
+    def on_stop(control, mainloop):
+        mainloop.quit()
+    sample.control.connect("gpsd-stopped", on_stop, loop)
+    def show_scan(wifiscanner):
+        gobject.timeout_add( 5000 , show_scan , sample )
+        print "scan results %s" % wifiscanner.report()
+        print "  tstamp %s" % wifiscanner.tstamp
+        c = 0
+        for k,v in wifiscanner.scanlist.iteritems() :
+           c += 1
+           print "    %s %s" % ( k , v )
+           if c > 5 :
+               print "    ..."
+               break
+        print
+    sample.start()
+    sample.scan()
+    gobject.timeout_add( 5100 , show_scan , sample )
+    loop.run()                                       
+    sample.stop()                                 
+