Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BMW: add wakeup (by locking door) #10903

Merged
merged 3 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions vehicle/bmw.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,15 @@ func NewBMWMiniFromConfig(brand string, other map[string]interface{}) (api.Vehic

api := bmw.NewAPI(log, brand, cc.Region, ts)

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

if err == nil {
v.Provider = bmw.NewProvider(api, cc.VIN, cc.Cache)
v.Provider = bmw.NewProvider(api, vehicle.VIN, cc.Cache)
}

return v, err
Expand Down
84 changes: 55 additions & 29 deletions vehicle/bmw/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/transport"
"golang.org/x/oauth2"
)

Expand All @@ -17,62 +18,87 @@ import (
// API is an api.Vehicle implementation for BMW cars
type API struct {
*request.Helper
xUserAgent string
region string
region string
}

// NewAPI creates a new vehicle
func NewAPI(log *util.Logger, brand, region string, identity oauth2.TokenSource) *API {
v := &API{
Helper: request.NewHelper(log),
xUserAgent: fmt.Sprintf("android(SP1A.210812.016.C1);%s;99.0.0(99999);row", brand),
region: strings.ToUpper(region),
Helper: request.NewHelper(log),
region: strings.ToUpper(region),
}

// replace client transport with authenticated transport
v.Client.Transport = &oauth2.Transport{
Source: identity,
Base: v.Client.Transport,
v.Client.Transport = &transport.Decorator{
Base: &oauth2.Transport{
Source: identity,
Base: v.Client.Transport,
},
Decorator: transport.DecorateHeaders(map[string]string{
"X-User-Agent": fmt.Sprintf("android(SP1A.210812.016.C1);%s;99.0.0(99999);row", brand),
}),
}

return v
}

// Vehicles implements returns the /user/vehicles api
func (v *API) Vehicles() ([]string, error) {
func (v *API) Vehicles() ([]Vehicle, error) {
var res []Vehicle
uri := fmt.Sprintf("%s/eadrax-vcs/v4/vehicles?apptimezone=120&appDateTime=%d", regions[v.region].CocoApiURI, time.Now().UnixMilli())
err := v.GetJSON(uri, &res)
return res, err
}

// Status implements the /user/vehicles/<vin>/status api
func (v *API) Status(vin string) (VehicleStatus, error) {
var res VehicleStatus
uri := fmt.Sprintf("%s/eadrax-vcs/v4/vehicles/state?apptimezone=120&appDateTime=%d", regions[v.region].CocoApiURI, time.Now().UnixMilli())

req, err := request.New(http.MethodGet, uri, nil, map[string]string{
"Content-Type": request.JSONContent,
"X-User-Agent": v.xUserAgent,
"bmw-vin": vin,
})
if err != nil {
return nil, err
if err == nil {
err = v.DoJSON(req, &res)
}

if err := v.DoJSON(req, &res); err != nil {
return nil, err
}
return res, err
}

var vehicles []string
for _, v := range res {
vehicles = append(vehicles, v.VIN)
}
const (
CHARGE_START = "start-charging"
CHARGE_STOP = "stop-charging"
DOOR_LOCK = "door-lock"
LIGHT_FLASH = "light-flash"

return vehicles, err
REMOTE_SERVICE_BASE_URL = "eadrax-vrccs/v3/presentation/remote-commands"
VEHICLE_CHARGING_BASE_URL = "eadrax-crccs/v1/vehicles"
)

var serviceUrls = map[string]string{
CHARGE_START: VEHICLE_CHARGING_BASE_URL,
CHARGE_STOP: VEHICLE_CHARGING_BASE_URL,
}

// Status implements the /user/vehicles/<vin>/status api
func (v *API) Status(vin string) (VehicleStatus, error) {
var res VehicleStatus
uri := fmt.Sprintf("%s/eadrax-vcs/v4/vehicles/state?apptimezone=120&appDateTime=%d", regions[v.region].CocoApiURI, time.Now().UnixMilli())
type Event struct {
EventID string
CreationTime time.Time
}

req, err := request.New(http.MethodGet, uri, nil, map[string]string{
"Content-Type": request.JSONContent,
"X-User-Agent": v.xUserAgent,
"bmw-vin": vin,
// Action implements the /remote-commands/<vin>/<service> api
func (v *API) Action(vin, action string) (Event, error) {
var res Event

path, ok := serviceUrls[action]
if !ok {
path = REMOTE_SERVICE_BASE_URL
}
uri := fmt.Sprintf("%s/%s/%s/%s", regions[v.region].CocoApiURI, path, vin, action)

req, err := request.New(http.MethodPost, uri, nil, map[string]string{
"Accept": request.JSONContent,
})

if err == nil {
err = v.DoJSON(req, &res)
}
Expand Down
23 changes: 23 additions & 0 deletions vehicle/bmw/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
// Provider implements the vehicle api
type Provider struct {
statusG func() (VehicleStatus, error)
actionS func(action string) error
}

// NewProvider creates a vehicle api provider
Expand All @@ -18,6 +19,10 @@ func NewProvider(api *API, vin string, cache time.Duration) *Provider {
statusG: provider.Cached(func() (VehicleStatus, error) {
return api.Status(vin)
}, cache),
actionS: func(action string) error {
_, err := api.Action(vin, action)
return err
},
}
return impl
}
Expand Down Expand Up @@ -90,3 +95,21 @@ func (v *Provider) Odometer() (float64, error) {

return float64(res.State.CurrentMileage), nil
}

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

func (v *Provider) WakeUp() error {
return v.actionS(DOOR_LOCK)
}

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

// StartCharge implements the api.VehicleChargeController interface
func (v *Provider) StartCharge() error {
return v.actionS(CHARGE_START)
}

// StopCharge implements the api.VehicleChargeController interface
func (v *Provider) StopCharge() error {
return v.actionS(CHARGE_STOP)
}
31 changes: 16 additions & 15 deletions vehicle/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ func ensureVehicle(vin string, list func() ([]string, error)) (string, error) {
}

// ensureVehicleEx extracts vehicle with matching VIN from list of vehicles
func ensureVehicleEx[Vehicle any](
func ensureVehicleEx[T any](
vin string,
list func() ([]Vehicle, error),
extract func(Vehicle) string,
) (Vehicle, error) {
list func() ([]T, error),
extract func(T) string,
) (T, error) {
var zero T

vehicles, err := list()
if err != nil {
return *new(Vehicle), fmt.Errorf("cannot get vehicles: %w", err)
return zero, fmt.Errorf("cannot get vehicles: %w", err)
}

// vin defined
if vin = strings.ToUpper(vin); vin != "" {
for _, vehicle := range vehicles {
if vin == extract(vehicle) {
Expand All @@ -33,17 +36,15 @@ func ensureVehicleEx[Vehicle any](
}

// vin defined but doesn't exist
err = fmt.Errorf("cannot find vehicle: %s", vin)
} else {
// vin empty
if len(vehicles) == 1 {
return vehicles[0], nil
}
return zero, fmt.Errorf("cannot find vehicle: %s", vin)
}

err = fmt.Errorf("cannot find vehicle, got: %v", lo.Map(vehicles, func(v Vehicle, _ int) string {
return extract(v)
}))
// vin empty
if len(vehicles) == 1 {
return vehicles[0], nil
}

return *new(Vehicle), err
return zero, fmt.Errorf("cannot find vehicle, got: %v", lo.Map(vehicles, func(v T, _ int) string {
return extract(v)
}))
}