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