-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathconfig.go
More file actions
223 lines (201 loc) · 5.61 KB
/
Copy pathconfig.go
File metadata and controls
223 lines (201 loc) · 5.61 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package agent
import (
"crypto/tls"
"encoding/json"
"time"
"github.com/absmach/magistrala/pkg/errors"
)
type ServerConfig struct {
Port string `json:"port"`
}
type ChanConfig struct {
CtrlID string `json:"ctrl_id"`
DataID string `json:"data_id"`
}
func (c ChanConfig) CtrlChan() string {
return c.CtrlID
}
func (c ChanConfig) DataChan() string {
return c.DataID
}
func (c ChanConfig) Validate() error {
if c.CtrlID == "" {
return errors.New("channels.ctrl_id is required")
}
if c.DataID == "" {
return errors.New("channels.data_id is required")
}
return nil
}
type NodeRedConfig struct {
URL string `json:"url"`
}
type LogConfig struct {
Level string `json:"level"`
}
type MQTTConfig struct {
URL string `json:"url"`
Username string `json:"username"`
Password string `json:"password"`
MTLS bool `json:"mtls"`
SkipTLSVer bool `json:"skip_tls_ver"`
Retain bool `json:"retain"`
QoS byte `json:"qos"`
CmdQoS byte `json:"cmd_qos"`
CAPath string `json:"ca_path"`
CertPath string `json:"cert_path"`
PrivKeyPath string `json:"priv_key_path"`
CA []byte `json:"-"`
Cert tls.Certificate `json:"-"`
ClientCert string `json:"client_cert"`
ClientKey string `json:"client_key"`
CaCert string `json:"ca_cert"`
}
type HeartbeatConfig struct {
Interval time.Duration
}
type TelemetryConfig struct {
Interval time.Duration `json:"interval"`
IncludeTemperature bool `json:"include_temperature"`
IncludeNetwork bool `json:"include_network"`
IncludeLoad bool `json:"include_load"`
}
type TerminalConfig struct {
SessionTimeout time.Duration `json:"session_timeout"`
}
type OTAConfig struct {
Enabled bool `json:"enabled"`
BinaryPath string `json:"binary_path"`
DownloadDir string `json:"download_dir"`
}
type CoAPConfig struct {
URL string `json:"url"`
PSK string `json:"psk"`
CertPath string `json:"cert_path"`
PrivKeyPath string `json:"priv_key_path"`
CAPath string `json:"ca_path"`
SkipTLSVer bool `json:"skip_tls_ver"`
MaxObserve uint `json:"max_observe"`
MaxRetransmits uint `json:"max_retransmits"`
KeepAlive uint64 `json:"keep_alive"`
ContentFormat int `json:"content_format"`
Cert string `json:"cert"`
Key string `json:"key"`
CA string `json:"ca"`
}
type ProvisionConfig struct {
ClientsURL string `json:"clients_url"`
ChannelsURL string `json:"channels_url"`
RulesEngineURL string `json:"rules_engine_url"`
Token string `json:"token"`
DBPath string `json:"db_path"`
DomainID string `json:"domain_id"`
}
type Config struct {
Server ServerConfig `json:"server"`
Terminal TerminalConfig `json:"terminal"`
Heartbeat HeartbeatConfig `json:"heartbeat"`
Telemetry TelemetryConfig `json:"telemetry"`
Channels ChanConfig `json:"channels"`
NodeRed NodeRedConfig `json:"nodered"`
Log LogConfig `json:"log"`
MQTT MQTTConfig `json:"mqtt"`
CoAP CoAPConfig `json:"coap"`
Transport string `json:"transport"`
OTA OTAConfig `json:"ota"`
Provision ProvisionConfig `json:"provision"`
DomainID string `json:"domain_id"`
CommandSecret string `json:"-"`
}
func NewConfig(sc ServerConfig, cc ChanConfig, nc NodeRedConfig, lc LogConfig, mc MQTTConfig, coc CoAPConfig, transport string, hc HeartbeatConfig, tc TerminalConfig, oc OTAConfig, tlc TelemetryConfig) Config {
return Config{
Server: sc,
Channels: cc,
NodeRed: nc,
Log: lc,
MQTT: mc,
CoAP: coc,
Transport: transport,
Heartbeat: hc,
Terminal: tc,
OTA: oc,
Telemetry: tlc,
}
}
// UnmarshalJSON parses the duration from JSON.
func (d *HeartbeatConfig) UnmarshalJSON(b []byte) error {
var v map[string]any
if err := json.Unmarshal(b, &v); err != nil {
return err
}
interval, ok := v["interval"]
if !ok {
return errors.New("missing value")
}
switch value := interval.(type) {
case float64:
d.Interval = time.Duration(value)
return nil
case string:
var err error
d.Interval, err = time.ParseDuration(value)
if err != nil {
return err
}
return nil
default:
return errors.New("invalid duration")
}
}
// UnmarshalJSON parses the duration from JSON.
func (d *TerminalConfig) UnmarshalJSON(b []byte) error {
var v map[string]any
if err := json.Unmarshal(b, &v); err != nil {
return err
}
session_timeout, ok := v["session_timeout"]
if !ok {
return errors.New("missing value")
}
switch value := session_timeout.(type) {
case float64:
d.SessionTimeout = time.Duration(value)
return nil
case string:
var err error
d.SessionTimeout, err = time.ParseDuration(value)
if err != nil {
return err
}
return nil
default:
return errors.New("invalid duration")
}
}
// UnmarshalJSON parses the duration from JSON.
func (d *TelemetryConfig) UnmarshalJSON(b []byte) error {
var v map[string]any
if err := json.Unmarshal(b, &v); err != nil {
return err
}
interval, ok := v["interval"]
if !ok {
return errors.New("missing value")
}
switch value := interval.(type) {
case float64:
d.Interval = time.Duration(value)
return nil
case string:
var err error
d.Interval, err = time.ParseDuration(value)
if err != nil {
return err
}
return nil
default:
return errors.New("invalid duration")
}
}