diff --git a/vehicle.go b/vehicle.go index be996ba..d99e0cf 100644 --- a/vehicle.go +++ b/vehicle.go @@ -464,7 +464,10 @@ func (v *Vehicle) GetClimateQuickPresets() { v.ClimateProfiles[cp.PresetType+cp.Name] = cp } } + } else { + v.client.logger.Debug("didn't find any climate quick presets") } + v.Updated = time.Now() } else { v.client.logger.Error("active STARLINK Security Plus subscription required") } @@ -481,21 +484,34 @@ func (v *Vehicle) GetClimatePresets() { if err != nil { v.client.logger.Error("error while parsing json", "request", "GetClimatePresets", "error", err.Error()) } + v.client.logger.Debug("CLIMATE SUBARU 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) - // } - 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) + if !ok { + v.client.logger.Error("error while parsing data json", "request", "GetClimatePresets", "error", err.Error()) } + + cProfiles := []ClimateProfile{} + err = json.Unmarshal([]byte(data), &cProfiles) + v.client.logger.Debug("climate subaru presets", "data", data) + + if err != nil { + v.client.logger.Error("error while parsing climate subaru presets json", "request", "GetClimatePresets", "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 + } + } + } else { + v.client.logger.Debug("didn't find any climate subaru presets") + } + v.Updated = time.Now() } else { v.client.logger.Error("active STARLINK Security Plus subscription required") } @@ -522,7 +538,7 @@ func (v *Vehicle) GetClimateUserPresets() { cProfiles := []ClimateProfile{} err = json.Unmarshal([]byte(data), &cProfiles) - v.client.logger.Debug("climate quick presets", "data", data) + v.client.logger.Debug("climate user presets", "data", data) if err != nil { v.client.logger.Error("error while parsing climate user presets json", "request", "GetClimateUserPresets", "error", err.Error()) @@ -536,14 +552,17 @@ func (v *Vehicle) GetClimateUserPresets() { v.ClimateProfiles[cp.PresetType+cp.Name] = cp } } + } else { + v.client.logger.Debug("didn't find any climate quick presets") } + v.Updated = time.Now() + // TODO: Separate EV and CONVETIONAL Climate Profiles // if v.isEV() && climateProfile.VehicleType == "phev" { // v.ClimateProfiles = append(v.ClimateProfiles, climateProfile) // } // 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") }