qixiaoning
6 天以前 2919bea2ffc28003aaefdf9619dc39cda11c1fd1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
}