From d66d529fa9e0db7653025d7521e7f7cebca9a8a9 Mon Sep 17 00:00:00 2001 From: Alex Savin Date: Sat, 31 May 2025 20:55:25 -0400 Subject: [PATCH] Some changes --- mysubaru.go | 4 +- vehicle.go | 244 +++++++++++++++++++++++++++++----------------------- 2 files changed, 137 insertions(+), 111 deletions(-) diff --git a/mysubaru.go b/mysubaru.go index 1c6e816..3c1dfd0 100644 --- a/mysubaru.go +++ b/mysubaru.go @@ -3,8 +3,6 @@ package mysubaru import ( "encoding/json" "time" - - "github.com/Jeffail/gabs/v2" ) // Response . @@ -262,7 +260,7 @@ type ServiceRequest struct { RemoteServiceState string `json:"remoteServiceState"` // started | finished | stopping SubState *string `json:"subState,omitempty"` // null ErrorCode *string `json:"errorCode,omitempty"` // null:null - Result *gabs.Container `json:"result,omitempty"` // null + Result json.RawMessage `json:"result,omitempty"` // null UpdateTime *time.Time `json:"updateTime,omitempty"` // timestamp Vin string `json:"vin"` // 4S4BTGND8L3137058 } diff --git a/vehicle.go b/vehicle.go index b2db235..6806fa7 100644 --- a/vehicle.go +++ b/vehicle.go @@ -790,121 +790,143 @@ func (v *Vehicle) GetVehicleCondition() { resp := v.client.execute(reqURL, GET, map[string]string{}, "", false) v.client.logger.Debug("http request output", "request", "GetVehicleCondition", "body", resp) - if v.client.isResponseSuccessfull(resp) { - respParsed, err := gabs.ParseJSON(resp) + var r Response + err := json.Unmarshal(resp, &r) + if err != nil { + v.client.logger.Error("error while parsing json", "request", "GetClimatePresets", "error", err.Error()) + } + + if r.Success { + var sr ServiceRequest + err = json.Unmarshal(r.Data, &sr) if err != nil { - v.client.logger.Error("error while parsing json", "request", "GetVehicleCondition", "error", err.Error()) + v.client.logger.Error("error while parsing json", "request", "GetClimatePresets", "error", err.Error()) + } + v.client.logger.Debug("http request output", "request", "GetVehicleStatus", "body", resp) + + var vc VehicleCondition + err = json.Unmarshal(sr.Result, &vc) + if err != nil { + v.client.logger.Error("error while parsing json", "request", "GetClimatePresets", "error", err.Error()) + } + v.client.logger.Debug("http request output", "request", "GetVehicleStatus", "body", resp) + + val := reflect.ValueOf(vc) + typeOfS := val.Type() + + for i := 0; i < val.NumField(); i++ { + fmt.Printf("Field: %s, Value: %v, Type: %v\n", typeOfS.Field(i).Name, val.Field(i).Interface(), val.Field(i).Type()) } - re := regexp.MustCompile(`[A-Z][^A-Z]*`) + // re := regexp.MustCompile(`[A-Z][^A-Z]*`) - for key, child := range respParsed.S("data").S("result").ChildrenMap() { - fmt.Printf("key: %v, value: %v\n", key, child.Data()) - if child.Data() == "NOT_EQUIPPED" || child.Data() == "UNKNOWN" || child.Data() == "16383" || child.Data() == "65535" || child.Data() == "None" || child.Data() == "-64.0" || child.Data() == nil { - continue - } - if strings.HasPrefix(key, "door") && strings.HasSuffix(key, "Position") { - pos := strings.TrimPrefix(key, "door") - pos = strings.TrimSuffix(pos, "Position") - submatchall := re.FindAllString(pos, -1) - v.client.logger.Debug("VEHICLE COND", "key", key, "data", child.Data(), "number", len(submatchall)) + // for key, child := range respParsed.S("data").S("result").ChildrenMap() { + // fmt.Printf("key: %v, value: %v\n", key, child.Data()) + // if child.Data() == "NOT_EQUIPPED" || child.Data() == "UNKNOWN" || child.Data() == "16383" || child.Data() == "65535" || child.Data() == "None" || child.Data() == "-64.0" || child.Data() == nil { + // continue + // } + // if strings.HasPrefix(key, "door") && strings.HasSuffix(key, "Position") { + // pos := strings.TrimPrefix(key, "door") + // pos = strings.TrimSuffix(pos, "Position") + // submatchall := re.FindAllString(pos, -1) + // v.client.logger.Debug("VEHICLE COND", "key", key, "data", child.Data(), "number", len(submatchall)) - if door, ok := v.Doors[pos]; ok { - door.Status = child.Data().(string) - door.Updated = time.Now() - } else { - door.Status = child.Data().(string) - door.Updated = time.Now() - v.Doors[pos] = Door{ - Position: submatchall[0], - Status: child.Data().(string), - Updated: time.Now(), - } - if len(submatchall) >= 2 { - if d, ok := v.Doors[pos]; ok { - d.SubPosition = submatchall[1] - v.Doors[pos] = d - } - } - } - } - if strings.HasPrefix(key, "door") && strings.HasSuffix(key, "LockStatus") { - pos := strings.TrimPrefix(key, "door") - pos = strings.TrimSuffix(pos, "LockStatus") - submatchall := re.FindAllString(pos, -1) - v.client.logger.Debug("VEHICLE COND", "key", key, "data", child.Data(), "number", len(submatchall)) + // if door, ok := v.Doors[pos]; ok { + // door.Status = child.Data().(string) + // door.Updated = time.Now() + // } else { + // door.Status = child.Data().(string) + // door.Updated = time.Now() + // v.Doors[pos] = Door{ + // Position: submatchall[0], + // Status: child.Data().(string), + // Updated: time.Now(), + // } + // if len(submatchall) >= 2 { + // if d, ok := v.Doors[pos]; ok { + // d.SubPosition = submatchall[1] + // v.Doors[pos] = d + // } + // } + // } + // } + // if strings.HasPrefix(key, "door") && strings.HasSuffix(key, "LockStatus") { + // pos := strings.TrimPrefix(key, "door") + // pos = strings.TrimSuffix(pos, "LockStatus") + // submatchall := re.FindAllString(pos, -1) + // v.client.logger.Debug("VEHICLE COND", "key", key, "data", child.Data(), "number", len(submatchall)) - if door, ok := v.Doors[pos]; ok { - door.Lock = child.Data().(string) - door.Updated = time.Now() - } else { - door.Lock = child.Data().(string) - door.Updated = time.Now() - v.Doors[pos] = Door{ - Position: submatchall[0], - Lock: child.Data().(string), - Updated: time.Now(), - } - if len(submatchall) >= 2 { - if d, ok := v.Doors[pos]; ok { - d.SubPosition = submatchall[1] - v.Doors[pos] = d - } - } - } - } - if strings.HasPrefix(key, "window") && strings.HasSuffix(key, "Status") { - pos := strings.TrimPrefix(key, "window") - 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 door, ok := v.Doors[pos]; ok { + // door.Lock = child.Data().(string) + // door.Updated = time.Now() + // } else { + // door.Lock = child.Data().(string) + // door.Updated = time.Now() + // v.Doors[pos] = Door{ + // Position: submatchall[0], + // Lock: child.Data().(string), + // Updated: time.Now(), + // } + // if len(submatchall) >= 2 { + // if d, ok := v.Doors[pos]; ok { + // d.SubPosition = submatchall[1] + // v.Doors[pos] = d + // } + // } + // } + // } + // if strings.HasPrefix(key, "window") && strings.HasSuffix(key, "Status") { + // pos := strings.TrimPrefix(key, "window") + // 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[pos]; ok { - window.Status = child.Data().(string) - window.Updated = time.Now() - } else { - window.Status = child.Data().(string) - window.Updated = time.Now() - v.Windows[pos] = Window{ - 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, "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 window, ok := v.Windows[pos]; ok { + // window.Status = child.Data().(string) + // window.Updated = time.Now() + // } else { + // window.Status = child.Data().(string) + // window.Updated = time.Now() + // v.Windows[pos] = Window{ + // 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, "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[pos]; ok { - tire.PressurePsi = child.Data().(string) - tire.Updated = time.Now() - } else { - tire.PressurePsi = child.Data().(string) - tire.Updated = time.Now() - v.Tires[pos] = Tire{ - Position: submatchall[0], - PressurePsi: child.Data().(string), - 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() - } + // if tire, ok := v.Tires[pos]; ok { + // tire.PressurePsi = child.Data().(string) + // tire.Updated = time.Now() + // } else { + // tire.PressurePsi = child.Data().(string) + // tire.Updated = time.Now() + // v.Tires[pos] = Tire{ + // Position: submatchall[0], + // PressurePsi: child.Data().(string), + // 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() + // } } // VEHICLE_STATE_TYPE >> IGNITION_OFF @@ -932,7 +954,13 @@ func (v *Vehicle) GetVehicleHealth() { resp := v.client.execute(reqURL, GET, params, "", false) v.client.logger.Debug("http request output", "request", "GetVehicleHealth", "body", resp) - if v.client.isResponseSuccessfull(resp) { + var r Response + err := json.Unmarshal(resp, &r) + if err != nil { + v.client.logger.Error("error while parsing json", "request", "GetVehicleHealth", "error", err.Error()) + } + + if r.Success { _, err := gabs.ParseJSON(resp) if err != nil { v.client.logger.Error("error while parsing json", "request", "GetVehicleHealth", "error", err.Error())