Separate devices.json config tracker data now lives outside of profiles. Use hardwareId instead of mac address for all devices actions

This commit is contained in:
loucass003 committed 2026-10-15 07:15:06 +02:00
1 parent 4e49f3aea2
commit 02ce2ef901
38 files changed
+334 -321

No files matched your search

@@ -3,6 +3,9 @@ package dev.slimevr
import dev.slimevr.bvh.BVHManager
import dev.slimevr.config.AppConfig
import dev.slimevr.config.ConfigStorage
import dev.slimevr.config.DEVICES_CONFIG_PATH
import dev.slimevr.config.DevicesConfig
import dev.slimevr.config.DevicesConfigState
import dev.slimevr.config.GlobalConfig
import dev.slimevr.config.GlobalConfigState
import dev.slimevr.config.Settings
@@ -12,6 +15,7 @@ import dev.slimevr.config.TextFileHandle
import dev.slimevr.config.UserConfig
import dev.slimevr.config.UserConfigData
import dev.slimevr.config.UserConfigState
import dev.slimevr.config.launchAutosave
import dev.slimevr.context.Context
import dev.slimevr.firmware.FirmwareManager
import dev.slimevr.heightcalibration.HeightCalibrationManager
@@ -205,6 +209,17 @@ fun buildTestSettings(scope: CoroutineScope): Settings {
return Settings(context, scope, NoopConfigStorage, "settings")
}
fun buildTestDevicesConfig(scope: CoroutineScope): DevicesConfig {
val context = Context.create(
initialState = DevicesConfigState(),
scope = scope,
reducer = ::reduceConfig,
name = "DevicesConfig[test]",
)
val autosave = launchAutosave(scope, context.state, NoopConfigStorage, toPath = { DEVICES_CONFIG_PATH }, serialize = { "" })
return DevicesConfig(context, autosave)
}
fun buildTestHeightCalibration(server: VRServer, userConfig: UserConfig, scope: CoroutineScope): HeightCalibrationManager {
val initialState = HeightCalibrationState(status = UserHeightCalibrationStatus.NONE, currentHeight = 1.6f, canDoUserHeightCalibration = true)
val context = Context.create(
@@ -244,6 +259,7 @@ fun buildTestAppConfig(scope: CoroutineScope): AppConfig {
globalConfig = GlobalConfig(globalContext).also { it.startObserving() },
userConfig = buildTestUserConfig(scope),
settings = buildTestSettings(scope),
devices = buildTestDevicesConfig(scope),
storage = NoopConfigStorage,
)
}
@@ -9,6 +9,7 @@ import dev.slimevr.buildTestVrServerStub
import dev.slimevr.skeleton.Skeleton
import dev.slimevr.skeleton.computeAllDefaultProportionsByBone
import dev.slimevr.tracker.Tracker
import dev.slimevr.tracker.TrackerActions
import dev.slimevr.tracker.behaviours.TrackerConfigBehaviour
import dev.slimevr.vmc.VMCInputBehaviour
import dev.slimevr.vmc.VMCManager
@@ -27,7 +28,6 @@ import java.net.DatagramSocket
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue
private fun settingsFile(name: String) = "settings/$name.json"
@@ -169,47 +169,25 @@ class ProfileSwitchTest {
private fun buildTracker(scope: TestScope, config: AppConfig): Tracker {
val server = buildTestVrServerStub(scope.backgroundScope)
val tracker = buildTestTracker(scope.backgroundScope, buildTestAppContext(server), config.settings, id = 1)
TrackerConfigBehaviour(config.settings, "test-1", Tracker.DEFAULT_STATE).observe(tracker)
TrackerConfigBehaviour(config.devices, "test-1").observe(tracker)
return tracker
}
@Test
fun `a live tracker takes the config of the selected profile and never writes its old one into it`() = runTest {
fun `switching the settings profile keeps the tracker config`() = runTest {
val storage = MemoryStorage()
putSettings(storage, "default", SettingsConfigState(trackers = mapOf("test-1" to TrackerConfig(bodyPart = BodyPart.HIP, customName = "hip"))))
putSettings(storage, "feet", SettingsConfigState(trackers = mapOf("test-1" to TrackerConfig(bodyPart = BodyPart.LEFT_FOOT))))
putSettings(storage, "other", SettingsConfigState())
val config = buildConfig(this, storage)
val tracker = buildTracker(this, config)
config.switchProfile(ProfileKind.SETTINGS, "feet")
runCurrent()
assertEquals(BodyPart.LEFT_FOOT, tracker.context.state.value.bodyPart)
assertNull(tracker.context.state.value.customName)
config.switchProfile(ProfileKind.SETTINGS, "default")
tracker.context.dispatch(TrackerActions.Update { copy(bodyPart = BodyPart.HIP) })
runCurrent()
config.switchProfile(ProfileKind.SETTINGS, "other")
runCurrent()
assertEquals(BodyPart.HIP, tracker.context.state.value.bodyPart)
assertEquals("hip", tracker.context.state.value.customName)
config.switchProfile(ProfileKind.SETTINGS, "feet")
runCurrent()
assertEquals(BodyPart.LEFT_FOOT, config.settings.context.state.value.data.trackers.getValue("test-1").bodyPart)
}
@Test
fun `a tracker missing from the selected profile returns to its creation state`() = runTest {
val storage = MemoryStorage()
putSettings(storage, "default", SettingsConfigState(trackers = mapOf("test-1" to TrackerConfig(bodyPart = BodyPart.HIP, customName = "hip"))))
val config = buildConfig(this, storage)
val tracker = buildTracker(this, config)
config.switchProfile(ProfileKind.SETTINGS, "default")
config.createProfile(ProfileKind.SETTINGS, "empty", copyFrom = null)
config.switchProfile(ProfileKind.SETTINGS, "empty")
runCurrent()
assertNull(tracker.context.state.value.bodyPart)
assertNull(tracker.context.state.value.customName)
assertEquals(BodyPart.HIP, config.devices.context.state.value.trackers.getValue("test-1").bodyPart)
}
private fun vmcHarness(scope: TestScope, config: AppConfig): VMCManager {
@@ -5,7 +5,7 @@ import dev.slimevr.VRServer
import dev.slimevr.VRServerActions
import dev.slimevr.buildTestAppContext
import dev.slimevr.buildTestSerial
import dev.slimevr.buildTestSettings
import dev.slimevr.buildTestDevicesConfig
import dev.slimevr.buildTestVrServerStub
import dev.slimevr.device.Device
import dev.slimevr.device.DeviceActions
@@ -72,7 +72,7 @@ class DoSerialFlashTest {
ssid = null,
password = null,
serialServer = serial.server,
settings = buildTestSettings(backgroundScope),
devices = buildTestDevicesConfig(backgroundScope),
server = vrServer,
flasher = failingFirmwareFlasher,
onStatus = { s, _ -> statuses += s },
@@ -96,7 +96,7 @@ class DoSerialFlashTest {
ssid = null,
password = null,
serialServer = serial.server,
settings = buildTestSettings(backgroundScope),
devices = buildTestDevicesConfig(backgroundScope),
server = buildTestVrServerStub(backgroundScope),
flasher = failingFirmwareFlasher,
onStatus = { s, _ -> statuses += s },
@@ -120,7 +120,7 @@ class DoSerialFlashTest {
ssid = null,
password = null,
serialServer = serial.server,
settings = buildTestSettings(backgroundScope),
devices = buildTestDevicesConfig(backgroundScope),
server = buildTestVrServerStub(backgroundScope),
flasher = failingFirmwareFlasher,
onStatus = { s, _ -> statuses += s },
@@ -144,7 +144,7 @@ class DoSerialFlashTest {
ssid = "wifi",
password = "pass",
serialServer = serial.server,
settings = buildTestSettings(backgroundScope),
devices = buildTestDevicesConfig(backgroundScope),
server = buildTestVrServerStub(backgroundScope),
onStatus = { s, _ -> statuses += s },
)
@@ -167,7 +167,7 @@ class DoSerialFlashTest {
ssid = "wifi",
password = "pass",
serialServer = serial.server,
settings = buildTestSettings(backgroundScope),
devices = buildTestDevicesConfig(backgroundScope),
server = buildTestVrServerStub(backgroundScope),
onStatus = { s, _ -> statuses += s },
)
@@ -194,7 +194,7 @@ class DoSerialFlashTest {
ssid = null,
password = null,
serialServer = serial.server,
settings = buildTestSettings(backgroundScope),
devices = buildTestDevicesConfig(backgroundScope),
server = buildTestVrServerStub(backgroundScope),
onStatus = { s, _ -> statuses += s },
)
@@ -225,7 +225,7 @@ class DoSerialFlashTest {
ssid = "wifi",
password = "pass",
serialServer = serial.server,
settings = buildTestSettings(backgroundScope),
devices = buildTestDevicesConfig(backgroundScope),
server = buildTestVrServerStub(backgroundScope),
onStatus = { s, _ -> statuses += s },
)
@@ -258,7 +258,7 @@ class DoSerialFlashTest {
ssid = "wifi",
password = "pass",
serialServer = serial.server,
settings = buildTestSettings(backgroundScope),
devices = buildTestDevicesConfig(backgroundScope),
server = buildTestVrServerStub(backgroundScope),
onStatus = { s, _ -> statuses += s },
)
@@ -294,7 +294,7 @@ class DoSerialFlashTest {
ssid = "wifi",
password = "pass",
serialServer = serial.server,
settings = buildTestSettings(backgroundScope),
devices = buildTestDevicesConfig(backgroundScope),
server = vrServer,
onStatus = { s, _ -> statuses += s },
)
@@ -4,7 +4,7 @@ import dev.slimevr.VRServer
import dev.slimevr.VRServerActions
import dev.slimevr.buildTestAppContext
import dev.slimevr.buildTestSerial
import dev.slimevr.buildTestSettings
import dev.slimevr.buildTestDevicesConfig
import dev.slimevr.buildTestVrServer
import dev.slimevr.context.Context
import dev.slimevr.device.Device
@@ -39,7 +39,7 @@ private fun buildManager(serialServer: dev.slimevr.serial.SerialServer, scope: C
reducer = ::reduceProvisioning,
name = "ProvisioningManagerTest",
)
return ProvisioningManager(context = context, serialServer = serialServer, settings = buildTestSettings(scope), scope = scope).also { it.startObserving() }
return ProvisioningManager(context = context, serialServer = serialServer, devices = buildTestDevicesConfig(scope), scope = scope).also { it.startObserving() }
}
private fun ProvisioningManager.trackerStatus(port: String = "COM1") = context.state.value.trackers[port]?.status
@@ -4,7 +4,7 @@ import dev.slimevr.TestAppContext
import dev.slimevr.TestSerial
import dev.slimevr.VRServer
import dev.slimevr.buildTestSerial
import dev.slimevr.buildTestSettings
import dev.slimevr.buildTestDevicesConfig
import dev.slimevr.buildTestVrServerStub
import dev.slimevr.context.Context
import dev.slimevr.provisioning.ProvisioningManager
@@ -33,7 +33,7 @@ private fun buildManager(serial: TestSerial, scope: CoroutineScope): Provisionin
return ProvisioningManager(
context = context,
serialServer = serial.server,
settings = buildTestSettings(scope),
devices = buildTestDevicesConfig(scope),
scope = scope,
).also { it.startObserving() }
}
@@ -7,8 +7,8 @@ import dev.slimevr.buildTestAppConfig
import dev.slimevr.buildTestTracker
import dev.slimevr.buildTestVrServerStub
import dev.slimevr.config.AppConfig
import dev.slimevr.config.DevicesConfigActions
import dev.slimevr.config.GlobalConfigActions
import dev.slimevr.config.SettingsActions
import dev.slimevr.config.UserConfigActions
import dev.slimevr.config.changeConsent
import dev.slimevr.context.Context
@@ -83,13 +83,14 @@ class SettingsResetTest {
val server = buildTestVrServerStub(backgroundScope)
val bridge = buildBridge(backgroundScope, config, server)
val mac = "AA:BB:CC:DD:EE:FF"
config.settings.context.dispatch(SettingsActions.AddAllowedUdpDevice(mac))
config.devices.context.dispatch(DevicesConfigActions.AddAllowedUdpDevice(mac))
val device = Device.create(
scope = server.context.scope,
appContext = bridge.appContext,
id = server.nextHandle(),
address = "127.0.0.1",
macAddress = mac,
hardwareId = mac,
origin = DeviceOrigin.UDP,
protocolVersion = 21,
)
@@ -97,7 +98,7 @@ class SettingsResetTest {
val tracker = buildTestTracker(server.context.scope, bridge.appContext, config.settings, server.nextHandle())
tracker.context.dispatch(TrackerActions.Update { copy(deviceId = device.context.state.value.id, hardwareId = "$mac:0") })
server.context.dispatch(VRServerActions.NewTracker(tracker.context.state.value.id, tracker))
config.settings.context.dispatch(SettingsActions.UpdateTracker("$mac:0") { copy(customName = "Left foot") })
config.devices.context.dispatch(DevicesConfigActions.UpdateTracker("$mac:0") { copy(customName = "Left foot") })
config.globalConfig.context.dispatch(
GlobalConfigActions.SetErrorReporting(
changeConsent(config.globalConfig.context.state.value.errorReporting, ErrorReportingConsent.ALLOWED),
@@ -111,7 +112,7 @@ class SettingsResetTest {
assertTrue(server.context.state.value.devices.isEmpty())
assertTrue(server.context.state.value.trackers.isEmpty())
assertTrue(config.settings.context.state.value.data.trackers.isEmpty())
assertTrue(config.settings.context.state.value.data.allowedUdpDevices.isEmpty())
assertTrue(config.devices.context.state.value.trackers.isEmpty())
assertTrue(config.devices.context.state.value.allowedUdpDevices.isEmpty())
}
}
@@ -1,35 +0,0 @@
package dev.slimevr.tracker
import dev.slimevr.config.TrackerConfig
import dev.slimevr.tracker.behaviours.TrackerConfigBehaviour
import io.github.axisangles.ktmath.Quaternion
import solarxr_protocol.datatypes.BodyPart
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
class TrackerProfileConfigTest {
private val base = Tracker.DEFAULT_STATE.copy(bodyPart = BodyPart.UPPER_CHEST)
@Test
fun `a profile config replaces what the previous profile set`() {
val live = base.copy(bodyPart = BodyPart.HIP, customName = "old")
val config = TrackerConfig(bodyPart = BodyPart.LEFT_FOOT, customName = "new")
val result = TrackerConfigBehaviour.applyProfileConfig(live, base, config, saveMountingReset = false)
assertEquals(BodyPart.LEFT_FOOT, result.bodyPart)
assertEquals("new", result.customName)
}
@Test
fun `a tracker unknown to the profile goes back to its creation state`() {
val live = base.copy(bodyPart = BodyPart.HIP, customName = "old", mountingOrientation = Quaternion(0f, 0f, 1f, 0f))
val result = TrackerConfigBehaviour.applyProfileConfig(live, base, null, saveMountingReset = false)
assertEquals(BodyPart.UPPER_CHEST, result.bodyPart)
assertNull(result.customName)
assertEquals(Quaternion.IDENTITY, result.mountingOrientation)
}
}
@@ -9,7 +9,7 @@ import dev.slimevr.buildTestSkeleton
import dev.slimevr.buildTestTracker
import dev.slimevr.buildTestVrServerStub
import dev.slimevr.config.AppConfig
import dev.slimevr.config.SettingsActions
import dev.slimevr.config.DevicesConfigActions
import dev.slimevr.device.Device
import dev.slimevr.skeleton.Skeleton
import dev.slimevr.skeleton.SkeletonActions
@@ -50,6 +50,7 @@ private fun addDevice(appContext: AppContextProvider, origin: DeviceOrigin): Dev
id = id,
address = "127.0.0.$id",
macAddress = MAC,
hardwareId = MAC,
origin = origin,
protocolVersion = 21,
)
@@ -77,7 +78,7 @@ private fun addTracker(
val hardwareId = "$MAC:$sensorId"
tracker.context.dispatch(TrackerActions.Update { copy(deviceId = device.context.state.value.id, hardwareId = hardwareId) })
appContext.server.context.dispatch(VRServerActions.NewTracker(id, tracker))
appContext.config.settings.context.dispatch(SettingsActions.UpdateTracker(hardwareId) { copy(bodyPart = bodyPart) })
appContext.config.devices.context.dispatch(DevicesConfigActions.UpdateTracker(hardwareId) { copy(bodyPart = bodyPart) })
return tracker
}
@@ -86,8 +87,8 @@ class ForgetDeviceTest {
@Test
fun `forget removes the udp device, its trackers, the allowed mac and saved config, and nothing else`() = runTest {
val appContext = buildContext(backgroundScope)
val settings = appContext.config.settings
settings.context.dispatch(SettingsActions.AddAllowedUdpDevice(MAC))
val devices = appContext.config.devices
devices.context.dispatch(DevicesConfigActions.AddAllowedUdpDevice(MAC))
val device = addDevice(appContext, DeviceOrigin.UDP)
val trackers = listOf(
addTracker(appContext, device, 0, BodyPart.UPPER_CHEST, TrackerStatus.OK),
@@ -108,8 +109,8 @@ class ForgetDeviceTest {
assertFalse(device.context.scope.isActive)
trackers.forEach { assertFalse(it.context.scope.isActive) }
assertTrue(hidDevice.context.scope.isActive)
assertFalse(MAC in settings.context.state.value.data.allowedUdpDevices)
assertEquals(setOf("$MAC:5"), settings.context.state.value.data.trackers.keys)
assertFalse(MAC in devices.context.state.value.allowedUdpDevices)
assertEquals(setOf("$MAC:5"), devices.context.state.value.trackers.keys)
assertEquals(false, appContext.skeleton.context.state.value.boneInputs[BodyPart.UPPER_CHEST]?.isRotationActive)
}
}
@@ -1,6 +1,8 @@
package dev.slimevr.udp
import dev.slimevr.solarxr.rpc.createUnknownDevice
import dev.slimevr.util.macAddressToULong
import dev.slimevr.util.udpHardwareId
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.runCurrent
@@ -17,7 +19,7 @@ private const val MAC = "AA:BB:CC:DD:EE:FF"
private const val OTHER_MAC = "11:22:33:44:55:66"
private fun device(mac: String = MAC, lastSeen: Long = 1000L, firmware: String? = "0.6.3") = UnknownDeviceState(
macAddress = mac,
hardwareId = mac,
address = "192.168.1.70/54321",
boardType = BoardType.SLIMEVR,
mcuType = McuType.ESP32_C3,
@@ -29,13 +31,17 @@ private val EMPTY = UdpServer.INITIAL_STATE
class UnknownDevicesTest {
@Test
fun `status follows the allowed list and the address drops its port`() {
fun `status follows the allowed list, and identity is the mac or the ip without port`() {
val allowed = createUnknownDevice(device(), setOf(MAC))
val waiting = createUnknownDevice(device(), setOf(OTHER_MAC))
assertEquals(UnknownDeviceStatus.ADDING, allowed.status)
assertEquals(UnknownDeviceStatus.WAITING, waiting.status)
assertEquals(0xC0A80146u, allowed.ipAddress)
assertEquals(MAC, udpHardwareId(MAC, "192.168.1.70/54321"))
assertEquals("192.168.1.70", udpHardwareId(null, "192.168.1.70/54321"))
assertEquals(0xAABBCCDDEEFFuL, macAddressToULong(MAC))
assertEquals(null, macAddressToULong("not a mac"))
}
@Test