aboutsummaryrefslogtreecommitdiff
path: root/config/config.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 /config/config.go
parentd544da3d08be66807831c03bf0f421c0addd8e9f (diff)
basic wake-on-lan actuator
Diffstat (limited to 'config/config.go')
-rw-r--r--config/config.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/config/config.go b/config/config.go
new file mode 100644
index 0000000..2d19506
--- /dev/null
+++ b/config/config.go
@@ -0,0 +1,66 @@
+package config
+
+import (
+ "encoding/json"
+ "io/ioutil"
+ "net"
+)
+
+type (
+ Config struct {
+ Broker string
+
+ MACsByTopic map[string]net.HardwareAddr
+ }
+
+ config struct {
+ MQTTBroker string `json:"mqttBroker"`
+ Devices map[string]struct {
+ MAC mac `json:"mac"`
+ Topic string `json:"topic"`
+ } `json:"devices"`
+ }
+
+ mac struct {
+ net.HardwareAddr
+ }
+)
+
+func ParseFile(path string) (*Config, error) {
+ bytes, err := ioutil.ReadFile(path)
+ if err != nil {
+ return nil, err
+ }
+
+ raw := config{}
+ if err := json.Unmarshal(bytes, &raw); err != nil {
+ return nil, err
+ }
+
+ return configFromConfig(raw), nil
+}
+
+func configFromConfig(raw config) *Config {
+ c := &Config{
+ Broker: raw.MQTTBroker,
+ MACsByTopic: map[string]net.HardwareAddr{},
+ }
+
+ for _, v := range raw.Devices {
+ c.MACsByTopic[v.Topic] = v.MAC.HardwareAddr
+ }
+
+ return c
+}
+
+func (m mac) MarshalText() ([]byte, error) {
+ return []byte(m.String()), nil
+}
+func (m *mac) UnmarshalText(raw []byte) error {
+ mm, err := net.ParseMAC(string(raw))
+ if err != nil {
+ return err
+ }
+ m.HardwareAddr = mm
+ return nil
+}