zhangqian
2023-12-13 20bbbcc1d14536ddaa498ac84a6008828df8dc1c
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package conf
 
import (
    "apsClient/pkg/convertx"
    "apsClient/pkg/logx"
    "apsClient/pkg/sqlitex"
    "apsClient/pkg/timex"
    "apsClient/utils"
    "github.com/spf13/viper"
    "log"
    "time"
)
 
var (
    // config file name
    configName = "apsClient"
    // config file paths
    configPaths = []string{
        "./",
        "../",
        "./conf",
        "../conf",
    }
)
 
type (
    System struct {
        Env              string // 环境值 develop test public
        Port             int    // 端口
        DeviceId         string //设备id
        NetSetShellPath  string //网络设置脚本路径
        NetUpShellName   string //网络停用脚本
        NetDownShellName string //网络开启脚本
    }
 
    Etcd struct {
        Endpoints   []string // etcd地址
        DialTimeout int      // etcd超时时间
        KeyPrefix   string   // etcd key前缀
        Tls         Tls      // etcd tls配置
    }
 
    Tls struct {
        CertFile string // 证书文件
        KeyFile  string // 私钥文件
        CaFile   string // ca文件
    }
 
    Rancher struct {
        Url   string // rancher地址
        Token string // rancher token
    }
 
    K8s struct {
        Image  string // k8s deployment 镜像
        DBHost string // db host
        ALHost string // al host
        Host   string // host
        IP     string // tmp ip
    }
 
    Services struct {
        ApsServer string
        Serial    string
    }
 
    nsqConf struct {
        NodeId         string
        NsqdAddr       string
        NsqlookupdAddr string
    }
 
    plc struct {
        FinishNumberTimeInterval int
        TotalNumberTimeInterval  int
        ModbusIntType            string
        SlaveId                  int
        Package                  string
        StandbyTime              int64
    }
 
    Prompt struct {
        SafeProduce     string `json:"safeProduce,omitempty"`
        PlcNotConnected string `json:"plcNotConnected,omitempty"`
    }
 
    config struct {
        // 系统配置
        System System
 
        // 日志
        Log logx.Conf
 
        // mysql配置
        Sqlite sqlitex.Conf
 
        //Services Address
        Services Services
 
        //NsqConf
        NsqConf nsqConf
 
        //PLC
        PLC plc
 
        Prompt Prompt
 
        CurrentDeviceID string //设置当前面板控制的设备
 
        SerfClusterStatus string //集群状态
 
        ClusterNodeQuantity int //集群节点数量
 
        SystemDeviceRunSince int64 //系统开始运行时间戳
    }
)
 
var (
    Conf  config
    Viper *viper.Viper
)
 
func init() {
    Viper = viper.New()
    Viper.SetConfigName(configName)
    for _, path := range configPaths {
        Viper.AddConfigPath(path)
    }
 
    if err := Viper.ReadInConfig(); err != nil {
        log.Fatalf("ReadInConfig err:%v", err)
    }
 
    if err := Viper.Unmarshal(&Conf); err != nil {
        log.Fatalf("Unmarshal err:%v", err)
    }
    if Conf.PLC.StandbyTime == 0 {
        Conf.PLC.StandbyTime = 300 //5分钟
    }
 
    uptimeStr, err := utils.Exec("uptime -s")
    if err == nil {
        t, err := timex.StringToTime(uptimeStr)
        if err == nil {
            Conf.SystemDeviceRunSince = t.Unix()
        }
    } else {
        Conf.SystemDeviceRunSince = time.Now().Unix()
    }
 
    SetUpTime()
 
    ShowConfig()
}
 
// WriteConfig 回写配置
func (c *config) WriteConfig() error {
    cs := convertx.StructToMap(Conf)
    for k, v := range cs {
        Viper.Set(k, v)
    }
    return Viper.WriteConfig()
}
 
func ShowConfig() {
    log.Println("......................................................")
    log.Printf("   System:                %+v", Conf.System)
    log.Printf("   Log:                   %+v", Conf.Log)
    log.Printf("   plc :                  %+v", Conf.PLC)
    log.Printf("   services :                  %+v", Conf.Services)
    log.Println("......................................................")
}
 
func SetUpTime() {
    uptimeStr, err := utils.Exec("uptime -s")
    if err == nil {
        t, err := timex.StringToTime(uptimeStr)
        if err == nil {
            Conf.SystemDeviceRunSince = t.Unix()
        }
    } else {
        Conf.SystemDeviceRunSince = time.Now().Unix()
    }
}