| 1 | package kasa |
| 2 | |
| 3 | type KasaClientEmeterService struct { |
| 4 | c *KasaClient |
| 5 | ctx *KasaRequestContext |
| 6 | } |
| 7 | |
| 8 | type GetRealtimeRequest struct { |
| 9 | } |
| 10 | |
| 11 | type GetRealtimeResponse struct { |
| 12 | // some fw/hw may use the following keys |
| 13 | Current float64 `mapstructure:"current"` // unit is A |
| 14 | Voltage float64 `mapstructure:"voltage"` // unit is V |
| 15 | Power float64 `mapstructure:"power"` // unit is W |
| 16 | Total float64 `mapstructure:"total"` // unit is kWh |
| 17 | |
| 18 | // some may use these |
| 19 | CurrentmA float64 `mapstructure:"current_ma"` |
| 20 | VoltagemV float64 `mapstructure:"voltage_mv"` |
| 21 | PowermW float64 `mapstructure:"power_mw"` |
| 22 | TotalWh float64 `mapstructure:"total_wh"` |
| 23 | } |
| 24 | |
| 25 | func (r *GetRealtimeResponse) Normalize() { |
| 26 | if r.TotalWh != -1 { |
| 27 | r.Current = r.CurrentmA / 1000 |
| 28 | r.Voltage = r.VoltagemV / 1000 |
| 29 | r.Power = r.PowermW / 1000 |
| 30 | r.Total = r.TotalWh / 1000 |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func (s *KasaClientEmeterService) GetRealtime() (*GetRealtimeResponse, error) { |
| 35 | response := GetRealtimeResponse{ |
| 36 | TotalWh: -1, |
| 37 | } |
| 38 | err := s.c.RPC("emeter", "get_realtime", s.ctx, GetRealtimeRequest{}, &response) |
| 39 | |
| 40 | response.Normalize() |
| 41 | |
| 42 | return &response, err |
| 43 | } |