feat(AveragePosition): method to emit average_base_coordinates message
This commit is contained in:
parent
81741d20c1
commit
abebc7d8c6
@ -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
|
||||
|
@ -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{}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -1,5 +1,6 @@
|
||||
package model
|
||||
|
||||
type Action struct {
|
||||
Name string `json:"name"`
|
||||
Name string `json:"name"`
|
||||
Paylaod map[string]any `json:"payload"`
|
||||
}
|
||||
|
@ -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{}
|
||||
|
Loading…
x
Reference in New Issue
Block a user