From 029f90de6895b68b5f3d1999858b09d055429679 Mon Sep 17 00:00:00 2001
From: Ethel Morgan <eth@ethulhu.co.uk>
Date: Fri, 19 Jun 2020 23:50:49 +0100
Subject: basic wake-on-lan actuator

---
 config/config.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)
 create mode 100644 config/config.go

(limited to 'config')

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
+}
-- 
cgit v1.2.3