Skip to content
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

Easee: add command response logging to improve debugging #7597

Merged
merged 3 commits into from
Apr 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 36 additions & 23 deletions charger/easee.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ type Easee struct {
phaseMode int
currentPower, sessionEnergy, totalEnergy,
currentL1, currentL2, currentL3 float64
rfid string
lp loadpoint.API
rfid string
resumeCommandTicks int64
lp loadpoint.API
}

func init() {
Expand Down Expand Up @@ -235,12 +236,13 @@ func (c *Easee) waitForInitialUpdate(done chan struct{}) {
close(done)
}

// observe handles the subscription messages
func (c *Easee) observe(typ string, i json.RawMessage) {
// ProductUpdate implements the signalr receiver
func (c *Easee) ProductUpdate(i json.RawMessage) {
var res easee.Observation

err := json.Unmarshal(i, &res)
if err != nil {
c.log.ERROR.Printf("invalid message: %s %s %v", i, typ, err)
c.log.ERROR.Printf("invalid message: %s %v", i, err)
return
}

Expand Down Expand Up @@ -276,6 +278,8 @@ func (c *Easee) observe(typ string, i json.RawMessage) {
}
c.updated = time.Now()

c.log.TRACE.Printf("%s: %s %v", res.Mid, res.ID, value)

switch res.ID {
case easee.USER_IDTOKEN:
c.rfid = res.Value
Expand All @@ -299,12 +303,6 @@ func (c *Easee) observe(typ string, i json.RawMessage) {
c.phaseMode = value.(int)
case easee.DYNAMIC_CHARGER_CURRENT:
c.dynamicChargerCurrent = value.(float64)
// ensure that charger current matches evcc's expectation
if c.dynamicChargerCurrent > 0 && c.dynamicChargerCurrent != c.current {
if err = c.MaxCurrent(int64(c.current)); err != nil {
c.log.ERROR.Println(err)
}
}
case easee.CHARGER_OP_MODE:
switch value.(int) {
case easee.ModeDisconnected:
Expand All @@ -324,23 +322,32 @@ func (c *Easee) observe(typ string, i json.RawMessage) {
value.(int) == easee.ModeCompleted ||
value.(int) == easee.ModeReadyToCharge
}

c.log.TRACE.Printf("%s %s: %s %v", typ, res.Mid, res.ID, value)
}

// ProductUpdate implements the signalr receiver
func (c *Easee) ProductUpdate(i json.RawMessage) {
c.observe("ProductUpdate", i)
}

// ChargerUpdate implements the signalr receiver
func (c *Easee) ChargerUpdate(i json.RawMessage) {
c.log.TRACE.Printf("JSON ChargerUpdate: %s", i)
// c.observe("ChargerUpdate", i)
}

// CommandResponse implements the signalr receiver
func (c *Easee) CommandResponse(i json.RawMessage) {
// c.observe("CommandResponse", i)
var res easee.SignalRCommandResponse

err := json.Unmarshal(i, &res)
if err != nil {
c.log.ERROR.Printf("invalid message: %s %v", i, err)
return
}

// reapply current limit after successfull resume command
if res.Ticks == c.resumeCommandTicks {
c.log.TRACE.Printf("resume confirmed, reapply current limit: %dA", int64(c.current))
c.resumeCommandTicks = 0
if err = c.MaxCurrent(int64(c.current)); err != nil {
c.log.ERROR.Println(err)
}
}
}

func (c *Easee) chargers() ([]easee.Charger, error) {
Expand Down Expand Up @@ -388,13 +395,19 @@ func (c *Easee) Enable(enable bool) error {
resp.Body.Close()
}

// resume/stop charger
action := easee.ChargePause
// resume
if enable {
action = easee.ChargeResume
var cmd easee.RestCommandResponse
uri := fmt.Sprintf("%s/chargers/%s/commands/resume_charging", easee.API, c.charger)
err := c.PostJSON(uri, &cmd)
naltatis marked this conversation as resolved.
Show resolved Hide resolved
if err == nil {
c.resumeCommandTicks = cmd.Ticks
}
return err
}

uri := fmt.Sprintf("%s/chargers/%s/commands/%s", easee.API, c.charger, action)
// pause
uri := fmt.Sprintf("%s/chargers/%s/commands/pause_charging", easee.API, c.charger)
_, err := c.Post(uri, request.JSONContent, nil)

return err
Expand Down
17 changes: 17 additions & 0 deletions charger/easee/signalr.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ type Observation struct {
Value string
}

type SignalRCommandResponse struct {
SerialNumber string
ID int
Timestamp time.Time
DeliveredAt time.Time
WasAccepted bool
ResultCode string
Comment string
Ticks int64
}

type RestCommandResponse struct {
Device string
CommandId int
Ticks int64
}

type DataType int

// https://github.com/Masterloop/Masterloop.Core.Types/blob/master/src/Masterloop.Core.Types/Base/DataType.cs
Expand Down
5 changes: 0 additions & 5 deletions charger/easee/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ package easee
// API is the Easee API endpoint
const API = "https://api.easee.cloud/api"

const (
ChargePause = "pause_charging"
ChargeResume = "resume_charging"
)

// charge mode definition
const (
ModeOffline int = 0
Expand Down
10 changes: 10 additions & 0 deletions util/request/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,13 @@ func (r *Helper) GetJSON(url string, res interface{}) error {
}
return err
}

// PostJSON executes HTTP POST request and decodes JSON response.
// It returns a StatusError on response codes other than HTTP 2xx.
func (r *Helper) PostJSON(url string, res interface{}) error {
naltatis marked this conversation as resolved.
Show resolved Hide resolved
req, err := New(http.MethodPost, url, nil, AcceptJSON)
if err == nil {
err = r.DoJSON(req, &res)
}
return err
}