package mqtt
|
|
import (
|
"encoding/json"
|
"fmt"
|
"io/ioutil"
|
"os"
|
|
"github.com/go-basic/uuid"
|
)
|
|
type Config struct {
|
Broker string `json:"broker"`
|
ClientId string `json:"client_id"`
|
Username string `json:"username"`
|
Password string `json:"password"`
|
}
|
|
const configPath = "/opt/vasystem/config/mqtt.conf"
|
|
const MQTT_CONFIG_TOPIC = "smartai/config"
|
|
var Options Config
|
|
func LoadConfig() {
|
if !pathExists(configPath) {
|
SaveConfig()
|
} else {
|
file, _ := os.Open(configPath)
|
defer file.Close()
|
|
fd := json.NewDecoder(file)
|
|
fd.Decode(&Options)
|
|
fmt.Printf("%v\n", Options)
|
}
|
}
|
|
func SaveConfig() {
|
Options = Config{
|
"192.168.1.235:1883",
|
"smart-ai-helmet-manager" + uuid.New(),
|
"admin",
|
"admin",
|
}
|
|
bytes, err := json.Marshal(Options)
|
if err == nil {
|
ioutil.WriteFile(configPath, bytes, 0744)
|
}
|
}
|
|
func pathExists(path string) bool {
|
_, err := os.Stat(path)
|
if err == nil {
|
return true
|
}
|
|
fmt.Printf("%v", err.Error())
|
return false
|
}
|