This repository was archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpolly.go
More file actions
67 lines (56 loc) · 1.71 KB
/
polly.go
File metadata and controls
67 lines (56 loc) · 1.71 KB
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
package polly
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/influxdata/influxdb-client-go/v2"
"github.com/influxdata/influxdb-client-go/v2/api"
)
// InfluxAsyncGet polls the Tesla Gen3 Wall Connector and writes results to
// InfluxDB with an aync, non-blocking client you supply. You must also
// supply the IP of the wall conenctor.
func InfluxAsyncGet(writeAPI *api.WriteAPI, wcIP string) {
client := *writeAPI
var data map[string]interface{}
resp, err := http.Get(fmt.Sprintf("http://%s/api/1/vitals", wcIP))
if err != nil {
fmt.Println("error - during GET of hpwc. Do you have the right IP?")
return
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &data)
// Output a dot (.) for every successful GET against the Wall Connector
// This helps people like me who need to see something to know it works
fmt.Printf(".")
p := influxdb2.NewPoint(
"hpwc",
map[string]string{
"product": "Gen3 HPWC",
"vendor": "Tesla",
"location": "Garage",
},
data,
time.Now())
client.WritePoint(p)
}
// Execute simply runs the totality of polly in your program. It is
// recommended you run this as a goroutine so your program can do
// other things.
func Execute() {
hpwcIP := os.Getenv("HPWC_IP")
influxIP := os.Getenv("INFLUX_IP")
client := influxdb2.NewClientWithOptions(fmt.Sprintf("http://%s:8086",influxIP), "my-token", influxdb2.DefaultOptions().SetBatchSize(20))
writeAPI := client.WriteAPI("admin", "admin123")
// The way this is set up, these likely don't get executed on ^C.
defer client.Close()
defer writeAPI.Flush()
// Simple, isn't it?
for {
go InfluxAsyncGet(&writeAPI, hpwcIP)
time.Sleep(time.Millisecond * 1000)
}
}