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