From 913e129927bb5eab570fcdd59ae9f92e9319ec10 Mon Sep 17 00:00:00 2001 From: Alex Savin Date: Thu, 29 May 2025 00:20:11 -0400 Subject: [PATCH] Added parsing way of tires --- mysubaru.go | 2 +- vehicle.go | 100 ++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 79 insertions(+), 23 deletions(-) diff --git a/mysubaru.go b/mysubaru.go index 414a4e9..3f83052 100644 --- a/mysubaru.go +++ b/mysubaru.go @@ -181,7 +181,7 @@ type VehicleStatus struct { WindowFrontRightStatus string `json:"windowFrontRightStatus"` // + "CLOSE" WindowRearLeftStatus string `json:"windowRearLeftStatus"` // + "CLOSE" WindowRearRightStatus string `json:"windowRearRightStatus"` // + "CLOSE" - WindowSunroofStatus string `json:"windowSunroofStatus"` // + "UNKNOWN" + WindowSunroofStatus string `json:"windowSunroofStatus"` // + "CLOSE" DoorBootPosition string `json:"doorBootPosition"` // CLOSED DoorEngineHoodPosition string `json:"doorEngineHoodPosition"` // CLOSED DoorFrontLeftPosition string `json:"doorFrontLeftPosition"` // CLOSED diff --git a/vehicle.go b/vehicle.go index 1faa5b7..a056cbd 100644 --- a/vehicle.go +++ b/vehicle.go @@ -175,6 +175,12 @@ func (v *Vehicle) String() string { // fmt.Printf("%d >> %+v\n", i+1, d) } + vString += "=== TIRES =====================\n" + for k, v := range v.Tires { + vString += fmt.Sprintf("%s >> %+v\n", k, v) + // fmt.Printf("%d >> %+v\n", i+1, d) + } + vString += "=== FEATURES =====================\n" for i, f := range v.Features { if !strings.HasSuffix(f, "_MIL") { @@ -618,7 +624,9 @@ func (v *Vehicle) GetVehicleStatus() { } } if strings.HasPrefix(key, "window") && strings.HasSuffix(key, "Status") { - submatchall := re.FindAllString(key, -1) + pos := strings.TrimPrefix(key, "door") + pos = strings.TrimSuffix(pos, "Status") + submatchall := re.FindAllString(pos, -1) v.client.logger.Debug("VEHICLE COND", "key", key, "data", child.Data(), "number", len(submatchall)) if window, ok := v.Windows[submatchall[0]+submatchall[1]]; ok { @@ -628,19 +636,42 @@ func (v *Vehicle) GetVehicleStatus() { window.Status = child.Data().(string) window.Updated = time.Now() v.Windows[submatchall[0]+submatchall[1]] = Window{ - Position: submatchall[0], - SubPosition: submatchall[1], - Status: child.Data().(string), - Updated: time.Now(), + Position: submatchall[0], + Status: child.Data().(string), + Updated: time.Now(), + } + if len(submatchall) >= 2 { + if w, ok := v.Windows[pos]; ok { + w.SubPosition = submatchall[1] + v.Windows[pos] = w + } } } } - if strings.HasPrefix(key, "tire") && !strings.HasSuffix(key, "Psi") { - v.client.logger.Debug("VEHICLE COND", "key", key, "data", child.Data()) - // submatchall := re.FindAllString(key, -1) - // for _, element := range submatchall { - // fmt.Println(element) - // } + if strings.HasPrefix(key, "tirePressure") && !strings.HasSuffix(key, "Psi") { + pos := strings.TrimPrefix(key, "tirePressure") + pos = strings.TrimSuffix(pos, "Psi") + submatchall := re.FindAllString(pos, -1) + v.client.logger.Debug("VEHICLE COND", "key", key, "data", child.Data(), "number", len(submatchall)) + + if tire, ok := v.Tires[submatchall[0]+submatchall[1]]; ok { + tire.PressurePsi = child.Data().(int) + tire.Updated = time.Now() + } else { + tire.PressurePsi = child.Data().(int) + tire.Updated = time.Now() + v.Tires[submatchall[0]+submatchall[1]] = Tire{ + Position: submatchall[0], + PressurePsi: child.Data().(int), + Updated: time.Now(), + } + if len(submatchall) >= 2 { + if t, ok := v.Tires[pos]; ok { + t.SubPosition = submatchall[1] + v.Tires[pos] = t + } + } + } } } @@ -765,7 +796,9 @@ func (v *Vehicle) GetVehicleCondition() { } } if strings.HasPrefix(key, "window") && strings.HasSuffix(key, "Status") { - submatchall := re.FindAllString(key, -1) + pos := strings.TrimPrefix(key, "door") + pos = strings.TrimSuffix(pos, "Status") + submatchall := re.FindAllString(pos, -1) v.client.logger.Debug("VEHICLE COND", "key", key, "data", child.Data(), "number", len(submatchall)) if window, ok := v.Windows[submatchall[0]+submatchall[1]]; ok { @@ -775,19 +808,42 @@ func (v *Vehicle) GetVehicleCondition() { window.Status = child.Data().(string) window.Updated = time.Now() v.Windows[submatchall[0]+submatchall[1]] = Window{ - Position: submatchall[0], - SubPosition: submatchall[1], - Status: child.Data().(string), - Updated: time.Now(), + Position: submatchall[0], + Status: child.Data().(string), + Updated: time.Now(), + } + if len(submatchall) >= 2 { + if w, ok := v.Windows[pos]; ok { + w.SubPosition = submatchall[1] + v.Windows[pos] = w + } } } } - if strings.HasPrefix(key, "tire") && !strings.HasSuffix(key, "Unit") { - v.client.logger.Debug("VEHICLE COND", "key", key, "data", child.Data()) - // submatchall := re.FindAllString(key, -1) - // for _, element := range submatchall { - // fmt.Println(element) - // } + if strings.HasPrefix(key, "tirePressure") && !strings.HasSuffix(key, "Psi") { + pos := strings.TrimPrefix(key, "tirePressure") + pos = strings.TrimSuffix(pos, "Psi") + submatchall := re.FindAllString(pos, -1) + v.client.logger.Debug("VEHICLE COND", "key", key, "data", child.Data(), "number", len(submatchall)) + + if tire, ok := v.Tires[submatchall[0]+submatchall[1]]; ok { + tire.PressurePsi = child.Data().(int) + tire.Updated = time.Now() + } else { + tire.PressurePsi = child.Data().(int) + tire.Updated = time.Now() + v.Tires[submatchall[0]+submatchall[1]] = Tire{ + Position: submatchall[0], + PressurePsi: child.Data().(int), + Updated: time.Now(), + } + if len(submatchall) >= 2 { + if t, ok := v.Tires[pos]; ok { + t.SubPosition = submatchall[1] + v.Tires[pos] = t + } + } + } } v.Updated = time.Now() }