Initial implementation of AP positions update
[wifihood] / wifiscanner / wifimap / replay.py
1
2 import config
3 import db
4
5 import gobject
6
7 import os
8
9 class ReplayScanner ( gobject.GObject ) :
10
11     def __init__ ( self , ifname="wlan0" ) :
12         gobject.GObject.__init__( self )
13         self.scan_timeout = 0
14         conf = config.Configuration( 'scanner' )
15         self.db = db.database( os.path.join( conf.homedir , "wifireplay.db" ) )
16
17         # Values specific to replaying
18         self._file = os.path.join( conf.homedir , "wiscan_gui.info" )
19         self._fd = None
20         self._current = None
21         self._speed = 10
22
23         # Values to be set by GPSDevice changed events
24         self.info = None, 0, 0, None, None, None, None
25         self.satellites = None
26         self.cells = None
27         self.ngps = 0
28
29         # Values to be set by wireless scans
30         self.scanlist = {}
31         self.tstamp = 0
32         self.nscan = 0
33         self.nfp = 0
34
35         # Values from the Scanner object
36         self.newap = 0
37         self.newaps = False
38         self.aps = []
39
40     def start ( self , timeout=5000 ) :
41         self.scan_timeout = timeout
42         self.db.open()
43         self._fd = open( self._file )
44         self._current = self._fd.readline().split()
45
46     def stop ( self ) :
47         self.scan_timeout = 0
48         self.db.close()
49         self._fd.close()
50
51     def scan ( self ) :
52
53         if not self.scan_timeout :
54             return
55
56         next = self._fd.readline().split()
57         if not next :
58             return
59         delta = float(next[0]) - float(self._current[0])
60         gobject.timeout_add( int(1000 / self._speed * delta) , self.scan )
61
62         self.info = [ self._current[1] ]
63         self.info.extend( map( lambda x : float(x) , self._current[2:8] ) )
64         if self.info[0] == "FIX" :
65             self.ngps += 1
66
67         self.nscan +=1
68         self.scanlist.clear()
69         self.tstamp = float(self._current[0])
70         for n in range(10, len(self._current), 2) :
71             self.nfp += 1
72             self.scanlist[ self._current[n] ] = int(self._current[n+1])
73
74         newap = 0
75         self.newaps = False
76         for mac,max_rss in self.scanlist.iteritems() :
77             stored = self.db.db.execute( "SELECT rss, lat/n, lon/n FROM ap WHERE mac='%s'" % mac ).fetchone()
78             if stored :
79                 self.aps.append( stored[1:] )
80                 if stored[0] > max_rss :
81                     max_rss = stored[0]
82                 self.db.update( mac , max_rss , self.tstamp , self.info[4:] )
83             else :
84                 newap += 1
85                 self.db.add( mac , max_rss , self.tstamp , self.info[4:] )
86         if newap :
87             self.newap += newap
88             self.newaps = True
89
90         self._current = next
91
92     def report ( self ) :
93         return "%d gps\t%d scan\t%d fp\t%d ap\t%d total ap" % ( self.ngps , self.nscan , self.nfp , self.newap , self.db.nrows() )
94
95
96 gobject.type_register(ReplayScanner)
97
98 if __name__ == "__main__" :
99     loop = gobject.MainLoop()
100     sample = ReplayScanner()
101     def show_scan(wifiscanner):
102         gobject.timeout_add( 5000 , show_scan , sample )
103         print "scan results %s" % wifiscanner.report()
104         print "  tstamp %s" % wifiscanner.tstamp
105         c = 0
106         for k,v in wifiscanner.scanlist.iteritems() :
107            c += 1
108            print "    %s %s" % ( k , v )
109            if c > 5 :
110                print "    ..."
111                break
112         print
113     sample.start()
114     sample.scan()
115     gobject.timeout_add( 5100 , show_scan , sample )
116     loop.run()                                       
117     sample.stop()                                 
118