/
main.go
173 lines (155 loc) · 5.21 KB
/
main.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
// Copyright 2015 Tamás Gulácsi
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package main of fronius gets the data from Solar.Web
package main
import (
"fmt"
stdlog "log"
"net/http"
"net/http/cookiejar"
"os"
"strings"
"sync"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/spf13/cobra"
)
var logger = log.NewLogfmtLogger(os.Stderr)
type config struct {
SystemID string
BaseURL, DataURL, LogonURL string
CookieJarPath string
dateFormat string
initURLs sync.Once
jar *cookiejar.Jar
jarMu sync.Mutex
*http.Client
initClient sync.Once
Logger log.Logger
}
func main() {
stdlog.SetOutput(log.NewStdlibAdapter(logger))
var (
conf = config{
CookieJarPath: "fronius.cookies",
BaseURL: "https://www.solarweb.com",
LogonURL: "{{BASE}}/Account/GuestLogOn?pvSystemId={{pvSystemID}}",
DataURL: "{{BASE}}/NewCharts/GetDetailData/{{pvSystemID}}/00000000-0000-0000-0000-000000000000/Day/{{2006/1/2}}",
}
)
dumpCmd := &cobra.Command{
Use: "dump",
Short: "dump data points from the given days (today is the default)",
Run: func(_ *cobra.Command, args []string) {
for data := range conf.dumpFromArgs(args) {
for k, points := range data {
for _, p := range points {
fmt.Fprintf(os.Stdout, "%q;%q;%.3f\n", k, p.Time, p.Energy)
}
}
}
},
}
var (
influxDB = "http://localhost:8086"
database = "fronius"
retentionPolicy = "default"
servePath = "/solarapi/v1/current/"
)
serveCmd := &cobra.Command{
Use: "serve",
Short: "accept push from the fronius datalogger",
Run: func(_ *cobra.Command, args []string) {
con, err := newInfluxClient(influxDB, database, retentionPolicy, logger)
if err != nil {
level.Error(logger).Log("msg", "influx connection", "error", err)
os.Exit(1)
}
http.Handle(servePath, solarAPIAccept{con})
addr := ":15015"
if len(args) > 0 {
addr = args[0]
}
level.Info(logger).Log("msg", "Start listening", "address", addr, "path", servePath)
http.ListenAndServe(addr, nil)
},
}
f := serveCmd.Flags()
f.StringVar(&servePath, "serve.path", servePath, "HTTP endpoint to publish")
f.StringVar(&influxDB, "server", influxDB, "influx server to connect to")
f.StringVar(&database, "database", database, "influx database to insert data into")
f.StringVar(&retentionPolicy, "retention", retentionPolicy, "retention policy to use")
mainCmd := &cobra.Command{
Use: "fronius",
Run: func(_ *cobra.Command, args []string) {
dumpCmd.Run(dumpCmd, args)
},
}
mainCmd.AddCommand(dumpCmd, serveCmd)
pflags := mainCmd.PersistentFlags()
pflags.StringVar(&conf.CookieJarPath, "cookiejar", conf.CookieJarPath, "path to the cookie storage file")
pflags.StringVar(&conf.BaseURL, "base", conf.BaseURL, "Solar.Web's base URL")
pflags.StringVar(&conf.LogonURL, "logon", conf.LogonURL, "Logon URL")
pflags.StringVar(&conf.DataURL, "data", conf.DataURL,
"URL of the detail data; the Go reference date (2006-01-02) will be replaced with the current date, in the given format.")
influxCmd := &cobra.Command{
Use: "influx",
Short: "insert data into the InfluxDB specified with the --server flag",
Run: func(_ *cobra.Command, args []string) {
ic, err := newInfluxClient(influxDB, database, retentionPolicy, logger)
if err != nil {
level.Error(logger).Log("msg", "influx connection", "error", err)
os.Exit(1)
}
points := make([]dataPoint, 0, 512)
for data := range conf.dumpFromArgs(args) {
for k, dps := range data {
for _, p := range dps {
points = append(points,
dataPoint{Name: k, Value: p.Energy, Time: p.Time, Unit: "kWh"})
}
}
}
if err := ic.Put("fronius energy", points...); err != nil {
level.Error(logger).Log("msg", "write batch to db", "error", err)
os.Exit(2)
}
},
}
f = influxCmd.Flags()
f.StringVar(&influxDB, "server", influxDB, "influx server to connect to")
f.StringVar(&database, "database", database, "influx database to insert data into")
f.StringVar(&retentionPolicy, "retention", retentionPolicy, "retention policy to use")
mainCmd.AddCommand(influxCmd)
if _, _, err := mainCmd.Find(os.Args[1:]); err != nil && strings.HasPrefix(err.Error(), "unknown command") {
mainCmd.SetArgs(append([]string{"dump"}, os.Args[1:]...))
}
mainCmd.Execute()
}
func (conf *config) dumpFromArgs(args []string) chan Series {
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "first argument must be the pvSystemID!")
os.Exit(1)
}
conf.SystemID = args[0]
c := make(chan Series, 1)
go func() {
if err := conf.getDaysSeries(c, args[1:]...); err != nil {
level.Error(logger).Log("getDaysSeries", "args", args, "error", err)
os.Exit(2)
}
}()
return c
}