Achieve full decoupling of Scanner object and graphical environment
[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 , ifname="wlan0" ) :
20         gps.GPSObject.__init__( self )
21         self.osso_context = osso.Context("wifi_scanner", "2.0", False)
22         osso_rpc = osso.Rpc(self.osso_context)
23         osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "wakeup", wait_reply = True)
24         self.db = db.database( os.path.join( conf.homedir , conf.dbname ) )
25
26         # Values to be set by wireless scans
27         self.nscan = 0
28         self.nfp = 0
29         self.newap = 0
30         self.scanlist = []
31
32     def start ( self ) :
33         osso_rpc = osso.Rpc(self.osso_context)
34         osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "start")
35         self.db.open()
36
37     def stop ( self ) :
38         osso_rpc = osso.Rpc(self.osso_context)
39         osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "stop")
40         self.db.close()
41
42     def scan ( self ) :
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         del self.scanlist[:]
51         tstamp = time.time()
52         latlon = None
53         if self.gps_state == "FIX" :
54             latlon = ( self.device.fix[4] , self.device.fix[5] , self.device.fix[7] )
55         self.store_scan( tstamp , scan_out , latlon )
56         if self._debug :
57             osso.SystemNote(self.osso_context).system_note_infoprint("Found %d APs" % len(scan_out) )
58         else :
59             self.write_logs( tstamp , scan_out )
60
61         return True
62
63     def store_scan ( self , timestamp , scan_out , gps_info ) :
64         for net in scan_out.split() :
65             self.nfp += 1
66             items = net.rsplit(":", 1)
67             self.scanlist.append( "%s %5d\n" % ( items[0] , int(items[1]) ) )
68             stored = self.db.get( items[0] )
69             if stored :
70                 max_rss = int(items[1])
71                 if stored[0] > max_rss :
72                     max_rss = stored[0]
73                 self.db.update( items[0] , max_rss , timestamp , gps_info )
74             else :
75                 self.newap += 1
76                 self.db.add( items[0] , int(items[1]) , timestamp , gps_info )
77
78     def store_legacy ( self , timestamp , scan_out , gps_info ) :
79         nets = scan_out.split()
80         while nets :
81             self.nfp += 1
82             items = ( nets.pop(0) , nets.pop(0) )
83             self.scanlist.append( "%s %5d\n" % ( items[0] , int(items[1]) ) )
84             stored = self.db.get( items[0] )
85             if stored :
86                 max_rss = int(items[1])
87                 if stored[0] > max_rss :
88                     max_rss = stored[0]
89                 self.db.update( items[0] , max_rss , timestamp , gps_info )
90             else :
91                 self.newap += 1
92                 self.db.add( items[0] , int(items[1]) , timestamp , gps_info )
93
94     def write_logs ( self , timestamp , out_str ) :
95             fd = open( os.path.join( conf.homedir , "wiscan_gui.info" ) , 'a' )
96             fd.write( "%s %s %s\n" % ( timestamp , self.gps_info , out_str ) )
97             fd.close()
98             if self.satellites :
99                 loclist = open( os.path.join( conf.homedir , "location.info" ) , 'a' )
100                 loclist.write ( "%s\n" % ( self.satellites ,) )
101                 loclist.close()
102             if self.cell_info :
103                 celllist = open( os.path.join( conf.homedir , "cell.info" ) , 'a' )
104                 celllist.write ( "%s\n" % ( self.cell_info ,) )
105                 celllist.close()
106
107     def report ( self ) :
108         return "%s\t%d scan\t%d fp\t%d ap\t%d total ap" % ( gps.GPSObject.report(self) , self.nscan , self.nfp , self.newap , self.db.nrows() )
109
110
111 gobject.type_register(Scanner)
112