feat: use /configuration/device and replice reachview system to define

antenna height

See Pyxis/fieldnotes#63 (comment)
This commit is contained in:
wpetit 2024-09-20 10:12:05 +02:00
parent b2cdf23fb7
commit bea49c78a9
8 changed files with 149 additions and 16 deletions

View File

@ -123,6 +123,21 @@ func (c *Client) SetBase(ctx context.Context, funcs ...protocol.SetBaseOptionFun
return nil return nil
} }
// GetBaseInfo implements protocol.Operations.
func (c *Client) GetBaseInfo(ctx context.Context) (*protocol.BaseInfo, error) {
_, ops, err := c.getProtocol(ctx)
if err != nil {
return nil, errors.WithStack(err)
}
baseInfo, err := ops.GetBaseInfo(ctx)
if err != nil {
return nil, errors.WithStack(err)
}
return baseInfo, nil
}
// Reboot implements protocol.Operations. // Reboot implements protocol.Operations.
func (c *Client) Reboot(ctx context.Context) error { func (c *Client) Reboot(ctx context.Context) error {
_, ops, err := c.getProtocol(ctx) _, ops, err := c.getProtocol(ctx)

View File

@ -2,6 +2,14 @@ package protocol
import "context" import "context"
type BaseInfo struct {
Mode string
AntennaOffset float64
Latitude float64
Longitude float64
Height float64
}
type Operations interface { type Operations interface {
// Connect initiates a new connection to the ReachView service // Connect initiates a new connection to the ReachView service
// It should be called before any other operation // It should be called before any other operation
@ -29,6 +37,8 @@ type Operations interface {
// SetBase updates the base configuration // SetBase updates the base configuration
SetBase(ctx context.Context, funcs ...SetBaseOptionFunc) error SetBase(ctx context.Context, funcs ...SetBaseOptionFunc) error
GetBaseInfo(ctx context.Context) (*BaseInfo, error)
// Reboot restarts the module // Reboot restarts the module
Reboot(ctx context.Context) error Reboot(ctx context.Context) error
} }

View File

@ -2,6 +2,7 @@ package testsuite
import ( import (
"context" "context"
"math"
"math/rand" "math/rand"
"os" "os"
"strconv" "strconv"
@ -156,7 +157,7 @@ var testCases = []operationTestCase{
latitude := -90 + rand.Float64()*180 latitude := -90 + rand.Float64()*180
longitude := -180 + rand.Float64()*360 longitude := -180 + rand.Float64()*360
height := rand.Float64() * 1000 height := rand.Float64() * 1000
antennaOffset := rand.Float64() * 2 antennaOffset := toFixed(rand.Float64()*2, 3)
opts := []protocol.SetBaseOptionFunc{ opts := []protocol.SetBaseOptionFunc{
protocol.WithBaseLatitude(latitude), protocol.WithBaseLatitude(latitude),
@ -173,13 +174,29 @@ var testCases = []operationTestCase{
return return
} }
config, err := ops.Configuration(ctx) baseInfo, err := ops.GetBaseInfo(ctx)
if err != nil { if err != nil {
t.Errorf("%+v", errors.WithStack(err)) t.Errorf("%+v", errors.WithStack(err))
return return
} }
t.Logf("updated configuration: %v", spew.Sdump(config)) t.Logf("base info: %v", spew.Sdump(baseInfo))
if e, g := latitude, baseInfo.Latitude; e != g {
t.Errorf("baseInfo.Latitude: expected '%v', got '%v'", e, g)
}
if e, g := longitude, baseInfo.Longitude; e != g {
t.Errorf("baseInfo.Longitude: expected '%v', got '%v'", e, g)
}
if e, g := height, baseInfo.Height; e != g {
t.Errorf("baseInfo.Height: expected '%v', got '%v'", e, g)
}
if e, g := antennaOffset, baseInfo.AntennaOffset; e != g {
t.Errorf("baseInfo.AntennaOffset: expected '%v', got '%v'", e, g)
}
}, },
}, },
@ -247,3 +264,9 @@ func TestOperations(t *testing.T, opsFactory OperationsFactoryFunc) {
}) })
} }
} }
func toFixed(n float64, precision int) float64 {
scale := math.Pow(10, float64(precision))
return math.Round(n*scale) / scale
}

View File

@ -297,6 +297,52 @@ func (o *Operations) SetBase(ctx context.Context, funcs ...protocol.SetBaseOptio
return nil return nil
} }
// GetBaseInfo implements protocol.Operations.
func (o *Operations) GetBaseInfo(ctx context.Context) (*protocol.BaseInfo, error) {
rawConfig, err := o.Configuration(ctx)
if err != nil {
return nil, errors.WithStack(err)
}
config := rawConfig.(*model.Configuration)
baseMode := config.BaseMode
var baseCoordinates *model.BaseCoordinates
if baseMode != nil && baseMode.BaseCoordinates != nil {
baseCoordinates = baseMode.BaseCoordinates
}
antennaOffset, err := strconv.ParseFloat(*baseCoordinates.AntennaOffset.Up, 64)
if err != nil {
return nil, errors.WithStack(err)
}
latitude, err := strconv.ParseFloat(*baseCoordinates.Coordinates[0], 64)
if err != nil {
return nil, errors.WithStack(err)
}
longitude, err := strconv.ParseFloat(*baseCoordinates.Coordinates[1], 64)
if err != nil {
return nil, errors.WithStack(err)
}
height, err := strconv.ParseFloat(*baseCoordinates.Coordinates[2], 64)
if err != nil {
return nil, errors.WithStack(err)
}
baseInfo := &protocol.BaseInfo{
Mode: *config.BaseMode.BaseCoordinates.Mode,
AntennaOffset: antennaOffset,
Height: height,
Latitude: latitude,
Longitude: longitude,
}
return baseInfo, nil
}
const ( const (
eventGetReachViewVersion = "get reachview version" eventGetReachViewVersion = "get reachview version"
eventReachViewVersionResults = "current reachview version" eventReachViewVersionResults = "current reachview version"

View File

@ -116,6 +116,16 @@ func (o *Operations) PostBaseCoordinates(ctx context.Context, base *model.Base)
return &updated, nil return &updated, nil
} }
func (o *Operations) PostDevice(ctx context.Context, device *model.ConfigurationDevice) (*model.ConfigurationDevice, error) {
var updated model.ConfigurationDevice
if err := o.PostJSON("/configuration/device", device, &updated); err != nil {
return nil, errors.WithStack(err)
}
return &updated, nil
}
func (o *Operations) GetUpdater(ctx context.Context) (*model.Updater, error) { func (o *Operations) GetUpdater(ctx context.Context) (*model.Updater, error) {
updater := &model.Updater{} updater := &model.Updater{}
if err := o.GetJSON("/updater", updater); err != nil { if err := o.GetJSON("/updater", updater); err != nil {

View File

@ -148,15 +148,7 @@ type Configuration struct {
} `json:"settings,omitempty"` } `json:"settings,omitempty"`
} `json:"base_corrections,omitempty"` } `json:"base_corrections,omitempty"`
} `json:"correction_input,omitempty"` } `json:"correction_input,omitempty"`
Device struct { Device ConfigurationDevice `json:"device,omitempty"`
AntennaHeight float64 `json:"antenna_height,omitempty"`
NightMode bool `json:"night_mode,omitempty"`
OnboardingShown bool `json:"onboarding_shown,omitempty"`
PowerOnBottomConnector bool `json:"power_on_bottom_connector,omitempty"`
PrivacyPolicyAccepted bool `json:"privacy_policy_accepted,omitempty"`
Role string `json:"role,omitempty"`
UsageAnalysisAccepted bool `json:"usage_analysis_accepted,omitempty"`
} `json:"device,omitempty"`
Logging struct { Logging struct {
Logs struct { Logs struct {
Autostart bool `json:"autostart,omitempty"` Autostart bool `json:"autostart,omitempty"`

View File

@ -0,0 +1,11 @@
package model
type ConfigurationDevice struct {
AntennaHeight float64 `json:"antenna_height,omitempty"`
NightMode bool `json:"night_mode,omitempty"`
OnboardingShown bool `json:"onboarding_shown,omitempty"`
PowerOnBottomConnector bool `json:"power_on_bottom_connector,omitempty"`
PrivacyPolicyAccepted bool `json:"privacy_policy_accepted,omitempty"`
Role string `json:"role,omitempty"`
UsageAnalysisAccepted bool `json:"usage_analysis_accepted,omitempty"`
}

View File

@ -60,6 +60,8 @@ func (o *Operations) Reboot(ctx context.Context) error {
return err return err
} }
const DeviceHeight = 0.134
// SetBase implements protocol.Operations. // SetBase implements protocol.Operations.
func (o *Operations) SetBase(ctx context.Context, funcs ...protocol.SetBaseOptionFunc) error { func (o *Operations) SetBase(ctx context.Context, funcs ...protocol.SetBaseOptionFunc) error {
config, err := o.GetConfiguration(ctx) config, err := o.GetConfiguration(ctx)
@ -96,17 +98,41 @@ func (o *Operations) SetBase(ctx context.Context, funcs ...protocol.SetBaseOptio
base.Coordinates.Longitude = *opts.Longitude base.Coordinates.Longitude = *opts.Longitude
} }
if opts.AntennaOffset != nil {
base.AntennaOffset = *opts.AntennaOffset
}
if _, err := o.PostBaseCoordinates(ctx, base); err != nil { if _, err := o.PostBaseCoordinates(ctx, base); err != nil {
return errors.WithStack(err) return errors.WithStack(err)
} }
if opts.AntennaOffset != nil {
device := &model.ConfigurationDevice{
AntennaHeight: *opts.AntennaOffset + DeviceHeight,
}
if _, err := o.PostDevice(ctx, device); err != nil {
return errors.WithStack(err)
}
}
return nil return nil
} }
// GetBaseInfo implements protocol.Operations.
func (o *Operations) GetBaseInfo(ctx context.Context) (*protocol.BaseInfo, error) {
config, err := o.GetConfiguration(ctx)
if err != nil {
return nil, errors.WithStack(err)
}
baseInfo := &protocol.BaseInfo{
Mode: config.BaseMode.BaseCoordinates.Mode,
AntennaOffset: config.BaseMode.BaseCoordinates.AntennaOffset - DeviceHeight,
Height: config.BaseMode.BaseCoordinates.Coordinates.Height,
Latitude: config.BaseMode.BaseCoordinates.Coordinates.Latitude,
Longitude: config.BaseMode.BaseCoordinates.Coordinates.Longitude,
}
return baseInfo, nil
}
// Configuration implements protocol.Operations. // Configuration implements protocol.Operations.
func (o *Operations) Configuration(ctx context.Context) (any, error) { func (o *Operations) Configuration(ctx context.Context) (any, error) {
config, err := o.GetConfiguration(ctx) config, err := o.GetConfiguration(ctx)