Easee: add token keep-alive (#8516)

This commit is contained in:
andig 2023-06-18 12:26:44 +02:00 • committed by GitHub
parent 24cea560f4
commit c73ada6264
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -177,9 +177,22 @@ func NewEasee(user, password, charger string, timeout time.Duration) (*Easee, er
err = os.ErrDeadlineExceeded
}
if err == nil {
go c.heartbeat()
}
return c, err
}
// heartbeat ensures tokens are refreshed even when not charging for longer time
func (c *Easee) heartbeat() {
for range time.Tick(6 * time.Hour) {
if _, err := c.chargerSite(c.charger); err != nil {
c.log.ERROR.Println("heartbeat:", err)
}
}
}
func (c *Easee) chargerSite(charger string) (easee.Site, error) {
var res easee.Site
uri := fmt.Sprintf("%s/chargers/%s/site", easee.API, charger)
@ -419,18 +432,18 @@ func (c *Easee) postJSONAndWait(uri string, data any) error {
}
defer resp.Body.Close()
if resp.StatusCode == 200 { //sync call
if resp.StatusCode == 200 { // sync call
return nil
}
if resp.StatusCode == 202 { //async call, wait for response
if resp.StatusCode == 202 { // async call, wait for response
var cmd easee.RestCommandResponse
if strings.Contains(uri, "/commands/") { //command endpoint
if strings.Contains(uri, "/commands/") { // command endpoint
if err := json.NewDecoder(resp.Body).Decode(&cmd); err != nil {
return err
}
} else { //settings endpoint
} else { // settings endpoint
var cmdArr []easee.RestCommandResponse
if err := json.NewDecoder(resp.Body).Decode(&cmdArr); err != nil {
return err
@ -441,9 +454,10 @@ func (c *Easee) postJSONAndWait(uri string, data any) error {
}
}
if cmd.Ticks == 0 { //Easee API thinks this was a noop
if cmd.Ticks == 0 { // api thinks this was a noop
return nil
}
return c.waitForTickResponse(cmd.Ticks)
}