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
|
package main
import (
"flag"
"log"
"go.eth.moe/catbus-actuator-wakeonlan/config"
"go.eth.moe/catbus-actuator-wakeonlan/mqtt"
"go.eth.moe/catbus-actuator-wakeonlan/wakeonlan"
)
var (
configPath = flag.String("config-path", "", "path to config")
)
func main() {
flag.Parse()
if *configPath == "" {
log.Fatal("must set -config-path")
}
config, err := config.ParseFile(*configPath)
if err != nil {
log.Fatalf("could not parse config file: %q", err)
}
brokerOptions := mqtt.NewClientOptions()
brokerOptions.AddBroker(config.Broker)
brokerOptions.SetAutoReconnect(true)
brokerOptions.SetConnectionLostHandler(func(_ mqtt.Client, err error) {
log.Printf("disconnected from MQTT broker %s: %v", config.Broker, err)
})
brokerOptions.SetOnConnectHandler(func(broker mqtt.Client) {
log.Printf("connected to MQTT broker %v", config.Broker)
for topic := range config.MACsByTopic {
token := broker.Subscribe(topic, mqtt.AtLeastOnce, func(_ mqtt.Client, msg mqtt.Message) {
mac, ok := config.MACsByTopic[msg.Topic()]
if !ok {
return
}
if err := wakeonlan.Wake(mac); err != nil {
log.Printf("could not send wake-on-lan: %v")
}
})
if err := token.Error(); err != nil {
log.Printf("could not subscribe to %q: %v", topic, err)
}
}
})
broker := mqtt.NewClient(brokerOptions)
if token := broker.Connect(); token.Error() != nil {
log.Fatalf("could not connect to MQTT broker: %v", token.Error())
}
select {}
}
|