/
fronius-wattpilot.go
147 lines (115 loc) · 3.2 KB
/
fronius-wattpilot.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package charger
import (
"errors"
"fmt"
"time"
wattpilot "github.com/mabunixda/wattpilot"
"github.com/thommyho/robotui/api"
"github.com/thommyho/robotui/util"
)
// Wattpilot charger implementation
type Wattpilot struct {
api *wattpilot.Wattpilot
}
func init() {
registry.Add("wattpilot", NewWattpilotFromConfig)
}
// NewWattpilotFromConfig creates a wattpilot charger from generic config
func NewWattpilotFromConfig(other map[string]interface{}) (api.Charger, error) {
var cc struct {
URI string
Password string
Cache time.Duration
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if cc.URI == "" || cc.Password == "" {
return nil, errors.New("must have uri and password")
}
return NewWattpilot(cc.URI, cc.Password, cc.Cache)
}
// NewWattpilot creates Wattpilot charger
func NewWattpilot(uri, password string, cache time.Duration) (api.Charger, error) {
c := &Wattpilot{
api: wattpilot.New(uri, password),
}
if err := c.api.Connect(); err != nil {
return nil, err
}
return c, nil
}
// Status implements the api.Charger interface
func (c *Wattpilot) Status() (api.ChargeStatus, error) {
car, err := c.api.GetProperty("car")
if err != nil {
return api.StatusNone, err
}
switch car.(float64) {
case 1.0:
return api.StatusA, nil
case 2.0, 5.0:
return api.StatusC, nil
case 3.0, 4.0:
return api.StatusB, nil
default:
return api.StatusNone, fmt.Errorf("car unknown result: %d", car)
}
}
// Enabled implements the api.Charger interface
func (c *Wattpilot) Enabled() (bool, error) {
resp, err := c.api.GetProperty("alw")
if err != nil {
return false, err
}
return resp.(bool), nil
}
// Enable implements the api.Charger interface
func (c *Wattpilot) Enable(enable bool) error {
var forceState int
if !enable {
forceState = 1 // off
}
return c.api.SetProperty("frc", forceState)
}
// MaxCurrent implements the api.Charger interface
func (c *Wattpilot) MaxCurrent(current int64) error {
return c.api.SetCurrent(float64(current))
}
var _ api.Meter = (*Wattpilot)(nil)
// CurrentPower implements the api.Meter interface
func (c *Wattpilot) CurrentPower() (float64, error) {
return c.api.GetPower()
}
var _ api.ChargeRater = (*Wattpilot)(nil)
// ChargedEnergy implements the api.ChargeRater interface
func (c *Wattpilot) ChargedEnergy() (float64, error) {
resp, err := c.api.GetProperty("wh")
if err != nil {
return 0, err
}
return resp.(float64) / 1e3, err
}
var _ api.PhaseCurrents = (*Wattpilot)(nil)
// Currents implements the api.PhaseCurrents interface
func (c *Wattpilot) Currents() (float64, float64, float64, error) {
return c.api.GetCurrents()
}
var _ api.PhaseVoltages = (*Wattpilot)(nil)
// Currents implements the api.PhaseCurrents interface
func (c *Wattpilot) Voltages() (float64, float64, float64, error) {
return c.api.GetVoltages()
}
var _ api.Identifier = (*Wattpilot)(nil)
// Identify implements the api.Identifier interface
func (c *Wattpilot) Identify() (string, error) {
return c.api.GetRFID()
}
var _ api.PhaseSwitcher = (*Wattpilot)(nil)
// Phases1p3p implements the api.PhaseSwitcher interface
func (c *Wattpilot) Phases1p3p(phases int) error {
if phases == 3 {
phases = 2
}
return c.api.SetProperty("psm", phases)
}