blob: 2d19506bf0dc923291e8379a6e646d6e55a106d9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
}
|