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

TP-Link charger: add ChargedEnergy based on get_daystat cmd #932

Merged
merged 2 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 23 additions & 0 deletions internal/charger/tplink.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,29 @@ func (c *TPLink) CurrentPower() (float64, error) {
return power, nil
}

var _ api.ChargeRater = (*TPLink)(nil)

// ChargedEnergy implements the api.ChargeRater interface
func (c *TPLink) ChargedEnergy() (float64, error) {
var resp tplink.DayStatResponse
year, month, day := time.Now().Date()
cmd := fmt.Sprintf(`{"emeter":{"get_daystat":{"day":%v,"month":%v,"year":%v}}}`, day, int(month), year)
if err := c.execCmd(cmd, &resp); err != nil {
return 0, err
}

if err := resp.Emeter.GetDaystat.ErrCode; err != 0 {
return 0, fmt.Errorf("get_daystat error %d", err)
}

energy := resp.Emeter.GetDaystat.DayList[len(resp.Emeter.GetDaystat.DayList)-1].EnergyWh / 1000
if energy == 0 {
energy = resp.Emeter.GetDaystat.DayList[len(resp.Emeter.GetDaystat.DayList)-1].Energy
}

return energy, nil
}

// execCmd executes an TP-Link Smart Home Protocol command and provides the response
func (c *TPLink) execCmd(cmd string, res interface{}) error {
// encode command message
Expand Down
28 changes: 24 additions & 4 deletions internal/charger/tplink/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,41 @@ type SystemResponse struct {
} `json:"system"`
}

// EmeterResponse is the TP-Link plug/outlet api emeter response
// EmeterResponse is the TP-Link plug/outlet api emeter get_realtime response
type EmeterResponse struct {
Emeter struct {
GetRealtime struct {
// 1st plug generation E-Meter Response
// 1st plug generation E-Meter get_realtime Response
Current float64 `json:"current,omitempty"`
Voltage float64 `json:"voltage,omitempty"`
Power float64 `json:"power,omitempty"`
Total float64 `json:"total,omitempty"`
// 2nd plug generation E-Meter Response
// 2nd plug generation E-Meter get_realtime Response
CurrentMa float64 `json:"current_ma,omitempty"`
VoltageMv float64 `json:"voltage_mv,omitempty"`
PowerMw float64 `json:"power_mw,omitempty"`
TotalWh float64 `json:"total_wh,omitempty"`
ErrCode int `json:"err_code,omitempty"`
// Common E-Meter get_realtime Response
ErrCode int `json:"err_code,omitempty"`
} `json:"get_realtime"`
} `json:"emeter"`
}

// DayStatResponse is the TP-Link plug/outlet api emeter get_realtime get_daystat response
type DayStatResponse struct {
Emeter struct {
GetDaystat struct {
DayList []struct {
Year int `json:"year,omitempty"`
Month int `json:"month,omitempty"`
Day int `json:"day,omitempty"`
// 1st plug generation E-Meter get_daystat Response
Energy float64 `json:"energy,omitempty"`
// 2nd plug generation E-Meter get_daystat Response
EnergyWh float64 `json:"energy_wh,omitempty"`
} `json:"day_list"`
// Common E-Meter get_daystat Response
ErrCode int `json:"err_code,omitempty"`
} `json:"get_daystat"`
} `json:"emeter"`
}
49 changes: 46 additions & 3 deletions internal/charger/tplink/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestUnmarshalTPLinkSystemResponses(t *testing.T) {
t.Error("GetSysinfo.Feature")
}

// Test 1st emeter generation response
// Test 1st emeter generation get_realtime response
var emeresp EmeterResponse
jsonstr = `{"emeter":{"get_realtime":{"current":0.033759,"voltage":234.824322,"power":3.121391,"total":0.015000,"err_code":0}}}`
if err := json.Unmarshal([]byte(jsonstr), &emeresp); err != nil {
Expand All @@ -63,9 +63,9 @@ func TestUnmarshalTPLinkSystemResponses(t *testing.T) {
t.Error("GetRealtime.ErrCode")
}

// Test 2nd emeter generation response
// Test 2nd emeter generation get_realtime response
var emeresp2 EmeterResponse
jsonstr = ` {"emeter":{"get_realtime":{"voltage_mv":237119,"current_ma":218,"power_mw":31259,"total_wh":107,"err_code":0}}}`
jsonstr = `{"emeter":{"get_realtime":{"voltage_mv":237119,"current_ma":218,"power_mw":31259,"total_wh":107,"err_code":0}}}`
if err := json.Unmarshal([]byte(jsonstr), &emeresp2); err != nil {
t.Error(err)
}
Expand All @@ -85,4 +85,47 @@ func TestUnmarshalTPLinkSystemResponses(t *testing.T) {
t.Error("GetRealtime.ErrCode")
}

// Test 1st emeter generation get_daystat response
var dstatresp DayStatResponse
jsonstr = `{"emeter":{"get_daystat":{"day_list":[{"year":2021,"month":4,"day":22,"energy":0},{"year":2021,"month":4,"day":23,"energy":0.016000},{"year":2021,"month":4,"day":24,"energy":0.020000},{"year":2021,"month":4,"day":25,"energy":0.005000},{"year":2021,"month":4,"day":26,"energy":0.245000}],"err_code":0}}}`
if err := json.Unmarshal([]byte(jsonstr), &dstatresp); err != nil {
t.Error(err)
}
if dstatresp.Emeter.GetDaystat.DayList[len(dstatresp.Emeter.GetDaystat.DayList)-1].Year != 2021 {
t.Error("GetDaystat.DayList[last].Year")
}
if dstatresp.Emeter.GetDaystat.DayList[len(dstatresp.Emeter.GetDaystat.DayList)-1].Month != 4 {
t.Error("GetDaystat.DayList[last].Month")
}
if dstatresp.Emeter.GetDaystat.DayList[len(dstatresp.Emeter.GetDaystat.DayList)-1].Day != 26 {
t.Error("GetDaystat.DayList[last].Day")
}
if dstatresp.Emeter.GetDaystat.DayList[len(dstatresp.Emeter.GetDaystat.DayList)-1].Energy != 0.245 {
t.Error("GetDaystat.DayList[last].Energy")
}
if dstatresp.Emeter.GetDaystat.ErrCode != 0 {
t.Error("GetDaystat.ErrCode")
}

// Test 2nd emeter generation get_daystat response
var dstatresp2 DayStatResponse
jsonstr = `{"emeter":{"get_daystat":{"day_list":[{"year":2021,"month":4,"day":18,"energy_wh":3},{"year":2021,"month":4,"day":19,"energy_wh":0},{"year":2021,"month":4,"day":20,"energy_wh":2029},{"year":2021,"month":4,"day":21,"energy_wh":1201},{"year":2021,"month":4,"day":22,"energy_wh":0},{"year":2021,"month":4,"day":23,"energy_wh":1059}],"err_code":0}}}`
if err := json.Unmarshal([]byte(jsonstr), &dstatresp2); err != nil {
t.Error(err)
}
if dstatresp2.Emeter.GetDaystat.DayList[len(dstatresp2.Emeter.GetDaystat.DayList)-1].Year != 2021 {
t.Error("GetDaystat.DayList[last].Year")
}
if dstatresp2.Emeter.GetDaystat.DayList[len(dstatresp2.Emeter.GetDaystat.DayList)-1].Month != 4 {
t.Error("GetDaystat.DayList[last].Month")
}
if dstatresp2.Emeter.GetDaystat.DayList[len(dstatresp2.Emeter.GetDaystat.DayList)-1].Day != 23 {
t.Error("GetDaystat.DayList[last].Day")
}
if dstatresp2.Emeter.GetDaystat.DayList[len(dstatresp2.Emeter.GetDaystat.DayList)-1].EnergyWh != 1059 {
t.Error("GetDaystat.DayList[last].EnergyWh")
}
if dstatresp2.Emeter.GetDaystat.ErrCode != 0 {
t.Error("GetDaystat.ErrCode")
}
}