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

61 lines
1.3 KiB
Go
Raw Normal View History

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