summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2025-12-20 14:11:13 +0100
committerxengineering <me@xengineering.eu>2025-12-20 14:11:13 +0100
commit224d52d1033d8ccce5087c9bee5a63457830a13a (patch)
treecfd0b61e66258a0121ef20d4572561aee84b8db3
parentd6fa255b1df26a12329a614601a027e074aabeb2 (diff)
downloadsia-server-224d52d1033d8ccce5087c9bee5a63457830a13a.tar
sia-server-224d52d1033d8ccce5087c9bee5a63457830a13a.tar.zst
sia-server-224d52d1033d8ccce5087c9bee5a63457830a13a.zip
Use default config
The default config JSON is embedded as bytes into the executable. Instead of constants the default values are now parsed from these embedded bytes.
-rw-r--r--cache.go2
-rw-r--r--config.go23
-rw-r--r--configs/default.json2
-rw-r--r--homematic.go22
-rw-r--r--main.go6
-rw-r--r--mqtt.go18
6 files changed, 37 insertions, 36 deletions
diff --git a/cache.go b/cache.go
index e5a4e4f..4e0eddc 100644
--- a/cache.go
+++ b/cache.go
@@ -33,7 +33,7 @@ func (c *Cache) Update(states States) {
for id, state := range states {
cached, known := c.States[id]
if !known || cached != state {
- topic := fmt.Sprintf("%s/contact/%s/state", TOPIC_PREFIX, id)
+ topic := fmt.Sprintf("contact/%s/state", id)
var payload []byte
if state == true {
payload = []byte("open")
diff --git a/config.go b/config.go
index 38f0af9..edd6f4b 100644
--- a/config.go
+++ b/config.go
@@ -9,17 +9,20 @@ import (
//go:embed configs/default.json
var defaultConfig []byte
-type StartupConfig struct {
- MQTT struct {
- Broker string `json:"broker"`
- ClientID string `json:"client-id"`
- TopicPrefix string `json:"topic-prefix"`
- } `json:"mqtt"`
+type MQTTConfig struct {
+ Broker string `json:"broker"`
+ ClientID string `json:"client-id"`
+ TopicPrefix string `json:"topic-prefix"`
+}
- Homematic struct {
- CCU string `json:"ccu"`
- PollingPeriodMilliseconds int `json:"polling-period-milliseconds"`
- } `json:"homematic"`
+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) FromJSON(data []byte) error {
diff --git a/configs/default.json b/configs/default.json
index 9d3fb07..b291185 100644
--- a/configs/default.json
+++ b/configs/default.json
@@ -6,6 +6,6 @@
},
"homematic": {
"ccu": "http://127.0.0.1:8080",
- "polling-period-milliseconds": 50
+ "polling-period": "50ms"
}
}
diff --git a/homematic.go b/homematic.go
index 7ccc1ad..4bae0ca 100644
--- a/homematic.go
+++ b/homematic.go
@@ -8,19 +8,19 @@ import (
"xengineering.eu/homematic-go/homematic"
)
-const (
- OPENCCU = `http://127.0.0.1:8080`
- POLLING_PERIOD = 50 * time.Millisecond
-)
-
-func HomematicRun(tx chan MQTTMessage) {
- req, inventory, err := Start()
+func HomematicRun(config HomematicConfig, tx chan MQTTMessage) {
+ req, inventory, err := Start(config.CCU)
if err != nil {
log.Fatalf("Failed startup process: %v", err)
}
cache := NewCache(tx)
+ pollingPeriod, err := time.ParseDuration(config.PollingPeriod)
+ if err != nil {
+ log.Fatalf("Failed to parse Homematic polling period: %v", err)
+ }
+
for {
start := time.Now()
@@ -28,18 +28,18 @@ func HomematicRun(tx chan MQTTMessage) {
cache.Update(states)
- WaitUntil(start.Add(POLLING_PERIOD))
+ WaitUntil(start.Add(pollingPeriod))
}
}
-func Start() (homematic.Requester, homematic.Devices, error) {
+func Start(ccu string) (homematic.Requester, homematic.Devices, error) {
var req homematic.Requester
var inventory homematic.Devices
var err error
- req = homematic.NewRequester(OPENCCU)
- log.Printf("Created Homematic requester (%s).", OPENCCU)
+ req = homematic.NewRequester(ccu)
+ log.Printf("Created Homematic requester (%s).", ccu)
inventory, err = req.ListDevices()
if err != nil {
diff --git a/main.go b/main.go
index 0e51a44..128720f 100644
--- a/main.go
+++ b/main.go
@@ -11,10 +11,12 @@ func main() {
log.Println("+++ Started Sia server +++")
defer log.Println("--- Stopped Sia server ---")
+ config := GetStartupConfig()
+
tx := make(chan MQTTMessage)
- go MQTTRun(tx)
- go HomematicRun(tx)
+ go MQTTRun(config.MQTT, tx)
+ go HomematicRun(config.Homematic, tx)
Await(syscall.SIGTERM, syscall.SIGINT)
}
diff --git a/mqtt.go b/mqtt.go
index 6d01103..a7b374d 100644
--- a/mqtt.go
+++ b/mqtt.go
@@ -9,9 +9,6 @@ import (
)
const (
- BROKER = `tcp://127.0.0.1:1883`
- CLIENT_ID = `siaserver`
- TOPIC_PREFIX = `sia`
QOS = byte(1)
RETAINED = true
MQTT_CONNECT_TIMEOUT = 1 * time.Second
@@ -28,14 +25,12 @@ type MQTTMessage struct {
Payload []byte
}
-func init() {
- mqttServerHealthTopic = fmt.Sprintf("%s/server/health", TOPIC_PREFIX)
-}
+func MQTTRun(config MQTTConfig, tx chan MQTTMessage) {
+ mqttServerHealthTopic = fmt.Sprintf("%s/server/health", config.TopicPrefix)
-func MQTTRun(tx chan MQTTMessage) {
opts := mqtt.NewClientOptions()
- opts.AddBroker(BROKER)
- opts.SetClientID(CLIENT_ID)
+ opts.AddBroker(config.Broker)
+ opts.SetClientID(config.ClientID)
opts.SetCleanSession(true)
opts.SetOnConnectHandler(MQTTOnConnectHandler)
opts.SetConnectionLostHandler(MQTTConnectionLostHandler)
@@ -56,12 +51,13 @@ func MQTTRun(tx chan MQTTMessage) {
defer client.Disconnect(MQTT_DISCONNECT_TIMEOUT_US)
for message := range tx {
- client.Publish(message.Topic, QOS, RETAINED, message.Payload)
+ topic := fmt.Sprintf("%s/%s", config.TopicPrefix, message.Topic)
+ client.Publish(topic, QOS, RETAINED, message.Payload)
}
}
func MQTTOnConnectHandler(c mqtt.Client) {
- log.Printf("Connected to MQTT broker (%s)", BROKER)
+ log.Printf("Connected to MQTT broker.")
c.Publish(mqttServerHealthTopic, QOS, true, []byte(`good`))
}