#!/usr/bin/python

from gps import *

#gpsSess = gps(host="127.0.0.1", mode=WATCH_ENABLE|WATCH_NEWSTYLE)
gpsSess = gps(host="172.25.36.43", mode=WATCH_ENABLE|WATCH_NEWSTYLE)

outLatDeg = 0
outLonDeg = 0
outAlt = 0
outLatMin = 0.0
outLonMin = 0.0
outLatNS = "?"
outLonEW = "?"
outLatErr = 0.0
outLonErr = 0.0
outAltErr = 0.0
outNumSatsUsed = 0
outNumSats = 0
outTrack = 0.0
outSpeed = 0.0
outClimb = 0.0
outTime = ""
outMode = 0
modes = [ "??????", "No Fix", "2D Fix", "3D Fix" ]


while True:
	rpt = gpsSess.next()
	print("\n================== Message Class: %s =========================== " % (rpt["class"]), end="\n")
	#print("%s\n" % (rpt))
	if rpt["class"] == "TPV":
		STR = "TPV: "
		if "lat" in rpt.keys(): STR += "Lat: %.6f " % rpt["lat"]
		if "epx" in rpt.keys(): STR += "LatErr: %.2f " % rpt["epx"]
		if "lon" in rpt.keys(): STR += "Lon: %.6f " % rpt["lon"]
		if "epy" in rpt.keys(): STR += "LatErr: %.2f " % rpt["epy"]
		if "alt" in rpt.keys(): STR += "Alt: %.6f " % rpt["alt"]
		if "epv" in rpt.keys(): STR += "AltErr: %.2f " % rpt["epv"]
		print(STR)
		
		STR = "     "
		if "device" in rpt.keys(): STR += "Dev: %s " % rpt["device"]
		if "status" in rpt.keys(): STR += "Status: %d " % rpt["status"]
		STR += "Mode: %d " % rpt["mode"]
		if "time" in rpt.keys(): STR += "Time: %s " % rpt["time"]
		if "ept" in rpt.keys(): STR += "TimeErr: %.2f " % rpt["ept"]
		STR = "     "
		if "track" in rpt.keys(): STR += "Trk: %.2f " % rpt["track"]
		if "epd" in rpt.keys(): STR += "TrkErr: %.2f " % rpt["epd"]
		if "speed" in rpt.keys(): STR += "Spd: %.2f " % rpt["speed"]
		if "eps" in rpt.keys(): STR += "SpdErr: %.2f " % rpt["eps"]
		if "climb" in rpt.keys(): STR += "Climb: %.2f " % rpt["climb"]
		if "epc" in rpt.keys(): STR += "ClimbErr: %.2f " % rpt["epc"]
		print(STR)

		if "alt" in rpt.keys(): outAlt = rpt["alt"]
		outMode = rpt["mode"]
		if "time" in rpt.keys(): outTime = rpt["time"]
		if "track" in rpt.keys(): outTrack = rpt["track"]
		if "speed" in rpt.keys(): outSpeed = rpt["speed"]
		if "climb" in rpt.keys(): outClimb = rpt["climb"]
		if "lat" in rpt.keys():
			outLatDeg = abs(int(rpt["lat"]))
			outLatNS = "S"
			if rpt["lat"] > 0: outLatNS = "N"
			outLatMin = (abs(rpt["lat"]) - outLatDeg) * 60
		if "lon" in rpt.keys():
			outLonDeg = abs(int(rpt["lon"]))
			outLonEW = "W"
			if rpt["lon"] > 0: outLonEW = "E"
			outLonMin = (abs(rpt["lon"]) - outLonDeg) * 60

	elif rpt["class"] == "SKY":
		STR = "SKY: "
		if "time" in rpt.keys(): STR += "Time: %.2f " % rpt["time"]
		if "xdop" in rpt.keys(): STR += "LonDil: %.2f " % rpt["xdop"]
		if "ydop" in rpt.keys(): STR += "LatDil: %.2f " % rpt["ydop"]
		if "vdop" in rpt.keys(): STR += "AltDil: %.2f " % rpt["vdop"]
		if "tdop" in rpt.keys(): STR += "TimeDil: %.2f " % rpt["tdop"]
		if "pdop" in rpt.keys(): STR += "SphereDil: %.2f " % rpt["pdop"]
		if "gdop" in rpt.keys(): STR += "HypSphereDil: %.2f " % rpt["gdop"]
		print(STR)
		
		STR = "     "
		STR += "Sats Known: %d " % len(rpt["satellites"])
		sats = 0
		for sat in rpt["satellites"]:
			if sat["used"] == True:
				sats += 1
		STR += "Sats Used: %d " % sats
		#print(STR)
		outNumSatsUsed = sats
		outNumSats= len(rpt["satellites"])

		for sat in rpt["satellites"]:
			system = "GNSS"
			if sat["PRN"] >= 128: system = "SBAS"
			if sat["PRN"] >= 64: system = "GLONASS"
			print("\tPRN: %7s %3d  az: %3d  el: %2d  ss: %3d  used: %s" % (system, sat["PRN"], sat["az"], sat["el"], sat["ss"], sat["used"]))

	elif rpt["class"] == "GST":
		STR = "GST: "
		if "time" in rpt.keys(): STR += "Time: %s " % rpt["time"]
		if "rms" in rpt.keys(): STR += "RMS: %.2f " % rpt["rms"]
		if "major" in rpt.keys(): STR += "Major: %.2f " % rpt["major"]
		if "minor" in rpt.keys(): STR += "Minor: %.2f " % rpt["minor"]
		if "orient" in rpt.keys(): STR += "Orient: %.2f " % rpt["orient"]
		if "lat" in rpt.keys(): STR += "LatErr: %.2f " % rpt["lat"]
		if "lon" in rpt.keys(): STR += "LonErr: %.2f " % rpt["lon"]
		if "alt" in rpt.keys(): STR += "AltErr: %.2f " % rpt["alt"]
		print(STR)
		if "lat" in rpt.keys(): outLatErr = rpt["lat"]
		if "lon" in rpt.keys(): outLonErr = rpt["lon"]
		if "alt" in rpt.keys(): outAltErr = rpt["alt"]

	elif rpt["class"] == "ATT":
		STR = "ATT: "
		print(STR)
	
	elif rpt["class"] == "DEVICES":
		STR = "DEVICES: "
		for dev in rpt["devices"]:
			for attr in dev.keys():
				STR += "%s: %s  " % (attr, dev[attr])
			# STR += "\n"
		if "remote" in rpt.keys(): STR += "Remote: %s " % rpt["remote"]
		print(STR)
	
	elif rpt["class"] == "VERSION":
		STR = "VERSION: "
		STR += "Release: %s " % rpt["release"]
		STR += "Rev: %s " % rpt["rev"]
		STR += "ProtoMajor: %s " % rpt["proto_major"]
		STR += "ProtoMinor: %s " % rpt["proto_minor"]
		if "remote" in rpt.keys(): STR += "Remote: %s " % rpt["remote"]
		print(STR)
	
	elif rpt["class"] == "WATCH":
		STR = "WATCH: "
		if "enable" in rpt.keys(): STR += "Enable: %s " % rpt["enable"]
		if "json" in rpt.keys(): STR += "JSON: %s " % rpt["json"]
		if "nmea" in rpt.keys(): STR += "NMEA: %s " % rpt["nmea"]
		if "raw" in rpt.keys(): STR += "Raw: %s " % rpt["raw"]
		if "scaled" in rpt.keys(): STR += "Scaled: %s " % rpt["scaled"]
		if "split24" in rpt.keys(): STR += "Split24: %s " % rpt["split24"]
		if "pps" in rpt.keys(): STR += "PPS: %s " % rpt["pps"]
		if "device" in rpt.keys(): STR += "Device: %s " % rpt["device"]
		if "remote" in rpt.keys(): STR += "Remote: %s " % rpt["remote"]
		print(STR)
	
	else:
		print(rpt["class"])


	#print("Time: %24s  Mode: %s  Sats: %2d/%2d  Lat: %02d %09.6f%s (Acc: %5.2fm)  Lon: %03d %09.6f%s (Acc: %5.2fm)  Alt: %5.0f\' (Acc: %4.0f\')" %(outTime, modes[outMode], outNumSatsUsed, outNumSats, outLatDeg, outLatMin, outLatNS, outLatErr, outLonDeg, outLonMin, outLonEW, outLonErr, outAlt * 3.28084, outAltErr * 3.28084))
	#print("      %24s  Track: %3d    Speed: %3d   Climb: %4d" % ("", int(outTrack), int(outSpeed), int(outClimb)))
