orion/emlid/reachview/configuration.go

80 lines
2.4 KiB
Go

package reachview
import (
"context"
"forge.cadoles.com/Pyxis/orion/emlid/common"
"github.com/pkg/errors"
)
const (
eventGetConfiguration = "get configuration"
eventCurrentConfiguration = "current configuration"
eventApplyConfiguration = "apply configuration"
eventConfigurationApplied = "configuration applied"
eventResetConfiguration = "reset configuration to default"
eventSettingsResetToDefault = "settings reset to default"
)
// Configuration fetches and return the current configuration of the ReachRS module
func (r *Client) Configuration(ctx context.Context) (*Configuration, error) {
configuration := &Configuration{}
if err := r.ReqResp(ctx, eventGetConfiguration, nil, eventCurrentConfiguration, configuration); err != nil {
return nil, err
}
return configuration, nil
}
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
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)
}
return nil
}