From 1bb5492d81385f91c856ad20b1313a795c5ab60a Mon Sep 17 00:00:00 2001 From: Andrey Egorov Date: Tue, 23 Jul 2024 18:30:04 +0300 Subject: [PATCH] Fixed update. --- api/modem/modem.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/api/modem/modem.go b/api/modem/modem.go index 0be65ef..a4a6ddc 100644 --- a/api/modem/modem.go +++ b/api/modem/modem.go @@ -249,9 +249,10 @@ func (m *modem) CalculateSpeed(newLatitude, newLongitude float64) { m.gpsInfo.Speed = earthRad * c / (math.Abs(float64(time.Since(m.lastUpdateTime)))) } -func decodeGpsInfo(strs [9]string) (GpsInfo, error) { +func decodeGpsInfo(str string) (GpsInfo, error) { var err error newGpsInfo := GpsInfo{} + strs := strings.Split(strings.Split(strings.Replace(str, " ", "", -1), "\n")[0], ",") newGpsInfo.Latitude, err = strconv.ParseFloat(strs[0], 64) if err != nil { @@ -273,9 +274,12 @@ func decodeGpsInfo(strs [9]string) (GpsInfo, error) { if err != nil { return GpsInfoNil, fmt.Errorf("parse speed: %w", err) } - newGpsInfo.Course, err = strconv.ParseFloat(strs[8], 64) - if err != nil { - return GpsInfoNil, fmt.Errorf("parse course: %w", err) + // Course sometimes may be null + if len(strs[8]) > 0 { + newGpsInfo.Course, err = strconv.ParseFloat(strs[8], 64) + if err != nil { + return GpsInfoNil, fmt.Errorf("parse course: %w", err) + } } return newGpsInfo, nil } @@ -311,9 +315,8 @@ func (m *modem) Update() error { return fmt.Errorf("error response") } m.logger.Println("Decoding data...") - coordinates := strings.Split(strings.Split(resp.RmFront("+CGPSINFO:").String(), "\n")[0], ",") - newGpsInfo, err := decodeGpsInfo([9]string(coordinates)) + newGpsInfo, err := decodeGpsInfo(strings.Split(strings.Replace(resp.RmFront("+CGPSINFO:").String(), "\r", "", -1), "\n")[0]) if err != nil { m.logger.Println("Gps info decode error:", err.Error()) return nil