This repository has been archived on 2024-08-02. You can view files and clone it, but cannot push or open issues or pull requests.
orion/emlid/reachview/broadcast.go

54 lines
1.2 KiB
Go
Raw Normal View History

2020-11-26 17:00:13 +01:00
package reachview
import (
"context"
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)
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():
c.Off(eventBroadcast)
close(out)
default:
out <- res
}
}
if err := c.On(eventBroadcast, handler); err != nil {
return nil, errors.WithStack(err)
}
return out, nil
}