forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscovergy.go
109 lines (90 loc) Β· 2.55 KB
/
discovergy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package meter
import (
"fmt"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/meter/discovergy"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/transport"
"github.com/samber/lo"
)
func init() {
registry.Add("discovergy", NewDiscovergyFromConfig)
}
type Discovergy struct {
dataG func() (discovergy.Reading, error)
scale float64
}
// NewDiscovergyFromConfig creates a new configurable meter
func NewDiscovergyFromConfig(other map[string]interface{}) (api.Meter, error) {
cc := struct {
User string
Password string
Meter string
Scale float64
Cache time.Duration
Timeout time.Duration
}{
Scale: 1,
Cache: time.Second,
Timeout: time.Minute,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if cc.User == "" || cc.Password == "" {
return nil, api.ErrMissingCredentials
}
basicAuth := transport.BasicAuthHeader(cc.User, cc.Password)
log := util.NewLogger("discgy").Redact(cc.User, cc.Password, cc.Meter, basicAuth)
client := request.NewHelper(log)
client.Transport = transport.BasicAuth(cc.User, cc.Password, client.Transport)
var meters []discovergy.Meter
if err := client.GetJSON(fmt.Sprintf("%s/meters", discovergy.API), &meters); err != nil {
return nil, err
}
var meterID string
if cc.Meter != "" {
for _, m := range meters {
if matchesIdentifier(cc.Meter, m) {
meterID = m.MeterID
break
}
}
} else if len(meters) == 1 {
meterID = meters[0].MeterID
}
if meterID == "" {
return nil, fmt.Errorf("could not determine meter id: %v", lo.Map(meters, func(m discovergy.Meter, _ int) string {
return m.FullSerialNumber
}))
}
dataG := provider.Cached(func() (discovergy.Reading, error) {
var res discovergy.Reading
uri := fmt.Sprintf("%s/last_reading?meterId=%s", discovergy.API, meterID)
err := client.GetJSON(uri, &res)
if err == nil && time.Since(time.UnixMilli(res.Time)) > cc.Timeout {
err = api.ErrTimeout
}
return res, err
}, cc.Cache)
m := &Discovergy{
dataG: dataG,
scale: cc.Scale,
}
return m, nil
}
func matchesIdentifier(id string, m discovergy.Meter) bool {
return id == m.MeterID || id == m.SerialNumber || id == m.FullSerialNumber
}
func (m *Discovergy) CurrentPower() (float64, error) {
res, err := m.dataG()
return m.scale * float64(res.Values.Power) / 1e3, err
}
var _ api.MeterEnergy = (*Discovergy)(nil)
func (m *Discovergy) TotalEnergy() (float64, error) {
res, err := m.dataG()
return float64(res.Values.Energy) / 1e10, err
}