#!/usr/bin/python #UDP listener import signal,sys def signal_handler(signal, frame): insock.close() print "wim: tomorrowtheground: interrupted" sys.exit(0) signal.signal(signal.SIGINT, signal_handler) from latLng import * from layers import * from xml2obj import * if len(sys.argv)<2: print "wim: usage: tomorrowtheground configfile [-D] (debug)][-T] (test)]" debug=False test=False if len(sys.argv)>2: if sys.argv[2]=="-D" or sys.argv[2]=="-d": debug=True print "wim: DEBUG mode" if len(sys.argv)>3: if sys.argv[3]=="-T" or sys.argv[3]=="-t": test=True print "wim: gps TEST mode" doc=xml2obj(open(sys.argv[1])) gpslayers=[] #catch invalid xml try: for i in doc.gps.index: #catch invalid xml try: g=indexlayer(i.file,i.ll1,i.ll2) for t in i.trigger: g.triggers.append(trigger(int(t.id),t.command,t.param)) gpslayers.append(g) except: print "wim: error parsing xml index entry" except: print "wim: no index layers found" #catch invalid xml try: for i in doc.gps.scale: #catch invalid xml try: g=scalelayer(i.file,i.ll1,i.ll2) g.setcommand(i.command) gpslayers.append(g) except: print "wim: error parsing xml index entry" except: print "wim: no scale layers found" from gpspoller import * gpsp="" try: gpsp = GpsPoller(doc.gpsdevice,test) gpsp.start() except: print "wim: gps device not found" from btscan import * scan=scanner("127.0.0.1",5401,False) for t in doc.bt.trigger: scan.d.dm.triggers[t.id]=(t.command,t.param) scan.start() import socket GUI_IP="0.0.0.0" GUI_PORT=5400 insock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM ) insock.bind( (GUI_IP,GUI_PORT) ) insock.settimeout(0.01) #non blocking, this sets the frame rate of checking PD_IP="127.0.0.1" PD_PORT=5401 outsock = socket.socket( socket.AF_INET,socket.SOCK_DGRAM ) pos=latLng() posChanged=False gpsfix=False while True: data="" try: data, addr = insock.recvfrom(128) if debug==True: print "wim: received:",data pos.parse(data) posChanged=True except: nothing=None if gpsp!="": #gps available if gpsp.fix>1: gpsfix=True outsock.sendto( "gps status 1\n", (PD_IP, PD_PORT) ) if gpsp.fix<2: gpsfix=False outsock.sendto( "gps status 0\n", (PD_IP, PD_PORT) ) check=gpsp.check() if check!=False: if debug==True: print "wim: received from gps",check[0],check[1] outsock.sendto("gps data "+str(check[0])+" "+str(check[1])+"\n",(PD_IP, PD_PORT) ) pos=latLng(check[0],check[1]) posChanged=True if posChanged: posChanged=False for layer in gpslayers: r=layer.checkcoord(pos) #returns a message or None if r!=None: if debug==True: print "wim: sending:",str(r[0]),str(r[1]) #pd needs \n at end of message outsock.sendto( str(r[0])+' '+str(r[1])+'\n', (PD_IP, PD_PORT) ) time.sleep(0.01)