forked from 42wim/plugctl
/
plug.go
385 lines (344 loc) · 9.21 KB
/
plug.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package main
import (
"bytes"
"compress/gzip"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"log"
"net"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
)
type plug struct {
device string
credentials string
csvfile string
latestEntry []byte
buffer bytes.Buffer
delay int
}
// plug methods
func (p *plug) exec(command string) string {
url := "http://" + p.credentials + "@" + p.device + plugURI
url = url + command
_, err := http.Get(url)
if err != nil {
log.Fatal("connection failed")
}
return parseResult(p)
}
func (p *plug) enable() {
fmt.Print("enabling plug..")
result := p.exec(plugEnable)
printResultSuccess(result)
}
func (p *plug) disable() {
fmt.Print("disabling plug..")
result := p.exec(plugDisable)
printResultSuccess(result)
}
func (p *plug) uptime() {
result := p.exec(plugUptime)
result = strings.Replace(result, "||", "", -1)
fmt.Println(result)
}
func (p *plug) reboot() {
fmt.Println("rebooting.")
p.exec(plugReboot)
}
func (p *plug) info(info string) (string, error) {
textarea := p.exec(plugInfo + info)
re := regexp.MustCompile("01(I|V|W|E)[0-9]+ 0*([0-9]+)")
result := re.FindStringSubmatch(textarea)
// if we don't have 2 matches something is wrong
if len(result) > 2 {
return string(result[2]), nil
} else {
return "", errors.New("info not found")
}
}
func (p *plug) toggle(reallytoggle bool) error {
status, err := p.status()
if err != nil {
return errors.New("Can't get power status")
}
if status == 1 {
fmt.Println("Power is on")
if reallytoggle {
p.disable()
}
} else {
fmt.Println("Power is off")
if reallytoggle {
p.enable()
}
}
return nil
}
func (p *plug) status() (int, error) {
result, err := p.info("W")
if err != nil {
return -1, err
}
resint, _ := strconv.Atoi(result)
if resint > 100 {
return 1, nil
} else {
return 0, nil
}
}
func (p *plug) infofull() string {
resultStr := ""
conn, err := p.DialTimeout("tcp", p.device, time.Duration(time.Second*3))
if err != nil {
log.Fatal("can't connect")
}
// get statistics from device and cleanup
status := sendln(conn, plugGetInfoStats, '#')
status = strings.Replace(status, plugGetInfoStats+"\r\n", "", 1)
status = strings.Replace(status, "#", "", 1)
// split up the 4 results a newline
results := strings.SplitN(status, "\r\n", 4)
re := regexp.MustCompile("01(I|V|W|E)[0-9]+ 0*([0-9]+)")
// for each GetInfo result, do a regexp match, adjust value and create a CSV record
for i, result := range results {
match := re.FindStringSubmatch(result)
value := "0"
// check if we got the right size of slice
if len(match) == 3 {
value = match[2]
}
temp, _ := strconv.ParseFloat(value, 32)
switch i {
case 0:
// mAmp/10 -> Amp
value = strconv.FormatFloat(temp/10000, 'f', 2, 32)
value = value + " Ampere - "
// centiWatt -> Watt
case 1:
value = strconv.FormatFloat(temp/100, 'f', 2, 32)
value = value + " Watt - "
// mWatt/h -> Watt/h
case 2:
value = strconv.FormatFloat(temp/1000, 'f', 2, 32)
value = value + " Watt/hour - "
// mVolt -> Volt
case 3:
value = strconv.FormatFloat(temp/1000, 'f', 2, 32)
value = value + " Volt"
}
resultStr = resultStr + value
}
return resultStr
}
func (p *plug) disableAP() {
fmt.Print("disabling AP...")
p.exec(plugDisableAP)
result := p.exec(plugIfconfig)
if strings.Contains(result, "ra0") {
fmt.Println("failed")
} else {
fmt.Println("success")
}
result = p.exec(plugGetAP)
fmt.Print("saving state...")
if result == plugResultAPundef || result == plugResultAPon {
_ = p.exec(plugSetAP + "+0")
result = p.exec(plugGetAP)
if result == plugResultAPoff {
fmt.Println("success")
} else {
fmt.Println("failed")
}
} else {
fmt.Println("already set")
}
}
func (p *plug) enableAP() {
fmt.Print("enabling AP...")
p.exec(plugEnableAP)
result := p.exec(plugIfconfig)
if strings.Contains(result, "ra0") {
fmt.Println("success")
} else {
fmt.Println("failed")
}
result = p.exec(plugGetAP)
fmt.Print("saving state...")
if result == plugResultAPundef || result == plugResultAPoff {
_ = p.exec(plugSetAP + "+1")
result = p.exec(plugGetAP)
if result == plugResultAPon {
fmt.Println("success")
} else {
fmt.Println("failed")
}
} else {
fmt.Println("already set")
}
}
func (p *plug) disableCloud() {
fmt.Println("disabling Cloud Access...")
for _, address := range plugDisableCloudAddresses {
fmt.Print("blocking " + address + " ...")
p.exec(plugRoute + "+add+" + address + "+dev+lo")
result := p.exec(plugRoute)
if strings.Contains(result, address) {
fmt.Println("success")
} else {
fmt.Println("failed")
}
}
}
func (p *plug) enableCloud() {
fmt.Println("enabling Cloud Access...")
for _, address := range plugDisableCloudAddresses {
fmt.Print("unblocking " + address + " ...")
p.exec(plugRoute + "+del+" + address + "+dev+lo")
result := p.exec(plugRoute)
if strings.Contains(result, address) {
fmt.Println("failed")
} else {
fmt.Println("success")
}
}
}
func (p *plug) raw(command string) {
fmt.Println("executing command:", command)
command = strings.Replace(command, " ", "+", -1)
result := p.exec(command)
result = strings.Replace(result, "||", "\n", -1)
fmt.Println(result)
}
func (p *plug) DialTimeout(network, address string, timeout time.Duration) (net.Conn, error) {
conn, err := net.DialTimeout(network, address, timeout)
s := strings.Split(p.credentials, ":")
login, pass := s[0], s[1]
if err != nil {
return nil, err
}
sendln(conn, "", '\n')
sendln(conn, login, '\n')
sendln(conn, pass, '#')
return conn, err
}
// use telnet connection
func (p *plug) rawt(command string) {
conn, err := p.DialTimeout("tcp", p.device, time.Duration(time.Second*30))
if err != nil {
log.Fatal("can't connect")
}
status := sendln(conn, command, '#')
status = strings.Replace(status, command+"\r\n", "", 1)
status = strings.Replace(status, "#", "", 1)
fmt.Print(status)
}
func (p *plug) daemon() {
var diskwriter *csv.Writer
var gzipwriter *gzip.Writer
fmt.Println("starting foreground daemon ;-)")
// write csv from disk into the buffer
fmt.Println("loading history (" + p.csvfile + ")")
p.buffer.Write(readcsv(p.csvfile))
// create/append the csvfile on disk
csvfile, err := os.OpenFile(p.csvfile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
log.Fatal("Error:", err)
}
defer csvfile.Close()
// create a bufferwriter (appends to csv already in p.buffer)
bufferwriter := csv.NewWriter(&p.buffer)
// compressed or not
if strings.Contains(p.csvfile, ".gz") {
gzipwriter, _ = gzip.NewWriterLevel(csvfile, gzip.BestCompression)
defer gzipwriter.Close()
// wrap csv around gzipwriter
diskwriter = csv.NewWriter(gzipwriter)
} else {
// create a diskwriter (appends to csv on disk)
diskwriter = csv.NewWriter(csvfile)
}
// connect via telnet to the device and login
conn, err := p.DialTimeout("tcp", p.device, time.Duration(time.Second*30))
if err != nil {
log.Fatal("can't connect")
}
// create http handlers
http.HandleFunc("/quit", webQuitHandler(diskwriter, gzipwriter, csvfile))
http.HandleFunc("/history", webHistoryHandler)
http.HandleFunc("/stream", webStreamHandler)
http.HandleFunc("/read.csv", webReadCsvHandler(p))
http.HandleFunc("/read.json", webReadJsonHandler(p))
// needed for occasionally flushing on a newline
recordcount := 0
// start infinite polling loop
for {
// measure how long it takes
start := time.Now()
// specify correct format for dygraph
record := []string{start.Format("2006/01/02 15:04:05")}
// get statistics from device and cleanup
status := sendln(conn, plugGetInfoStats, '#')
status = strings.Replace(status, plugGetInfoStats+"\r\n", "", 1)
status = strings.Replace(status, "#", "", 1)
// split up the 4 results a newline
results := strings.SplitN(status, "\r\n", 4)
re := regexp.MustCompile("01(I|V|W|E)[0-9]+ 0*([0-9]+)")
// for each GetInfo result, do a regexp match, adjust value and create a CSV record
for i, result := range results {
match := re.FindStringSubmatch(result)
value := "0"
// check if we got the right size of slice
if len(match) == 3 {
value = match[2]
}
temp, _ := strconv.ParseFloat(value, 32)
switch i {
case 0:
// mAmp/10 -> Amp
value = strconv.FormatFloat(temp/10000, 'f', 2, 32)
// centiWatt -> Watt
case 1:
value = strconv.FormatFloat(temp/100, 'f', 2, 32)
// mWatt/h -> Watt/h | mVolt -> Volt
case 2, 3:
value = strconv.FormatFloat(temp/1000, 'f', 2, 32)
}
record = append(record, value)
recordcount += 1
}
// latestentry is needed in JSON for the realtime streaming
p.latestEntry, _ = json.Marshal(record)
// write the record to disk
err := diskwriter.Write(record)
if err != nil {
fmt.Println("Error:", err)
}
// write the record to buffer (in memory)
err = bufferwriter.Write(record)
if err != nil {
fmt.Println("Error:", err)
}
// flush disk every 25 records
if recordcount%100 == 0 {
diskwriter.Flush()
if strings.Contains(p.csvfile, ".gz") {
gzipwriter.Flush()
}
}
// flush memory immediately
bufferwriter.Flush()
if debug {
fmt.Print(record)
fmt.Println(" took", time.Since(start))
}
// sleep the right amount of time
time.Sleep(time.Second*time.Duration(p.delay) - time.Since(start))
}
}