Handle geocordinates under active scanning
[wifihood] / wifiscanner / 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 = None
22         if widget :
23             self.osso_context = osso.Context("wifi_scanner", "2.0", False)
24             osso_rpc = osso.Rpc(self.osso_context)
25             osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "wakeup", wait_reply = True)
26         self._timer = None
27         self.nscan = 0
28         self.nfp = 0
29         self.scanlist = None
30         self.newap = 0
31         self.db = db.database( os.path.join( conf.homedir , conf.dbname ) )
32
33     def start ( self ) :
34         if self.osso_context :
35             osso_rpc = osso.Rpc(self.osso_context)
36             scan_out = osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "start")
37         self.db.open()
38
39     def stop ( self ) :
40         osso_rpc = osso.Rpc(self.osso_context)
41         scan_out = osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "stop")
42         self.db.close()
43
44     def scan ( self ) :
45         osso_rpc = osso.Rpc(self.osso_context)
46         try :
47             scan_out = osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "scan", wait_reply = True)
48             self.nscan +=1
49         except Exception , ex :
50             osso.SystemNote(self.osso_context).system_note_infoprint("Exception scanning %s" % ex )
51             return True
52         if self.scanlist :
53             start, end = self.scanlist.get_bounds()
54             self.scanlist.delete( start , end )
55         tstamp = time.time()
56         latlon = None
57         if self.gps_state == "FIX" :
58             latlon = ( self.device.fix[4] , self.device.fix[5] , self.device.fix[7] )
59         out_str = self.store_scan( scan_out , tstamp , scan_out , latlon )
60         self.refresh_infowin( tstamp )
61         if self._debug :
62             osso.SystemNote(self.osso_context).system_note_infoprint("Found %d APs" % len(scan_out) )
63         else :
64             self.write_logs( out_str )
65
66         return True
67
68     def store_scan ( self , timestamp , scan_out , gps_info ) :
69         out_str = ""
70         for net in scan_out.split() :
71             self.nfp += 1
72             items = net.rsplit(":", 1)
73             out_str += " %s %s" % ( items[0] , items[1] )
74             if self.scanlist :
75                 self.scanlist.insert_at_cursor( "%s %5d\n" % ( items[0] , int(items[1]) ) )
76             stored = self.db.get( items[0] )
77             if stored :
78                 max_rss = int(items[1])
79                 if stored[0] > max_rss :
80                     max_rss = stored[0]
81                 self.db.update( items[0] , max_rss , timestamp , gps_info )
82             else :
83                 self.newap += 1
84                 self.db.add( items[0] , int(items[1]) , timestamp , gps_info )
85         return out_str
86
87     def store_logscan ( self , timestamp , scan_out , gps_info ) :
88         nets = scan_out.split()
89         while nets :
90             self.nfp += 1
91             items = ( nets.pop(0) , nets.pop(0) )
92             if self.scanlist :
93                 self.scanlist.insert_at_cursor( "%s %5d\n" % ( items[0] , int(items[1]) ) )
94             stored = self.db.get( items[0] )
95             if stored :
96                 max_rss = int(items[1])
97                 if stored[0] > max_rss :
98                     max_rss = stored[0]
99                 self.db.update( items[0] , max_rss , timestamp , gps_info )
100             else :
101                 self.newap += 1
102                 self.db.add( items[0] , int(items[1]) , timestamp , gps_info )
103
104     def write_logs ( self , timestamp , out_str ) :
105             fd = open( os.path.join( conf.homedir , "wiscan_gui.info" ) , 'a' )
106             fd.write( "%s %s%s\n" % ( timestamp , self.gps_info , out_str ) )
107             fd.close()
108             if self.satellites :
109                 loclist = open( os.path.join( conf.homedir , "location.info" ) , 'a' )
110                 loclist.write ( "%s\n" % ( self.satellites ,) )
111                 loclist.close()
112             if self.cell_info :
113                 celllist = open( os.path.join( conf.homedir , "cell.info" ) , 'a' )
114                 celllist.write ( "%s\n" % ( self.cell_info ,) )
115                 celllist.close()
116
117     def set_infowin ( self , statuswin , listwin ) :
118         gps.GPSObject.set_infowin( self , statuswin )
119         self.scanlist = listwin
120
121     def refresh_infowin ( self ) :
122         if self.status :
123             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 , self.newap , self.db.nrows() ) )
124
125
126 gobject.type_register(Scanner)
127