Skip to content

Commit

Permalink
Fix go-e cloud updates (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Jul 17, 2020
1 parent 0f02f6f commit 5bb0c58
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions charger/go-e.go
Expand Up @@ -24,14 +24,15 @@ type goeCloudResponse struct {

// goeStatusResponse is the API response if status not OK
type goeStatusResponse struct {
Car int `json:"car,string"` // car status
Alw int `json:"alw,string"` // allow charging
Amp int `json:"amp,string"` // current [A]
Err int `json:"err,string"` // error
Stp int `json:"stp,string"` // stop state
Tmp int `json:"tmp,string"` // temperature [°C]
Dws int `json:"dws,string"` // energy [Ws]
Nrg []int `json:"nrg"` // voltage, current, power
Fwv string `json:"fwv"` // firmware version - indicates local response
Car int `json:"car,string"` // car status
Alw int `json:"alw,string"` // allow charging
Amp int `json:"amp,string"` // current [A]
Err int `json:"err,string"` // error
Stp int `json:"stp,string"` // stop state
Tmp int `json:"tmp,string"` // temperature [°C]
Dws int `json:"dws,string"` // energy [Ws]
Nrg []int `json:"nrg"` // voltage, current, power
}

// GoE charger implementation
Expand Down Expand Up @@ -119,6 +120,8 @@ func (c *GoE) apiStatus() (status goeStatusResponse, err error) {
return status, err
}

// apiUpdate invokes either cloud or local api
// goeStatusResponse is only valid for local api. Use Fwv if valid.
func (c *GoE) apiUpdate(payload string) (goeStatusResponse, error) {
if c.token == "" {
return c.localResponse("mqtt", payload)
Expand All @@ -133,6 +136,11 @@ func (c *GoE) apiUpdate(payload string) (goeStatusResponse, error) {
return status, err
}

// isValid checks is status response is local
func isValid(status goeStatusResponse) bool {
return status.Fwv != ""
}

// Status implements the Charger.Status interface
func (c *GoE) Status() (api.ChargeStatus, error) {
status, err := c.apiStatus()
Expand Down Expand Up @@ -177,7 +185,7 @@ func (c *GoE) Enable(enable bool) error {
}

status, err := c.apiUpdate(fmt.Sprintf("alw=%d", b))
if err == nil && status.Alw != b {
if err == nil && isValid(status) && status.Alw != b {
return fmt.Errorf("alw update failed: %d", status.Amp)
}

Expand All @@ -187,7 +195,7 @@ func (c *GoE) Enable(enable bool) error {
// MaxCurrent implements the Charger.MaxCurrent interface
func (c *GoE) MaxCurrent(current int64) error {
status, err := c.apiUpdate(fmt.Sprintf("amp=%d", current))
if err == nil && int64(status.Amp) != current {
if err == nil && isValid(status) && int64(status.Amp) != current {
return fmt.Errorf("amp update failed: %d", status.Amp)
}

Expand Down

0 comments on commit 5bb0c58

Please sign in to comment.