This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
/
config.go
120 lines (102 loc) · 4.05 KB
/
config.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
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
// Package config implements functions to assist with attribute evaluation in the SLAM service.
// Warning: The code in this package might be transient. Stability is not guaranteed.
package config
import (
"github.com/edaniels/golog"
"github.com/pkg/errors"
"go.viam.com/utils"
)
// newError returns an error specific to a failure in the SLAM config.
func newError(configError string) error {
return errors.Errorf("SLAM Service configuration error: %s", configError)
}
// DetermineDeleteProcessedData will determine the value of the deleteProcessData attribute
// based on the useLiveData and deleteData input parameters.
func DetermineDeleteProcessedData(logger golog.Logger, deleteData *bool, useLiveData bool) bool {
var deleteProcessedData bool
if deleteData == nil {
deleteProcessedData = useLiveData
} else {
deleteProcessedData = *deleteData
if !useLiveData && deleteProcessedData {
logger.Debug("a value of true cannot be given for delete_processed_data when in offline mode, setting to false")
deleteProcessedData = false
}
}
return deleteProcessedData
}
// DetermineUseLiveData will determine the value of the useLiveData attribute
// based on the liveData input parameter and sensor list.
func DetermineUseLiveData(logger golog.Logger, liveData *bool, sensors []string) (bool, error) {
// If liveData is not set, default it to true and require a sensor to have been provided.
if liveData == nil {
if len(sensors) == 0 {
return false, newError("sensors field cannot be empty")
}
return true, nil
}
useLiveData := *liveData
if useLiveData && len(sensors) == 0 {
return false, newError("sensors field cannot be empty when use_live_data is set to true")
}
return useLiveData, nil
}
// Config describes how to configure the SLAM service.
type Config struct {
Sensors []string `json:"sensors"`
ConfigParams map[string]string `json:"config_params"`
DataDirectory string `json:"data_dir"`
UseLiveData *bool `json:"use_live_data"`
DataRateMsec int `json:"data_rate_msec"`
MapRateSec *int `json:"map_rate_sec"`
Port string `json:"port"`
DeleteProcessedData *bool `json:"delete_processed_data"`
}
// Validate creates the list of implicit dependencies.
func (config *Config) Validate(path string) ([]string, error) {
if config.ConfigParams["mode"] == "" {
return nil, utils.NewConfigValidationFieldRequiredError(path, "config_params[mode]")
}
if config.DataDirectory == "" {
return nil, utils.NewConfigValidationFieldRequiredError(path, "data_dir")
}
if config.DataRateMsec < 0 {
return nil, errors.New("cannot specify data_rate_msec less than zero")
}
if config.MapRateSec != nil && *config.MapRateSec < 0 {
return nil, errors.New("cannot specify map_rate_sec less than zero")
}
deps := config.Sensors
return deps, nil
}
// GetOptionalParameters sets any unset optional config parameters to the values passed to this function,
// and returns them.
func GetOptionalParameters(config *Config, defaultPort string,
defaultDataRateMsec, defaultMapRateSec int, logger golog.Logger,
) (string, int, int, bool, bool, error) {
port := config.Port
if config.Port == "" {
port = defaultPort
}
dataRateMsec := config.DataRateMsec
if config.DataRateMsec == 0 {
dataRateMsec = defaultDataRateMsec
logger.Debugf("no data_rate_msec given, setting to default value of %d", defaultDataRateMsec)
}
mapRateSec := 0
if config.MapRateSec == nil {
logger.Debugf("no map_rate_sec given, setting to default value of %d", defaultMapRateSec)
mapRateSec = defaultMapRateSec
} else {
mapRateSec = *config.MapRateSec
}
if mapRateSec == 0 {
logger.Info("setting slam system to localization mode")
}
useLiveData, err := DetermineUseLiveData(logger, config.UseLiveData, config.Sensors)
if err != nil {
return "", 0, 0, false, false, err
}
deleteProcessedData := DetermineDeleteProcessedData(logger, config.DeleteProcessedData, useLiveData)
return port, dataRateMsec, mapRateSec, useLiveData, deleteProcessedData, nil
}