diff --git a/vehicle.go b/vehicle.go index bdc29ac..be996ba 100644 --- a/vehicle.go +++ b/vehicle.go @@ -514,34 +514,36 @@ func (v *Vehicle) GetClimateUserPresets() { } v.client.logger.Debug("CLIMATE USER SETTINGS OUTPUT", "body", respParsed) - // ONLY FOR THAT REQUEST BECAUSE OF API SENDS BACK ESCAPED DATA IN DATA FIELD - for _, child := range respParsed.S("data").Children() { - // logger.Debugf("key: %v, value: %v\n", key, child.Data().(string)) - var climateProfile ClimateProfile - json.Unmarshal([]byte(child.Data().(string)), &climateProfile) - - // if v.isEV() && climateProfile.VehicleType == "phev" { - // v.ClimateProfiles = append(v.ClimateProfiles, climateProfile) - // } - // if !v.isEV() && climateProfile.VehicleType == "gas" { - // v.ClimateProfiles = append(v.ClimateProfiles, climateProfile) - // } + // ONLY FOR THAT REQUEST BECAUSE OF API SENDS BACK ESCAPING DATA IN DATA FIELD + data, ok := respParsed.Path("data").Data().(string) + if !ok { + v.client.logger.Error("error while parsing data json", "request", "GetClimateUserPresets", "error", err.Error()) } - v.Updated = time.Now() - // // ONLY FOR THAT REQUEST BECAUSE OF API SENDS BACK ESCAPING DATA IN DATA FIELD - // data, ok := respParsed.Path("data").Data().(string) - // // rawIn := json.RawMessage(in) - // // bytes, err := rawIn.MarshalJSON() - // // if err != nil { - // // panic(err) - // // } - // // value == string, ok == false - // if !ok { - // // TODO: Work with errorCode - // panic(data) + cProfiles := []ClimateProfile{} + err = json.Unmarshal([]byte(data), &cProfiles) + v.client.logger.Debug("climate quick presets", "data", data) + + if err != nil { + v.client.logger.Error("error while parsing climate user presets json", "request", "GetClimateUserPresets", "error", err.Error()) + } + + if len(cProfiles) > 0 { + for _, cp := range cProfiles { + if _, ok := v.ClimateProfiles[cp.PresetType+cp.Name]; ok { + v.ClimateProfiles[cp.PresetType+cp.Name] = cp + } else { + v.ClimateProfiles[cp.PresetType+cp.Name] = cp + } + } + } + // if v.isEV() && climateProfile.VehicleType == "phev" { + // v.ClimateProfiles = append(v.ClimateProfiles, climateProfile) // } - // logger.Debugf("PRESETS: %+v\n", data) + // if !v.isEV() && climateProfile.VehicleType == "gas" { + // v.ClimateProfiles = append(v.ClimateProfiles, climateProfile) + // } + v.Updated = time.Now() } else { v.client.logger.Error("active STARLINK Security Plus subscription required") }