package reachview import ( "context" "sync" gosocketio "forge.cadoles.com/Pyxis/golang-socketio" "github.com/mitchellh/mapstructure" "github.com/pkg/errors" ) const ( eventBroadcast = "broadcast" ) const ( BroadcastRoverStatus = "rover_status" BroadcastBaseStatus = "base_status" BroadcastRTKStatus = "rtk_status" BroadcastObservations = "observations" BroadcastTimeMarks = "time_marks" BroadcastBatteryStatus = "battery_status" ) // Broadcast is a broadcasted message containing modules status informations. type Broadcast struct { Name string `mapstructure:"name"` Payload map[string]interface{} `mapstructure:"payload"` } // Broadcast listens for broadcast messages. func (c *Client) Broadcast(ctx context.Context) (chan Broadcast, error) { out := make(chan Broadcast) closer := new(sync.Once) handler := func(_ *gosocketio.Channel, data interface{}) { res := Broadcast{} if err := mapstructure.WeakDecode(data, &res); err != nil { c.Logf("error while decoding broadcast message: %s", errors.WithStack(err)) } select { case <-ctx.Done(): closer.Do(func() { c.Off(eventBroadcast) close(out) }) return default: out <- res } } if err := c.On(eventBroadcast, handler); err != nil { return nil, errors.WithStack(err) } return out, nil }