b45346f0b684ca557ed3e9f176758d95c0505f34
[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         self.write = False
26
27         # Values to be set by wireless scans
28         self.newap = 0
29         self.newaps = False
30         self.aps = []
31
32     def start ( self , timeout=5000 , writelog=False ) :
33         gps.GPSObject.start( self )
34         wifiscan.WifiScanner.start( self , timeout )
35         self.db.open()
36         self.write = writelog
37
38     def stop ( self ) :
39         gps.GPSObject.stop( self )
40         wifiscan.WifiScanner.stop( self )
41         self.db.close()
42         self.write = False
43
44     def scan ( self ) :
45         wifiscan.WifiScanner.scan( self )
46         newap = 0
47         self.newaps = False
48         for mac,max_rss in self.scanlist.iteritems() :
49             stored = self.db.db.execute( "SELECT rss, lat/n, lon/n FROM ap WHERE mac='%s'" % mac ).fetchone()
50             if stored :
51                 self.aps.append( stored[1:] )
52                 if stored[0] > max_rss :
53                     max_rss = stored[0]
54                 self.db.update( mac , max_rss , self.tstamp , self.info[4:] )
55             else :
56                 newap += 1
57                 self.db.add( mac , max_rss , self.tstamp , self.info[4:] )
58         if newap :
59             self.newap += newap
60             self.newaps = True
61         if self.write :
62             self.write_logs()
63
64     def write_logs ( self ) :
65             fd = open( os.path.join( self.homedir , "wiscan_gui.info" ) , 'a' )
66             fd.write( "%s %s %s\n" % ( self.tstamp , " ".join(self.info) , " ".join(self.scanlist) ) )
67             fd.close()
68             if self.satellites :
69                 loclist = open( os.path.join( self.homedir , "location.info" ) , 'a' )
70                 loclist.write ( "%s\n" % ( self.satellites ,) )
71                 loclist.close()
72             if self.cells :
73                 celllist = open( os.path.join( self.homedir , "cell.info" ) , 'a' )
74                 celllist.write ( "%s\n" % ( self.cells ,) )
75                 celllist.close()
76
77     def report ( self ) :
78         # BUG : if report is called after close, db.nrows() will produce an exception
79         return "%s\t%s\t%d ap\t%d total ap" % ( gps.GPSObject.report(self) , wifiscan.WifiScanner.report(self) , self.newap , self.db.nrows() )
80
81
82 gobject.type_register(Scanner)
83
84 if __name__ == "__main__" :
85     loop = gobject.MainLoop()
86     sample = Scanner()
87     def on_stop(control, mainloop):
88         mainloop.quit()
89     sample.control.connect("gpsd-stopped", on_stop, loop)
90     def show_scan(wifiscanner):
91         gobject.timeout_add( 5000 , show_scan , sample )
92         print "scan results %s" % wifiscanner.report()
93         print "  tstamp %s" % wifiscanner.tstamp
94         c = 0
95         for k,v in wifiscanner.scanlist.iteritems() :
96            c += 1
97            print "    %s %s" % ( k , v )
98            if c > 5 :
99                print "    ..."
100                break
101         print
102     sample.start()
103     sample.scan()
104     gobject.timeout_add( 5100 , show_scan , sample )
105     loop.run()                                       
106     sample.stop()                                 
107