Skip to content

Commit

Permalink
Smart Hello: fix gen-1 api (evcc-io#15788)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Aug 29, 2024
1 parent 7cad292 commit bcc83df
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
6 changes: 3 additions & 3 deletions vehicle/smart/hello/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (v *API) request(method, path string, params url.Values, body io.Reader) (*

func (v *API) Vehicles() ([]string, error) {
var res struct {
Code ResponseCode
Code Int
Message string
Error Error
Data struct {
Expand Down Expand Up @@ -146,7 +146,7 @@ func (v *API) UpdateSession(vin string) error {
}

var res struct {
Code ResponseCode
Code Int
Message string
Error Error
}
Expand All @@ -161,7 +161,7 @@ func (v *API) Status(vin string) (VehicleStatus, error) {
}

var res struct {
Code ResponseCode
Code Int
Message string
Error Error
Data struct {
Expand Down
2 changes: 1 addition & 1 deletion vehicle/smart/hello/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ x-api-signature-version:1.0
return nonce, ts, sign, nil
}

func responseError(err error, code ResponseCode, msg string, errS Error) error {
func responseError(err error, code Int, msg string, errS Error) error {
var body error

if code != 0 && code != ResponseOK {
Expand Down
2 changes: 1 addition & 1 deletion vehicle/smart/hello/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (v *Identity) appToken(token *oauth2.Token) (*oauth2.Token, string, error)
})

var res struct {
Code ResponseCode
Code Int
Message string
Data AppToken
}
Expand Down
40 changes: 30 additions & 10 deletions vehicle/smart/hello/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,38 @@ import (

const ResponseOK = 1000

type ResponseCode int
type Int int

func (rc *ResponseCode) UnmarshalJSON(data []byte) error {
i, err := strconv.Atoi(strings.Trim(string(data), `"`))
func (rc *Int) UnmarshalJSON(data []byte) error {
plain := strings.Trim(string(data), `"`)
if plain == "" {
*rc = Int(0)
return nil
}
v, err := strconv.Atoi(plain)
if err == nil {
*rc = Int(v)
}
return err
}

type Bool bool

func (rc *Bool) UnmarshalJSON(data []byte) error {
plain := strings.Trim(string(data), `"`)
if plain == "" {
*rc = Bool(false)
return nil
}
v, err := strconv.ParseBool(plain)
if err == nil {
*rc = ResponseCode(i)
*rc = Bool(v)
}
return err
}

type Error struct {
Code ResponseCode
Code Int
Message string
}

Expand All @@ -38,11 +58,11 @@ type VehicleStatus struct {
UsageMode int `json:"usageMode,string"` // "0",
EngineStatus string `json:"engineStatus"` // "engine_off",
Position struct {
Altitude int `json:"altitude,string"` // "117",
PosCanBeTrusted bool `json:"posCanBeTrusted,string"` // "true",
Latitude int `json:"latitude,string"` // "18...",
CarLocatorStatUploadEn bool `json:"carLocatorStatUploadEn,string"` // "true",
Longitude int `json:"longitude,string"` // "28..."
Altitude Int `json:"altitude"` // "117",
PosCanBeTrusted Bool `json:"posCanBeTrusted"` // "true",
Latitude Int `json:"latitude"` // "18...",
CarLocatorStatUploadEn Bool `json:"carLocatorStatUploadEn"` // "true",
Longitude Int `json:"longitude"` // "28..."
}
DistanceToEmpty int `json:"distanceToEmpty,string"` // "0",
CarMode int `json:"carMode,string"` // "0",
Expand Down
9 changes: 8 additions & 1 deletion vehicle/smart/hello/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@ const data = `{
}`

func TestUnmarshal(t *testing.T) {
var res VehicleStatus
var res struct {
Code Int
Message string
Error Error
Data struct {
VehicleStatus VehicleStatus
}
}
require.NoError(t, json.Unmarshal([]byte(data), &res))
}

0 comments on commit bcc83df

Please sign in to comment.