From daadb9b6788b1f6928ef980e4d250c31dbd4f2f8 Mon Sep 17 00:00:00 2001 From: cmsassot Date: Tue, 8 Jul 2025 09:00:13 +0200 Subject: [PATCH] feat(reachview): GetModemConfiguration func --- cmd/message/main.go | 70 +++++++++++++++++++ reach/client/operations.go | 15 ++++ reach/client/protocol/operations.go | 3 + reach/client/protocol/testsuite/operations.go | 23 ++++++ .../protocol/testsuite/operations_test.go | 16 ----- reach/client/protocol/v1/operations.go | 5 ++ reach/client/protocol/v2/internal.go | 10 +++ reach/client/protocol/v2/model/action.go | 2 +- reach/client/protocol/v2/model/modem.go | 22 ++++++ reach/client/protocol/v2/operations.go | 11 +++ 10 files changed, 160 insertions(+), 17 deletions(-) create mode 100644 cmd/message/main.go delete mode 100644 reach/client/protocol/testsuite/operations_test.go diff --git a/cmd/message/main.go b/cmd/message/main.go new file mode 100644 index 0000000..f3c3860 --- /dev/null +++ b/cmd/message/main.go @@ -0,0 +1,70 @@ +package main + +import ( + "context" + "encoding/json" + "flag" + "fmt" + "log/slog" + "os" + + reach "forge.cadoles.com/cadoles/go-emlid/reach/client" + "forge.cadoles.com/cadoles/go-emlid/reach/client/logger" + "github.com/pkg/errors" +) + +var ( + host string = "192.168.42.1" + filter string = "task" + rawLogLevel string = "ERROR" +) + +func init() { + flag.StringVar(&rawLogLevel, "log-level", rawLogLevel, "log level") + flag.StringVar(&host, "host", host, "the reachrs module host") + flag.StringVar(&filter, "filter", filter, "filter the type messages by name") +} + +func main() { + flag.Parse() + + ctx := context.Background() + client := reach.NewClient(host) + + logLevel, err := logger.ParseLevel(rawLogLevel) + if err != nil { + fmt.Printf("[FATAL] %+v", errors.WithStack(err)) + os.Exit(1) + } + + slog.SetLogLoggerLevel(logLevel) + + if err := client.Connect(ctx); err != nil { + fmt.Printf("[FATAL] %+v", errors.WithStack(err)) + os.Exit(1) + } + + defer func() { + if err := client.Close(ctx); err != nil { + fmt.Printf("[FATAL] %+v", errors.WithStack(err)) + os.Exit(1) + } + }() + fmt.Println(filter) + messages, err := reach.OnMessageType(ctx, client, filter) + if err != nil { + fmt.Printf("[FATAL] %+v", errors.WithStack(err)) + os.Exit(1) + } + + for b := range messages { + + data, err := json.MarshalIndent(b, "", " ") + if err != nil { + fmt.Printf("[ERROR] %+v", errors.WithStack(err)) + continue + } + + fmt.Println(string(data)) + } +} diff --git a/reach/client/operations.go b/reach/client/operations.go index 86ef123..c98b675 100644 --- a/reach/client/operations.go +++ b/reach/client/operations.go @@ -207,4 +207,19 @@ func (c *Client) SetModem(ctx context.Context, funcs ...protocol.SetModemOptions return nil } +// GetModemConfiguration implements protocol.Operations. +func (c *Client) GetModemConfiguration(ctx context.Context) (any, error) { + _, ops, err := c.getProtocol(ctx) + if err != nil { + return nil, errors.WithStack(err) + } + + config, err := ops.GetModemConfiguration(ctx) + if err != nil { + return nil, errors.WithStack(err) + } + + return config, nil +} + var _ protocol.Operations = &Client{} diff --git a/reach/client/protocol/operations.go b/reach/client/protocol/operations.go index e3ddc49..8c9fe1c 100644 --- a/reach/client/protocol/operations.go +++ b/reach/client/protocol/operations.go @@ -61,4 +61,7 @@ type Operations interface { //SetModem updates mobile data config SetModem(ctx context.Context, funcs ...SetModemOptionsFunc) error + + //GetModemConfiguration mobile data config + GetModemConfiguration(ctx context.Context) (any, error) } diff --git a/reach/client/protocol/testsuite/operations.go b/reach/client/protocol/testsuite/operations.go index 0c56772..d7bd535 100644 --- a/reach/client/protocol/testsuite/operations.go +++ b/reach/client/protocol/testsuite/operations.go @@ -377,6 +377,29 @@ var testCases = []operationTestCase{ t.Logf("Message : %+v", taskMsg) }, }, + { + Name: "GetModemConfiguration", + Run: func(t *testing.T, ops protocol.Operations) { + ctx := context.Background() + + if err := ops.Connect(ctx); err != nil { + t.Errorf("%+v", errors.WithStack(err)) + return + } + + defer func() { + if err := ops.Close(ctx); err != nil { + t.Errorf("%+v", errors.WithStack(err)) + } + }() + config, err := ops.GetModemConfiguration(ctx) + if err != nil { + t.Errorf("%+v", errors.WithStack(err)) + return + } + t.Logf("Modem configuration : %+v", config) + }, + }, } func TestOperations(t *testing.T, opsFactory OperationsFactoryFunc) { diff --git a/reach/client/protocol/testsuite/operations_test.go b/reach/client/protocol/testsuite/operations_test.go deleted file mode 100644 index 8948c9e..0000000 --- a/reach/client/protocol/testsuite/operations_test.go +++ /dev/null @@ -1,16 +0,0 @@ -package testsuite - -import ( - "testing" - - "forge.cadoles.com/cadoles/go-emlid/reach/client" - "forge.cadoles.com/cadoles/go-emlid/reach/client/protocol" -) - -func TestReachOperations(t *testing.T) { - opsFactory := func(addr string) (protocol.Operations, error) { - return client.NewClient(addr), nil - } - - TestOperations(t, opsFactory) -} diff --git a/reach/client/protocol/v1/operations.go b/reach/client/protocol/v1/operations.go index 8aad20c..1db989a 100644 --- a/reach/client/protocol/v1/operations.go +++ b/reach/client/protocol/v1/operations.go @@ -395,4 +395,9 @@ func (o *Operations) SetModem(ctx context.Context, funcs ...protocol.SetModemOpt return protocol.ErrUnimplemented } +// Deprecated : is no longer maintained for modules in V1 +func (o *Operations) GetModemConfiguration(ctx context.Context) (any, error) { + return nil, protocol.ErrUnimplemented +} + var _ protocol.Operations = &Operations{} diff --git a/reach/client/protocol/v2/internal.go b/reach/client/protocol/v2/internal.go index b0027ec..083b404 100644 --- a/reach/client/protocol/v2/internal.go +++ b/reach/client/protocol/v2/internal.go @@ -173,3 +173,13 @@ func (o *Operations) PostModem(ctx context.Context, config *model.ModemAuthentic return &updated, nil } + +func (o *Operations) GetModem(ctx context.Context) (*model.ModemConfiguration, error) { + config := &model.ModemConfiguration{} + + if err := o.GetJSON("/modem/1/info", config); err != nil { + return nil, errors.WithStack(err) + } + + return config, nil +} diff --git a/reach/client/protocol/v2/model/action.go b/reach/client/protocol/v2/model/action.go index 2f0ed35..a16380b 100644 --- a/reach/client/protocol/v2/model/action.go +++ b/reach/client/protocol/v2/model/action.go @@ -2,5 +2,5 @@ package model type Action struct { Name string `json:"name"` - Paylaod map[string]any `json:"payload"` + Paylaod map[string]any `json:"payload,omitempty"` } diff --git a/reach/client/protocol/v2/model/modem.go b/reach/client/protocol/v2/model/modem.go index bfcf7da..b8f72f8 100644 --- a/reach/client/protocol/v2/model/modem.go +++ b/reach/client/protocol/v2/model/modem.go @@ -10,3 +10,25 @@ type ModemAuthentication struct { Password string `json:"password,omitempty"` } `json:"authentication"` } + +type ModemConfiguration struct { + AccessTechnology string `json:"access_technology"` + AllowedModes []string `json:"allowed_modes"` + AvailableAPNs []string `json:"available_apns,omitempty"` + CurrentAPN string `json:"current_apn"` + CurrentMode string `json:"current_mode"` + FailReason *string `json:"fail_reason,omitempty"` + IMEI string `json:"imei"` + InternetAvailable string `json:"internet_available,omitempty"` + LockReason *string `json:"lock_reason,omitempty"` + OperatorName string `json:"operator_name"` + PreferredMode string `json:"preferred_mode"` + RegistrationState string `json:"registration_state"` + RSSI int `json:"rssi"` + State string `json:"state"` + Stats struct { + Since string `json:"since"` + UsageMB string `json:"usage_mb"` + } `json:"stats"` + UnlockRetries int `json:"unlock_retries"` +} diff --git a/reach/client/protocol/v2/operations.go b/reach/client/protocol/v2/operations.go index bd79346..5deeb39 100644 --- a/reach/client/protocol/v2/operations.go +++ b/reach/client/protocol/v2/operations.go @@ -399,4 +399,15 @@ func (o *Operations) SetModem(ctx context.Context, funcs ...protocol.SetModemOpt return nil } +// SetModem implements protocol.Operations. +func (o *Operations) GetModemConfiguration(ctx context.Context) (any, error) { + config, err := o.GetModem(ctx) + if err != nil { + return nil, errors.WithStack(err) + } + + return config, nil + +} + var _ protocol.Operations = &Operations{}