-
Notifications
You must be signed in to change notification settings - Fork 0
/
influx.go
45 lines (35 loc) · 870 Bytes
/
influx.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
package main
import (
"time"
log "github.com/sirupsen/logrus"
_ "github.com/influxdata/influxdb1-client"
influx "github.com/influxdata/influxdb1-client/v2"
)
var influxAddr string
// PublishInflux this DoorState to the InfluxDB.
func (doorState DoorState) PublishInflux() error {
if influxAddr == "" {
log.Debug("Skipping publishing InfluxDB as not configured")
return nil
}
client, err := influx.NewHTTPClient(influx.HTTPConfig{
Addr: influxAddr,
})
if err != nil {
return err
}
defer client.Close()
state := map[string]interface{}{
"flti": doorState.FltiOnly,
"open": doorState.Open,
}
bp, _ := influx.NewBatchPoints(influx.BatchPointsConfig{
Database: "doorstate",
})
pt, err := influx.NewPoint("door", nil, state, time.Unix(doorState.Timestamp, 0))
if err != nil {
return err
}
bp.AddPoint(pt)
return client.Write(bp)
}