applied the same start-stop scheme to the BeamNG provider
This commit is contained in:
+17
-14
@@ -28,7 +28,7 @@ type BeamNG struct {
|
|||||||
updaters [telemetry.MaxFields]func(*telemetry.TelemetryField)
|
updaters [telemetry.MaxFields]func(*telemetry.TelemetryField)
|
||||||
|
|
||||||
// stream control
|
// stream control
|
||||||
streamCh chan telemetry.TelemetryData
|
wg sync.WaitGroup
|
||||||
streamCancel context.CancelFunc
|
streamCancel context.CancelFunc
|
||||||
|
|
||||||
// timing
|
// timing
|
||||||
@@ -46,12 +46,11 @@ func NewBeamNGProvider(logger *slog.Logger, opts *bngsdk.Options) (*BeamNG, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
provider := &BeamNG{
|
provider := &BeamNG{
|
||||||
logger: logger.With("TelemetryProvider", NAME),
|
logger: logger.With("TelemetryProvider", NAME),
|
||||||
streamCh: make(chan telemetry.TelemetryData, 1),
|
data: telemetry.NewTelemetryData(),
|
||||||
data: telemetry.NewTelemetryData(),
|
SDK: beam,
|
||||||
SDK: beam,
|
og: &bngsdk.Outgauge{},
|
||||||
og: &bngsdk.Outgauge{},
|
ticker: time.NewTicker(time.Second / 60),
|
||||||
ticker: time.NewTicker(time.Second / 60),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
provider.updaters = [telemetry.MaxFields]func(*telemetry.TelemetryField){
|
provider.updaters = [telemetry.MaxFields]func(*telemetry.TelemetryField){
|
||||||
@@ -110,9 +109,9 @@ func (b *BeamNG) Stream() (<-chan telemetry.TelemetryData, error) {
|
|||||||
ctx, b.streamCancel = context.WithCancel(context.Background())
|
ctx, b.streamCancel = context.WithCancel(context.Background())
|
||||||
|
|
||||||
// Start the stream
|
// Start the stream
|
||||||
b.stream(ctx)
|
ch := b.stream(ctx)
|
||||||
|
|
||||||
return b.streamCh, nil
|
return ch, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BeamNG) Subscribe(requestFields []telemetry.FieldID) {
|
func (b *BeamNG) Subscribe(requestFields []telemetry.FieldID) {
|
||||||
@@ -162,7 +161,6 @@ func (b *BeamNG) Subscribe(requestFields []telemetry.FieldID) {
|
|||||||
|
|
||||||
func (b *BeamNG) readData() {
|
func (b *BeamNG) readData() {
|
||||||
slog.Debug("READING THIS DATA")
|
slog.Debug("READING THIS DATA")
|
||||||
// BUG: getting stuck in here
|
|
||||||
ogSnapshot, err := b.SDK.Update()
|
ogSnapshot, err := b.SDK.Update()
|
||||||
slog.Debug("THE DATA WAS READ")
|
slog.Debug("THE DATA WAS READ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -193,10 +191,15 @@ func (b *BeamNG) readData() {
|
|||||||
b.data.LastDataPoll = time.Now()
|
b.data.LastDataPoll = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BeamNG) stream(ctx context.Context) {
|
func (b *BeamNG) stream(ctx context.Context) <-chan telemetry.TelemetryData {
|
||||||
b.data.InitialTime = time.Now()
|
b.data.InitialTime = time.Now()
|
||||||
|
outCh := make(chan telemetry.TelemetryData)
|
||||||
|
b.wg.Add(1)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
defer b.wg.Done()
|
||||||
|
defer close(outCh)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// Explicitly intercept cancellation
|
// Explicitly intercept cancellation
|
||||||
select {
|
select {
|
||||||
@@ -205,8 +208,6 @@ func (b *BeamNG) stream(ctx context.Context) {
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: add a method to check if there's data available, or make this happen
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
@@ -217,7 +218,7 @@ func (b *BeamNG) stream(ctx context.Context) {
|
|||||||
|
|
||||||
// Publish data
|
// Publish data
|
||||||
select {
|
select {
|
||||||
case b.streamCh <- *b.data:
|
case outCh <- *b.data:
|
||||||
slog.Debug("PUBLISHED DATA")
|
slog.Debug("PUBLISHED DATA")
|
||||||
default:
|
default:
|
||||||
// skip this data, don't allow publishers to lag behind
|
// skip this data, don't allow publishers to lag behind
|
||||||
@@ -225,4 +226,6 @@ func (b *BeamNG) stream(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
return outCh
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user