Show on the map the displacement of the detected APs
[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         self.oldpos = {}
40
41     def start ( self , timeout=5000 , writelog=False ) :
42         self.scan_timeout = timeout
43         self.db.open()
44         self._fd = open( self._file )
45         self._current = self._fd.readline().split()
46
47     def stop ( self ) :
48         self.scan_timeout = 0
49         self.db.close()
50         self._fd.close()
51
52     def scan ( self ) :
53
54         if not self.scan_timeout :
55             return
56
57         next = self._fd.readline().split()
58         if not next :
59             return
60         delta = float(next[0]) - float(self._current[0])
61         gobject.timeout_add( int(1000 / self._speed * delta) , self.scan )
62
63         self.info = [ self._current[1] ]
64         self.info.extend( map( lambda x : float(x) , self._current[2:8] ) )
65         if self.info[0] == "FIX" :
66             self.ngps += 1
67
68         self.nscan +=1
69         self.scanlist.clear()
70         self.tstamp = float(self._current[0])
71         for n in range(10, len(self._current), 2) :
72             self.nfp += 1
73             self.scanlist[ self._current[n] ] = int(self._current[n+1])
74
75         newap = 0
76         self.newaps = False
77         for mac,max_rss in self.scanlist.iteritems() :
78             stored = self.db.db.execute( "SELECT rss, n, lat, lon FROM ap WHERE mac='%s'" % mac ).fetchone()
79             if stored :
80                 if not self.aps.has_key( mac ) :
81                     self.aps[ mac ] = list(stored[1:])
82                 if stored[0] > max_rss :
83                     max_rss = stored[0]
84                 self.db.update( mac , max_rss , self.tstamp , self.info[4:] )
85             else :
86                 newap += 1
87                 if not self.aps.has_key( mac ) :
88                     self.aps[ mac ] = [ 0 , 0 , 0 ]
89                 self.db.add( mac , max_rss , self.tstamp , self.info[4:] )
90             if self.aps[mac][0] != 0 :
91                 self.oldpos[mac] = self.aps[mac][1] / self.aps[mac][0] , self.aps[mac][2] / self.aps[mac][0]
92             self.aps[mac][0] += 1
93             self.aps[mac][1] += self.info[4]
94             self.aps[mac][2] += self.info[5]
95         if newap :
96             self.newap += newap
97             self.newaps = True
98
99         self._current = next
100
101     def report ( self ) :
102         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() )
103
104
105 gobject.type_register(ReplayScanner)
106
107 if __name__ == "__main__" :
108     loop = gobject.MainLoop()
109     sample = ReplayScanner()
110     def show_scan(wifiscanner):
111         gobject.timeout_add( 5000 , show_scan , sample )
112         print "scan results %s" % wifiscanner.report()
113         print "  tstamp %s" % wifiscanner.tstamp
114         c = 0
115         for k,v in wifiscanner.scanlist.iteritems() :
116            c += 1
117            print "    %s %s" % ( k , v )
118            if c > 5 :
119                print "    ..."
120                break
121         print
122     sample.start()
123     sample.scan()
124     gobject.timeout_add( 5100 , show_scan , sample )
125     loop.run()                                       
126     sample.stop()                                 
127