Simplify extracting vehicle from list (#4692)
This commit is contained in:
parent
103107a3f2
commit
21169b84b6
7 changed files with 41 additions and 43 deletions
|
|
@ -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
|
||||
},
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -96,10 +96,10 @@ 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
|
||||
},
|
||||
)
|
||||
|
||||
|
|
@ -107,7 +107,7 @@ func newPSA(log *util.Logger, brand, realm, id, secret string, other map[string]
|
|||
return nil, err
|
||||
}
|
||||
|
||||
v.Provider = psa.NewProvider(api, vid, cc.Cache)
|
||||
v.Provider = psa.NewProvider(api, vehicle.ID, cc.Cache)
|
||||
|
||||
return v, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
},
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
},
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
},
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue