-
Notifications
You must be signed in to change notification settings - Fork 0
/
powerwall.go
108 lines (96 loc) · 2.13 KB
/
powerwall.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
package main
// example of how you would build a sampler into the main package
//
// this example does NOT use the helper functions for dealing with structures,
// but it could
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/itrs-group/cordial/pkg/plugins"
"github.com/itrs-group/cordial/pkg/samplers"
)
func init() {
// geneos.EnableDebugLog()
}
type PowerwallSampler struct {
samplers.Samplers
pwurl string
}
func NewPW(s plugins.Connection, name string, group string) (*PowerwallSampler, error) {
c := new(PowerwallSampler)
c.Plugins = c
return c, c.New(s, name, group)
}
var pwcols = []string{
"meterName",
"last_communication_time",
"instant_power",
"instant_reactive_power",
"instant_apparent_power",
"frequency",
"energy_exported",
"energy_imported",
"instant_average_voltage",
"instant_total_current",
"i_a_current",
"i_b_current",
"i_c_current",
"timeout",
}
var pwrows = []string{
"site",
"battery",
"load",
"solar",
}
func (p *PowerwallSampler) InitSampler() (err error) {
pwurl, err := p.Parameter("POWERWALL_URL")
if err != nil {
return
}
p.Headline("powerwallURL", p.pwurl)
p.pwurl = pwurl + "/meters/aggregates"
return
}
func (p PowerwallSampler) DoSample() (err error) {
if p.pwurl == "" {
err = fmt.Errorf("No URL defined in sampler parameters (POWERWALL_URL)")
return
}
resp, err := http.Get(p.pwurl)
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(resp.Body)
var data map[string]map[string]interface{}
err = json.Unmarshal(body, &data)
// all numbers are float64 at this point, don't forget to reconvert
table := make([][]string, len(pwrows))
for row, rowname := range pwrows {
table[row] = make([]string, len(pwcols)+1)
table[row][0] = rowname
for col, colname := range pwcols[1:] {
dv := data[rowname][colname]
if dv == nil {
continue
}
var tv string
switch dv.(type) {
case string:
tv = dv.(string)
case float64:
tv = fmt.Sprintf("%.2f", dv.(float64))
default:
tv = fmt.Sprintf("%v", dv)
}
table[row][col+1] = tv
}
}
err = p.UpdateTable(pwcols, table...)
if err != nil {
log.Fatal(err)
}
return
}