Skip to content

Commit

Permalink
parse gps
Browse files Browse the repository at this point in the history
  • Loading branch information
joshspicer committed Mar 26, 2023
1 parent 5026286 commit 148c8c6
Showing 1 changed file with 58 additions and 16 deletions.
74 changes: 58 additions & 16 deletions server/nodeRouter.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import (
"fmt"
"io"
"log"
"net/http"
"strconv"
"strings"
"time"

Expand All @@ -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 {
Expand All @@ -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)
}

0 comments on commit 148c8c6

Please sign in to comment.