import sys import re import time import datetime import subprocess pattern = re.compile(r"^GPS Location:\s+(\S+)\s+\S\s+(\S+)" r"\s+\S\s+\S+\s+(\S+)\s+") bad_coordinate = "69696969.000" bad_freshness = "1e+308" stale = 900.0 # position must not be older than this many seconds timeout = datetime.timedelta(0,300) # wait up to this many seconds fo log to be appended glider = sys.argv[1] log = sys.argv[2] handle = open(log) content = [] timer = datetime.datetime.now() while True: line = handle.readline() if line: timer = datetime.datetime.now() content.append(line) match = pattern.search(line) if match: lat, lon, fresh = match.groups() if lat == bad_coordinate: continue if lon == bad_coordinate: continue if fresh == bad_freshness: continue if float(fresh) >= stale: print "\nStale GPS fix for %s: %.2f seconds old in %s." % (glider, float(fresh), log), sys.stdout.flush() continue print "\nFresh GPS fix for %s: %.3f %.3f." % (glider, float(lat), float(lon)), sys.stdout.flush() process = subprocess.Popen([sys.executable, "/home/localuser/pilottools/lb/toolshed/points/points.py", glider, lat, lon, "bearing", "0"]) process.wait() break else: if datetime.datetime.now() - timer > timeout: print "\nTimeout reading %s log %s." % (glider, log) break time.sleep(5) handle.close()