From 0905fc55e7427a4e024fc5dc73f10be45200f110 Mon Sep 17 00:00:00 2001 From: Ethel Morgan Date: Sat, 20 Jun 2020 00:13:53 +0100 Subject: rename the project to catbus-wakeonlan --- cmd/catbus-actuator-wakeonlan/main.go | 81 +++++++++++++++++++++++++++++++++++ default.nix | 4 +- go.mod | 2 +- main.go | 81 ----------------------------------- 4 files changed, 84 insertions(+), 84 deletions(-) create mode 100644 cmd/catbus-actuator-wakeonlan/main.go delete mode 100644 main.go diff --git a/cmd/catbus-actuator-wakeonlan/main.go b/cmd/catbus-actuator-wakeonlan/main.go new file mode 100644 index 0000000..ada08a0 --- /dev/null +++ b/cmd/catbus-actuator-wakeonlan/main.go @@ -0,0 +1,81 @@ +// SPDX-FileCopyrightText: 2020 Ethel Morgan +// +// SPDX-License-Identifier: MIT + +package main + +import ( + "context" + "flag" + "log" + + "go.eth.moe/catbus-wakeonlan/config" + "go.eth.moe/catbus-wakeonlan/logger" + "go.eth.moe/catbus-wakeonlan/mqtt" + "go.eth.moe/catbus-wakeonlan/wakeonlan" +) + +var ( + configPath = flag.String("config-path", "", "path to config") +) + +func main() { + flag.Parse() + + if *configPath == "" { + log.Fatal("must set -config-path") + } + + log, _ := logger.FromContext(context.Background()) + + config, err := config.ParseFile(*configPath) + if err != nil { + log.AddField("config-path", *configPath) + log.WithError(err).Fatal("could not parse config file") + } + + log.AddField("broker-uri", config.Broker) + + brokerOptions := mqtt.NewClientOptions() + brokerOptions.AddBroker(config.Broker) + brokerOptions.SetAutoReconnect(true) + brokerOptions.SetConnectionLostHandler(func(_ mqtt.Client, err error) { + log := log + if err != nil { + log = log.WithError(err) + } + log.Error("disconnected from MQTT broker") + }) + brokerOptions.SetOnConnectHandler(func(broker mqtt.Client) { + log.Info("connected to MQTT 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 + } + + log.AddField("mac", mac) + log.AddField("topic", topic) + if err := wakeonlan.Wake(mac); err != nil { + log.WithError(err).Error("could not send wake-on-lan packet") + return + } + log.Info("sent wake-on-lan packet") + }) + if err := token.Error(); err != nil { + log := log.WithError(err) + log.AddField("topic", topic) + log.Error("could not subscribe to MQTT topic") + } + } + }) + + broker := mqtt.NewClient(brokerOptions) + if token := broker.Connect(); token.Error() != nil { + log.WithError(token.Error()).Fatal("could not connect to MQTT broker") + } + + select {} +} diff --git a/default.nix b/default.nix index d915704..efee9fa 100644 --- a/default.nix +++ b/default.nix @@ -6,9 +6,9 @@ with pkgs; buildGoModule rec { - name = "catbus-actuator-wakeonlan-${version}"; + name = "catbus-wakeonlan-${version}"; version = "latest"; - goPackagePath = "go.eth.moe/catbus-actuator-wakeonlan"; + goPackagePath = "go.eth.moe/catbus-wakeonlan"; modSha256 = "0nj0ny9692bqcw04fh74g8hqgfh3qc095fsq0y9cy677kp7l2q94"; diff --git a/go.mod b/go.mod index 4a884fa..6b5cbb8 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: MIT -module go.eth.moe/catbus-actuator-wakeonlan +module go.eth.moe/catbus-wakeonlan go 1.14 diff --git a/main.go b/main.go deleted file mode 100644 index a3ca9d3..0000000 --- a/main.go +++ /dev/null @@ -1,81 +0,0 @@ -// SPDX-FileCopyrightText: 2020 Ethel Morgan -// -// SPDX-License-Identifier: MIT - -package main - -import ( - "context" - "flag" - "log" - - "go.eth.moe/catbus-actuator-wakeonlan/config" - "go.eth.moe/catbus-actuator-wakeonlan/logger" - "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") - } - - log, _ := logger.FromContext(context.Background()) - - config, err := config.ParseFile(*configPath) - if err != nil { - log.AddField("config-path", *configPath) - log.WithError(err).Fatal("could not parse config file") - } - - log.AddField("broker-uri", config.Broker) - - brokerOptions := mqtt.NewClientOptions() - brokerOptions.AddBroker(config.Broker) - brokerOptions.SetAutoReconnect(true) - brokerOptions.SetConnectionLostHandler(func(_ mqtt.Client, err error) { - log := log - if err != nil { - log = log.WithError(err) - } - log.Error("disconnected from MQTT broker") - }) - brokerOptions.SetOnConnectHandler(func(broker mqtt.Client) { - log.Info("connected to MQTT 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 - } - - log.AddField("mac", mac) - log.AddField("topic", topic) - if err := wakeonlan.Wake(mac); err != nil { - log.WithError(err).Error("could not send wake-on-lan packet") - return - } - log.Info("sent wake-on-lan packet") - }) - if err := token.Error(); err != nil { - log := log.WithError(err) - log.AddField("topic", topic) - log.Error("could not subscribe to MQTT topic") - } - } - }) - - broker := mqtt.NewClient(brokerOptions) - if token := broker.Connect(); token.Error() != nil { - log.WithError(token.Error()).Fatal("could not connect to MQTT broker") - } - - select {} -} -- cgit v1.2.3