Skip to content

Commit

Permalink
Cupra: fix api (#15081)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Jul 27, 2024
1 parent aa2964d commit 530d7a1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
18 changes: 17 additions & 1 deletion vehicle/seat/cupra/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,23 @@ func (v *API) Vehicles(userID string) ([]Vehicle, error) {
// Status implements the /status response
func (v *API) Status(userID, vin string) (Status, error) {
var res Status
uri := fmt.Sprintf("%s/v2/users/%s/vehicles/%s/mycar", BaseURL, userID, vin)
uri := fmt.Sprintf("%s/v5/users/%s/vehicles/%s/mycar", BaseURL, userID, vin)
err := v.GetJSON(uri, &res)
return res, err
}

// ParkingPosition implements the /parkingposition response
func (v *API) ParkingPosition(vin string) (Position, error) {
var res Position
uri := fmt.Sprintf("%s/v1/vehicles/%s/parkingposition", BaseURL, vin)
err := v.GetJSON(uri, &res)
return res, err
}

// Mileage implements the /mileage response
func (v *API) Mileage(vin string) (Mileage, error) {
var res Mileage
uri := fmt.Sprintf("%s/v1/vehicles/%s/mileage", BaseURL, vin)
err := v.GetJSON(uri, &res)
return res, err
}
Expand Down
28 changes: 22 additions & 6 deletions vehicle/seat/cupra/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (

// Provider is an api.Vehicle implementation for Seat Cupra cars
type Provider struct {
statusG func() (Status, error)
action func(string, string) error
statusG func() (Status, error)
positionG func() (Position, error)
milageG func() (Mileage, error)
action func(string, string) error
}

// NewProvider creates a vehicle api provider
Expand All @@ -20,6 +22,12 @@ func NewProvider(api *API, userID, vin string, cache time.Duration) *Provider {
statusG: provider.Cached(func() (Status, error) {
return api.Status(userID, vin)
}, cache),
positionG: provider.Cached(func() (Position, error) {
return api.ParkingPosition(vin)
}, cache),
milageG: provider.Cached(func() (Mileage, error) {
return api.Mileage(vin)
}, cache),
action: func(action, cmd string) error {
return api.Action(vin, action, cmd)
},
Expand All @@ -32,7 +40,7 @@ var _ api.Battery = (*Provider)(nil)
// Soc implements the api.Vehicle interface
func (v *Provider) Soc() (float64, error) {
res, err := v.statusG()
return res.Engines.Primary.Level, err
return res.Engines.Primary.LevelPct, err
}

var _ api.ChargeState = (*Provider)(nil)
Expand Down Expand Up @@ -81,15 +89,23 @@ var _ api.VehicleRange = (*Provider)(nil)
// Range implements the api.VehicleRange interface
func (v *Provider) Range() (int64, error) {
res, err := v.statusG()
return int64(res.Engines.Primary.Range.Value), err
return int64(res.Engines.Primary.RangeKm), err
}

var _ api.VehicleOdometer = (*Provider)(nil)

// Odometer implements the api.VehicleOdometer interface
func (v *Provider) Odometer() (float64, error) {
res, err := v.statusG()
return res.Measurements.MileageKm, err
res, err := v.milageG()
return res.MileageKm, err
}

var _ api.VehiclePosition = (*Provider)(nil)

// Position implements the api.VehiclePosition interface
func (v *Provider) Position() (float64, float64, error) {
res, err := v.positionG()
return res.Lat, res.Lon, err
}

var _ api.VehicleClimater = (*Provider)(nil)
Expand Down
16 changes: 11 additions & 5 deletions vehicle/seat/cupra/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ type Vehicle struct {
type Engine struct {
Type string
FuelType string
Range struct {
Value float64
Unit string
}
Level float64
RangeKm float64
LevelPct float64
}

type Status struct {
Expand All @@ -28,6 +25,7 @@ type Status struct {
ChargeMode string
Active bool
RemainingTime int64
CurrentPct float64
ProgressBarPct float64
}
Climatisation struct {
Expand All @@ -41,3 +39,11 @@ type Status struct {
MileageKm float64
}
}

type Mileage struct {
MileageKm float64
}

type Position struct {
Lat, Lon float64
}

0 comments on commit 530d7a1

Please sign in to comment.