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