Skip to content

Commit

Permalink
Simplify extracting vehicle from list (evcc-io#4692)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Oct 2, 2022
1 parent 103107a commit 21169b8
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 43 deletions.
6 changes: 3 additions & 3 deletions vehicle/bluelink.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ func newBluelinkFromConfig(brand string, other map[string]interface{}, settings

api := bluelink.NewAPI(log, settings.URI, identity)

_, vehicle, err := ensureVehicleWithFeature(
vehicle, err := ensureVehicleEx(
cc.VIN, api.Vehicles,
func(v bluelink.Vehicle) (string, bluelink.Vehicle) {
return v.VIN, v
func(v bluelink.Vehicle) string {
return v.VIN
},
)

Expand Down
27 changes: 12 additions & 15 deletions vehicle/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,28 @@ import (
"strings"
)

// ensureVehicleWithFeature extracts VIN from list of VINs returned from `list` function
// ensureVehicle extracts VIN from list of VINs returned from `list` function
func ensureVehicle(vin string, list func() ([]string, error)) (string, error) {
vin, _, err := ensureVehicleWithFeature(vin, list, func(v string) (string, string) {
return v, ""
return ensureVehicleEx(vin, list, func(v string) string {
return v
})

return vin, err
}

// ensureVehicleWithFeature extracts VIN and feature from list of vehicles of type V returned from `list` function
func ensureVehicleWithFeature[Vehicle, Feature any](
// ensureVehicleEx extracts vehicle with matching VIN from list of vehicles
func ensureVehicleEx[Vehicle any](
vin string,
list func() ([]Vehicle, error),
extract func(Vehicle) (string, Feature),
) (string, Feature, error) {
extract func(Vehicle) string,
) (Vehicle, error) {
vehicles, err := list()
if err != nil {
return "", *new(Feature), fmt.Errorf("cannot get vehicles: %w", err)
return *new(Vehicle), fmt.Errorf("cannot get vehicles: %w", err)
}

if vin = strings.ToUpper(vin); vin != "" {
for _, vehicle := range vehicles {
if v, res := extract(vehicle); v == vin {
return v, res, nil
if vin == extract(vehicle) {
return vehicle, nil
}
}

Expand All @@ -37,12 +35,11 @@ func ensureVehicleWithFeature[Vehicle, Feature any](
} else {
// vin empty
if len(vehicles) == 1 {
vin, res := extract(vehicles[0])
return vin, res, nil
return vehicles[0], nil
}

err = fmt.Errorf("cannot find vehicle: %v", vehicles)
}

return "", *new(Feature), err
return *new(Vehicle), err
}
8 changes: 4 additions & 4 deletions vehicle/psa.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,18 @@ func newPSA(log *util.Logger, brand, realm, id, secret string, other map[string]

api := psa.NewAPI(log, identity, realm, cc.Credentials.ID)

_, vid, err := ensureVehicleWithFeature(
vehicle, err := ensureVehicleEx(
cc.VIN, api.Vehicles,
func(v psa.Vehicle) (string, string) {
return v.VIN, v.ID
func(v psa.Vehicle) string {
return v.VIN
},
)

if err != nil {
return nil, err
}

v.Provider = psa.NewProvider(api, vid, cc.Cache)
v.Provider = psa.NewProvider(api, vehicle.ID, cc.Cache)

return v, err
}
25 changes: 13 additions & 12 deletions vehicle/renault.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,24 @@ func NewRenaultDaciaFromConfig(brand string, other map[string]interface{}) (api.

accountID, err := api.Person(identity.PersonID, brand)

var car kamereon.Vehicle
if err == nil {
cc.VIN, car, err = ensureVehicleWithFeature(cc.VIN,
func() ([]kamereon.Vehicle, error) {
return api.Vehicles(accountID)
},
func(v kamereon.Vehicle) (string, kamereon.Vehicle) {
return v.VIN, v
},
)
if err != nil {
return nil, err
}

vehicle, err := ensureVehicleEx(cc.VIN,
func() ([]kamereon.Vehicle, error) {
return api.Vehicles(accountID)
},
func(v kamereon.Vehicle) string {
return v.VIN
},
)

if err == nil {
err = car.Available()
err = vehicle.Available()
}

v.Provider = renault.NewProvider(api, accountID, cc.VIN, cc.Cache)
v.Provider = renault.NewProvider(api, accountID, vehicle.VIN, cc.Cache)

return v, err
}
6 changes: 3 additions & 3 deletions vehicle/skoda-enyaq.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func NewEnyaqFromConfig(other map[string]interface{}) (api.Vehicle, error) {

api := skoda.NewAPI(log, ts)

_, vehicle, err := ensureVehicleWithFeature(
vehicle, err := ensureVehicleEx(
cc.VIN, api.Vehicles,
func(v skoda.Vehicle) (string, skoda.Vehicle) {
return v.VIN, v
func(v skoda.Vehicle) string {
return v.VIN
},
)

Expand Down
6 changes: 3 additions & 3 deletions vehicle/tesla.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ func NewTeslaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
return nil, err
}

cc.VIN, v.vehicle, err = ensureVehicleWithFeature(
v.vehicle, err = ensureVehicleEx(
cc.VIN, client.Vehicles,
func(v *tesla.Vehicle) (string, *tesla.Vehicle) {
return v.Vin, v
func(v *tesla.Vehicle) string {
return v.Vin
},
)

Expand Down
6 changes: 3 additions & 3 deletions vehicle/tronity.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ func NewTronityFromConfig(other map[string]interface{}) (api.Vehicle, error) {
Base: v.Client.Transport,
}

_, vehicle, err := ensureVehicleWithFeature(
vehicle, err := ensureVehicleEx(
cc.VIN, v.vehicles,
func(v tronity.Vehicle) (string, tronity.Vehicle) {
return v.VIN, v
func(v tronity.Vehicle) string {
return v.VIN
},
)

Expand Down

0 comments on commit 21169b8

Please sign in to comment.