2018-09-21 12:31:52 +02:00
|
|
|
package reachview
|
2018-09-20 17:28:18 +02:00
|
|
|
|
2020-11-23 11:15:30 +01:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"forge.cadoles.com/Pyxis/orion/emlid/common"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
2018-09-26 15:56:06 +02:00
|
|
|
|
2018-09-20 17:28:18 +02:00
|
|
|
const (
|
2020-11-23 11:15:30 +01:00
|
|
|
eventGetConfiguration = "get configuration"
|
|
|
|
eventCurrentConfiguration = "current configuration"
|
|
|
|
eventApplyConfiguration = "apply configuration"
|
|
|
|
eventConfigurationApplied = "configuration applied"
|
|
|
|
eventResetConfiguration = "reset configuration to default"
|
|
|
|
eventSettingsResetToDefault = "settings reset to default"
|
2018-09-20 17:28:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Configuration fetches and return the current configuration of the ReachRS module
|
2018-09-26 15:56:06 +02:00
|
|
|
func (r *Client) Configuration(ctx context.Context) (*Configuration, error) {
|
2018-09-20 17:28:18 +02:00
|
|
|
configuration := &Configuration{}
|
2018-09-26 15:56:06 +02:00
|
|
|
if err := r.ReqResp(ctx, eventGetConfiguration, nil, eventCurrentConfiguration, configuration); err != nil {
|
2018-09-20 17:28:18 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return configuration, nil
|
|
|
|
}
|
2018-09-26 15:56:06 +02:00
|
|
|
|
|
|
|
type configurationApplied struct {
|
|
|
|
Configuration *Configuration `mapstructure:"configuration,omitempty"`
|
|
|
|
Result string `mapstructure:"result"`
|
|
|
|
Constraints *Constraints `mapstructure:"Constraints,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ConfigurationApplySuccess -
|
|
|
|
ConfigurationApplySuccess = "success"
|
|
|
|
// ConfigurationApplyFailed -
|
|
|
|
ConfigurationApplyFailed = "failed"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ApplyConfiguration applies the given configuration
|
|
|
|
func (r *Client) ApplyConfiguration(ctx context.Context, config *Configuration) (string, *Configuration, error) {
|
|
|
|
res := &configurationApplied{}
|
|
|
|
if err := r.ReqResp(ctx, eventApplyConfiguration, config, eventConfigurationApplied, res); err != nil {
|
|
|
|
return ConfigurationApplyFailed, nil, err
|
|
|
|
}
|
|
|
|
return res.Result, res.Configuration, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResetConfiguration resets the module configuration to factory defaults
|
2020-11-23 11:15:30 +01:00
|
|
|
func (r *Client) ResetConfiguration(ctx context.Context) error {
|
|
|
|
err := r.HandleVersion(
|
|
|
|
ctx,
|
|
|
|
common.NewVersionHandler("< 2.24.0", func(ctx context.Context, version string, stable bool) error {
|
|
|
|
res := &configurationApplied{}
|
|
|
|
if err := r.ReqResp(ctx, eventResetConfiguration, nil, eventConfigurationApplied, res); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.Result != ConfigurationApplySuccess {
|
|
|
|
return errors.New(res.Result)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}),
|
|
|
|
common.NewVersionHandler(">= 2.24.0", func(ctx context.Context, version string, stable bool) error {
|
|
|
|
if err := r.ReqResp(ctx, eventResetConfiguration, nil, eventSettingsResetToDefault, nil); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
2018-09-26 15:56:06 +02:00
|
|
|
}
|
2020-11-23 11:15:30 +01:00
|
|
|
|
|
|
|
return nil
|
2018-09-26 15:56:06 +02:00
|
|
|
}
|