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