This repository has been archived by the owner on Oct 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiotawatt.go
230 lines (193 loc) · 5.1 KB
/
iotawatt.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package iotawatt
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/glasslabs/looking-glass/module/types"
)
const apiQueryPath = "query"
// Config is the module configuration.
type Config struct {
URL string `yaml:"url"`
Inputs []string `yaml:"inputs"`
Interval time.Duration `yaml:"interval"`
}
// NewConfig creates a default configuration for the module.
func NewConfig() *Config {
return &Config{
Interval: time.Minute,
}
}
// Module is a clock module.
type Module struct {
name string
path string
cfg *Config
ui types.UI
log types.Logger
baseURL *url.URL
qryVals url.Values
done chan struct{}
}
// New returns a running clock module.
func New(_ context.Context, cfg *Config, info types.Info, ui types.UI) (io.Closer, error) {
qryValues := url.Values{
"format": []string{"json"},
"resolution": []string{"low"},
"missing": []string{"skip"},
"begin": []string{"s-1h"},
"end": []string{"s"},
"group": []string{"auto"},
}
inputs := append([]string{"time.utc.unix"}, cfg.Inputs...)
qryValues.Set("select", "["+strings.Join(inputs, ",")+"]")
u, err := url.Parse(cfg.URL)
if err != nil {
return nil, fmt.Errorf("could not parse url: %w", err)
}
m := &Module{
name: info.Name,
path: info.Path,
cfg: cfg,
ui: ui,
log: info.Log,
baseURL: u,
qryVals: qryValues,
done: make(chan struct{}),
}
if err = m.loadCSS("assets/style.css"); err != nil {
return nil, err
}
if err = m.renderHTML("assets/index.html"); err != nil {
return nil, err
}
go m.run()
return m, nil
}
type series struct {
Data [][]float64 `json:"data"`
}
func (m *Module) run() {
c := http.Client{}
ticker := time.NewTicker(m.cfg.Interval)
defer ticker.Stop()
for {
select {
case <-m.done:
return
case <-ticker.C:
}
var raw [][]float64
if err := m.request(c, &raw); err != nil {
m.log.Error("Could not get current IoTaWatt data", "module", "iotawatt", "id", m.name, "error", err.Error())
continue
}
l := len(m.cfg.Inputs)
rl := len(raw)
var current float64
for i := 1; i <= l; i++ {
current += raw[rl-1][i]
}
series := make([]series, l)
for _, row := range raw {
if int(row[0])%20 != 0 {
continue
}
for j := 1; j <= l; j++ {
series[j-1].Data = append(series[j-1].Data, []float64{row[0], row[j]})
}
}
if err := m.renderCurrent(current); err != nil {
m.log.Error("Could not update current", "module", "iotawatt", "id", m.name, "error", err.Error())
}
b, err := json.Marshal(series)
if err != nil {
m.log.Error("could not encode data", "module", "iotawatt", "id", m.name, "error", err.Error())
continue
}
if _, err = m.ui.Eval("iotaWattSeries = %s", string(b)); err != nil {
m.log.Error("Could not update series", "module", "iotawatt", "id", m.name, "error", err.Error())
}
if _, err = m.ui.Eval("iotaWattChart.update({series: iotaWattSeries},true,true)"); err != nil {
m.log.Error("Could not update chart", "module", "iotawatt", "id", m.name, "error", err.Error())
}
}
}
func (m *Module) loadCSS(path string) error {
css, err := os.ReadFile(filepath.Clean(filepath.Join(m.path, path)))
if err != nil {
return fmt.Errorf("iotawatt: could not read css: %w", err)
}
return m.ui.LoadCSS(string(css))
}
func (m *Module) renderHTML(path string) error {
html, err := os.ReadFile(filepath.Clean(filepath.Join(m.path, path)))
if err != nil {
return fmt.Errorf("iotawatt: could not read html: %w", err)
}
if err = m.ui.LoadHTML(string(html)); err != nil {
return fmt.Errorf("iotawatt: could not load html: %w", err)
}
_, err = m.ui.Eval("invokeModuleScripts(%q)", m.name)
return err
}
func (m *Module) renderCurrent(watt float64) error {
const docSelector = "document.querySelector('#%s .current')"
unit := "W"
removeClass := "kW"
if watt > 100 {
unit = "kW"
removeClass = "W"
watt /= 1000
}
w := int(watt)
d := int(watt*10) - (w * 10)
ws := strconv.Itoa(w)
ds := strconv.Itoa(d)
if _, err := m.ui.Eval(docSelector+".innerHTML = '%s<sel>.%s %s</sel>'", m.name, ws, ds, unit); err != nil {
return err
}
_, err := m.ui.Eval(docSelector+".classList.remove('%s')", m.name, removeClass)
if err != nil {
return err
}
_, err = m.ui.Eval(docSelector+".classList.add('%s')", m.name, unit)
return err
}
func (m *Module) request(c http.Client, v interface{}) error {
u, err := m.baseURL.Parse(apiQueryPath)
if err != nil {
return fmt.Errorf("could not parse url: %w", err)
}
u.RawQuery = m.qryVals.Encode()
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return fmt.Errorf("could create request: %w", err)
}
resp, err := c.Do(req)
if err != nil {
return fmt.Errorf("could not parse url: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != 200 {
return errors.New("expected status code")
}
if err = json.NewDecoder(resp.Body).Decode(v); err != nil {
return fmt.Errorf("could not parse data: %w", err)
}
return nil
}
// Close stops and closes the module.
func (m *Module) Close() error {
close(m.done)
return nil
}