mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-12-03 15:22:29 -08:00
Merge driver and feeder bridge
This commit is contained in:
10 files changed
+80
-183
No files matched your search
@@ -10,8 +10,6 @@ object BaseBehaviour : VRServerBehaviour {
|
||||
is VRServerActions.NewDevice -> state.copy(devices = state.devices + (action.deviceId to action.context))
|
||||
is VRServerActions.DriverConnected -> state.copy(drivers = state.drivers + (action.bridge.id to action.bridge))
|
||||
is VRServerActions.DriverDisconnected -> state.copy(drivers = state.drivers - action.bridgeId)
|
||||
is VRServerActions.FeederConnected -> state.copy(feeders = state.feeders + (action.bridge.id to action.bridge))
|
||||
is VRServerActions.FeederDisconnected -> state.copy(feeders = state.feeders - action.bridgeId)
|
||||
is VRServerActions.SolarXRConnected -> state.copy(solarxr = state.solarxr + (action.connection.id to action.connection))
|
||||
is VRServerActions.SolarXRDisconnected -> state.copy(solarxr = state.solarxr - action.connectionId)
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import solarxr_protocol.datatypes.hardware_info.McuType
|
||||
|
||||
enum class DeviceOrigin {
|
||||
DRIVER,
|
||||
FEEDER,
|
||||
UDP,
|
||||
HID,
|
||||
OSC,
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
package dev.slimevr.driver
|
||||
|
||||
import dev.slimevr.AppLogger
|
||||
import dev.slimevr.VRServerActions
|
||||
import dev.slimevr.device.Device
|
||||
import dev.slimevr.device.DeviceActions
|
||||
import dev.slimevr.device.DeviceOrigin
|
||||
import dev.slimevr.tracker.Tracker
|
||||
import dev.slimevr.tracker.TrackerActions
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import solarxr_protocol.datatypes.TrackerStatus
|
||||
import solarxr_protocol.datatypes.hardware_info.ImuType
|
||||
|
||||
object DriverBaseBehaviour : DriverBridgeBehaviour {
|
||||
override fun reduce(state: DriverBridgeState, action: DriverBridgeActions): DriverBridgeState = when (action) {
|
||||
is DriverBridgeActions.AddTracker -> state.copy(trackers = state.trackers + (action.id to action.tracker))
|
||||
is DriverBridgeActions.UpdateProtocolVersion -> state.copy(protocolVersion = action.version)
|
||||
}
|
||||
|
||||
@@ -15,10 +23,17 @@ object DriverBaseBehaviour : DriverBridgeBehaviour {
|
||||
receiver.context.dispatch(DriverBridgeActions.UpdateProtocolVersion(event.protocolVersion))
|
||||
}
|
||||
|
||||
receiver.inbound.on<DriverBridgeInbound.TrackerAdded> { event ->
|
||||
handleTrackerAdded(receiver, event.id, event.serial)
|
||||
}
|
||||
|
||||
receiver.inbound.on<DriverBridgeInbound.TrackerPosition> { event ->
|
||||
receiver.appContext.server.getTracker(event.trackerId)?.context?.dispatch(
|
||||
TrackerActions.SetRotation(rotation = event.rotation),
|
||||
)
|
||||
val tracker = receiver.context.state.value.trackers[event.trackerId]
|
||||
if (tracker != null) {
|
||||
tracker.context.dispatch(TrackerActions.SetRotation(rotation = event.rotation))
|
||||
} else {
|
||||
AppLogger.steamvr.warn("Failed to find tracker ${event.trackerId}")
|
||||
}
|
||||
}
|
||||
|
||||
// Should be safe: StateFlow never delivers two emissions concurrently to the same collector.
|
||||
@@ -53,4 +68,48 @@ object DriverBaseBehaviour : DriverBridgeBehaviour {
|
||||
}
|
||||
.launchIn(receiver.context.scope)
|
||||
}
|
||||
|
||||
private fun handleTrackerAdded(receiver: DriverBridge, id: Int, serial: String) {
|
||||
val server = receiver.appContext.server
|
||||
val settings = receiver.appContext.config.settings
|
||||
val scope = server.context.scope
|
||||
val existingTracker = server.context.state.value.trackers.values
|
||||
.find { tracker -> tracker.context.state.value.hardwareId == serial }
|
||||
|
||||
val device = if (existingTracker != null) {
|
||||
server.getDevice(existingTracker.context.state.value.deviceId)
|
||||
?: error("could not find existing device for serial $serial")
|
||||
} else {
|
||||
val deviceId = server.nextHandle()
|
||||
val newDevice = Device.create(
|
||||
scope = scope,
|
||||
id = deviceId,
|
||||
address = serial,
|
||||
macAddress = serial,
|
||||
origin = DeviceOrigin.DRIVER,
|
||||
protocolVersion = 0,
|
||||
)
|
||||
server.context.dispatch(VRServerActions.NewDevice(deviceId, newDevice))
|
||||
|
||||
val trackerId = server.nextHandle()
|
||||
val tracker = Tracker.create(
|
||||
scope = scope,
|
||||
id = trackerId,
|
||||
deviceId = deviceId,
|
||||
sensorType = ImuType.Other,
|
||||
hardwareId = serial,
|
||||
origin = DeviceOrigin.DRIVER,
|
||||
server = server,
|
||||
settings = settings,
|
||||
)
|
||||
server.context.dispatch(VRServerActions.NewTracker(trackerId, tracker))
|
||||
|
||||
receiver.context.dispatch(DriverBridgeActions.AddTracker(id, tracker))
|
||||
tracker.context.dispatch(TrackerActions.SetStatus(TrackerStatus.OK))
|
||||
|
||||
newDevice
|
||||
}
|
||||
|
||||
device.context.dispatch(DeviceActions.Update { copy(protocolVersion = 0) })
|
||||
}
|
||||
}
|
||||
@@ -6,20 +6,26 @@ import dev.slimevr.VRServerActions
|
||||
import dev.slimevr.context.Behaviour
|
||||
import dev.slimevr.context.Context
|
||||
import dev.slimevr.context.ManagedContext
|
||||
import dev.slimevr.tracker.Tracker
|
||||
import dev.slimevr.tracker.TrackerActions
|
||||
import io.github.axisangles.ktmath.Quaternion
|
||||
import io.github.axisangles.ktmath.Vector3
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import solarxr_protocol.datatypes.TrackerStatus
|
||||
|
||||
data class DriverBridgeState(
|
||||
val protocolVersion: Int,
|
||||
val trackers: Map<Int, Tracker>,
|
||||
)
|
||||
|
||||
sealed interface DriverBridgeActions {
|
||||
data class AddTracker(val id: Int, val tracker: Tracker) : DriverBridgeActions
|
||||
data class UpdateProtocolVersion(val version: Int) : DriverBridgeActions
|
||||
}
|
||||
|
||||
sealed interface DriverBridgeInbound {
|
||||
data class Version(val protocolVersion: Int) : DriverBridgeInbound
|
||||
data class TrackerAdded(val id: Int, val serial: String) : DriverBridgeInbound
|
||||
data class TrackerPosition(val trackerId: Int, val rotation: Quaternion, val position: Vector3?) : DriverBridgeInbound
|
||||
}
|
||||
|
||||
@@ -46,6 +52,9 @@ class DriverBridge(
|
||||
fun disconnect() {
|
||||
dispose()
|
||||
appContext.server.context.dispatch(VRServerActions.DriverDisconnected(id))
|
||||
context.state.value.trackers.forEach { (_, tracker) ->
|
||||
tracker.context.dispatch(TrackerActions.SetStatus(TrackerStatus.DISCONNECTED))
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
@@ -53,7 +62,10 @@ class DriverBridge(
|
||||
val behaviours = listOf(DriverBaseBehaviour)
|
||||
|
||||
val managedContext = ManagedContext.create(
|
||||
initialState = DriverBridgeState(protocolVersion = 0),
|
||||
initialState = DriverBridgeState(
|
||||
protocolVersion = 0,
|
||||
trackers = emptyMap(),
|
||||
),
|
||||
scope = scope,
|
||||
behaviours = behaviours,
|
||||
name = "Driver[$id]",
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
package dev.slimevr.feeder
|
||||
|
||||
import dev.slimevr.VRServerActions
|
||||
import dev.slimevr.device.Device
|
||||
import dev.slimevr.device.DeviceActions
|
||||
import dev.slimevr.device.DeviceOrigin
|
||||
import dev.slimevr.tracker.Tracker
|
||||
import dev.slimevr.tracker.TrackerActions
|
||||
import solarxr_protocol.datatypes.hardware_info.ImuType
|
||||
|
||||
object FeederBaseBehaviour : FeederBridgeBehaviour {
|
||||
override fun reduce(state: FeederBridgeState, action: FeederBridgeActions): FeederBridgeState = when (action) {
|
||||
is FeederBridgeActions.UpdateProtocolVersion -> state.copy(protocolVersion = action.version, firmware = action.firmware)
|
||||
}
|
||||
|
||||
override fun observe(receiver: FeederBridge) {
|
||||
receiver.inbound.on<FeederBridgeInbound.Version> { event ->
|
||||
receiver.context.dispatch(FeederBridgeActions.UpdateProtocolVersion(event.protocolVersion, event.firmware))
|
||||
}
|
||||
|
||||
receiver.inbound.on<FeederBridgeInbound.TrackerAdded> { event ->
|
||||
handleTrackerAdded(receiver, event.serial)
|
||||
}
|
||||
|
||||
receiver.inbound.on<FeederBridgeInbound.TrackerPosition> { event ->
|
||||
receiver.appContext.server.getTracker(event.trackerId)?.context?.dispatch(
|
||||
TrackerActions.SetRotation(rotation = event.rotation),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleTrackerAdded(receiver: FeederBridge, serial: String) {
|
||||
val server = receiver.appContext.server
|
||||
val settings = receiver.appContext.config.settings
|
||||
val scope = server.context.scope
|
||||
val existingTracker = server.context.state.value.trackers.values
|
||||
.find { tracker -> tracker.context.state.value.hardwareId == serial }
|
||||
|
||||
val device = if (existingTracker != null) {
|
||||
server.getDevice(existingTracker.context.state.value.deviceId)
|
||||
?: error("could not find existing device for serial $serial")
|
||||
} else {
|
||||
val deviceId = server.nextHandle()
|
||||
val newDevice = Device.create(
|
||||
scope = scope,
|
||||
id = deviceId,
|
||||
address = serial,
|
||||
macAddress = serial,
|
||||
origin = DeviceOrigin.FEEDER,
|
||||
protocolVersion = 0,
|
||||
)
|
||||
server.context.dispatch(VRServerActions.NewDevice(deviceId, newDevice))
|
||||
|
||||
val trackerId = server.nextHandle()
|
||||
val tracker = Tracker.create(
|
||||
scope = scope,
|
||||
id = trackerId,
|
||||
deviceId = deviceId,
|
||||
sensorType = ImuType.MPU9250,
|
||||
hardwareId = serial,
|
||||
origin = DeviceOrigin.FEEDER,
|
||||
server = server,
|
||||
settings = settings,
|
||||
)
|
||||
server.context.dispatch(VRServerActions.NewTracker(trackerId, tracker))
|
||||
|
||||
newDevice
|
||||
}
|
||||
|
||||
device.context.dispatch(DeviceActions.Update { copy(protocolVersion = 0) })
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package dev.slimevr.feeder
|
||||
|
||||
import dev.slimevr.AppContextProvider
|
||||
import dev.slimevr.EventDispatcher
|
||||
import dev.slimevr.VRServerActions
|
||||
import dev.slimevr.context.Behaviour
|
||||
import dev.slimevr.context.Context
|
||||
import dev.slimevr.context.ManagedContext
|
||||
import io.github.axisangles.ktmath.Quaternion
|
||||
import io.github.axisangles.ktmath.Vector3
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
|
||||
data class FeederBridgeState(
|
||||
val protocolVersion: Int,
|
||||
val firmware: String?,
|
||||
)
|
||||
|
||||
sealed interface FeederBridgeActions {
|
||||
data class UpdateProtocolVersion(val version: Int, val firmware: String?) : FeederBridgeActions
|
||||
}
|
||||
|
||||
sealed interface FeederBridgeInbound {
|
||||
data class Version(val protocolVersion: Int, val firmware: String?) : FeederBridgeInbound
|
||||
data class TrackerAdded(val serial: String) : FeederBridgeInbound
|
||||
data class TrackerPosition(val trackerId: Int, val rotation: Quaternion, val position: Vector3?) : FeederBridgeInbound
|
||||
}
|
||||
|
||||
typealias FeederBridgeContext = Context<FeederBridgeState, FeederBridgeActions>
|
||||
typealias FeederBridgeBehaviour = Behaviour<FeederBridgeState, FeederBridgeActions, FeederBridge>
|
||||
|
||||
class FeederBridge(
|
||||
val id: Int,
|
||||
val context: FeederBridgeContext,
|
||||
val appContext: AppContextProvider,
|
||||
val inbound: EventDispatcher<FeederBridgeInbound> = EventDispatcher(),
|
||||
private val managedContext: ManagedContext<FeederBridgeState, FeederBridgeActions>? = null,
|
||||
) {
|
||||
fun dispose() = managedContext?.dispose()
|
||||
|
||||
fun startObserving() = context.observeAll(this)
|
||||
|
||||
fun disconnect() {
|
||||
dispose()
|
||||
appContext.server.context.dispatch(VRServerActions.FeederDisconnected(id))
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun create(id: Int, appContext: AppContextProvider, scope: CoroutineScope): FeederBridge {
|
||||
val behaviours = listOf(FeederBaseBehaviour)
|
||||
|
||||
val managedContext = ManagedContext.create(
|
||||
initialState = FeederBridgeState(protocolVersion = 0, firmware = null),
|
||||
scope = scope,
|
||||
behaviours = behaviours,
|
||||
name = "Feeder[$id]",
|
||||
)
|
||||
|
||||
val bridge = FeederBridge(id = id, context = managedContext.context, appContext = appContext, managedContext = managedContext)
|
||||
bridge.startObserving()
|
||||
appContext.server.context.dispatch(VRServerActions.FeederConnected(bridge))
|
||||
|
||||
return bridge
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import dev.slimevr.context.Behaviour
|
||||
import dev.slimevr.context.Context
|
||||
import dev.slimevr.device.Device
|
||||
import dev.slimevr.driver.DriverBridge
|
||||
import dev.slimevr.feeder.FeederBridge
|
||||
import dev.slimevr.solarxr.SolarXRBridge
|
||||
import dev.slimevr.tracker.Tracker
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
@@ -16,7 +15,6 @@ data class VRServerState(
|
||||
val trackers: Map<Int, Tracker>,
|
||||
val devices: Map<Int, Device>,
|
||||
val drivers: Map<Int, DriverBridge>,
|
||||
val feeders: Map<Int, FeederBridge>,
|
||||
val solarxr: Map<Int, SolarXRBridge>,
|
||||
)
|
||||
|
||||
@@ -25,8 +23,6 @@ sealed interface VRServerActions {
|
||||
data class NewDevice(val deviceId: Int, val context: Device) : VRServerActions
|
||||
data class DriverConnected(val bridge: DriverBridge) : VRServerActions
|
||||
data class DriverDisconnected(val bridgeId: Int) : VRServerActions
|
||||
data class FeederConnected(val bridge: FeederBridge) : VRServerActions
|
||||
data class FeederDisconnected(val bridgeId: Int) : VRServerActions
|
||||
data class SolarXRConnected(val connection: SolarXRBridge) : VRServerActions
|
||||
data class SolarXRDisconnected(val connectionId: Int) : VRServerActions
|
||||
}
|
||||
@@ -52,7 +48,6 @@ class VRServer(
|
||||
trackers = emptyMap(),
|
||||
devices = emptyMap(),
|
||||
drivers = emptyMap(),
|
||||
feeders = emptyMap(),
|
||||
solarxr = emptyMap(),
|
||||
),
|
||||
scope = scope,
|
||||
|
||||
Reference in new issue
Block a user