diff --git a/reach/client/helper.go b/reach/client/helper.go index 2e37c29..28f5167 100644 --- a/reach/client/helper.go +++ b/reach/client/helper.go @@ -41,6 +41,7 @@ func OnMessage[T any](ctx context.Context, client *Client, mType string) (chan T type Broadcast struct { Name string `mapstructure:"name" json:"name"` Payload any `mapstructure:"payload" json:"payload"` + State string `mapstructure:"state" json:"state"` } // OnBroadcast listens for ReachView "broadcast" messages diff --git a/reach/client/operations.go b/reach/client/operations.go index d446bd8..83abd19 100644 --- a/reach/client/operations.go +++ b/reach/client/operations.go @@ -152,4 +152,18 @@ func (c *Client) Reboot(ctx context.Context) error { return nil } +// AveragePosition implements protocol.Operations. +func (c *Client) AveragePosition(ctx context.Context) (*protocol.TaskMessage, error) { + _, ops, err := c.getProtocol(ctx) + if err != nil { + return nil, errors.WithStack(err) + } + + taskMsg, err := ops.AveragePosition(ctx) + if err != nil { + return nil, errors.WithStack(err) + } + + return taskMsg, err +} var _ protocol.Operations = &Client{} diff --git a/reach/client/protocol/operations.go b/reach/client/protocol/operations.go index 72a5bae..d916e70 100644 --- a/reach/client/protocol/operations.go +++ b/reach/client/protocol/operations.go @@ -1,6 +1,8 @@ package protocol -import "context" +import ( + "context" +) type BaseInfo struct { Mode string @@ -8,6 +10,12 @@ type BaseInfo struct { Latitude float64 Longitude float64 Height float64 + Accumulation int +} +type TaskMessage struct { + Name string `json:"name"` + State string `json:"state"` + Payload map[string]interface{} `json:"payload"` } type Operations interface { @@ -41,4 +49,7 @@ type Operations interface { // Reboot restarts the module Reboot(ctx context.Context) error + + // AveragePosition gathers data and computes the average position + AveragePosition(ctx context.Context) (*TaskMessage, error) } diff --git a/reach/client/protocol/testsuite/operations.go b/reach/client/protocol/testsuite/operations.go index 9e66d75..6a72599 100644 --- a/reach/client/protocol/testsuite/operations.go +++ b/reach/client/protocol/testsuite/operations.go @@ -240,6 +240,30 @@ var testCases = []operationTestCase{ }, }, + { + Name: "AveragePosition", + 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)) + } + }() + + taskmessage, err := ops.AveragePosition(ctx) + if err != nil { + t.Errorf("%+v", errors.WithStack(err)) + return + } + t.Logf("Task Message : %s", taskmessage) + }, + }, } func TestOperations(t *testing.T, opsFactory OperationsFactoryFunc) { diff --git a/reach/client/protocol/v2/model/action.go b/reach/client/protocol/v2/model/action.go index 57096f6..2f0ed35 100644 --- a/reach/client/protocol/v2/model/action.go +++ b/reach/client/protocol/v2/model/action.go @@ -1,5 +1,6 @@ package model type Action struct { - Name string `json:"name"` + Name string `json:"name"` + Paylaod map[string]any `json:"payload"` } diff --git a/reach/client/protocol/v2/operations.go b/reach/client/protocol/v2/operations.go index 7a8afb1..0ec7a72 100644 --- a/reach/client/protocol/v2/operations.go +++ b/reach/client/protocol/v2/operations.go @@ -2,6 +2,7 @@ package v2 import ( "context" + "encoding/json" "net/http" "sync" @@ -264,4 +265,35 @@ func (o *Operations) On(ctx context.Context, event string) (chan any, error) { return out, nil } +func (o *Operations) AveragePosition(ctx context.Context) (*protocol.TaskMessage, error) { + var err error + + go func() { + <-ctx.Done() + err = ctx.Err() + }() + + if err = o.client.Emit("task", &model.Action{Name: "average_base_coordinates"}); err != nil { + return nil, err + } + ch, err := o.On(ctx, "task_status") + for message := range ch { + // Convertir vers notre struct + jsonData, err := json.Marshal(message) + if err != nil { + continue + } + + var taskMsg protocol.TaskMessage + if err := json.Unmarshal(jsonData, &taskMsg); err != nil { + continue + } + + if taskMsg.State == "completed" { + return &taskMsg, nil + } + } + return nil, err +} + var _ protocol.Operations = &Operations{}