From 029f90de6895b68b5f3d1999858b09d055429679 Mon Sep 17 00:00:00 2001 From: Ethel Morgan Date: Fri, 19 Jun 2020 23:50:49 +0100 Subject: basic wake-on-lan actuator --- main.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 main.go (limited to 'main.go') diff --git a/main.go b/main.go new file mode 100644 index 0000000..219cf8d --- /dev/null +++ b/main.go @@ -0,0 +1,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 {} +} -- cgit v1.2.3