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