@@ -13,14 +13,18 @@ import { runUpgradeForStudio, validateConfigForStudio } from '../../../migration
1313import { NoteSeverity } from '@sofie-automation/blueprints-integration'
1414import { DBRundownPlaylist } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist'
1515import { ServerClientAPI } from '../../client'
16- import { assertNever } from '../../../lib/tempLib'
16+ import { assertNever , literal } from '../../../lib/tempLib'
1717import { getCurrentTime } from '../../../lib/lib'
1818import { StudioJobs } from '@sofie-automation/corelib/dist/worker/studio'
19- import { DBStudio } from '@sofie-automation/corelib/dist/dataModel/Studio'
19+ import { DBStudio , StudioDeviceSettings } from '@sofie-automation/corelib/dist/dataModel/Studio'
2020import { PeripheralDevice } from '@sofie-automation/corelib/dist/dataModel/PeripheralDevice'
2121import { ServerPlayoutAPI } from '../../playout/playout'
2222import { assertConnectionHasOneOfPermissions } from '../../../security/auth'
2323import { UserPermissions } from '@sofie-automation/meteor-lib/dist/userPermissions'
24+ import {
25+ applyAndValidateOverrides ,
26+ ObjectOverrideSetOp ,
27+ } from '@sofie-automation/corelib/dist/settings/objectWithOverrides'
2428
2529const PERMISSIONS_FOR_PLAYOUT_USERACTION : Array < keyof UserPermissions > = [ 'studio' ]
2630
@@ -213,7 +217,8 @@ class StudiosServerAPI implements StudiosRestAPI {
213217 _connection : Meteor . Connection ,
214218 _event : string ,
215219 studioId : StudioId ,
216- deviceId : PeripheralDeviceId
220+ deviceId : PeripheralDeviceId ,
221+ configId : string | undefined
217222 ) : Promise < ClientAPI . ClientResponse < void > > {
218223 const studio = await Studios . findOneAsync ( studioId )
219224 if ( ! studio )
@@ -238,9 +243,33 @@ class StudiosServerAPI implements StudiosRestAPI {
238243 412
239244 )
240245 }
246+
247+ // If no configId is provided, use the id of the device
248+ configId = configId || unprotectString ( device . _id )
249+
250+ // Ensure that the requested config blob exists
251+ const availableDeviceSettings = applyAndValidateOverrides ( studio . peripheralDeviceSettings . deviceSettings ) . obj
252+ if ( ! availableDeviceSettings [ configId ] ) {
253+ await Studios . updateAsync ( studioId , {
254+ $push : {
255+ 'peripheralDeviceSettings.deviceSettings.overrides' : literal < ObjectOverrideSetOp > ( {
256+ op : 'set' ,
257+ path : configId ,
258+ value : literal < StudioDeviceSettings > ( {
259+ name : device . name ,
260+ options : { } ,
261+ } ) ,
262+ } ) ,
263+ } ,
264+ } )
265+ }
266+
241267 await PeripheralDevices . updateAsync ( deviceId , {
242268 $set : {
243- studioId, // nocommit - this
269+ studioAndConfigId : {
270+ studioId,
271+ configId,
272+ } ,
244273 } ,
245274 } )
246275
@@ -259,11 +288,17 @@ class StudiosServerAPI implements StudiosRestAPI {
259288 UserError . from ( new Error ( `Studio does not exist` ) , UserErrorMessage . StudioNotFound ) ,
260289 404
261290 )
262- await PeripheralDevices . updateAsync ( deviceId , {
263- $unset : {
264- studioId : 1 , // nocommit - this
291+ await PeripheralDevices . updateAsync (
292+ {
293+ _id : deviceId ,
294+ 'studioAndConfigId.studioId' : studioId ,
265295 } ,
266- } )
296+ {
297+ $unset : {
298+ studioAndConfigId : 1 ,
299+ } ,
300+ }
301+ )
267302
268303 return ClientAPI . responseSuccess ( undefined , 200 )
269304 }
@@ -392,7 +427,7 @@ export function registerRoutes(registerRoute: APIRegisterHook<StudiosRestAPI>):
392427 }
393428 )
394429
395- registerRoute < { studioId : string } , { deviceId : string } , void > (
430+ registerRoute < { studioId : string } , { deviceId : string ; configId : string | undefined } , void > (
396431 'put' ,
397432 '/studios/:studioId/devices' ,
398433 new Map ( [
@@ -403,9 +438,10 @@ export function registerRoutes(registerRoute: APIRegisterHook<StudiosRestAPI>):
403438 async ( serverAPI , connection , events , params , body ) => {
404439 const studioId = protectString < StudioId > ( params . studioId )
405440 const deviceId = protectString < PeripheralDeviceId > ( body . deviceId )
406- logger . info ( `API PUT: Attach device ${ deviceId } to studio ${ studioId } ` )
441+ const configId = body . configId
442+ logger . info ( `API PUT: Attach device ${ deviceId } to studio ${ studioId } (${ configId } )` )
407443
408- return await serverAPI . attachDeviceToStudio ( connection , events , studioId , deviceId )
444+ return await serverAPI . attachDeviceToStudio ( connection , events , studioId , deviceId , configId )
409445 }
410446 )
411447
0 commit comments