2 Commits

Author SHA1 Message Date
6c110b9a8b Add: check in RmFront 2024-08-08 15:56:23 +03:00
cb07b6ac62 Fixed GPS info parsing. 2024-08-08 15:52:52 +03:00
2 changed files with 14 additions and 5 deletions

View File

@ -9,6 +9,9 @@ func (resp Resp) Check() bool {
} }
func (resp Resp) RmFront(str string) Resp { func (resp Resp) RmFront(str string) Resp {
if !resp.Check() {
return RespNil
}
return Resp(string(resp)[len(str):]) return Resp(string(resp)[len(str):])
} }

View File

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