-
Notifications
You must be signed in to change notification settings - Fork 0
/
drehtuer.go
87 lines (69 loc) · 2.04 KB
/
drehtuer.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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"os/signal"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
log "github.com/sirupsen/logrus"
)
// DoorState represents the door state's JSON as sent over the MQTT.
type DoorState struct {
Timestamp int64 `json:"timestamp"`
FltiOnly bool `json:"flti_only"`
Open bool `json:"door_open"`
}
func (doorState DoorState) String() string {
return fmt.Sprintf("Time: %v, FLTI only: %t, Open: %t",
time.Unix(doorState.Timestamp, 0), doorState.FltiOnly, doorState.Open)
}
// handleDoorMessage is called from the MQTT client for new messages with the door topic.
func handleDoorMessage(_ mqtt.Client, msg mqtt.Message) {
log.WithFields(log.Fields{
"topic": msg.Topic(),
"payload": string(msg.Payload()),
}).Debug("Received MQTT message")
var doorState DoorState
if err := json.Unmarshal(msg.Payload(), &doorState); err != nil {
log.WithError(err).Error("Unmarshaling JSON errored")
} else {
log.WithField("door", doorState).Info("Received MQTT door state")
if err := doorState.PublishInflux(); err != nil {
log.WithError(err).Error("Publishing to InfluxDB failed")
}
if err := doorState.UpdatePromMetrics(); err != nil {
log.WithError(err).Error("Updating Prometheus failed")
}
}
}
// waitSigint blocks the current thread until a SIGINT appears.
func waitSigint() {
signalSyn := make(chan os.Signal, 1)
signal.Notify(signalSyn, os.Interrupt)
<-signalSyn
log.Info("Received SIGINT, closing down..")
}
func init() {
var debugFlag bool
flag.BoolVar(&debugFlag, "verbose", false, "Verbose logging output")
flag.StringVar(&influxAddr, "influx", "", "InfluxDB address, optional")
flag.StringVar(&prometheusListener, "prometheus", "", "Prometheus listening address, optional")
flag.StringVar(&mqttBroker, "mqtt", "", "MQTT broker")
flag.Parse()
if debugFlag {
log.StandardLogger().SetLevel(log.DebugLevel)
}
if mqttBroker == "" {
flag.Usage()
os.Exit(1)
}
}
func main() {
setupMqttLogger()
setupMqtt()
setupPrometheus()
waitSigint()
teardownMqtt()
}