zhangzengfei
2023-08-11 a335f66c4c520728be640ca4e7029ce6f45b8f3d
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
63
64
65
66
67
68
69
70
71
72
73
package config
 
import (
    "bytes"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "os"
)
 
type Config struct {
    ParentId          string `json:"parent_id"`            // 主账号id, 用于请求web接口
    JWTKey            string `json:"jwt_key"`              // 生成jwt的key
    NsqServer         string `json:"nsq_server"`           // nsq TCP服务端地址
    PubPLCDataTopic   string `json:"pub_plc_data_topic"`   // 发布plc数据的topic
    WritePLCDataTopic string `json:"write_plc_data_topic"` // 接收plc配置数据的topic
    SubDeviceTopic    string `json:"sub_device_topic"`     // 接收设备变更通知的topic
    DeviceListWebApi  string `json:"device_List_webapi"`   // 获取设备列表的接口, aps 提供, http接口
    PostPLCDataWebApi string `json:"post_plc_data_webapi"` // 上传给aps plc数据的接口地址. aps 提供, http接口
}
 
const configPath = "config.json"
 
var Options Config
 
func DefaultConfig() {
    Options.ParentId = "guangsheng"
    Options.JWTKey = "abcdefghijklmn"
    Options.NsqServer = "fai365.com:4150"
    Options.PubPLCDataTopic = "aps.factory.plc.livedata"
    Options.WritePLCDataTopic = "aps.factory.plc.write"
    Options.SubDeviceTopic = ""
    Options.DeviceListWebApi = "aps.factory.plc.device"
    Options.PostPLCDataWebApi = ""
}
 
func Load() {
    if !pathExists(configPath) {
        DefaultConfig()
        SaveConfig()
    } else {
        file, _ := os.Open(configPath)
        defer file.Close()
 
        fd := json.NewDecoder(file)
 
        fd.Decode(&Options)
    }
}
 
func SaveConfig() error {
    b, err := json.Marshal(Options)
    if err == nil {
        var out bytes.Buffer
 
        err = json.Indent(&out, b, "", "    ")
        if err == nil {
            err = ioutil.WriteFile(configPath, out.Bytes(), 0644)
        }
    }
 
    return err
}
 
func pathExists(path string) bool {
    _, err := os.Stat(path)
    if err == nil {
        return true
    }
 
    fmt.Printf("%v", err.Error())
    return false
}