forked from evcc-io/evcc
/
pantabox.go
120 lines (94 loc) · 2.68 KB
/
pantabox.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
package charger
import (
"fmt"
"strings"
"github.com/connectorjs/evcm/api"
"github.com/connectorjs/evcm/util"
"github.com/connectorjs/evcm/util/request"
)
// Pantabox charger implementation
type Pantabox struct {
*request.Helper
uri string
}
func init() {
registry.Add("pantabox", NewPantaboxFromConfig)
}
// NewPantaboxFromConfig creates a Pantabox charger from generic config
func NewPantaboxFromConfig(other map[string]interface{}) (api.Charger, error) {
var cc struct {
URI string
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return NewPantabox(util.DefaultScheme(cc.URI, "http"))
}
// NewPantabox creates Pantabox charger
func NewPantabox(uri string) (*Pantabox, error) {
log := util.NewLogger("pantabox")
wb := &Pantabox{
Helper: request.NewHelper(log),
uri: util.DefaultScheme(strings.TrimRight(uri, "/"), "http") + "/api",
}
return wb, nil
}
// Status implements the api.Charger interface
func (wb *Pantabox) Status() (api.ChargeStatus, error) {
var res struct {
State string
}
if err := wb.GetJSON(wb.uri+"/charger/state", &res); err != nil {
return api.StatusNone, err
}
return api.ChargeStatusString(res.State)
}
// Enabled implements the api.Charger interface
func (wb *Pantabox) Enabled() (bool, error) {
var res struct {
Enabled int `json:",string"`
}
err := wb.GetJSON(wb.uri+"/charger/enabled", &res)
return res.Enabled > 0, err
}
// Enable implements the api.Charger interface
func (wb *Pantabox) Enable(enable bool) error {
resp, err := wb.Post(wb.uri+"/charger/enable", request.PlainContent, strings.NewReader(fmt.Sprintf("%t", enable)))
if err == nil {
defer resp.Body.Close()
if resp.StatusCode != 200 {
err = fmt.Errorf("status: %d", resp.StatusCode)
}
}
return err
}
// MaxCurrent implements the api.Charger interface
func (wb *Pantabox) MaxCurrent(current int64) error {
resp, err := wb.Post(wb.uri+"/charger/current", request.PlainContent, strings.NewReader(fmt.Sprintf("%d", current)))
if err == nil {
defer resp.Body.Close()
if resp.StatusCode != 200 {
err = fmt.Errorf("status: %d", resp.StatusCode)
}
}
return err
}
var _ api.Meter = (*Pantabox)(nil)
// CurrentPower implements the api.Meter interface
func (wb *Pantabox) CurrentPower() (float64, error) {
var res struct {
Power float64 `json:",string"`
}
err := wb.GetJSON(wb.uri+"/meter/power", &res)
return res.Power, err
}
var _ api.Diagnosis = (*Pantabox)(nil)
// Diagnose implements the api.Diagnosis interface
func (wb *Pantabox) Diagnose() {
var curr struct {
MaxCurrent int `json:",string"`
}
if err := wb.GetJSON(wb.uri+"/charger/maxcurrent", &curr); err == nil {
fmt.Printf("\tMax current:\t%dA\n", curr.MaxCurrent)
}
}