41f2e5c0515ee5f8b027d214aba0385ec8b8121d
[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 conf = config.Configuration()
17
18 class Scanner ( gps.GPSObject , wifiscan.WifiScanner ) :
19
20     def __init__ ( self , ifname="wlan0" ) :
21         gps.GPSObject.__init__( self )
22         wifiscan.WifiScanner.__init__( self , ifname )
23         self.db = db.database( os.path.join( conf.homedir , conf.dbname ) )
24
25         # Values to be set by wireless scans
26         self.newap = 0
27
28     def start ( self , timeout=5000 ) :
29         gps.GPSObject.start( self )
30         wifiscan.WifiScanner.start( self , timeout )
31         self.db.open()
32
33     def stop ( self ) :
34         gps.GPSObject.stop( self )
35         wifiscan.WifiScanner.stop( self )
36         self.db.close()
37
38     def scan ( self ) :
39         wifiscan.WifiScanner.scan( self )
40         for mac,max_rss in self.scanlist.iteritems() :
41             stored = self.db.get( mac )
42             if stored :
43                 if stored[0] > max_rss :
44                     max_rss = stored[0]
45                 self.db.update( mac , max_rss , self.tstamp , self.info[4:] )
46             else :
47                 self.newap += 1
48                 self.db.add( mac , max_rss , self.tstamp , self.info[4:] )
49         self.write_logs()
50
51     def write_logs ( self ) :
52             fd = open( os.path.join( conf.homedir , "wiscan_gui.info" ) , 'a' )
53             fd.write( "%s %s %s\n" % ( self.tstamp , self.info , self.scanlist ) )
54             fd.close()
55             if self.satellites :
56                 loclist = open( os.path.join( conf.homedir , "location.info" ) , 'a' )
57                 loclist.write ( "%s\n" % ( self.satellites ,) )
58                 loclist.close()
59             if self.cells :
60                 celllist = open( os.path.join( conf.homedir , "cell.info" ) , 'a' )
61                 celllist.write ( "%s\n" % ( self.cells ,) )
62                 celllist.close()
63
64     def report ( self ) :
65         # BUG : if report is called after close, db.nrows() will produce an exception
66         return "%s\t%s\t%d ap\t%d total ap" % ( gps.GPSObject.report(self) , wifiscan.WifiScanner.report(self) , self.newap , self.db.nrows() )
67
68
69 gobject.type_register(Scanner)
70
71 if __name__ == "__main__" :
72     loop = gobject.MainLoop()
73     sample = Scanner()
74     def on_stop(control, mainloop):
75         mainloop.quit()
76     sample.control.connect("gpsd-stopped", on_stop, loop)
77     def show_scan(wifiscanner):
78         gobject.timeout_add( 5000 , show_scan , sample )
79         print "scan results %s" % wifiscanner.report()
80         print "  tstamp %s" % wifiscanner.tstamp
81         c = 0
82         for k,v in wifiscanner.scanlist.iteritems() :
83            c += 1
84            print "    %s %s" % ( k , v )
85            if c > 5 :
86                print "    ..."
87                break
88         print
89     sample.start()
90     sample.scan()
91     gobject.timeout_add( 5100 , show_scan , sample )
92     loop.run()                                       
93     sample.stop()                                 
94