Commits

Jason Scheirer committed b87065c

Oh my god this code is awful

Comments (0)

Files changed (1)

 import curses
 import datetime
+import time
 import signal
+import sqlite3
 from bikesniff import gpsfix, networks
 
 class CursesUI(object):
-    def __init__(self):
+    def __init__(self, logger=None):
+        self.logger = logger
         self.stdscr = curses.initscr()
         curses.start_color()
         import twisted.internet.reactor
         self.stdscr.keypad(0)
         curses.echo()
         curses.endwin()
+        self.logger.dead = True
         self.reactor.stop()
     def update(self):
         try:
+            height, width = self.stdscr.getmaxyx()
             self.stdscr.addstr(1, 1, str(datetime.datetime.now()), curses.A_BOLD)
+            if self.logger.lock:
+                text = "Fix"
+                colr = curses.COLOR_GREEN
+            else:
+                text = "No Fix"
+                colr = curses.COLOR_RED
+            self.stdscr.addstr(1, width - (len(text)+2), text, colr)
             self.stdscr.border()
             self.stdscr.refresh()
         except:
             else:
                 self.reactor.callLater(0.5, self.update)
 
+class NetworkSnifferLogger(object):
+    def __init__(self):
+        import twisted.internet.reactor
+        self.dead = False
+        self.lock = False
+        self.reactor = twisted.internet.reactor
+        self.netlist = networks.NetworkList()
+        self.cache = {}
+        self.gps = gpsfix.GpsD(self.reactor)
+        self.conn = sqlite3.connect("gpslog.sqlite")
+        self.conn.text_factory = sqlite3.OptimizedUnicode
+        self.reactor.callLater(0, self.update)
+    def update(self):
+        self.netlist.update()
+        coords = self.gps.coordinates
+        self.lock = bool(coords)
+        lat, lon = coords if coords else (None, None)
+        if coords is not None:
+            for network in self.netlist.networks:
+                print network['ssid'],
+                key = network['hwaddress']
+                row = (str(datetime.datetime.now()), lat, lon, 
+                        network['hwaddress'], network['ssid'],
+                        network['strength'])
+                urow = tuple(row[3:])
+                insert = False
+                if key not in self.cache:
+                    self.cache[key] = {'age': time.time(), 'data': urow}
+                    insert = True
+                else:
+                    if self.cache[key]['data'] != urow:
+                        insert = True
+                        self.cache[key]['data'] = urow
+                        self.cache[key]['age'] = time.time()
+                if insert:
+                    #print row
+                    try:
+                        self.conn.execute("""INSERT INTO networks(timestamp,
+                                                                  lat, lon, 
+                                                                  hwaddress, 
+                                                                  ssid,
+                                                                  strength)
+                                 VALUES (?, ?, ?, ?, ?, ?)""", list(row))
+                    except Exception, e:
+                        self.err = e
+                    else:
+                        self.err = None
+        if not self.dead:
+            self.reactor.callLater(1, self.update)
+
 if __name__ == "__main__":
     import dbus.mainloop.glib
     import gobject
     dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
 
     import twisted.internet.reactor
-    netlist = networks.NetworkList()
-    gps = gpsfix.GpsD(twisted.internet.reactor)
-    
-    ui = CursesUI()
+
+    sniffer = NetworkSnifferLogger()
+    ui = CursesUI(sniffer)
 
     twisted.internet.reactor.run()