Skip to content

Commit

Permalink
Bluelink: update api (#13259)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Apr 2, 2024
1 parent b721561 commit f062380
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
66 changes: 50 additions & 16 deletions vehicle/bluelink/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,33 @@ var _ api.Battery = (*Provider)(nil)
// Soc implements the api.Battery interface
func (v *Provider) Soc() (float64, error) {
res, err := v.statusG()
if err != nil {
return 0, err
}

if err == nil {
if res.EvStatus != nil {
return res.EvStatus.BatteryStatus, nil
}

return 0, err
if res.Battery != nil {
return float64(res.Battery.BatSoc), nil
}

return 0, api.ErrNotAvailable
}

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

// Status implements the api.Battery interface
func (v *Provider) Status() (api.ChargeStatus, error) {
status := api.StatusNone

res, err := v.statusG()
if err != nil {
return status, err
}

status := api.StatusNone
if err == nil {
if res.EvStatus != nil {
status = api.StatusA
if res.EvStatus.BatteryPlugin > 0 || res.EvStatus.ChargePortDoorOpenStatus == 1 {
status = api.StatusB
Expand All @@ -121,16 +132,19 @@ func (v *Provider) Status() (api.ChargeStatus, error) {
}
}

return status, err
return status, api.ErrNotAvailable
}

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

// FinishTime implements the api.VehicleFinishTimer interface
func (v *Provider) FinishTime() (time.Time, error) {
res, err := v.statusG()
if err != nil {
return time.Time{}, err
}

if err == nil {
if res.EvStatus != nil {
remaining := res.EvStatus.RemainTime2.Atc.Value

if remaining == 0 {
Expand All @@ -141,58 +155,78 @@ func (v *Provider) FinishTime() (time.Time, error) {
return ts.Add(time.Duration(remaining) * time.Minute), err
}

return time.Time{}, err
return time.Time{}, api.ErrNotAvailable
}

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

// Range implements the api.VehicleRange interface
func (v *Provider) Range() (int64, error) {
res, err := v.statusG()
if err != nil {
return 0, err
}

if err == nil {
if res.EvStatus != nil {
if dist := res.EvStatus.DrvDistance; len(dist) == 1 {
return int64(dist[0].RangeByFuel.EvModeRange.Value), nil
}

return 0, api.ErrNotAvailable
}

return 0, err
return 0, api.ErrNotAvailable
}

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

// Range implements the api.VehicleRange interface
func (v *Provider) Odometer() (float64, error) {
res, err := v.statusLG()
return res.ResMsg.VehicleStatusInfo.Odometer.Value, err
if err != nil {
return 0, err
}

if res.ResMsg.VehicleStatusInfo.Odometer != nil {
return res.ResMsg.VehicleStatusInfo.Odometer.Value, err
}

return 0, api.ErrNotAvailable
}

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

// GetLimitSoc implements the api.SocLimiter interface
func (v *Provider) GetLimitSoc() (int64, error) {
res, err := v.statusG()
if err != nil {
return 0, err
}

if err == nil {
if res.EvStatus != nil {
for _, targetSOC := range res.EvStatus.ReservChargeInfos.TargetSocList {
if targetSOC.PlugType == plugTypeAC {
return int64(targetSOC.TargetSocLevel), nil
}
}
}

return 0, err
return 0, api.ErrNotAvailable
}

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

// Position implements the api.VehiclePosition interface
func (v *Provider) Position() (float64, float64, error) {
res, err := v.statusLG()
coord := res.ResMsg.VehicleStatusInfo.VehicleLocation.Coord
return coord.Lat, coord.Lon, err
if err != nil {
return 0, 0, err
}

if res.ResMsg.VehicleStatusInfo.VehicleLocation != nil {
coord := res.ResMsg.VehicleStatusInfo.VehicleLocation.Coord
return coord.Lat, coord.Lon, err
}

return 0, 0, api.ErrNotAvailable
}

var _ api.Resurrector = (*Provider)(nil)
Expand Down
9 changes: 6 additions & 3 deletions vehicle/bluelink/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ type StatusLatestResponse struct {
ResMsg struct {
VehicleStatusInfo struct {
VehicleStatus VehicleStatus
VehicleLocation VehicleLocation
Odometer Odometer
VehicleLocation *VehicleLocation
Odometer *Odometer
}
}
}

type VehicleStatus struct {
Time string
EvStatus struct {
EvStatus *struct {
BatteryCharge bool
BatteryStatus float64
BatteryPlugin int
Expand All @@ -42,6 +42,9 @@ type VehicleStatus struct {
DrvDistance []DrivingDistance
ReservChargeInfos ReservChargeInfo
}
Battery *struct {
BatSoc int
}
Vehicles []Vehicle
}

Expand Down

0 comments on commit f062380

Please sign in to comment.