Skip to content

Free Motion Capture for Everyone πŸ’€βœ¨

TypeScript

fix camera id stuff

jonmatthis9c34364906d9
diff --git a/freemocap-ui/src/store/slices/cameras/cameras-slice.ts b/freemocap-ui/src/store/slices/cameras/cameras-slice.ts
index ef58d754..3b8c4630 100644
--- a/freemocap-ui/src/store/slices/cameras/cameras-slice.ts
+++ b/freemocap-ui/src/store/slices/cameras/cameras-slice.ts
@@ -6,6 +6,7 @@ import {
CamerasState,
createDefaultCameraConfig,
extractConfigSettings,
+ reconcileDetectedCameras,
} from './cameras-types';
import {camerasConnectOrUpdate, closeCameras, detectCameras, pauseUnpauseCameras, recommendExposureSent,} from './cameras-thunks';
import {serverStateReceived, serverDisconnected} from '../connection/connection-slice';
@@ -137,21 +138,7 @@ export const cameraSlice = createSlice({
})
.addCase(detectCameras.fulfilled, (state, action) => {
state.isLoading = false;
- const previousCameras = state.cameras;
- state.cameras = action.payload.map(camera => {
- const existing = previousCameras.find(cam => cam.id === camera.id);
- if (!existing) return camera;
- return {
- ...camera,
- actualConfig: existing.actualConfig,
- desiredConfig: existing.desiredConfig,
- hasConfigMismatch: existing.hasConfigMismatch,
- connectionStatus: existing.connectionStatus,
- selected: existing.selected,
- realtimeEnabled: existing.realtimeEnabled,
- metrics: existing.metrics,
- };
- });
+ state.cameras = reconcileDetectedCameras(state.cameras, action.payload);
persistAllCameraSettings(state);
})
.addCase(detectCameras.rejected, (state, action) => {
diff --git a/freemocap-ui/src/store/slices/cameras/cameras-types.ts b/freemocap-ui/src/store/slices/cameras/cameras-types.ts
index cc88b9f4..2d815e59 100644
--- a/freemocap-ui/src/store/slices/cameras/cameras-types.ts
+++ b/freemocap-ui/src/store/slices/cameras/cameras-types.ts
@@ -177,3 +177,36 @@ export function extractConfigSettings(
writer_fourcc: config.writer_fourcc,
};
}
+
+// Reconcile a fresh detection result against the cameras already in the store.
+//
+// A camera's `camera_id` is stable across replugs (it is a hash of the device
+// path), but its `camera_index` (the OS enumeration slot) is volatile β€” plugging
+// or unplugging a device can renumber the others. So for a camera we already
+// know, we keep the user's tuned settings but always take `camera_index` from the
+// fresh detection. This mirrors what the persistence layer does: `extractConfigSettings`
+// deliberately omits `camera_index` because it is detection-owned, never a saved
+// setting. Trusting a cached index here is what lets two cameras claim the same
+// index and get rejected by the backend's uniqueness check.
+export function reconcileDetectedCameras(
+ previousCameras: Camera[],
+ detectedCameras: Camera[],
+): Camera[] {
+ return detectedCameras.map(camera => {
+ const existing = previousCameras.find(cam => cam.id === camera.id);
+ if (!existing) return camera;
+ return {
+ ...camera,
+ actualConfig: existing.actualConfig,
+ desiredConfig: {
+ ...existing.desiredConfig,
+ camera_index: camera.index,
+ },
+ hasConfigMismatch: existing.hasConfigMismatch,
+ connectionStatus: existing.connectionStatus,
+ selected: existing.selected,
+ realtimeEnabled: existing.realtimeEnabled,
+ metrics: existing.metrics,
+ };
+ });
+}