Fixed update.

This commit is contained in:
Andrey Egorov 2024-07-23 18:30:04 +03:00
parent b0a5fa81d5
commit 1bb5492d81

View File

@ -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