aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorEthel Morgan <eth@ethulhu.co.uk>2020-06-19 23:50:49 +0100
committerEthel Morgan <eth@ethulhu.co.uk>2020-06-19 23:50:49 +0100
commit029f90de6895b68b5f3d1999858b09d055429679 (patch)
tree091541f5cb5d9e3d948ae69be5a5ac53aa7954c0 /main.go
parentd544da3d08be66807831c03bf0f421c0addd8e9f (diff)
basic wake-on-lan actuator
Diffstat (limited to 'main.go')
-rw-r--r--main.go60
1 files changed, 60 insertions, 0 deletions
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 {}
+}