Use points of different size to signal that some new AP was found
[wifihood] / wifiscanner / wifimap / scanner.py
1
2 try :
3     import osso
4 except :
5     import osso_wrapper as osso
6
7 import time
8
9 import config , db
10 import gps , wifiscan
11
12 import gobject
13
14 import os
15
16 class Scanner ( gps.GPSObject , wifiscan.WifiScanner ) :
17
18     def __init__ ( self , ifname="wlan0" ) :
19         gps.GPSObject.__init__( self )
20         wifiscan.WifiScanner.__init__( self , ifname )
21         conf = config.Configuration( 'scanner' )
22         self.homedir = conf.homedir
23         self.db = db.database( os.path.join( self.homedir , conf.dbname ) )
24
25         # Values to be set by wireless scans
26         self.newap = 0
27         self.newaps = False
28
29     def start ( self , timeout=5000 ) :
30         gps.GPSObject.start( self )
31         wifiscan.WifiScanner.start( self , timeout )
32         self.db.open()
33
34     def stop ( self ) :
35         gps.GPSObject.stop( self )
36         wifiscan.WifiScanner.stop( self )
37         self.db.close()
38
39     def scan ( self ) :
40         wifiscan.WifiScanner.scan( self )
41         newap = 0
42         self.newaps = False
43         for mac,max_rss in self.scanlist.iteritems() :
44             stored = self.db.get( mac )
45             if stored :
46                 if stored[0] > max_rss :
47                     max_rss = stored[0]
48                 self.db.update( mac , max_rss , self.tstamp , self.info[4:] )
49             else :
50                 newap += 1
51                 self.db.add( mac , max_rss , self.tstamp , self.info[4:] )
52         if newap :
53             self.newap += newap
54             self.newaps = True
55         self.write_logs()
56
57     def write_logs ( self ) :
58             fd = open( os.path.join( self.homedir , "wiscan_gui.info" ) , 'a' )
59             fd.write( "%s %s %s\n" % ( self.tstamp , " ".join(self.info) , " ".join(self.scanlist) ) )
60             fd.close()
61             if self.satellites :
62                 loclist = open( os.path.join( self.homedir , "location.info" ) , 'a' )
63                 loclist.write ( "%s\n" % ( self.satellites ,) )
64                 loclist.close()
65             if self.cells :
66                 celllist = open( os.path.join( self.homedir , "cell.info" ) , 'a' )
67                 celllist.write ( "%s\n" % ( self.cells ,) )
68                 celllist.close()
69
70     def report ( self ) :
71         # BUG : if report is called after close, db.nrows() will produce an exception
72         return "%s\t%s\t%d ap\t%d total ap" % ( gps.GPSObject.report(self) , wifiscan.WifiScanner.report(self) , self.newap , self.db.nrows() )
73
74
75 gobject.type_register(Scanner)
76
77 if __name__ == "__main__" :
78     loop = gobject.MainLoop()
79     sample = Scanner()
80     def on_stop(control, mainloop):
81         mainloop.quit()
82     sample.control.connect("gpsd-stopped", on_stop, loop)
83     def show_scan(wifiscanner):
84         gobject.timeout_add( 5000 , show_scan , sample )
85         print "scan results %s" % wifiscanner.report()
86         print "  tstamp %s" % wifiscanner.tstamp
87         c = 0
88         for k,v in wifiscanner.scanlist.iteritems() :
89            c += 1
90            print "    %s %s" % ( k , v )
91            if c > 5 :
92                print "    ..."
93                break
94         print
95     sample.start()
96     sample.scan()
97     gobject.timeout_add( 5100 , show_scan , sample )
98     loop.run()                                       
99     sample.stop()                                 
100