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