#!/usr/bin/env python # # Program to automatically save the situation at certain intervals from xplra import XPlane, ProtocolException import sys import os import time if __name__ == "__main__": interval = float(sys.argv[1]) xplanePath = sys.argv[2] name = sys.argv[3] newName = name + ".new.sit" savePath = "output/situations/" + newName basePath = os.path.join(xplanePath, "Output", "situations", name) path = basePath + ".sit" newPath = basePath + ".new.sit" oldPath = basePath + ".old.sit" xplane = XPlane() while True: print "Connecting to X-Plane..." while not xplane.isConnected: try: xplane.connect() except: print "Connection failed, sleeping..." time.sleep(0.5) print "Connected to X-Plane" try: while True: try: xplane.saveSituation(savePath) try: if os.path.exists(path): os.rename(path, oldPath) os.rename(newPath, path) print "Situation saved at " + time.strftime("%H:%M:%S") except OSError, e: print "I/O error occured: " + str(e) except ProtocolException, e: print "Protocol error occured: " + str(e) time.sleep(interval) except Exception, e: print "Connection failed: " + str(e) xplane.disconnect()