Fix ClientID

This commit is contained in:
2026-02-02 13:42:10 +07:00
parent eb9f28e341
commit 7f298edf60
@@ -7,7 +7,8 @@ import (
"os"
"strings"
"time"
"crypto/rand"
"strconv"
"math/rand"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/go-redis/redis/v8"
@@ -39,7 +40,7 @@ func main() {
rdb := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", redisHost, redisPort),
Password: redisPassword,
DB: atoi(redisDB),
DB: parseInt(redisDB),
})
// Ping to verify connection
@@ -102,7 +103,7 @@ func handleMessage(ctx context.Context, rdb *redis.Client, msg mqtt.Message) {
return
}
if err := rdb.SetEX(ctx, key, data, time.Duration(atoi(redisSetEX))*time.Second).Err(); err != nil {
if err := rdb.SetEX(ctx, key, data, time.Duration(parseInt(redisSetEX))*time.Second).Err(); err != nil {
log.Printf("Failed to set Redis key: %v", err)
}
}
@@ -114,11 +115,20 @@ func getEnv(key, defaultVal string) string {
return defaultVal
}
func parseInt(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
log.Printf("Failed to parse int from %s: %v", s, err)
return 0
}
return i
}
func randomString() string {
b := make([]byte, 4)
if _, err := rand.Read(b); err != nil {
log.Printf("Failed to generate random string: %v", err)
return ""
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
b := make([]byte, 10)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return hex.EncodeToString(b)
return string(b)
}