import time
-import config , db , gps
-
import gobject
import os
-conf = config.Configuration()
-
-class Scanner ( gps.GPSObject ) :
+class WifiScanner ( gobject.GObject ) :
def __init__ ( self , ifname="wlan0" ) :
- gps.GPSObject.__init__( self )
+ 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.db = db.database( os.path.join( conf.homedir , conf.dbname ) )
# Values to be set by wireless scans
+ self.scanlist = []
+ self.tstamp = 0
self.nscan = 0
self.nfp = 0
- self.newap = 0
- self.scanlist = []
def start ( self ) :
osso_rpc = osso.Rpc(self.osso_context)
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)
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)
try :
scan_out = osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "scan", wait_reply = True)
self.nscan +=1
+ del self.scanlist[:]
+ 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]) ) )
except Exception , ex :
osso.SystemNote(self.osso_context).system_note_infoprint("Exception scanning %s" % ex )
- return True
- del self.scanlist[:]
- tstamp = time.time()
- latlon = None
- if self.gps_state == "FIX" :
- latlon = ( self.device.fix[4] , self.device.fix[5] , self.device.fix[7] )
- self.store_scan( tstamp , scan_out , latlon )
- if self._debug :
- osso.SystemNote(self.osso_context).system_note_infoprint("Found %d APs" % len(scan_out) )
- else :
- self.write_logs( tstamp , scan_out )
-
- return True
-
- def store_scan ( self , timestamp , scan_out , gps_info ) :
- for net in scan_out.split() :
- self.nfp += 1
- items = net.rsplit(":", 1)
- self.scanlist.append( "%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 store_legacy ( self , timestamp , scan_out , gps_info ) :
- nets = scan_out.split()
- while nets :
- self.nfp += 1
- items = ( nets.pop(0) , nets.pop(0) )
- self.scanlist.append( "%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( 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 report ( self ) :
- return "%s\t%d scan\t%d fp\t%d ap\t%d total ap" % ( gps.GPSObject.report(self) , self.nscan , self.nfp , self.newap , self.db.nrows() )
+ return "%d scan\t%d fp" % ( self.nscan , self.nfp )
+
+gobject.type_register(WifiScanner)
-gobject.type_register(Scanner)
+if __name__ == "__main__" :
+ loop = gobject.MainLoop()
+ sample = WifiScanner()
+ sample.start()
+ for i in range(10) :
+ sample.scan()
+ print "scan results : %s" % sample.report()
+ print " tsamp %s\n %s" % ( sample.tstamp , " ".join( sample.scanlist ) )
+ print
+ sample.stop()