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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
package main
import (
_ "embed"
"encoding/json"
"fmt"
"log"
"net"
"net/url"
"regexp"
"strconv"
"time"
)
const (
MQTT_BROKER_REGEX = `^tcp://127\.0\.0\.1:\d+$`
MQTT_CLIENT_ID_REGEX = `^[0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]{1,23}$`
MQTT_TOPIC_PREFIX_REGEX = `^[a-zA-Z0-9]{1,20}$`
)
var (
mqttBrokerRegexp *regexp.Regexp
mqttClientIDRegexp *regexp.Regexp
mqttTopicPrefixRegexp *regexp.Regexp
)
func init() {
mqttBrokerRegexp = regexp.MustCompile(MQTT_BROKER_REGEX)
mqttClientIDRegexp = regexp.MustCompile(MQTT_CLIENT_ID_REGEX)
mqttTopicPrefixRegexp = regexp.MustCompile(MQTT_TOPIC_PREFIX_REGEX)
}
//go:embed configs/default.json
var defaultConfig []byte
type MQTTConfig struct {
Broker string `json:"broker"`
ClientID string `json:"client-id"`
TopicPrefix string `json:"topic-prefix"`
}
type HomematicConfig struct {
CCU string `json:"ccu"`
PollingPeriod string `json:"polling-period"`
}
type StartupConfig struct {
MQTT MQTTConfig `json:"mqtt"`
Homematic HomematicConfig `json:"homematic"`
}
func (sc StartupConfig) Validate() error {
if !mqttBrokerRegexp.MatchString(sc.MQTT.Broker) {
return fmt.Errorf(
"mqtt/broker configuration '%s' does not match regular expression '%s'.",
sc.MQTT.Broker,
MQTT_BROKER_REGEX,
)
}
if !mqttClientIDRegexp.MatchString(sc.MQTT.ClientID) {
return fmt.Errorf(
"mqtt/client-id configuration '%s' does not match regular expression '%s'.",
sc.MQTT.ClientID,
MQTT_CLIENT_ID_REGEX,
)
}
if !mqttTopicPrefixRegexp.MatchString(sc.MQTT.TopicPrefix) {
return fmt.Errorf(
"mqtt/topic-prefix configuration '%s' does not match regular expression '%s'.",
sc.MQTT.TopicPrefix,
MQTT_TOPIC_PREFIX_REGEX,
)
}
cu, err := url.Parse(sc.Homematic.CCU)
if err != nil {
return fmt.Errorf("homematic/ccu configuration '%s' cannot be parsed as URL: %v", sc.Homematic.CCU, err)
}
if cu.Scheme != `http` {
return fmt.Errorf("homematic/ccu configuration '%s' must use scheme 'http'.", sc.Homematic.CCU)
}
host, portstring, err := net.SplitHostPort(cu.Host)
if err != nil {
return fmt.Errorf("homematic/ccu configuration '%s' can't be split into hostname and port: %v", sc.Homematic.CCU, err)
}
ip := net.ParseIP(host)
if ip == nil {
return fmt.Errorf("homematic/ccu configuration '%s' must use a plain IP address as host.", sc.Homematic.CCU)
}
_, err = strconv.Atoi(portstring)
if err != nil {
return fmt.Errorf("homematic/ccu configuration '%s' must use a numeric port: %v", sc.Homematic.CCU, err)
}
_, err = time.ParseDuration(sc.Homematic.PollingPeriod)
if err != nil {
return fmt.Errorf("homematic/polling-period configuration '%s' could not be parsed to duration: %v", sc.Homematic.PollingPeriod, err)
}
return nil
}
func (sc *StartupConfig) FromJSON(data []byte) error {
err := json.Unmarshal(data, sc)
if err != nil {
return fmt.Errorf("Failed to unmarshal configuration: %w", err)
}
err = sc.Validate()
if err != nil {
return fmt.Errorf("Could not validate configuration: %w", err)
}
return nil
}
func GetStartupConfig() StartupConfig {
config := StartupConfig{}
err := config.FromJSON(defaultConfig)
if err != nil {
log.Fatalf("Could not parse default config: %v", err)
}
return config
}
|