97a77548fe6ea0d5f8278bb829ab60945bddde27
[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 config , db , gps
10
11 import gobject
12
13 import os
14
15 conf = config.Configuration()
16
17 class Scanner ( gps.GPSObject ) :
18
19     def __init__ ( self , widget=None , ifname="wlan0" ) :
20         gps.GPSObject.__init__( self , widget )
21         self.osso_context = osso.Context("wifi_scanner", "2.0", False)
22         osso_rpc = osso.Rpc(self.osso_context)
23         scan_out = osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "wakeup", wait_reply = True)
24         self._timer = None
25         self.nscan = 0
26         self.nfp = 0
27         self.scanlist = None
28         self.aplist = {}
29         self.db = db.database( os.path.join( conf.homedir , conf.dbname ) )
30
31     def start ( self ) :
32         osso_rpc = osso.Rpc(self.osso_context)
33         scan_out = osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "start")
34         self.db.open()
35
36     def stop ( self ) :
37         osso_rpc = osso.Rpc(self.osso_context)
38         scan_out = osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "stop")
39         self.db.close()
40
41     def scan ( self ) :
42         timestamp = time.time()
43         osso_rpc = osso.Rpc(self.osso_context)
44         try :
45             scan_out = osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "scan", wait_reply = True)
46             self.nscan +=1
47         except Exception , ex :
48             osso.SystemNote(self.osso_context).system_note_infoprint("Exception scanning %s" % ex )
49             return True
50         out_str = ""
51         if self.scanlist :
52             start, end = self.scanlist.get_bounds()
53             self.scanlist.delete( start , end )
54         for net in scan_out.split() :
55             self.nfp += 1
56             items = net.rsplit(":", 1)
57             out_str += " %s %s" % ( items[0] , items[1] )
58             if self.scanlist :
59                 self.scanlist.insert_at_cursor( "%s %5d\n" % ( items[0] , int(items[1]) ) )
60             stored = self.db.get( items[0] )
61             if stored :
62                 max_rss = int(items[1])
63                 if stored[0] > max_rss :
64                     max_rss = stored[0]
65                 self.db.update( items[0] , max_rss , timestamp )
66             else :
67                 self.db.add( items[0] , int(items[1]) , timestamp )
68             self.aplist[ items[0] ] = 1
69         self.refresh_infowin()
70         if self._debug :
71             osso.SystemNote(self.osso_context).system_note_infoprint("Found %d APs" % len(scan_out) )
72         else :
73             fd = open( os.path.join( conf.homedir , "wiscan_gui.info" ) , 'a' )
74             fd.write( "%s %s%s\n" % ( timestamp , self.gps_info , out_str ) )
75             fd.close()
76             if self.satellites :
77                 loclist = open( os.path.join( conf.homedir , "location.info" ) , 'a' )
78                 loclist.write ( "%s\n" % ( self.satellites ,) )
79                 loclist.close()
80             if self.cell_info :
81                 celllist = open( os.path.join( conf.homedir , "cell.info" ) , 'a' )
82                 celllist.write ( "%s\n" % ( self.cell_info ,) )
83                 celllist.close()
84
85         return True
86
87     def set_infowin ( self , statuswin , listwin ) :
88         gps.GPSObject.set_infowin( self , statuswin )
89         self.scanlist = listwin
90
91     def refresh_infowin ( self ) :
92         if self.status :
93             self.status.set_text( "%d gps\t%d scan\t%d fp\t%d ap\t%d total ap" % ( self.ngps , self.nscan , self.nfp , len(self.aplist.keys()) , self.db.nrows() ) )
94
95
96 gobject.type_register(Scanner)
97