REFACTORING : implement setter methods for coordinates and zoom to make easier the...
[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
38     def start ( self , timeout=5000 ) :
39         self.scan_timeout = timeout
40         self.db.open()
41         self._fd = open( self._file )
42         self._current = self._fd.readline().split()
43
44     def stop ( self ) :
45         self.scan_timeout = 0
46         self.db.close()
47         self._fd.close()
48
49     def scan ( self ) :
50
51         if not self.scan_timeout :
52             return
53
54         next = self._fd.readline().split()
55         delta = float(next[0]) - float(self._current[0])
56         gobject.timeout_add( int(1000 / self._speed * delta) , self.scan )
57
58         self.info = [ self._current[1] ]
59         self.info.extend( map( lambda x : float(x) , self._current[2:8] ) )
60         if self.info[0] == "FIX" :
61             self.ngps += 1
62
63         self.nscan +=1
64         self.scanlist.clear()
65         self.tstamp = float(self._current[0])
66         for n in range(11, len(self._current), 2) :
67             self.nfp += 1
68             self.scanlist[ self._current[n] ] = int(self._current[n+1])
69
70         for mac,max_rss in self.scanlist.iteritems() :
71             stored = self.db.get( mac )
72             if stored :
73                 if stored[0] > max_rss :
74                     max_rss = stored[0]
75                 self.db.update( mac , max_rss , self.tstamp , self.info[4:] )
76             else :
77                 self.newap += 1
78                 self.db.add( mac , max_rss , self.tstamp , self.info[4:] )
79
80         self._current = next
81
82     def report ( self ) :
83         return "%d gps\t%d scan\t%d fp\t%d ap\t%d total ap" % ( self.ngps , self.nscan , self.nfp , self.newap , -1 )
84
85
86 gobject.type_register(ReplayScanner)
87
88 if __name__ == "__main__" :
89     loop = gobject.MainLoop()
90     sample = ReplayScanner()
91     def show_scan(wifiscanner):
92         gobject.timeout_add( 5000 , show_scan , sample )
93         print "scan results %s" % wifiscanner.report()
94         print "  tstamp %s" % wifiscanner.tstamp
95         c = 0
96         for k,v in wifiscanner.scanlist.iteritems() :
97            c += 1
98            print "    %s %s" % ( k , v )
99            if c > 5 :
100                print "    ..."
101                break
102         print
103     sample.start()
104     sample.scan()
105     gobject.timeout_add( 5100 , show_scan , sample )
106     loop.run()                                       
107     sample.stop()                                 
108