-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
127 lines (104 loc) · 3.51 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"time"
"github.com/fdawg4l/tesla-gateway-getter/pkg/build"
"github.com/fdawg4l/tesla-gateway-getter/pkg/gateway"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/spf13/viper"
)
type Config struct {
Email string `mapstructure: "TESLA_EMAIL"`
Password string `mapstructure: "TESLA_PASSWORD"`
Gateway string `mapstructure: "TESLA_GATEWAY"`
InfluxHost string `mapstructure: "TESLA_INFLUXHOST"`
InfluxBucket string `mapstructure: "TESLA_INFLUXBUCKET"`
InfluxOrg string `mapstructure: "TESLA_INFLUXORG"`
InfluxToken string `mapstructure: "TESLA_INFLUXTOKEN"`
Interval uint `mapstructure: "TESLA_INTERVAL"`
// ServerPort is for the local REST endpoint to reflect values
// retrived from the TEG.
ServerPort uint `mapstructure: "TESLA_SERVERPORT"`
}
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
config := new(Config)
viper.SetEnvPrefix("TESLA")
viper.BindEnv("gateway")
viper.BindEnv("password")
viper.BindEnv("email")
viper.BindEnv("influxhost")
viper.BindEnv("influxbucket")
viper.BindEnv("influxorg")
viper.BindEnv("influxtoken")
viper.BindEnv("interval")
viper.BindEnv("port")
viper.AutomaticEnv()
if err := viper.Unmarshal(config); err != nil {
log.Fatalf("error: %s", err.Error())
}
interval := time.Duration(config.Interval)
if interval == 0 {
interval = 30
}
if config.ServerPort == 0 {
config.ServerPort = 80
}
log.Printf("%s -- build %s", os.Args[0], build.GitCommitID)
log.Printf("Using influx host=%s bucket=%s every=%dseconds reflector port=%d", config.InfluxHost, config.InfluxBucket, interval, config.ServerPort)
reflector := gateway.NewReflector()
http.Handle("/", reflector)
go func() {
log.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", config.ServerPort), nil))
}()
// Create a new client using an InfluxDB server base URL and an authentication token
influxClient := influxdb2.NewClient(config.InfluxHost, config.InfluxToken)
defer influxClient.Close()
// Use blocking write client for writes to desired bucket
writeAPI := influxClient.WriteAPIBlocking(config.InfluxOrg, config.InfluxBucket)
u, err := url.Parse(config.Gateway)
if err != nil {
log.Fatalf("error connecting to gateway: %s", err.Error())
}
log.Printf("Using gateway=%s", u.String())
gatewayClient, err := gateway.NewClient(u, config.Email, config.Password)
if err != nil {
log.Fatalf("error connecting to gateway: %s", err.Error())
}
t := time.NewTicker(interval * time.Second)
defer t.Stop()
for {
select {
case <-ctx.Done():
log.Printf("exiting")
return
case <-t.C:
agg, err := gatewayClient.Aggregates()
if err != nil {
log.Fatalf("error getting aggregates: %s", err.Error())
}
soe, err := gatewayClient.SOE()
if err != nil {
log.Fatalf("error getting soe: %s", err.Error())
}
// Create point using full params constructor
// write point immediately
p := influxdb2.NewPoint("http", nil, agg.Values, time.Now())
if err := writeAPI.WritePoint(context.Background(), p); err != nil {
log.Fatalf("error getting writing aggregates to influx %s", err.Error())
}
p = influxdb2.NewPoint("http", nil, map[string]interface{}{"percentage": soe.Percentage}, time.Now())
if err := writeAPI.WritePoint(context.Background(), p); err != nil {
log.Fatalf("error getting writing soe to influx %s", err.Error())
}
reflector.Reflect(agg.Values, soe.Percentage)
}
}
}