feat: initial commit

This commit is contained in:
2024-07-30 14:28:39 +02:00
commit 4729c54076
39 changed files with 3850 additions and 0 deletions

View File

@ -0,0 +1,7 @@
package v1
import "forge.cadoles.com/cadoles/go-emlid/reach/client/protocol"
func init() {
protocol.Register(&Protocol{})
}

View File

@ -0,0 +1,91 @@
package v1
import (
"context"
"sync"
"forge.cadoles.com/cadoles/go-emlid/reach/client/protocol"
"forge.cadoles.com/cadoles/go-emlid/reach/client/protocol/v1/model"
"forge.cadoles.com/cadoles/go-emlid/reach/client/socketio"
"github.com/mitchellh/mapstructure"
"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"
)
type configurationApplied struct {
Configuration *model.Configuration `mapstructure:"configuration,omitempty"`
Result string `mapstructure:"result"`
Constraints *model.Constraints `mapstructure:"constraints,omitempty"`
}
func (o *Operations) ApplyConfiguration(ctx context.Context, config *model.Configuration) (string, *model.Configuration, error) {
res := &configurationApplied{}
if err := o.ReqResp(ctx, eventApplyConfiguration, config, eventConfigurationApplied, res); err != nil {
return configurationApplyFailed, nil, err
}
return res.Result, res.Configuration, nil
}
func (o *Operations) RequestConfiguration(ctx context.Context) (*model.Configuration, error) {
configuration := &model.Configuration{}
if err := o.ReqResp(ctx, eventGetConfiguration, nil, eventCurrentConfiguration, configuration); err != nil {
return nil, err
}
return configuration, nil
}
// ReqResp emits an event with the given data and waits for a response
func (o *Operations) ReqResp(ctx context.Context,
requestEvent string, requestData any,
responseEvent string, res any) error {
o.mutex.RLock()
defer o.mutex.RUnlock()
if o.client == nil || !o.client.Alive() {
return errors.WithStack(protocol.ErrClosed)
}
var err error
var wg sync.WaitGroup
var once sync.Once
done := func() {
o.client.Off(responseEvent)
wg.Done()
}
wg.Add(1)
go func() {
<-ctx.Done()
err = ctx.Err()
once.Do(done)
}()
err = o.client.On(responseEvent, func(_ *socketio.Channel, data interface{}) {
if res != nil {
err = mapstructure.WeakDecode(data, res)
}
once.Do(done)
})
if err != nil {
return errors.Wrapf(err, "error while binding to '%s' event", responseEvent)
}
if err = o.client.Emit(requestEvent, requestData); err != nil {
return errors.Wrapf(err, "error while emitting event '%s'", requestEvent)
}
wg.Wait()
return err
}

View File

@ -0,0 +1,243 @@
package model
var (
// On -
On = String("on")
// Off -
Off = String("off")
// True -
True = Bool(true)
// False -
False = Bool(false)
// GPSARModeFixAndHold -
GPSARModeFixAndHold = String("fix-and-hold")
// GPSARModeContinuous -
GPSARModeContinuous = String("continuous")
// PositionningModeKinematic -
PositionningModeKinematic = String("kinematic")
// PositionningModeSingle -
PositionningModeSingle = String("single")
// PositionningModeStatic -
PositionningModeStatic = String("static")
// IOFormatRTCM3 -
IOFormatRTCM3 = String("rtcm3")
// IOTypeLoRa -
IOTypeLoRa = String("lora")
// BaseCoordinatesModeManual -
BaseCoordinatesModeManual = String("manual")
// BaseCoordinatesModeAverageFix -
BaseCoordinatesModeAverageFix = String("fix-and-hold")
// BaseCoordinatesModeAverageSingle -
BaseCoordinatesModeAverageSingle = String("single-and-hold")
// BaseCoordinatesModeAverageFloat -
BaseCoordinatesModeAverageFloat = String("float-and-hold")
// BaseCoordinatesFormatLLH -
BaseCoordinatesFormatLLH = String("llh")
// BaseCoordinatesFormatXYZ -
BaseCoordinatesFormatXYZ = String("xyz")
)
// String returns an string pointer
// This is a helper to constructs partials configations objects
// for the ApplyConfiguration() method
func String(v string) *string {
return &v
}
// Int returns an int pointer
// This is a helper to constructs partials configations objects
// for the ApplyConfiguration() method
func Int(v int) *int {
return &v
}
// Bool returns a bool pointer
// This is a helper to constructs partials configations objects
// for the ApplyConfiguration() method
func Bool(v bool) *bool {
return &v
}
// Float returns a float64 pointer
// This is a helper to constructs partials configations objects
// for the ApplyConfiguration() method
func Float(v float64) *float64 {
return &v
}
// Configuration -
type Configuration struct {
RTKSettings *RTKSettings `mapstructure:"rtk settings,omitempty" json:"rtk settings,omitempty"`
CorrectionInput *CorrectionInput `mapstructure:"correction input,omitempty" json:"correction input,omitempty"`
PositionOutput *PositionOutput `mapstructure:"position output,omitempty" json:"position output,omitempty"`
BaseMode *BaseMode `mapstructure:"base mode,omitempty" json:"base mode,omitempty"`
Logging *Logging `mapstructure:"logging,omitempty" json:"logging,omitempty"`
Bluetooth *Bluetooth `mapstructure:"bluetooth,omitempty" json:"bluetooth,omitempty"`
LoRa *LoRa `mapstructure:"lora,omitempty" json:"lora,omitempty"`
Constraints *Constraints `mapstructure:"constraints,omitempty" json:"constraints,omitempty"`
}
// RTKSettings -
type RTKSettings struct {
GLONASSARMode *string `mapstructure:"glonass ar mode,omitempty" json:"glonass ar mode,omitempty"`
UpdateRate *float64 `mapstructure:"update rate,omitempty" json:"update rate,omitempty"`
ElevationMaskAngle *string `mapstructure:"elevation mask angle,omitempty" json:"elevation mask angle,omitempty"`
MaxHorizontalAcceleration *string `mapstructure:"max horizontal acceleration,omitempty" json:"max horizontal acceleration,omitempty"`
SNRMask *string `mapstructure:"snr mask,omitempty" json:"snr mask,omitempty"`
GPSARMode *string `mapstructure:"gps ar mode,omitempty" json:"gps ar mode,omitempty"`
PositionningMode *string `mapstructure:"positioning mode,omitempty" json:"positioning mode,omitempty"`
PositioningSystems *PositionningSystems `mapstructure:"positioning systems,omitempty" json:"positioning systems,omitempty"`
MaxVerticalAcceleration *string `mapstructure:"max vertical acceleration,omitempty" json:"max vertical acceleration,omitempty"`
}
// PositionningSystems -
type PositionningSystems struct {
GLONASS *bool `mapstructure:"glonass,omitempty" json:"glonass,omitempty"`
SBAS *bool `mapstructure:"sbas,omitempty" json:"sbas,omitempty"`
QZS *bool `mapstructure:"qzs,omitempty" json:"qzs,omitempty"`
QZSS *bool `mapstructure:"qzss,omitempty" json:"qzss,omitempty"`
Compass *bool `mapstructure:"compass,omitempty" json:"compass,omitempty"`
Galileo *bool `mapstructure:"galileo,omitempty" json:"galileo,omitempty"`
GPS *bool `mapstructure:"gps,omitempty" json:"gps,omitempty"`
}
// CorrectionInput -
type CorrectionInput struct {
Input2 *Input2 `mapstructure:"input2,omitempty" json:"input2,omitempty"`
Input3 *Input3 `mapstructure:"input3,omitempty" json:"input3,omitempty"`
}
// Input -
type Input struct {
Path *string `mapstructure:"path,omitempty" json:"path,omitempty"`
Type *string `mapstructure:"type,omitempty" json:"type,omitempty"`
Enabled *bool `mapstructure:"enabled,omitempty" json:"enabled,omitempty"`
Format *string `mapstructure:"format,omitempty" json:"format,omitempty"`
}
// Input2 -
type Input2 struct {
Input `mapstructure:",squash"`
SendPositionToBase *string `mapstructure:"send position to base,omitempty" json:"send position to base,omitempty"`
}
// Input3 -
type Input3 struct {
Input `mapstructure:",squash"`
}
// PositionOutput -
type PositionOutput struct {
Output1 *Output `mapstructure:"output1,omitempty" json:"output1,omitempty"`
Output2 *Output `mapstructure:"output2,omitempty" json:"output2,omitempty"`
Output3 *Output `mapstructure:"output3,omitempty" json:"output3,omitempty"`
Output4 *Output `mapstructure:"output4,omitempty" json:"output4,omitempty"`
}
// Output -
type Output struct {
Path *string `mapstructure:"path,omitempty" json:"path,omitempty"`
Type *string `mapstructure:"type,omitempty" json:"type,omitempty"`
Enabled *bool `mapstructure:"enabled,omitempty" json:"enabled,omitempty"`
Format *string `mapstructure:"format,omitempty" json:"format,omitempty"`
}
// BaseMode -
type BaseMode struct {
Output *Output `mapstructure:"output,omitempty" json:"output,omitempty"`
BaseCoordinates *BaseCoordinates `mapstructure:"base coordinates,omitempty" json:"base coordinates,omitempty"`
RTCM3Messages *RTCM3Messages `mapstructure:"rtcm3 messages,omitempty" json:"rtcm3 messages,omitempty"`
}
// BaseCoordinates -
type BaseCoordinates struct {
Format *string `mapstructure:"format,omitempty" json:"format,omitempty"`
AntennaOffset *AntennaOffset `mapstructure:"antenna offset,omitempty" json:"antenna offset,omitempty"`
Accumulation *string `mapstructure:"accumulation,omitempty" json:"accumulation,omitempty"`
Coordinates []*string `mapstructure:"coordinates,omitempty" json:"coordinates,omitempty"`
Mode *string `mapstructure:"mode,omitempty" json:"mode,omitempty"`
}
// AntennaOffset -
type AntennaOffset struct {
East *string `mapstructure:"east,omitempty" json:"east,omitempty"`
North *string `mapstructure:"north,omitempty" json:"north,omitempty"`
Up *string `mapstructure:"up,omitempty" json:"up,omitempty"`
}
// RTCM3Messages -
type RTCM3Messages struct {
// GPS L1 code and phase and ambiguities and carrier-to-noise ratio
Type1002 *RTCMMessageType `mapstructure:"1002,omitemtpy" json:"1002,omitemtpy"`
// Station coordinates XYZ for antenna reference point and antenna height.
Type1006 *RTCMMessageType `mapstructure:"1006,omitemtpy" json:"1006,omitemtpy"`
// Antenna serial number.
Type1008 *RTCMMessageType `mapstructure:"1008,omitemtpy" json:"1008,omitemtpy"`
// GLONASS L1 code and phase and ambiguities and carrier-to-noise ratio.
Type1010 *RTCMMessageType `mapstructure:"1010,omitemtpy" json:"1010,omitemtpy"`
// GPS ephemeris.
Type1019 *RTCMMessageType `mapstructure:"1019,omitemtpy" json:"1019,omitemtpy"`
// GLONASS ephemeris.
Type1020 *RTCMMessageType `mapstructure:"1020,omitemtpy" json:"1020,omitemtpy"`
// The type 7 Multiple Signal Message format for Europes Galileo system
Type1097 *RTCMMessageType `mapstructure:"1097,omitemtpy" json:"1097,omitemtpy"`
// Full SBAS pseudo-ranges, carrier phases, Doppler and signal strength (high resolution)
Type1107 *RTCMMessageType `mapstructure:"1107,omitemtpy" json:"1107,omitemtpy"`
// Full QZSS pseudo-ranges, carrier phases, Doppler and signal strength (high resolution)
Type1117 *RTCMMessageType `mapstructure:"1117,omitemtpy" json:"1117,omitemtpy"`
// Full BeiDou pseudo-ranges, carrier phases, Doppler and signal strength (high resolution)
Type1127 *RTCMMessageType `mapstructure:"1127,omitemtpy" json:"1127,omitemtpy"`
}
// RTCMMessageType -
type RTCMMessageType struct {
Frequency *string `mapstructure:"frequency,omitempty" json:"frequency,omitempty"`
Enabled *bool `mapstructure:"enabled,omitempty" json:"enabled,omitempty"`
}
// Logging -
type Logging struct {
Correction *LoggingService `mapstructure:"correction,omitempty" json:"correction,omitempty"`
Interval *int `mapstructure:"interval,omitempty" json:"interval,omitempty"`
Solution *LoggingService `mapstructure:"solution,omitempty" json:"solution,omitempty"`
Raw *LoggingService `mapstructure:"raw,omitempty" json:"raw,omitempty"`
Base *LoggingService `mapstructure:"base,omitempty" json:"base,omitempty"`
Overwrite *bool `mapstructure:"overwrite,omitempty" json:"overwrite,omitempty"`
}
// LoggingService -
type LoggingService struct {
Started *bool `mapstructure:"started,omitempty" json:"started,omitempty"`
Version *string `mapstructure:"version,omitempty" json:"version,omitempty"`
Format *string `mapstructure:"format,omitempty" json:"format,omitempty"`
}
// Bluetooth -
type Bluetooth struct {
Enabled *bool `mapstructure:"enabled,omitempty" json:"enabled,omitempty"`
Discoverable *bool `mapstructure:"discoverable,omitempty" json:"discoverable,omitempty"`
Pin *int `mapstructure:"pin,omitempty" json:"pin,omitempty"`
}
// LoRa -
type LoRa struct {
AirRate *string `mapstructure:"air rate,omitempty" json:"air rate,omitempty"`
Frequency *float64 `mapstructure:"frequency,omitempty" json:"frequency,omitempty"`
OutputPower *string `mapstructure:"output power,omitempty" json:"output power,omitempty"`
}
// Constraints -
type Constraints struct {
LoRa *LoRaConstraints `mapstructure:"lora,omitempty" json:"lora,omitempty"`
}
// LoRaConstraints -
type LoRaConstraints struct {
Frequency [][]int `mapstructure:"frequency,omitempty" json:"frequency,omitempty"`
}
// LoRaFrequencyRange -
type LoRaFrequencyRange struct {
Min *int `mapstructure:"min,omitempty" json:"min,omitempty"`
Max *int `mapstructure:"max,omitempty" json:"max,omitempty"`
}

View File

@ -0,0 +1,283 @@
package v1
import (
"context"
"strconv"
"strings"
"sync"
"forge.cadoles.com/cadoles/go-emlid/reach/client/protocol"
"forge.cadoles.com/cadoles/go-emlid/reach/client/protocol/v1/model"
"forge.cadoles.com/cadoles/go-emlid/reach/client/socketio"
"github.com/pkg/errors"
)
type Operations struct {
addr string
client *socketio.Client
mutex sync.RWMutex
}
// Close implements protocol.Operations.
func (o *Operations) Close(ctx context.Context) error {
o.mutex.Lock()
defer o.mutex.Unlock()
if o.client == nil {
return nil
}
o.client.Close()
return nil
}
const (
// EventBrowserConnected is emitted after the initial connection to the
// ReachView endpoint
eventBrowserConnected = "browser connected"
)
// Connect implements protocol.Operations.
func (o *Operations) Connect(ctx context.Context) error {
o.mutex.Lock()
defer o.mutex.Unlock()
if o.client != nil {
o.client.Close()
}
endpoint, err := socketio.EndpointFromHAddr(o.addr)
if err != nil {
return errors.WithStack(err)
}
client := socketio.NewClient(endpoint)
if err := client.Connect(); err != nil {
return errors.WithStack(err)
}
// Notifies the ReachView endpoint of a new connection.
// See misc/reachview/update_main.js line 297
payload := map[string]string{
"data": "I'm connected",
}
if err := client.Emit(eventBrowserConnected, payload); err != nil {
return errors.WithStack(err)
}
o.client = client
return nil
}
// Emit implements protocol.Operations.
func (o *Operations) Emit(ctx context.Context, mType string, message any) error {
o.mutex.RLock()
defer o.mutex.RUnlock()
if o.client == nil || !o.client.Alive() {
return errors.WithStack(protocol.ErrClosed)
}
if err := o.client.Emit(mType, message); err != nil {
return errors.WithStack(err)
}
return nil
}
// On implements protocol.Operations.
func (o *Operations) On(ctx context.Context, event string) (chan any, error) {
o.mutex.RLock()
defer o.mutex.RUnlock()
if o.client == nil || !o.client.Alive() {
return nil, errors.WithStack(protocol.ErrClosed)
}
out := make(chan any)
closer := new(sync.Once)
handler := func(ch *socketio.Channel, data any) {
select {
case <-ctx.Done():
closer.Do(func() {
o.mutex.RLock()
defer o.mutex.RUnlock()
ch.Close()
close(out)
if o.client == nil {
return
}
})
return
default:
out <- data
}
}
if err := o.client.On(event, handler); err != nil {
return nil, errors.WithStack(err)
}
return out, nil
}
// Alive implements protocol.Operations.
func (o *Operations) Alive(ctx context.Context) (bool, error) {
o.mutex.RLock()
defer o.mutex.RUnlock()
if o.client == nil || !o.client.Alive() {
return false, errors.WithStack(protocol.ErrClosed)
}
return o.client.Alive(), nil
}
// Configuration implements protocol.Operations.
func (o *Operations) Configuration(ctx context.Context) (any, error) {
config, err := o.RequestConfiguration(ctx)
if err != nil {
return nil, errors.WithStack(err)
}
return config, nil
}
const (
eventReboot = "reboot"
)
// Reboot implements protocol.Operations.
func (o *Operations) Reboot(ctx context.Context) error {
o.mutex.RLock()
defer o.mutex.RUnlock()
if o.client == nil || !o.client.Alive() {
return errors.WithStack(protocol.ErrClosed)
}
var err error
var wg sync.WaitGroup
var once sync.Once
done := func() {
o.client.Off(socketio.OnDisconnection)
wg.Done()
}
wg.Add(1)
go func() {
<-ctx.Done()
err = ctx.Err()
once.Do(done)
}()
err = o.client.On(socketio.OnDisconnection, func(h *socketio.Channel, data any) {
once.Do(done)
})
if err != nil {
return errors.Wrapf(err, "error while binding to '%s' event", socketio.OnDisconnection)
}
if err = o.client.Emit(eventReboot, nil); err != nil {
return err
}
wg.Wait()
return err
}
const (
// ConfigurationApplySuccess -
configurationApplySuccess = "success"
// ConfigurationApplyFailed -
configurationApplyFailed = "failed"
)
// SetBase implements protocol.Operations.
func (o *Operations) SetBase(ctx context.Context, funcs ...protocol.SetBaseOptionFunc) error {
opts := protocol.NewSetBaseOptions(funcs...)
var lat string
if opts.Latitude != nil {
lat = strconv.FormatFloat(*opts.Latitude, 'f', -1, 64)
}
var lon string
if opts.Longitude != nil {
lon = strconv.FormatFloat(*opts.Longitude, 'f', -1, 64)
}
var alt string
if opts.Height != nil {
alt = strconv.FormatFloat(*opts.Height, 'f', -1, 64)
}
var antennaHeight string
if opts.AntennaOffset != nil {
antennaHeight = strconv.FormatFloat(*opts.AntennaOffset, 'f', -1, 64)
}
config := &model.Configuration{
BaseMode: &model.BaseMode{
BaseCoordinates: &model.BaseCoordinates{
Accumulation: model.String("1"),
Coordinates: []*string{
model.String(lat),
model.String(lon),
model.String(alt),
},
AntennaOffset: &model.AntennaOffset{
East: model.String("0"),
North: model.String("0"),
Up: model.String(antennaHeight),
},
Format: model.BaseCoordinatesFormatLLH,
Mode: model.BaseCoordinatesModeManual,
},
},
}
result, _, err := o.ApplyConfiguration(ctx, config)
if err != nil {
return errors.New("configuration update failed")
}
if result != configurationApplySuccess {
return errors.New("configuration update failed")
}
return nil
}
const (
eventGetReachViewVersion = "get reachview version"
eventReachViewVersionResults = "current reachview version"
)
type reachViewVersion struct {
Version string `json:"version"`
Stable bool `json:"bool"`
}
// Version implements protocol.Operations.
func (o *Operations) Version(ctx context.Context) (string, bool, error) {
res := &reachViewVersion{}
if err := o.ReqResp(ctx, eventGetReachViewVersion, nil, eventReachViewVersionResults, res); err != nil {
return "", false, err
}
return strings.TrimSpace(res.Version), res.Stable, nil
}
var _ protocol.Operations = &Operations{}

View File

@ -0,0 +1,31 @@
package v1
import (
"context"
"testing"
"forge.cadoles.com/cadoles/go-emlid/reach/client/protocol"
"forge.cadoles.com/cadoles/go-emlid/reach/client/protocol/testsuite"
"github.com/pkg/errors"
)
func TestProtocolV1Operations(t *testing.T) {
proto := &Protocol{}
factory := func(addr string) (protocol.Operations, error) {
ctx := context.Background()
available, err := proto.Available(ctx, addr)
if err != nil {
return nil, errors.WithStack(err)
}
if !available {
return nil, errors.New("protocol is not available")
}
return proto.Operations(addr), nil
}
testsuite.TestOperations(t, factory)
}

View File

@ -0,0 +1,29 @@
package v1
import (
"context"
"forge.cadoles.com/cadoles/go-emlid/reach/client/protocol"
)
const Identifier protocol.Identifier = "v1"
type Protocol struct {
}
// Available implements protocol.Protocol.
func (p *Protocol) Available(ctx context.Context, addr string) (bool, error) {
return true, nil
}
// Identifier implements protocol.Protocol.
func (p *Protocol) Identifier() protocol.Identifier {
return Identifier
}
// Operations implements protocol.Protocol.
func (p *Protocol) Operations(addr string) protocol.Operations {
return &Operations{addr: addr}
}
var _ protocol.Protocol = &Protocol{}