-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf.go
89 lines (72 loc) · 2.02 KB
/
conf.go
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
package conf
import (
"encoding/json"
"io/ioutil"
"os"
"time"
)
type NodeIdData struct {
NodeIdAddress string `json:"node_id"`
NodeName string `json:"node_name"`
}
type ServerSignals struct {
StationName string `json:"station_name"`
OPCUAddress string `json:"opcuaddress"`
NodeIds []NodeIdData `json:"nodeids"`
}
type Conf struct {
BootstrapServers string `json:"bootstrap_servers"`
SignalsFilename string `json:"signals_filename"`
PublishingTopicname string `json:"publishing_topicname"`
ServersData []ServerSignals
CleaningThresholdMsec int64 `json:"cleaning_threshold_msec"`
PullRateMsec int64 `json:"pull_rate_msec"`
PubRateMsec int64 `json:"pub_rate_msec"`
cleaning_threshold time.Duration
pull_rate time.Duration
pub_rate time.Duration
}
func NewConf(filepath string) (*Conf, error) {
var conf *Conf = new(Conf)
conf_file, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer conf_file.Close()
json_bytes, err := ioutil.ReadAll(conf_file)
if err != nil {
return nil, err
}
err = json.Unmarshal(json_bytes, conf)
if err != nil {
return nil, err
}
conf.cleaning_threshold = time.Duration(conf.CleaningThresholdMsec * int64(time.Millisecond))
conf.pull_rate = time.Duration(conf.PullRateMsec * int64(time.Millisecond))
conf.pub_rate = time.Duration(conf.PubRateMsec * int64(time.Millisecond))
conf.ServersData = []ServerSignals{}
//I then proceed to obtain the servers data
signals_file, err := os.Open(conf.SignalsFilename)
if err != nil {
return nil, err
}
defer signals_file.Close()
json_bytes, err = ioutil.ReadAll(signals_file)
if err != nil {
return nil, err
}
err = json.Unmarshal(json_bytes, &conf.ServersData)
if err != nil {
return nil, err
}
return conf, nil
}
func (c *Conf) GetCleaningThreshold() time.Duration {
return c.cleaning_threshold
}
func (c *Conf) GetPullRate() time.Duration {
return c.pull_rate
}
func (c *Conf) GetPubRate() time.Duration {
return c.pub_rate
}