BUGFIX : call the scan() method of the proper object
[wifihood] / wifimap / wifiscan.py
1
2 try :
3     import osso
4 except :
5     import osso_wrapper as osso
6
7 import time
8
9 import gobject
10
11 import os
12
13 class WifiScanner ( gobject.GObject ) :
14
15     def __init__ ( self , ifname="wlan0" ) :
16         gobject.GObject.__init__( self )
17         self.osso_context = osso.Context("wifi_scanner", "2.0", False)
18         osso_rpc = osso.Rpc(self.osso_context)
19         osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "wakeup", wait_reply = True)
20         self.scan_timeout = 0
21
22         # Values to be set by wireless scans
23         self.scanlist = {}
24         self.tstamp = 0
25         self.nscan = 0
26         self.nfp = 0
27
28     def start ( self , timeout=5000 ) :
29         osso_rpc = osso.Rpc(self.osso_context)
30         osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "start")
31         self.scan_timeout = timeout
32
33     def stop ( self ) :
34         osso_rpc = osso.Rpc(self.osso_context)
35         osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "stop")
36         self.scan_timeout = 0
37
38     def scan ( self ) :
39         osso_rpc = osso.Rpc(self.osso_context)
40         try :
41             scan_out = osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "scan", wait_reply = True)
42             if self.scan_timeout :
43                 gobject.timeout_add( self.scan_timeout , self.scan )
44             self.nscan +=1
45             self.scanlist.clear()
46             self.tstamp = time.time()
47             for net in scan_out.split() :
48                 self.nfp += 1
49                 items = net.rsplit(":", 1)
50                 self.scanlist[ items[0] ] = int(items[1])
51         except Exception , ex :
52             osso.SystemNote(self.osso_context).system_note_infoprint("Exception scanning %s" % ex )
53
54     def report ( self ) :
55         return "%d scan\t%d fp" % ( self.nscan , self.nfp )
56
57
58 gobject.type_register(WifiScanner)
59
60 if __name__ == "__main__" :
61     loop = gobject.MainLoop()
62     sample = WifiScanner()
63     sample.start()                                                        
64     def show_scan(sample):
65         gobject.timeout_add( 5000 , show_scan , sample )
66         print "scan results : %s" % sample.report()
67         print "  tstamp %s" % sample.tstamp
68         c = 0
69         for k,v in sample.scanlist.iteritems() :
70            c += 1
71            print "    %s %s" % ( k , v )
72            if c > 5 :
73                print "    ..."
74                break
75         print
76     sample.scan()                                                        
77     gobject.timeout_add( 5100 , show_scan , sample )
78     loop.run()                                       
79     sample.stop()                                 
80