Add configurable mqtt port

This commit is contained in:
Sam W 2022-04-17 02:44:06 +01:00
parent 041ad2f5a3
commit 0913422296
2 changed files with 5 additions and 1 deletions

View File

@ -1,5 +1,6 @@
[mqtt] [mqtt]
Host = "192.168.0.123" Host = "192.168.0.123"
Port = 1883
[[targets]] [[targets]]
Host = "192.168.0.222" Host = "192.168.0.222"

View File

@ -34,6 +34,7 @@ type targetConfig struct {
type config struct { type config struct {
MQTT struct { MQTT struct {
Host string Host string
Port uint16
} }
Targets []targetConfig Targets []targetConfig
} }
@ -206,7 +207,9 @@ func main() {
conf, err := parseConfig(*configpath) conf, err := parseConfig(*configpath)
check(err) check(err)
mqttOpts := mqtt.NewClientOptions().AddBroker(fmt.Sprintf("tcp://%s:1883", conf.MQTT.Host)).SetClientID("apc2mqtt") mqttOpts := mqtt.NewClientOptions().AddBroker(
fmt.Sprintf("tcp://%s:%d", conf.MQTT.Host, conf.MQTT.Port),
).SetClientID("apc2mqtt")
mqttClient := mqtt.NewClient(mqttOpts) mqttClient := mqtt.NewClient(mqttOpts)
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil { if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error()) panic(token.Error())