Handle geocoordinates in logscanner
[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         out_str = self.store_scan( scan_out , tstamp , scan_out )
57         self.refresh_infowin( tstamp )
58         if self._debug :
59             osso.SystemNote(self.osso_context).system_note_infoprint("Found %d APs" % len(scan_out) )
60         else :
61             self.write_logs( out_str )
62
63         return True
64
65     def store_scan ( self , timestamp , scan_out ) :
66         out_str = ""
67         for net in scan_out.split() :
68             self.nfp += 1
69             items = net.rsplit(":", 1)
70             out_str += " %s %s" % ( items[0] , items[1] )
71             if self.scanlist :
72                 self.scanlist.insert_at_cursor( "%s %5d\n" % ( items[0] , int(items[1]) ) )
73             stored = self.db.get( items[0] )
74             if stored :
75                 max_rss = int(items[1])
76                 if stored[0] > max_rss :
77                     max_rss = stored[0]
78                 self.db.update( items[0] , max_rss , timestamp )
79             else :
80                 self.newap += 1
81                 self.db.add( items[0] , int(items[1]) , timestamp )
82         return out_str
83
84     def store_logscan ( self , timestamp , scan_out , gps_info=None ) :
85         nets = scan_out.split()
86         while nets :
87             self.nfp += 1
88             items = ( nets.pop(0) , nets.pop(0) )
89             if self.scanlist :
90                 self.scanlist.insert_at_cursor( "%s %5d\n" % ( items[0] , int(items[1]) ) )
91             stored = self.db.get( items[0] )
92             if stored :
93                 max_rss = int(items[1])
94                 if stored[0] > max_rss :
95                     max_rss = stored[0]
96                 self.db.update( items[0] , max_rss , timestamp , gps_info )
97             else :
98                 self.newap += 1
99                 self.db.add( items[0] , int(items[1]) , timestamp , gps_info )
100
101     def write_logs ( self , timestamp , out_str ) :
102             fd = open( os.path.join( conf.homedir , "wiscan_gui.info" ) , 'a' )
103             fd.write( "%s %s%s\n" % ( timestamp , self.gps_info , out_str ) )
104             fd.close()
105             if self.satellites :
106                 loclist = open( os.path.join( conf.homedir , "location.info" ) , 'a' )
107                 loclist.write ( "%s\n" % ( self.satellites ,) )
108                 loclist.close()
109             if self.cell_info :
110                 celllist = open( os.path.join( conf.homedir , "cell.info" ) , 'a' )
111                 celllist.write ( "%s\n" % ( self.cell_info ,) )
112                 celllist.close()
113
114     def set_infowin ( self , statuswin , listwin ) :
115         gps.GPSObject.set_infowin( self , statuswin )
116         self.scanlist = listwin
117
118     def refresh_infowin ( self ) :
119         if self.status :
120             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() ) )
121
122
123 gobject.type_register(Scanner)
124