subscribe: set client_id for MQTT subscriber to ensure unique identification
This commit is contained in:
@@ -1,24 +1,24 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"git.piskot.si/SeminarM2/lambdaiot-core/internal/mqtt"
|
||||||
"git.piskot.si/SeminarM2/lambdaiot-core/internal/mqtt"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MQTTPing publishes the current timestamp to the configured MQTT topic
|
// MQTTPing publishes the current timestamp to the configured MQTT topic
|
||||||
func MQTTPing(c *gin.Context) {
|
func MQTTPing(c *gin.Context) {
|
||||||
ts := time.Now().Format(time.RFC3339)
|
ts := time.Now().Format(time.RFC3339)
|
||||||
topic := os.Getenv("MQTT_TOPIC")
|
topic := os.Getenv("MQTT_TOPIC")
|
||||||
if topic == "" {
|
if topic == "" {
|
||||||
topic = "lambdaiot"
|
topic = "lambdaiot"
|
||||||
}
|
}
|
||||||
if err := mqtt.PublishDefault(topic, []byte(ts)); err != nil {
|
if err := mqtt.PublishDefault(topic, []byte(ts)); err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{"timestamp": ts})
|
c.JSON(http.StatusOK, gin.H{"timestamp": ts})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ client.on_connect = on_connect
|
|||||||
client.on_message = on_message
|
client.on_message = on_message
|
||||||
if username:
|
if username:
|
||||||
client.username_pw_set(username, password)
|
client.username_pw_set(username, password)
|
||||||
|
client.client_id = f"subscribe-{os.getpid()}"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
client.connect(host, port, 60)
|
client.connect(host, port, 60)
|
||||||
|
|||||||
Reference in New Issue
Block a user