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

Wrap vehicles on error to prevent application not starting #1215

Merged
merged 3 commits into from
Jul 16, 2021
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
4 changes: 3 additions & 1 deletion vehicle/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/andig/evcc/api"
"github.com/andig/evcc/vehicle/wrapper"
)

const interval = 15 * time.Minute
Expand Down Expand Up @@ -43,7 +44,8 @@ func NewFromConfig(typ string, other map[string]interface{}) (v api.Vehicle, err
factory, err := registry.Get(strings.ToLower(typ))
if err == nil {
if v, err = factory(other); err != nil {
err = fmt.Errorf("cannot create vehicle '%s': %w", typ, err)
// wrap any created errors to prevent fatals
v, err = wrapper.New(v, err)
}
} else {
err = fmt.Errorf("invalid vehicle type: %s", typ)
Expand Down
26 changes: 12 additions & 14 deletions vehicle/vehicle.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@ type embed struct {
}

// Title implements the Vehicle.Title interface
func (m *embed) Title() string {
return m.Title_
func (v *embed) Title() string {
return v.Title_
}

// Capacity implements the Vehicle.Capacity interface
func (m *embed) Capacity() int64 {
return m.Capacity_
func (v *embed) Capacity() int64 {
return v.Capacity_
}

var _ api.Identifier = (*embed)(nil)

// Identify implements the api.Identifier interface
func (m *embed) Identify() (string, error) {
return m.Identifier_, nil
func (v *embed) Identify() (string, error) {
return v.Identifier_, nil
}

//go:generate go run ../cmd/tools/decorate.go -f decorateVehicle -b api.Vehicle -t "api.ChargeState,Status,func() (api.ChargeStatus, error)" -t "api.VehicleRange,Range,func() (int64, error)"
Expand Down Expand Up @@ -109,15 +107,15 @@ func NewConfigurableFromConfig(other map[string]interface{}) (api.Vehicle, error
}

// SoC implements the api.Vehicle interface
func (m *Vehicle) SoC() (float64, error) {
return m.chargeG()
func (v *Vehicle) SoC() (float64, error) {
return v.chargeG()
}

// SoC implements the api.Vehicle interface
func (m *Vehicle) status() (api.ChargeStatus, error) {
func (v *Vehicle) status() (api.ChargeStatus, error) {
status := api.StatusF

statusS, err := m.statusG()
statusS, err := v.statusG()
if err == nil {
status = api.ChargeStatus(statusS)
}
Expand All @@ -126,6 +124,6 @@ func (m *Vehicle) status() (api.ChargeStatus, error) {
}

// rng implements the api.VehicleRange interface
func (m *Vehicle) rng() (int64, error) {
return m.rangeG()
func (v *Vehicle) rng() (int64, error) {
return v.rangeG()
}
41 changes: 41 additions & 0 deletions vehicle/wrapper/wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package wrapper

import (
"fmt"

"github.com/andig/evcc/api"
)

// Wrapper wraps an api.Vehicle to capture initialization errors
type Wrapper struct {
err error
}

// New creates a new Vehicle
func New(w api.Vehicle, err error) (api.Vehicle, error) {
v := &Wrapper{
err: fmt.Errorf("vehicle not available: %w", err),
}

return v, nil
}

// Title implements the Vehicle.Title interface
func (v *Wrapper) Title() string {
return "unavailable"
}

// Capacity implements the Vehicle.Capacity interface
func (v *Wrapper) Capacity() int64 {
return 0
}

// Identify implements the api.Identifier interface
func (v *Wrapper) Identify() (string, error) {
return "", v.err
}

// SoC implements the api.Vehicle interface
func (v *Wrapper) SoC() (float64, error) {
return 0, v.err
}