From fe732d806f51ec38d5a6f80ee1225457aeb72408 Mon Sep 17 00:00:00 2001 From: andig Date: Thu, 7 Jul 2022 22:55:36 +0200 Subject: [PATCH] FritzDect: simplify --- charger/fritzdect.go | 8 ++--- meter/fritzdect/fritzdect.go | 58 +++++++++++++++++------------------- 2 files changed, 30 insertions(+), 36 deletions(-) diff --git a/charger/fritzdect.go b/charger/fritzdect.go index 2f67165ed..e47b3cc85 100644 --- a/charger/fritzdect.go +++ b/charger/fritzdect.go @@ -43,12 +43,12 @@ func NewFritzDECTFromConfig(other map[string]interface{}) (api.Charger, error) { func NewFritzDECT(uri, ain, user, password string, standbypower float64) (*FritzDECT, error) { conn, err := fritzdect.NewConnection(uri, ain, user, password) - fd := &FritzDECT{ + m := &FritzDECT{ conn: conn, standbypower: standbypower, } - return fd, err + return m, err } // Enabled implements the api.Charger interface @@ -58,10 +58,6 @@ func (c *FritzDECT) Enabled() (bool, error) { return false, err } - if resp == "inval" { - return false, api.ErrNotAvailable - } - return strconv.ParseBool(resp) } diff --git a/meter/fritzdect/fritzdect.go b/meter/fritzdect/fritzdect.go index 6e0ddb416..698cb552f 100644 --- a/meter/fritzdect/fritzdect.go +++ b/meter/fritzdect/fritzdect.go @@ -77,40 +77,42 @@ func NewConnection(uri, ain, user, password string) (*Connection, error) { } // ExecCmd execautes an FritzDECT AHA-HTTP-Interface command -func (fd *Connection) ExecCmd(function string) (string, error) { +func (c *Connection) ExecCmd(function string) (string, error) { // refresh Fritzbox session id - if time.Since(fd.Updated) >= 10*time.Minute { - err := fd.getSessionID() - if err != nil { + if time.Since(c.Updated) >= 10*time.Minute { + if err := c.getSessionID(); err != nil { return "", err } // update session timestamp - fd.Updated = time.Now() + c.Updated = time.Now() } parameters := url.Values{ - "sid": []string{fd.SID}, - "ain": []string{fd.AIN}, + "sid": []string{c.SID}, + "ain": []string{c.AIN}, "switchcmd": []string{function}, } - uri := fmt.Sprintf("%s/webservices/homeautoswitch.lua", fd.URI) - response, err := fd.GetBody(uri + "?" + parameters.Encode()) - return strings.TrimSpace(string(response)), err + uri := fmt.Sprintf("%s/webservices/homeautoswitch.lua", c.URI) + body, err := c.GetBody(uri + "?" + parameters.Encode()) + + res := strings.TrimSpace(string(body)) + + if err == nil && res == "inval" { + err = api.ErrNotAvailable + } + + return res, err } // CurrentPower implements the api.Meter interface -func (fd *Connection) CurrentPower() (float64, error) { +func (c *Connection) CurrentPower() (float64, error) { // power value in 0,001 W (current switch power, refresh approximately every 2 minutes) - resp, err := fd.ExecCmd("getswitchpower") + resp, err := c.ExecCmd("getswitchpower") if err != nil { return 0, err } - if resp == "inval" { - return 0, api.ErrNotAvailable - } - power, err := strconv.ParseFloat(resp, 64) return power / 1000, err // mW ==> W @@ -119,17 +121,13 @@ func (fd *Connection) CurrentPower() (float64, error) { var _ api.MeterEnergy = (*Connection)(nil) // CurrentPower implements the api.MeterEnergy interface -func (fd *Connection) TotalEnergy() (float64, error) { +func (c *Connection) TotalEnergy() (float64, error) { // Energy value in Wh (total switch energy, refresh approximately every 2 minutes) - resp, err := fd.ExecCmd("getswitchenergy") + resp, err := c.ExecCmd("getswitchenergy") if err != nil { return 0, err } - if resp == "inval" { - return 0, api.ErrNotAvailable - } - energy, err := strconv.ParseFloat(resp, 64) return energy / 1000, err // Wh ==> KWh @@ -138,9 +136,9 @@ func (fd *Connection) TotalEnergy() (float64, error) { // Fritzbox helpers (credits to https://github.com/rsdk/ahago) // getSessionID fetches a session-id based on the username and password in the connection struct -func (fd *Connection) getSessionID() error { - uri := fmt.Sprintf("%s/login_sid.lua", fd.URI) - body, err := fd.GetBody(uri) +func (c *Connection) getSessionID() error { + uri := fmt.Sprintf("%s/login_sid.lua", c.URI) + body, err := c.GetBody(uri) if err != nil { return err } @@ -153,18 +151,18 @@ func (fd *Connection) getSessionID() error { if err = xml.Unmarshal(body, &v); err == nil && v.SID == "0000000000000000" { var challresp string - if challresp, err = createChallengeResponse(v.Challenge, fd.Password); err == nil { + if challresp, err = createChallengeResponse(v.Challenge, c.Password); err == nil { params := url.Values{ - "username": []string{fd.User}, + "username": []string{c.User}, "response": []string{challresp}, } - if body, err = fd.GetBody(uri + "?" + params.Encode()); err == nil { + if body, err = c.GetBody(uri + "?" + params.Encode()); err == nil { err = xml.Unmarshal(body, &v) if v.SID == "0000000000000000" { - return errors.New("invalid username (" + fd.User + ") or password") + return errors.New("invalid user or password") } - fd.SID = v.SID + c.SID = v.SID } } }