Fixed GPS info parsing.

This commit is contained in:
Andrey Egorov 2024-08-08 15:52:52 +03:00
parent be99ed7fbf
commit cb07b6ac62

View File

@ -2,6 +2,8 @@ package gps
import (
"fmt"
"io"
"log"
"math"
"strconv"
"strings"
@ -44,14 +46,18 @@ func (gps *Data) decode(str string) error {
var err error
newGpsInfo := Data{}
strs := strings.Split(strings.Split(strings.Replace(str, " ", "", -1), "\n")[0], ",")
logger := log.New(io.Discard, "modem-gps", log.LstdFlags)
if len(strs) < 7 {
return fmt.Errorf("ERROR: too small msg: %s", strs)
}
newGpsInfo.Latitude, err = strconv.ParseFloat(strs[0], 64)
if err != nil {
return fmt.Errorf("parse latitude: %w", err)
logger.Println("ERROR parse latitude:", err.Error())
}
newGpsInfo.Longitude, err = strconv.ParseFloat(strs[2], 64)
if err != nil {
return fmt.Errorf("parse longitude: %w", err)
logger.Println("ERROR parse longitude:", err.Error())
}
newGpsInfo.Latitude /= 100
newGpsInfo.Longitude /= 100
@ -61,17 +67,17 @@ func (gps *Data) decode(str string) error {
newGpsInfo.Time = strs[5]
newGpsInfo.Altitude, err = strconv.ParseFloat(strs[6], 64)
if err != nil {
return fmt.Errorf("parse altitude: %w", err)
logger.Println("ERROR parse altitude:", err.Error())
}
newGpsInfo.Speed, err = strconv.ParseFloat(strs[7], 64)
if err != nil {
return fmt.Errorf("parse speed: %w", err)
logger.Println("ERROR parse speed:", err.Error())
}
// Course sometimes may be null
if len(strs[8]) > 0 {
newGpsInfo.Course, err = strconv.ParseFloat(strs[8], 64)
if err != nil {
return fmt.Errorf("parse course: %w", err)
logger.Println("ERROR parse course:", err.Error())
}
}
*gps = newGpsInfo