From 55d30a197e438e8c84c0b8bf3067d8cc010fba66 Mon Sep 17 00:00:00 2001 From: Leland Sindt Date: Mon, 15 Mar 2021 21:14:59 -0500 Subject: [PATCH] add random string to mqtt clientID to prevent concurrent connections --- src/v1_TeslaMateAPICarsStatus.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/v1_TeslaMateAPICarsStatus.go b/src/v1_TeslaMateAPICarsStatus.go index 955af1a..3acb71b 100644 --- a/src/v1_TeslaMateAPICarsStatus.go +++ b/src/v1_TeslaMateAPICarsStatus.go @@ -11,6 +11,7 @@ import ( mqtt "github.com/eclipse/paho.mqtt.golang" "github.com/gin-gonic/gin" _ "github.com/lib/pq" + "github.com/thanhpk/randstr" ) // defining a lot of vars that are used my the MQTT MessageHandler @@ -229,11 +230,11 @@ func TeslaMateAPICarsStatusV1(c *gin.Context) { // create options for the MQTT client connection opts := mqtt.NewClientOptions().AddBroker(mqttURL) // setting generic MQTT settings in opts - opts.SetKeepAlive(2 * time.Second) // setting keepalive for client - opts.SetDefaultPublishHandler(f) // using f mqtt.MessageHandler function - opts.SetPingTimeout(1 * time.Second) // setting pingtimeout for client - opts.SetClientID("teslamateapi") // setting mqtt client id for TeslaMateApi - opts.SetCleanSession(true) // removal of all subscriptions on disconnect + opts.SetKeepAlive(2 * time.Second) // setting keepalive for client + opts.SetDefaultPublishHandler(f) // using f mqtt.MessageHandler function + opts.SetPingTimeout(1 * time.Second) // setting pingtimeout for client + opts.SetClientID("teslamateapi-" + randstr.String(4)) // setting mqtt client id for TeslaMateApi + opts.SetCleanSession(true) // removal of all subscriptions on disconnect // creating MQTT connection with options m := mqtt.NewClient(opts)