REFACTORING : move tstamp into real scanner object
[wifihood] / wifiscanner / wifimap / wifiscan.py
1
2 try :
3     import osso
4 except :
5     import hildongtk.osso 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.nscan = 0
25         self.nfp = 0
26
27     def start ( self , timeout=5000 ) :
28         osso_rpc = osso.Rpc(self.osso_context)
29         osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "start")
30         self.scan_timeout = timeout
31
32     def stop ( self ) :
33         osso_rpc = osso.Rpc(self.osso_context)
34         osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "stop")
35         self.scan_timeout = 0
36
37     def scan ( self ) :
38         osso_rpc = osso.Rpc(self.osso_context)
39         try :
40             scan_out = osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "scan", wait_reply = True)
41             if self.scan_timeout :
42                 gobject.timeout_add( self.scan_timeout , self.scan )
43             # BUG : if scan is called after stop (maybe in other cases), 'ERROR' is returned and split raises exception
44             self.nscan +=1
45             self.scanlist.clear()
46             for net in scan_out.split() :
47                 self.nfp += 1
48                 items = net.rsplit(":", 1)
49                 self.scanlist[ items[0] ] = int(items[1])
50         except Exception , ex :
51             osso.SystemNote(self.osso_context).system_note_infoprint("Exception scanning %s" % ex )
52
53     def __str__ ( self ) :
54         output = map( lambda x : "%s %s" % x , self.scanlist.iteritems() )
55         return " ".join( output )
56
57     def report ( self ) :
58         return "%d scan\t%d fp" % ( self.nscan , self.nfp )
59
60
61 gobject.type_register(WifiScanner)
62
63 if __name__ == "__main__" :
64     loop = gobject.MainLoop()
65     sample = WifiScanner()
66     sample.start()                                                        
67     def show_scan(sample):
68         gobject.timeout_add( 5000 , show_scan , sample )
69         print "scan results : %s" % sample.report()
70         c = 0
71         for k,v in sample.scanlist.iteritems() :
72            c += 1
73            print "    %s %s" % ( k , v )
74            if c > 5 :
75                print "    ..."
76                break
77         print
78     sample.scan()                                                        
79     gobject.timeout_add( 5100 , show_scan , sample )
80     loop.run()                                       
81     sample.stop()                                 
82