-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(vehicle): add support for LeapMotor API #29666
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
Closed
Closed
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
41237d0
feat(vehicle): add support for leapmotor
syphernl 0cc838b
fmt
syphernl 06a68c5
refactor(leapmotor): simplify identity helpers
syphernl 0f76630
refactor(leapmotor): address review feedback
syphernl 3847d5a
go mod tidy: promote gmsm to direct dependency
syphernl 8ca5a81
Merge branch 'master' into feat/leapmotor_api_integration
syphernl 52dc5c0
refactor(leapmotor): address review feedback (round 2)
syphernl 71a5e8c
Merge branch 'master' into feat/leapmotor_api_integration
syphernl 9239a15
feat(leapmotor): add optional vehicle-side charge control
syphernl c7e9c56
Merge branch 'master' into feat/leapmotor_api_integration
syphernl 7a626c9
fix(leapmotor): persist refreshed token to settings in Refresh()
syphernl 7897de9
Merge branch 'master' into feat/leapmotor_api_integration
syphernl f13d020
Merge branch 'master' into feat/leapmotor_api_integration
syphernl 90c5605
Leapmotor: use C10 status endpoint for B10/B11
syphernl 59f7d9a
Leapmotor: decode signal-based C10/B10 status
syphernl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package vehicle | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/evcc-io/evcc/api" | ||
| "github.com/evcc-io/evcc/util" | ||
| "github.com/evcc-io/evcc/vehicle/leapmotor" | ||
| ) | ||
|
|
||
| // Leapmotor is an api.Vehicle implementation for Leapmotor cars. | ||
| type Leapmotor struct { | ||
| *embed | ||
| *leapmotor.Provider | ||
| } | ||
|
|
||
| func init() { | ||
| registry.Add("leapmotor", NewLeapmotorFromConfig) | ||
| } | ||
|
|
||
| // NewLeapmotorFromConfig creates a new Leapmotor vehicle from config. | ||
| func NewLeapmotorFromConfig(other map[string]any) (api.Vehicle, error) { | ||
| cc := struct { | ||
| embed `mapstructure:",squash"` | ||
| User, Password, VIN string | ||
| AppCert, AppKey string | ||
|
syphernl marked this conversation as resolved.
Outdated
|
||
| Cache time.Duration | ||
| }{ | ||
| Cache: interval, | ||
| } | ||
|
|
||
| if err := util.DecodeOther(other, &cc); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if cc.User == "" || cc.Password == "" { | ||
| return nil, api.ErrMissingCredentials | ||
| } | ||
| if cc.AppCert == "" || cc.AppKey == "" { | ||
| return nil, fmt.Errorf("leapmotor: app_cert and app_key are required (extract from Leapmotor APK)") | ||
| } | ||
|
|
||
| log := util.NewLogger("leapmotor").Redact(cc.User, cc.Password, cc.VIN) | ||
|
|
||
| identity, err := leapmotor.NewIdentity(log, cc.AppCert, cc.AppKey, cc.User, cc.Password) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if err := identity.Login(); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| api := leapmotor.NewAPI(log, identity) | ||
|
|
||
| vehicles, err := api.Vehicles() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("leapmotor: get vehicles: %w", err) | ||
| } | ||
| if len(vehicles) == 0 { | ||
| return nil, fmt.Errorf("leapmotor: no vehicles found on account") | ||
| } | ||
|
|
||
| var matched *leapmotor.Vehicle | ||
| for i := range vehicles { | ||
| v := &vehicles[i] | ||
| if cc.VIN == "" || v.VIN == cc.VIN { | ||
| matched = v | ||
| break | ||
| } | ||
| } | ||
| if matched == nil { | ||
| return nil, fmt.Errorf("leapmotor: VIN %s not found on account", cc.VIN) | ||
| } | ||
|
|
||
| return &Leapmotor{ | ||
| embed: &cc.embed, | ||
| Provider: leapmotor.NewProvider(api, matched.VIN, matched.CarType, cc.Cache), | ||
| }, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package leapmotor | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
| ) | ||
|
|
||
| const ( | ||
| BaseURL = "https://appgateway.leapmotor-international.de" | ||
| appVersion = "1.12.3" | ||
| source = "leapmotor" | ||
| channel = "1" | ||
| deviceType = "1" | ||
| p12EncAlg = "1" | ||
| policyID = "20260204" | ||
| defaultLang = "en" | ||
| ) | ||
|
|
||
| type apiEnvelope[T any] struct { | ||
| Code int `json:"code"` | ||
| Message string `json:"message"` | ||
| Data T `json:"data"` | ||
| } | ||
|
|
||
| // LoginResponse holds fields returned by the login endpoint. | ||
| type LoginResponse struct { | ||
| ID json.Number `json:"id"` | ||
| UID string `json:"uid"` | ||
| Token string `json:"token"` | ||
| RefreshToken string `json:"refreshToken"` | ||
| SignIkm string `json:"signIkm"` | ||
| SignSalt string `json:"signSalt"` | ||
| SignInfo string `json:"signInfo"` | ||
| Base64Cert string `json:"base64Cert"` | ||
| } | ||
|
|
||
| // Vehicle is a vehicle entry from the account vehicle list. | ||
| type Vehicle struct { | ||
| VIN string `json:"vin"` | ||
| CarType string `json:"carType"` | ||
| } | ||
|
|
||
| // StatusData holds the vehicle status fields relevant to EVCC. | ||
| type StatusData struct { | ||
| Soc *int `json:"soc"` | ||
| ChargeState *int `json:"chargeState"` | ||
| ChargeRemainTime *int `json:"chargeRemainTime"` | ||
| BatteryCurrent *float64 `json:"batteryCurrent"` | ||
| BatteryVoltage *float64 `json:"batteryVoltage"` | ||
| ExpectedMileage *int `json:"expectedMileage"` | ||
| Speed *int `json:"speed"` | ||
| TotalMileage *int `json:"totalMileage"` | ||
| } | ||
|
|
||
| // apiPost sends a POST to fullURL with the given headers and body, returns the response body. | ||
| func apiPost(client *http.Client, fullURL string, headers map[string]string, body string) ([]byte, error) { | ||
| req, err := http.NewRequest(http.MethodPost, fullURL, strings.NewReader(body)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| for k, v := range headers { | ||
| req.Header.Set(k, v) | ||
| } | ||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer resp.Body.Close() | ||
| return io.ReadAll(resp.Body) | ||
| } | ||
|
syphernl marked this conversation as resolved.
|
||
|
|
||
| // parseEnvelope decodes the API envelope, returning Data or an error for non-zero codes. | ||
| func parseEnvelope[T any](body []byte) (T, error) { | ||
| var res apiEnvelope[T] | ||
| var zero T | ||
| if err := json.Unmarshal(body, &res); err != nil { | ||
| return zero, err | ||
| } | ||
| if res.Code != 0 { | ||
| return zero, fmt.Errorf("api %d: %s", res.Code, res.Message) | ||
| } | ||
| return res.Data, nil | ||
| } | ||
|
syphernl marked this conversation as resolved.
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.