From 224d52d1033d8ccce5087c9bee5a63457830a13a Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 20 Dec 2025 14:11:13 +0100 Subject: 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. --- mqtt.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'mqtt.go') 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`)) } -- cgit v1.2.3-70-g09d2