mirror of
https://github.com/tobiasehlert/teslamateapi.git
synced 2026-02-27 09:54:18 +08:00
add random string to mqtt clientID to prevent concurrent connections
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user