Initial implementation of AP positions update
[wifihood] / wifiscanner / wifimap / replay.py
index d40bfb6..649828e 100644 (file)
@@ -1,17 +1,18 @@
 
 import config
+import db
 
 import gobject
 
 import os
 
-conf = config.Configuration()
-
 class ReplayScanner ( gobject.GObject ) :
 
-    def __init__ ( self ) :
+    def __init__ ( self , ifname="wlan0" ) :
         gobject.GObject.__init__( self )
         self.scan_timeout = 0
+        conf = config.Configuration( 'scanner' )
+        self.db = db.database( os.path.join( conf.homedir , "wifireplay.db" ) )
 
         # Values specific to replaying
         self._file = os.path.join( conf.homedir , "wiscan_gui.info" )
@@ -33,14 +34,18 @@ class ReplayScanner ( gobject.GObject ) :
 
         # Values from the Scanner object
         self.newap = 0
+        self.newaps = False
+        self.aps = []
 
     def start ( self , timeout=5000 ) :
         self.scan_timeout = timeout
+        self.db.open()
         self._fd = open( self._file )
         self._current = self._fd.readline().split()
 
     def stop ( self ) :
         self.scan_timeout = 0
+        self.db.close()
         self._fd.close()
 
     def scan ( self ) :
@@ -49,24 +54,43 @@ class ReplayScanner ( gobject.GObject ) :
             return
 
         next = self._fd.readline().split()
+        if not next :
+            return
         delta = float(next[0]) - float(self._current[0])
         gobject.timeout_add( int(1000 / self._speed * delta) , self.scan )
 
-        self.info = self._current[1:8]
+        self.info = [ self._current[1] ]
+        self.info.extend( map( lambda x : float(x) , self._current[2:8] ) )
         if self.info[0] == "FIX" :
             self.ngps += 1
 
         self.nscan +=1
         self.scanlist.clear()
         self.tstamp = float(self._current[0])
-        for n in range(11, len(self._current), 2) :
+        for n in range(10, len(self._current), 2) :
             self.nfp += 1
             self.scanlist[ self._current[n] ] = int(self._current[n+1])
 
+        newap = 0
+        self.newaps = False
+        for mac,max_rss in self.scanlist.iteritems() :
+            stored = self.db.db.execute( "SELECT rss, lat/n, lon/n FROM ap WHERE mac='%s'" % mac ).fetchone()
+            if stored :
+                self.aps.append( stored[1:] )
+                if stored[0] > max_rss :
+                    max_rss = stored[0]
+                self.db.update( mac , max_rss , self.tstamp , self.info[4:] )
+            else :
+                newap += 1
+                self.db.add( mac , max_rss , self.tstamp , self.info[4:] )
+        if newap :
+            self.newap += newap
+            self.newaps = True
+
         self._current = next
 
     def report ( self ) :
-        return "%d gps\t%d scan\t%d fp\t%d ap\t%d total ap" % ( self.ngps , self.nscan , self.nfp , self.newap , -1 )
+        return "%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() )
 
 
 gobject.type_register(ReplayScanner)