Device reconnection handling #15
@@ -34,6 +34,7 @@ const (
|
|||||||
sendDataCMDID types.Command = 7
|
sendDataCMDID types.Command = 7
|
||||||
newLayoutCMDID types.Command = 8
|
newLayoutCMDID types.Command = 8
|
||||||
healthCheckCMDID types.Command = 9
|
healthCheckCMDID types.Command = 9
|
||||||
|
resetCMDID types.Command = 10
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -404,6 +405,17 @@ func (d *CDashDisplay) UnloadLayout() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cds *CDashDisplay) reset() error {
|
||||||
|
err := cds.WT.SendCommand(resetCMDID, []byte{0x01, 0x02, 0x03, 0x04}, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(3000 * time.Millisecond)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (d *CDashDisplay) SendData(data *telemetry.TelemetryData) error {
|
func (d *CDashDisplay) SendData(data *telemetry.TelemetryData) error {
|
||||||
packet := d.encodePacket(data)
|
packet := d.encodePacket(data)
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,14 @@ func (cds *CDashDisplay) setupForBeamNG() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cds *CDashDisplay) Setup(provider string) error {
|
func (cds *CDashDisplay) Setup(provider string) error {
|
||||||
|
// Doesn't matter which one we are picking, we need to reset the CDashDisplay
|
||||||
|
// first - we will improve this setup behaviour later on with an Update to
|
||||||
|
// change it without dropping connection or some shit
|
||||||
|
err := cds.reset()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
switch provider {
|
switch provider {
|
||||||
case constants.IRacingProviderName:
|
case constants.IRacingProviderName:
|
||||||
return cds.setupForIracing()
|
return cds.setupForIracing()
|
||||||
|
|||||||
@@ -86,6 +86,8 @@ type PeripheralStateStore struct {
|
|||||||
Messages chan string
|
Messages chan string
|
||||||
// Callbacks
|
// Callbacks
|
||||||
telemetryProvider func() (string, error)
|
telemetryProvider func() (string, error)
|
||||||
|
// Internal State
|
||||||
|
isStreaming bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPeripheralStateStore(
|
func NewPeripheralStateStore(
|
||||||
@@ -175,12 +177,22 @@ func (pss *PeripheralStateStore) DeleteDevice(pname string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pss *PeripheralStateStore) GetStreamingState() bool {
|
||||||
|
pss.mu.RLock()
|
||||||
|
defer pss.mu.RUnlock()
|
||||||
|
return pss.isStreaming
|
||||||
|
}
|
||||||
|
|
||||||
// "Events" [START] ------------------------------------------------------------
|
// "Events" [START] ------------------------------------------------------------
|
||||||
func (ds *DeviceService) onDeviceTimedOut(pname string) {
|
func (ds *DeviceService) onDeviceTimedOut(pname string) {
|
||||||
ds.PSS.setDeviceTimedOut(pname)
|
ds.PSS.setDeviceTimedOut(pname)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pss *PeripheralStateStore) OnStartStream() {
|
func (pss *PeripheralStateStore) OnStartStream() {
|
||||||
|
pss.mu.Lock()
|
||||||
|
pss.isStreaming = true
|
||||||
|
pss.mu.Unlock()
|
||||||
|
|
||||||
for _, state := range pss.GetStates() {
|
for _, state := range pss.GetStates() {
|
||||||
if state.State == DeviceIsConfigured {
|
if state.State == DeviceIsConfigured {
|
||||||
pss.setDeviceIsStreaming(state.device.Name)
|
pss.setDeviceIsStreaming(state.device.Name)
|
||||||
@@ -188,6 +200,12 @@ func (pss *PeripheralStateStore) OnStartStream() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pss *PeripheralStateStore) OnStopStream() {
|
||||||
|
pss.mu.Lock()
|
||||||
|
pss.isStreaming = false
|
||||||
|
pss.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
// "Events" [END] --------------------------------------------------------------
|
// "Events" [END] --------------------------------------------------------------
|
||||||
|
|
||||||
// Device State Handling [START] -----------------------------------------------
|
// Device State Handling [START] -----------------------------------------------
|
||||||
@@ -325,6 +343,7 @@ func (pss *PeripheralStateStore) configurePeripheral(
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (pss *PeripheralStateStore) handleDeviceTimedOut(pname string) error {
|
func (pss *PeripheralStateStore) handleDeviceTimedOut(pname string) error {
|
||||||
|
pss.Messages <- "device " + pname + " timed out\n"
|
||||||
pss.discoverPeripheral(pname, pss.setDeviceReconnected, pss.setDeviceTimedOut)
|
pss.discoverPeripheral(pname, pss.setDeviceReconnected, pss.setDeviceTimedOut)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -367,6 +386,11 @@ func (pss *PeripheralStateStore) handleDeviceIsUnconfigured(pname string) error
|
|||||||
func (pss *PeripheralStateStore) handleDeviceIsConfigured(pname string) error {
|
func (pss *PeripheralStateStore) handleDeviceIsConfigured(pname string) error {
|
||||||
// Here we have to check wheter we are streaming or not. If we aren't streaming
|
// Here we have to check wheter we are streaming or not. If we aren't streaming
|
||||||
// then we ought to do a healthcheck on the peripheral
|
// then we ought to do a healthcheck on the peripheral
|
||||||
|
if pss.GetStreamingState() {
|
||||||
|
pss.Messages <- "returning device " + pname + " into streaming\n"
|
||||||
|
pss.setDeviceIsStreaming(pname)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -431,10 +455,6 @@ func (pss *PeripheralStateStore) HandleDeviceState() {
|
|||||||
if updatedState.State == DeviceIsConnected ||
|
if updatedState.State == DeviceIsConnected ||
|
||||||
updatedState.State == DeviceIsUnconfigured ||
|
updatedState.State == DeviceIsUnconfigured ||
|
||||||
updatedState.State == DeviceIsConfigured {
|
updatedState.State == DeviceIsConfigured {
|
||||||
pss.Messages <- fmt.Sprintf(
|
|
||||||
"Performing healthcheck. STATE: %s\n",
|
|
||||||
DeviceStateToStr(updatedState.State),
|
|
||||||
)
|
|
||||||
pss.performHealthCheck(pName, updatedState)
|
pss.performHealthCheck(pName, updatedState)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user