automated stream start
May return the user ability of stopping and starting but its really what we want
This commit is contained in:
+6
-1
@@ -83,14 +83,17 @@ func (ds *DeviceService) PeripheralExists(pname string) bool {
|
|||||||
// Getters [END] ---------------------------------------------------------------
|
// Getters [END] ---------------------------------------------------------------
|
||||||
|
|
||||||
// Actions [START] -------------------------------------------------------------
|
// Actions [START] -------------------------------------------------------------
|
||||||
func (ds *DeviceService) StartStream() {
|
func (ds *DeviceService) StartStream(ch <-chan telemetry.TelemetryData) {
|
||||||
// NOTE: i'm using this pattern a whole lot. Maybe I can create a struct to handle this
|
// NOTE: i'm using this pattern a whole lot. Maybe I can create a struct to handle this
|
||||||
|
ds.SetTelemetryChannel(ch)
|
||||||
|
|
||||||
var ctx context.Context
|
var ctx context.Context
|
||||||
ctx, ds.streamCancel = context.WithCancel(context.Background())
|
ctx, ds.streamCancel = context.WithCancel(context.Background())
|
||||||
|
|
||||||
ds.PSS.OnStartStream()
|
ds.PSS.OnStartStream()
|
||||||
|
|
||||||
go ds.transmit(ctx)
|
go ds.transmit(ctx)
|
||||||
|
ds.Messages <- "Device service started stream\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ds *DeviceService) StopStream() {
|
func (ds *DeviceService) StopStream() {
|
||||||
@@ -154,6 +157,8 @@ func (ds *DeviceService) peripheralConfigured(pname string, fields []telemetry.F
|
|||||||
// We need to retrigger field subscription here
|
// We need to retrigger field subscription here
|
||||||
subscribedTo := ds.triggerFieldSubscription(fields)
|
subscribedTo := ds.triggerFieldSubscription(fields)
|
||||||
ds.Messages <- fmt.Sprintf("subscribed to fields: %q\n", subscribedTo)
|
ds.Messages <- fmt.Sprintf("subscribed to fields: %q\n", subscribedTo)
|
||||||
|
|
||||||
|
ds.PSS.OnStartStreamPeripheral(pname)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Callbacks [END] -------------------------------------------------------------
|
// Callbacks [END] -------------------------------------------------------------
|
||||||
|
|||||||
@@ -211,6 +211,17 @@ func (pss *PeripheralStateStore) OnStartStream() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pss *PeripheralStateStore) OnStartStreamPeripheral(pname string) {
|
||||||
|
state, err := pss.GetState(pname)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if state.State == DeviceIsConfigured {
|
||||||
|
pss.setDeviceIsStreaming(pname)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (pss *PeripheralStateStore) OnStopStream() {
|
func (pss *PeripheralStateStore) OnStopStream() {
|
||||||
pss.mu.Lock()
|
pss.mu.Lock()
|
||||||
pss.isStreaming = false
|
pss.isStreaming = false
|
||||||
@@ -349,6 +360,7 @@ func (pss *PeripheralStateStore) configurePeripheral(
|
|||||||
onFailure(pname)
|
onFailure(pname)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
pss.Messages <- fmt.Sprintf("Configuring peripheral for provider: %s\n", provider)
|
||||||
|
|
||||||
err = state.Peripheral.Setup(provider)
|
err = state.Peripheral.Setup(provider)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ func NewOrchestrator(logger *slog.Logger) (*Orchestrator, error) {
|
|||||||
devService.triggerFieldSubscription = telemService.SubscribeToFields
|
devService.triggerFieldSubscription = telemService.SubscribeToFields
|
||||||
|
|
||||||
// Setup telemetry service callbacks
|
// Setup telemetry service callbacks
|
||||||
// telemService.getRequiredFields = devService.GetRequiredFields
|
telemService.startedStreaming = devService.StartStream
|
||||||
|
|
||||||
return &Orchestrator{
|
return &Orchestrator{
|
||||||
DeviceService: devService,
|
DeviceService: devService,
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ type TelemetryService struct {
|
|||||||
CtxHealthcheck context.Context
|
CtxHealthcheck context.Context
|
||||||
healthCheckCancel context.CancelFunc
|
healthCheckCancel context.CancelFunc
|
||||||
// Callbacks
|
// Callbacks
|
||||||
|
startedStreaming func(ch <-chan telemetry.TelemetryData)
|
||||||
// Devices data request
|
// Devices data request
|
||||||
// peripheralProvider func() []peripheral.Peripheral
|
// peripheralProvider func() []peripheral.Peripheral
|
||||||
// getRequiredFields func() []telemetry.FieldID
|
// getRequiredFields func() []telemetry.FieldID
|
||||||
@@ -204,21 +205,17 @@ func (t *TelemetryService) StopStream() {
|
|||||||
t.isStreaming = false
|
t.isStreaming = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TelemetryService) StartStream() {
|
func (t *TelemetryService) StartStream() <-chan telemetry.TelemetryData {
|
||||||
slog.Debug("Stream started")
|
|
||||||
|
|
||||||
// Start the new stream
|
// Start the new stream
|
||||||
if t.activeProvider == nil {
|
if t.activeProvider == nil {
|
||||||
slog.Debug("there's no active provider. not starting the stream")
|
slog.Debug("there's no active provider. not starting the stream")
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop the provider healthcheck
|
// Stop the provider healthcheck
|
||||||
t.healthCheckCancel()
|
t.healthCheckCancel()
|
||||||
|
|
||||||
simInCh, _ := t.activeProvider.Stream()
|
simInCh, _ := t.activeProvider.Stream()
|
||||||
// TODO: the provider needs to be able to tell the data has stopped
|
|
||||||
// so we can restart the provider lookup routine
|
|
||||||
|
|
||||||
// Create the context so we can control the lifecycle
|
// Create the context so we can control the lifecycle
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
@@ -227,6 +224,10 @@ func (t *TelemetryService) StartStream() {
|
|||||||
// Multiplex this data
|
// Multiplex this data
|
||||||
go t.multiplexData(ctx, simInCh)
|
go t.multiplexData(ctx, simInCh)
|
||||||
t.isStreaming = true
|
t.isStreaming = true
|
||||||
|
|
||||||
|
t.Messages <- "Provider has started streaming data\n"
|
||||||
|
|
||||||
|
return simInCh
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TelemetryService) multiplexData(ctx context.Context, dataCh <-chan telem.TelemetryData) {
|
func (t *TelemetryService) multiplexData(ctx context.Context, dataCh <-chan telem.TelemetryData) {
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ func (t *TelemetryService) onFindProvider(prov telem.TelemetryProvider) {
|
|||||||
// Attach to the provider
|
// Attach to the provider
|
||||||
t.logger.Info("found provider for " + prov.Name())
|
t.logger.Info("found provider for " + prov.Name())
|
||||||
t.Messages <- fmt.Sprintf("Found provider \"%s\"\n", prov.Name())
|
t.Messages <- fmt.Sprintf("Found provider \"%s\"\n", prov.Name())
|
||||||
|
|
||||||
err := t.SwitchProvider(prov)
|
err := t.SwitchProvider(prov)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Messages <- fmt.Sprintf("Failed to switch to provider: %+v\n", err.Error())
|
t.Messages <- fmt.Sprintf("Failed to switch to provider: %+v\n", err.Error())
|
||||||
@@ -26,6 +27,9 @@ func (t *TelemetryService) onFindProvider(prov telem.TelemetryProvider) {
|
|||||||
// Start the healthcheck on our provider so we can drop it if it stops
|
// Start the healthcheck on our provider so we can drop it if it stops
|
||||||
t.CtxHealthcheck, t.healthCheckCancel = context.WithCancel(context.Background())
|
t.CtxHealthcheck, t.healthCheckCancel = context.WithCancel(context.Background())
|
||||||
go t.ProviderMonitor(t.CtxHealthcheck)
|
go t.ProviderMonitor(t.CtxHealthcheck)
|
||||||
|
|
||||||
|
ch := t.StartStream()
|
||||||
|
t.startedStreaming(ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TelemetryService) onProviderStopsMidStream() {
|
func (t *TelemetryService) onProviderStopsMidStream() {
|
||||||
|
|||||||
@@ -88,23 +88,9 @@ func (sc *StreamingCtrl) registerHooks() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: this isn't the startstop method/action anymore. It should show up the stream
|
||||||
|
// visualizer - TODO: add metrics to the visualizer too, instead of just data
|
||||||
func (sc *StreamingCtrl) StartStop() {
|
func (sc *StreamingCtrl) StartStop() {
|
||||||
if sc.TelemServ.IsStreaming() {
|
|
||||||
slog.Info("stopping stream")
|
|
||||||
|
|
||||||
sc.TelemServ.StopStream()
|
|
||||||
sc.DevService.StopStream()
|
|
||||||
|
|
||||||
sc.isRunning = false
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// stream is not running, we have to start it now
|
|
||||||
// NOTE:
|
|
||||||
// Subscribe the only existing device - needs to be discovered by now
|
|
||||||
slog.Debug("setting the data stream for device servie")
|
|
||||||
sc.DevService.SetTelemetryChannel(sc.TelemServ.SubscribeListener("DeviceService", 1))
|
|
||||||
|
|
||||||
dev, err := sc.DevService.GetPeripheral(uidevice.NAME)
|
dev, err := sc.DevService.GetPeripheral(uidevice.NAME)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if uiDev, ok := dev.(*uidevice.UIDevice); ok {
|
if uiDev, ok := dev.(*uidevice.UIDevice); ok {
|
||||||
@@ -112,14 +98,6 @@ func (sc *StreamingCtrl) StartStop() {
|
|||||||
go sc.listenToUIStream()
|
go sc.listenToUIStream()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Debug("starting services")
|
|
||||||
sc.DevService.StartStream()
|
|
||||||
sc.TelemServ.StartStream()
|
|
||||||
|
|
||||||
sc.isRunning = true
|
|
||||||
|
|
||||||
slog.Debug("starting stream")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sc *StreamingCtrl) parseStreamUpdateForm(form *views.StreamOptionsView) (*models.StreamOptions, error) {
|
func (sc *StreamingCtrl) parseStreamUpdateForm(form *views.StreamOptionsView) (*models.StreamOptions, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user