-
Notifications
You must be signed in to change notification settings - Fork 1
/
sungrow.go
93 lines (80 loc) · 2.17 KB
/
sungrow.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
package main
import (
"flag"
"fmt"
"github.com/nItroTools/sungrow-go/ws"
"log"
"net"
"strings"
"time"
)
type inverter struct {
ws *ws.WS
ipS string
ip net.IP
port int
path string
data string
separator string
types []string
}
var inv *inverter
func main() {
inv = &inverter{}
// Flags
flags()
// Connect to inverter
inv.ws = ws.NewWS(inv.ip, inv.port, inv.path)
if err := inv.ws.Connect(); err != nil {
log.Fatalln(err)
}
defer inv.ws.Close()
// Output timestamp row
fmt.Printf("%s%s%s%s%s\n", "time", inv.separator, time.Now().Format(time.RFC3339), inv.separator, "RFC3339")
// Fetch values from inverter
for _, t := range inv.types {
switch t {
case "pv":
_ = inv.ws.Pv(pvKeys, inv.separator)
break
case "battery":
_ = inv.ws.Battery(batteryKeys, inv.separator)
break
}
}
}
// flags defines, parses and validates command-line flags from os.Args[1:]
func flags() {
ipS := flag.String("ip", "", "IP address of the Sungrow inverter")
port := flag.Int("port", 8082, "WebSocket port of the Sungrow inverter")
path := flag.String("path", "/ws/home/overview", "Server path from where data is requested")
data := flag.String("data", "pv,battery", "Select the data to be requested comma separated.\nPossible values are \"pv\" and \"battery\"")
separator := flag.String("separator", ",", "Output data separator")
flag.Parse()
inv.ipS = *ipS
inv.port = *port
inv.path = *path
inv.data = *data
inv.separator = *separator
// Validate flags
flagsValidate()
}
// flagsValidate validates all flags
func flagsValidate() {
if inv.ip = net.ParseIP(inv.ipS); inv.ip == nil {
log.Fatalln("Required parameter 'ip' not set or invalid ip address!\n'sungrow-go -help' lists available parameters.")
}
inv.types = strings.Split(inv.data, ",")
if len(inv.types) < 1 {
log.Fatalln("Required parameter 'data' not set or invalid value!\n'sungrow-go -help' lists available parameters and values.")
}
for _, t := range inv.types {
switch t {
case "pv":
case "battery":
break
default:
log.Fatalf("Invalid value \"%s\" for parameter 'data'!\n'sungrow-go -help' lists available parameters and values.\n", t)
}
}
}