-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
43 lines (39 loc) · 1.02 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
package main
import (
"log"
"time"
)
func main() {
config, err := NewConfigFromEnv()
if err != nil {
log.Fatalln("Failed to initialize config:", err)
}
t := config.MQTTClient.Connect()
if t.WaitTimeout(10 * time.Second) {
if t.Error() != nil {
log.Fatalln("Failed to connect to MQTT:", t.Error())
}
} else {
log.Fatalln("Timeout connecting to MQTT")
}
var subscriber *Subscriber
if config.SubscribeTopic != "" {
subscriber, err = NewSubscriber(config.MQTTClient, config.SubscribeTopic, *config.MDNSInterface)
if err != nil {
log.Fatalln("Failed to initialize subscriber:", err)
}
}
if config.PublishTopic != "" {
filter := func(id ServiceIdentity) bool {
return subscriber == nil || !subscriber.HasService(id)
}
publisher := NewPublisher(config.MQTTClient, config.PublishTopic, *config.MDNSInterface, filter)
publisher.Run("_hap._tcp", "local")
} else if config.SubscribeTopic != "" {
// TODO: Find a better way to block
for {
time.Sleep(time.Minute)
}
}
log.Println("Nothing to do.")
}