From 148c8c63cc36eaf7e983698d7bdc93307191001c Mon Sep 17 00:00:00 2001 From: Josh Spicer Date: Sun, 26 Mar 2023 18:56:39 +0000 Subject: [PATCH] parse gps --- server/nodeRouter.go | 74 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 16 deletions(-) diff --git a/server/nodeRouter.go b/server/nodeRouter.go index bd1024a..8f667ff 100644 --- a/server/nodeRouter.go +++ b/server/nodeRouter.go @@ -1,10 +1,10 @@ package main import ( - "fmt" "io" "log" "net/http" + "strconv" "strings" "time" @@ -28,8 +28,6 @@ func NodeRouter() *gin.Engine { func getNodeInfo(c *gin.Context) { - // ioWR := c.MustGet("ioWR").(io.ReadWriter) - // Make new IO ReadWriter m, err := serial.New(serial.WithPort("/dev/ttyUSB2"), serial.WithBaud(115200)) if err != nil { @@ -39,24 +37,68 @@ func getNodeInfo(c *gin.Context) { defer m.Close() var mio io.ReadWriter = m - modem := at.New(mio, at.WithTimeout(2*time.Second)) + modem := at.New(mio, at.WithTimeout(5*time.Second)) - info, err := modem.Command("I") - if err != nil { - c.String(http.StatusInternalServerError, fmt.Sprintf("Error getting modem info, %s", err.Error())) - return + var info string = "ERR" + var signalStrength string = "ERR" + var gpsLatitude float32 = 12.517572 + var gpsLongitude float32 = -69.9649462 + + infoArr, err := modem.Command("I") + if err == nil { + info = infoArr[0] } - signalStrength, err := modem.Command("+CSQ") - if err != nil { - c.String(http.StatusInternalServerError, fmt.Sprintf("Error getting signal strength, %s", err.Error())) - return + + // +CSQ: 23,99 + signalStrengthArr, err := modem.Command("+CSQ") + if err == nil { + signalStrength = strings.Join(signalStrengthArr, " ") + } + + // +QGPSLOC: 174111.0,4736.8397N,12218.8860W,1.5,98.7,3,224.01,0.0,0.0,260323,05 + gpsLocationArr, err := modem.Command("+QGPSLOC?") + if err == nil { + // Convert AT latitude and longitude response + var tmpLatitude = convertGpsLocation(gpsLocationArr[1]) + var tmpLongitude = convertGpsLocation(gpsLocationArr[2]) + + if tmpLatitude != -1 && tmpLongitude != -1 { + gpsLatitude = tmpLatitude + gpsLongitude = tmpLongitude + } } c.JSON(http.StatusOK, gin.H{ "modemInfo": info[0], - "signal": strings.Join(signalStrength, " "), - "accessoriesBattery": -1, // Mock - "gpsLatitude": 12.517572, // Mock - "gpsLongitude": -69.9649462, // Mock + "signal": signalStrength, + "accessoriesBattery": -1, // Mock + "gpsLatitude": gpsLatitude, + "gpsLongitude": gpsLongitude, }) } + +func convertGpsLocation(gpsLocation string) float32 { + // 4736.8397N + // 12218.8860W + + // Remove and store the last character + compassDirection := gpsLocation[len(gpsLocation)-1:] + gpsLocation = gpsLocation[:len(gpsLocation)-1] + + // Move the decimal point two places to the left + // 4736.8397 -> 47.368397 + // 12218.8860 -> 122.18886 + gpsLocation = gpsLocation[:len(gpsLocation)-2] + "." + gpsLocation[len(gpsLocation)-2:] + // If compass direction is South or West, make the number negative + if compassDirection == "S" || compassDirection == "W" { + gpsLocation = "-" + gpsLocation + } + + // Convert to float + gpsLocationFloat, err := strconv.ParseFloat(gpsLocation, 32) + if err != nil { + log.Println(err) + return -1 + } + return float32(gpsLocationFloat) +}