mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-24 11:01:20 -08:00
Driver status and enable
This commit is contained in:
1 parent
aa3ab3942a
commit
bbcfc99061
18 files changed
+317
-81
No files matched your search
@@ -56,6 +56,7 @@ data class BoneRoutingConfig(
|
||||
|
||||
@Serializable
|
||||
data class DriverConfig(
|
||||
val enabled: Boolean = true,
|
||||
val sendDerivedVelocity: Boolean = false, // TODO do we actually need that or can we disable OpenVR's prediction
|
||||
)
|
||||
|
||||
|
||||
@@ -41,8 +41,14 @@ sealed interface DriverBridgeOutbound {
|
||||
typealias DriverBridgeContext = Context<DriverBridgeState, DriverBridgeActions>
|
||||
typealias DriverBridgeBehaviour = Behaviour<DriverBridgeState, DriverBridgeActions, DriverBridge>
|
||||
|
||||
enum class DriverBridgeSource {
|
||||
DRIVER,
|
||||
FEEDER,
|
||||
}
|
||||
|
||||
class DriverBridge(
|
||||
val id: Int,
|
||||
val source: DriverBridgeSource,
|
||||
val context: DriverBridgeContext,
|
||||
val appContext: AppContextProvider,
|
||||
val inbound: EventDispatcher<DriverBridgeInbound> = EventDispatcher("Driver.inbound", context.scope, capacity = 128),
|
||||
@@ -62,7 +68,7 @@ class DriverBridge(
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun create(id: Int, appContext: AppContextProvider, scope: CoroutineScope): DriverBridge {
|
||||
fun create(id: Int, source: DriverBridgeSource, appContext: AppContextProvider, scope: CoroutineScope): DriverBridge {
|
||||
val behaviours = listOf(DriverOutgoingTrackersBehaviour(), DriverIncomingTrackersBehaviour())
|
||||
|
||||
val managedContext = ManagedContext.create(
|
||||
@@ -75,7 +81,7 @@ class DriverBridge(
|
||||
name = "Driver[$id]",
|
||||
)
|
||||
|
||||
val bridge = DriverBridge(id = id, context = managedContext.context, appContext = appContext, managedContext = managedContext)
|
||||
val bridge = DriverBridge(id = id, source = source, context = managedContext.context, appContext = appContext, managedContext = managedContext)
|
||||
bridge.startObserving()
|
||||
appContext.server.context.dispatch(VRServerActions.DriverConnected(bridge))
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import dev.slimevr.AppContextProvider
|
||||
import dev.slimevr.config.BoneRoutingConfig
|
||||
import dev.slimevr.device.DeviceOrigin
|
||||
import dev.slimevr.driver.DRIVER_SUPPORTED_BONES
|
||||
import dev.slimevr.driver.DriverBridgeSource
|
||||
import dev.slimevr.tracker.TrackerState
|
||||
import dev.slimevr.vmc.VMC_SUPPORTED_BONES
|
||||
import dev.slimevr.vrcosc.VRC_OSC_SUPPORTED_BONES
|
||||
@@ -55,44 +56,46 @@ fun overrideRoutes(config: BoneRoutingConfig): Routes = config.manualRoutes.orEm
|
||||
|
||||
fun isActive(states: OutputStates, output: RoutingOutput): Boolean = states[output] == RoutingOutputState.ACTIVE
|
||||
|
||||
fun platformSupports(appContext: AppContextProvider, output: RoutingOutput): Boolean = when (output) {
|
||||
RoutingOutput.DRIVER -> appContext.featureFlags.supportsDriver
|
||||
RoutingOutput.VRC_OSC, RoutingOutput.VMC -> true
|
||||
}
|
||||
fun driverStateFlow(appContext: AppContextProvider): Flow<RoutingOutputState> = combine(
|
||||
appContext.config.settings.context.state.map { it.data.driverConfig.enabled },
|
||||
appContext.server.context.state.map { state ->
|
||||
state.drivers.values.any { it.source == DriverBridgeSource.DRIVER }
|
||||
},
|
||||
) { enabled, connected ->
|
||||
when {
|
||||
!appContext.featureFlags.supportsDriver -> RoutingOutputState.UNSUPPORTED
|
||||
!enabled -> RoutingOutputState.INACTIVE
|
||||
connected -> RoutingOutputState.ACTIVE
|
||||
else -> RoutingOutputState.ENABLED
|
||||
}
|
||||
}.distinctUntilChanged()
|
||||
|
||||
private fun vrcOscStateFlow(appContext: AppContextProvider): Flow<RoutingOutputState> = combine(
|
||||
appContext.config.settings.context.state.map { it.data.vrcOscConfig.enabled },
|
||||
appContext.vrcOscManager.context.state.map { it.status.outputState == VRCOSCOutputState.READY },
|
||||
) { enabled, hasTarget ->
|
||||
when {
|
||||
!enabled -> RoutingOutputState.INACTIVE
|
||||
hasTarget -> RoutingOutputState.ACTIVE
|
||||
else -> RoutingOutputState.ENABLED
|
||||
}
|
||||
}.distinctUntilChanged()
|
||||
|
||||
private fun vmcStateFlow(appContext: AppContextProvider): Flow<RoutingOutputState> = appContext.config.settings.context.state
|
||||
.map { if (it.data.vmcConfig.enabled) RoutingOutputState.ACTIVE else RoutingOutputState.INACTIVE }
|
||||
.distinctUntilChanged()
|
||||
|
||||
/** Emits the output states as the driver connects and the output configs change. */
|
||||
fun outputStatesFlow(appContext: AppContextProvider): Flow<OutputStates> {
|
||||
val settings = appContext.config.settings
|
||||
|
||||
return combine(
|
||||
appContext.server.context.state.map { it.drivers.isNotEmpty() }.distinctUntilChanged(),
|
||||
settings.context.state.map { it.data.vrcOscConfig.enabled }.distinctUntilChanged(),
|
||||
settings.context.state.map { it.data.vmcConfig.enabled }.distinctUntilChanged(),
|
||||
appContext.vrcOscManager.context.state
|
||||
.map { it.status.outputState == VRCOSCOutputState.READY }
|
||||
.distinctUntilChanged(),
|
||||
) { driverConnected, vrcOscEnabled, vmcEnabled, vrcOscHasTarget ->
|
||||
RoutingOutput.entries.associateWith { output ->
|
||||
if (!platformSupports(appContext, output)) {
|
||||
RoutingOutputState.UNSUPPORTED
|
||||
} else {
|
||||
when (output) {
|
||||
RoutingOutput.DRIVER -> if (driverConnected) RoutingOutputState.ACTIVE else RoutingOutputState.INACTIVE
|
||||
|
||||
RoutingOutput.VRC_OSC -> when {
|
||||
!vrcOscEnabled -> RoutingOutputState.INACTIVE
|
||||
vrcOscHasTarget -> RoutingOutputState.ACTIVE
|
||||
else -> RoutingOutputState.ENABLED
|
||||
}
|
||||
|
||||
// Writes to a fixed address whether or not anything is there, so being
|
||||
// on is the same as transmitting.
|
||||
// FIXME: Maybe we should assume that we need to receive data from VMC for it to be considered working?
|
||||
RoutingOutput.VMC -> if (vmcEnabled) RoutingOutputState.ACTIVE else RoutingOutputState.INACTIVE
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fun outputStatesFlow(appContext: AppContextProvider): Flow<OutputStates> = combine(
|
||||
driverStateFlow(appContext),
|
||||
vrcOscStateFlow(appContext),
|
||||
vmcStateFlow(appContext),
|
||||
) { driver, vrcOsc, vmc ->
|
||||
mapOf(
|
||||
RoutingOutput.DRIVER to driver,
|
||||
RoutingOutput.VRC_OSC to vrcOsc,
|
||||
RoutingOutput.VMC to vmc,
|
||||
)
|
||||
}
|
||||
|
||||
/** What the user asked for, including outputs that are switched off. */
|
||||
|
||||
@@ -1,28 +1,47 @@
|
||||
package dev.slimevr.solarxr
|
||||
|
||||
import dev.slimevr.AppContextProvider
|
||||
import dev.slimevr.config.Settings
|
||||
import dev.slimevr.config.SettingsActions
|
||||
import dev.slimevr.routing.driverStateFlow
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.drop
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import solarxr_protocol.rpc.ChangeDriverSettingsRequest
|
||||
import solarxr_protocol.rpc.DriverConnectionState
|
||||
import solarxr_protocol.rpc.DriverSettingsRequest
|
||||
import solarxr_protocol.rpc.DriverSettingsResponse
|
||||
import solarxr_protocol.rpc.DriverStatusChangeResponse
|
||||
import solarxr_protocol.rpc.DriverStatusRequest
|
||||
import solarxr_protocol.rpc.RoutingOutputState
|
||||
|
||||
class DriverSettingsBehaviour(
|
||||
private val settings: Settings,
|
||||
private val appContext: AppContextProvider,
|
||||
) : SolarXRBridgeBehaviour {
|
||||
private val settings: Settings get() = appContext.config.settings
|
||||
|
||||
override fun observe(receiver: SolarXRBridge) {
|
||||
receiver.rpcDispatcher.on<DriverSettingsRequest> {
|
||||
val config = settings.context.state.value.data.driverConfig
|
||||
receiver.sendRpc(DriverSettingsResponse(sendDerivedVelocity = config.sendDerivedVelocity))
|
||||
receiver.sendRpc(
|
||||
DriverSettingsResponse(
|
||||
sendDerivedVelocity = config.sendDerivedVelocity,
|
||||
enabled = config.enabled,
|
||||
),
|
||||
)
|
||||
}.launchIn(receiver.context.scope)
|
||||
|
||||
settings.context.state
|
||||
.drop(1)
|
||||
.map { DriverSettingsResponse(sendDerivedVelocity = it.data.driverConfig.sendDerivedVelocity) }
|
||||
.map {
|
||||
DriverSettingsResponse(
|
||||
sendDerivedVelocity = it.data.driverConfig.sendDerivedVelocity,
|
||||
enabled = it.data.driverConfig.enabled,
|
||||
)
|
||||
}
|
||||
.distinctUntilChanged()
|
||||
.onEach(receiver::sendRpc)
|
||||
.launchIn(receiver.context.scope)
|
||||
@@ -30,9 +49,32 @@ class DriverSettingsBehaviour(
|
||||
receiver.rpcDispatcher.on<ChangeDriverSettingsRequest> { req ->
|
||||
settings.context.dispatch(
|
||||
SettingsActions.Update {
|
||||
copy(driverConfig = driverConfig.copy(sendDerivedVelocity = req.sendDerivedVelocity == true))
|
||||
copy(
|
||||
driverConfig = driverConfig.copy(
|
||||
sendDerivedVelocity = req.sendDerivedVelocity == true,
|
||||
enabled = req.enabled == true,
|
||||
),
|
||||
)
|
||||
},
|
||||
)
|
||||
}.launchIn(receiver.context.scope)
|
||||
|
||||
val status = driverStateFlow(appContext).map { state ->
|
||||
when (state) {
|
||||
RoutingOutputState.UNSUPPORTED -> DriverConnectionState.UNSUPPORTED
|
||||
RoutingOutputState.INACTIVE -> DriverConnectionState.DISABLED
|
||||
RoutingOutputState.ENABLED -> DriverConnectionState.WAITING
|
||||
RoutingOutputState.ACTIVE -> DriverConnectionState.CONNECTED
|
||||
}
|
||||
}
|
||||
|
||||
receiver.rpcDispatcher.on<DriverStatusRequest> {
|
||||
receiver.sendRpc(DriverStatusChangeResponse(state = status.first()))
|
||||
}.launchIn(receiver.context.scope)
|
||||
|
||||
status
|
||||
.drop(1)
|
||||
.onEach { receiver.sendRpc(DriverStatusChangeResponse(state = it)) }
|
||||
.launchIn(receiver.context.scope)
|
||||
}
|
||||
}
|
||||
@@ -78,7 +78,7 @@ class SolarXRBridge(
|
||||
add(HeightCalibrationBehaviour(appContext.heightCalibrationManager))
|
||||
add(ProvisioningBehaviour(appContext.server, appContext.provisioningManager))
|
||||
add(BoneRoutingBehaviour(appContext))
|
||||
add(DriverSettingsBehaviour(appContext.config.settings))
|
||||
add(DriverSettingsBehaviour(appContext))
|
||||
add(SkeletonSettingsBehaviour(appContext.config.settings))
|
||||
add(SkeletonProportionsBehaviour(appContext.config.userConfig, appContext.skeleton))
|
||||
add(TrackingChecklistBehaviour(appContext.trackingChecklist, appContext.config.settings))
|
||||
|
||||
@@ -8,6 +8,7 @@ import dev.slimevr.config.MountingMethods
|
||||
import dev.slimevr.config.Settings
|
||||
import dev.slimevr.device.DeviceOrigin
|
||||
import dev.slimevr.device.DeviceState
|
||||
import dev.slimevr.driver.DriverBridgeSource
|
||||
import dev.slimevr.networkprofile.NetworkProfileManager
|
||||
import dev.slimevr.resets.ResetBodyParts
|
||||
import dev.slimevr.resets.ResetsManager
|
||||
@@ -180,7 +181,9 @@ class SteamVRHandsCheckBehaviour(
|
||||
combine(
|
||||
trackerStatesFlow(server),
|
||||
boneRouting.context.state.map { state -> state.routes },
|
||||
server.context.state.map { state -> state.drivers.isNotEmpty() }.distinctUntilChanged(),
|
||||
server.context.state
|
||||
.map { state -> state.drivers.values.any { it.source == DriverBridgeSource.DRIVER } }
|
||||
.distinctUntilChanged(),
|
||||
::computeStep,
|
||||
)
|
||||
.distinctUntilChanged()
|
||||
|
||||
Reference in new issue
Block a user