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

Add Fiat api #1229

Merged
merged 8 commits into from
Jul 21, 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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/andig/gosunspec v0.0.0-20210511114617-aa30cf9b7a3f // indirect
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef
github.com/avast/retry-go v3.0.0+incompatible
github.com/aws/aws-sdk-go v1.40.3
github.com/benbjohnson/clock v1.1.0
github.com/bogosj/tesla v1.0.1-0.20210712193118-0e5b7bb5e924
github.com/containrrr/shoutrrr v0.4.4
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef/go.mod h1:JS7h
github.com/avast/retry-go v2.6.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHSxpiH9JdtuBj0=
github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
github.com/aws/aws-sdk-go v1.40.3 h1:NzjcLRsb+C9L1dVPajdNbdzkuPBi0pQJWiQW0eYJGo8=
github.com/aws/aws-sdk-go v1.40.3/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q=
github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
Expand Down Expand Up @@ -383,6 +385,10 @@ github.com/jeremywohl/flatten v1.0.1 h1:LrsxmB3hfwJuE+ptGOijix1PIfOoKLJ3Uee/mzbg
github.com/jeremywohl/flatten v1.0.1/go.mod h1:4AmD/VxjWcI5SRB0n6szE2A6s2fsNHDLO0nAlMHgfLQ=
github.com/jinzhu/copier v0.3.2 h1:QdBOCbaouLDYaIPFfi1bKv5F5tPpeTwXe4sD0jqtz5w=
github.com/jinzhu/copier v0.3.2/go.mod h1:24xnZezI2Yqac9J61UC6/dG/k76ttpq0DdJI3QmUvro=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/joeshaw/carwings v0.0.0-20191118152321-61b46581307a/go.mod h1:tB0OlpicmRVTL1Vksc5XRiYo+wkK2kl/GI7eGMIl6Rs=
github.com/joeshaw/carwings v0.0.0-20210616193757-7fbc6f7c3c73 h1:0GC7kKnW8vFHZB1txMdeiaVx1X65V9x8sWi5xoxUCfc=
github.com/joeshaw/carwings v0.0.0-20210616193757-7fbc6f7c3c73/go.mod h1:tB0OlpicmRVTL1Vksc5XRiYo+wkK2kl/GI7eGMIl6Rs=
Expand Down
13 changes: 13 additions & 0 deletions util/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package util

import "math/rand"

// RandomString creates random string of N integers
func RandomString(n int) string {
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
s := make([]rune, n)
for i := range s {
s[i] = letters[rand.Intn(len(letters))]
}
return string(s)
}
168 changes: 168 additions & 0 deletions vehicle/fiat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package vehicle

import (
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"

"github.com/andig/evcc/api"
"github.com/andig/evcc/provider"
"github.com/andig/evcc/util"
"github.com/andig/evcc/util/request"
"github.com/andig/evcc/vehicle/fiat"
)

// https://github.com/TA2k/ioBroker.fiat

// Fiat is an api.Vehicle implementation for Fiat cars
type Fiat struct {
*embed
*request.Helper
vin string
identity *fiat.Identity
statusG func() (interface{}, error)
}

func init() {
registry.Add("fiat", NewFiatFromConfig)
}

// NewFiatFromConfig creates a new vehicle
func NewFiatFromConfig(other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
User, Password, VIN string
Cache time.Duration
}{
Cache: interval,
}

if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}

if cc.User == "" || cc.Password == "" {
return nil, errors.New("missing credentials")
}

log := util.NewLogger("fiat")

v := &Fiat{
embed: &cc.embed,
Helper: request.NewHelper(log),
vin: strings.ToUpper(cc.VIN),
identity: fiat.NewIdentity(log, cc.User, cc.Password),
}

err := v.identity.Login()
if err != nil {
return v, fmt.Errorf("login failed: %w", err)
}

if cc.VIN == "" {
v.vin, err = findVehicle(v.vehicles())
if err == nil {
log.DEBUG.Printf("found vehicle: %v", v.vin)
}
}

v.statusG = provider.NewCached(func() (interface{}, error) {
return v.status()
}, cc.Cache).InterfaceGetter()

return v, err
}

func (v *Fiat) request(method, uri string, body io.ReadSeeker) (*http.Request, error) {
headers := map[string]string{
"Content-Type": "application/json",
"X-Clientapp-Version": "1.0",
"ClientrequestId": util.RandomString(16),
"X-Api-Key": fiat.XApiKey,
"X-Originator-Type": "web",
}

req, err := request.New(method, uri, body, headers)
if err == nil {
err = v.identity.Sign(req, body)
}

return req, err
}

func (v *Fiat) vehicles() ([]string, error) {
var res fiat.Vehicles

uri := fmt.Sprintf("%s/v4/accounts/%s/vehicles?stage=ALL", fiat.ApiURI, v.identity.UID())

req, err := v.request(http.MethodGet, uri, nil)
if err == nil {
err = v.DoJSON(req, &res)
}

var vehicles []string
if err == nil {
for _, v := range res.Vehicles {
vehicles = append(vehicles, v.VIN)
}
}

return vehicles, err
}

func (v *Fiat) status() (interface{}, error) {
var res fiat.Status

uri := fmt.Sprintf("%s/v2/accounts/%s/vehicles/%s/status", fiat.ApiURI, v.identity.UID(), v.vin)

req, err := v.request(http.MethodGet, uri, nil)
if err == nil {
err = v.DoJSON(req, &res)
}

return res, err
}

// SoC implements the api.Vehicle interface
func (v *Fiat) SoC() (float64, error) {
res, err := v.statusG()
if res, ok := res.(fiat.Status); err == nil && ok {
return float64(res.EvInfo.Battery.StateOfCharge), nil
}

return 0, err
}

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

// Range implements the api.VehicleRange interface
func (v *Fiat) Range() (int64, error) {
res, err := v.statusG()
if res, ok := res.(fiat.Status); err == nil && ok {
return int64(res.EvInfo.Battery.DistanceToEmpty.Value), nil
}

return 0, err
}

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

// Status implements the api.ChargeState interface
func (v *Fiat) Status() (api.ChargeStatus, error) {
status := api.StatusA // disconnected

res, err := v.statusG()
if res, ok := res.(fiat.Status); err == nil && ok {
if res.EvInfo.Battery.PlugInStatus {
status = api.StatusB // connected, not charging
}
if res.EvInfo.Battery.ChargingStatus == "CHARGING" {
status = api.StatusC // charging
}
}

return status, err
}
Loading