This repository has been archived on 2024-08-02. You can view files and clone it, but cannot push or open issues or pull requests.
orion/emlid/reachview/configuration.go

80 lines
2.4 KiB
Go
Raw Permalink Normal View History

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-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
func (r *Client) Configuration(ctx context.Context) (*Configuration, error) {
2018-09-20 17:28:18 +02:00
configuration := &Configuration{}
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
}
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)
}
2020-11-23 11:15:30 +01:00
return nil
}