Skip to content

Commit 2130558

Browse files
committed
wip: change assignment
1 parent fff3573 commit 2130558

File tree

9 files changed

+140
-61
lines changed

9 files changed

+140
-61
lines changed

meteor/server/api/peripheralDevice.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ import { convertPeripheralDeviceForGateway } from '../publications/peripheralDev
6767
import { executePeripheralDeviceFunction } from './peripheralDevice/executeFunction'
6868
import KoaRouter from '@koa/router'
6969
import bodyParser from 'koa-bodyparser'
70-
import { DBStudio, StudioDeviceSettings } from '@sofie-automation/corelib/dist/dataModel/Studio'
70+
import { DBStudio } from '@sofie-automation/corelib/dist/dataModel/Studio'
7171

7272
const apmNamespace = 'peripheralDevice'
7373
export namespace ServerPeripheralDeviceAPI {
@@ -439,7 +439,8 @@ export namespace ServerPeripheralDeviceAPI {
439439
if (
440440
// Debug states are only valid for Playout devices and must be enabled with the `debugState` option
441441
peripheralDevice.type !== PeripheralDeviceType.PLAYOUT ||
442-
!peripheralDevice.studioId // Must be attached to a studio`
442+
!peripheralDevice.studioId || // Must be attached to a studio`
443+
!peripheralDevice.configId // Must be assigned a config
443444
) {
444445
return {}
445446
}
@@ -456,9 +457,10 @@ export namespace ServerPeripheralDeviceAPI {
456457
studioForDevice.peripheralDeviceSettings.deviceSettings
457458
).obj
458459

459-
const settingsForDevice = Object.values<StudioDeviceSettings>(studioDeviceSettings).find(
460-
(s) => s.peripheralDeviceId === peripheralDevice._id
461-
)
460+
const settingsForDevice = studioDeviceSettings[peripheralDevice.configId]
461+
// Object.values<StudioDeviceSettings>(studioDeviceSettings).find(
462+
// (s) => s.peripheralDeviceId === access.deviceId
463+
// )
462464
if (!settingsForDevice) return {}
463465

464466
// Make sure debugState is enabled

meteor/server/api/studio/api.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { check } from '../../lib/check'
33
import { registerClassToMeteorMethods } from '../../methods'
44
import { NewStudiosAPI, StudiosAPIMethods } from '@sofie-automation/meteor-lib/dist/api/studios'
55
import { DBStudio } from '@sofie-automation/corelib/dist/dataModel/Studio'
6-
import { literal, getRandomId } from '../../lib/tempLib'
6+
import { literal, getRandomId, protectString } from '../../lib/tempLib'
77
import { lazyIgnore } from '../../lib/lib'
88
import { stringifyError } from '@sofie-automation/shared-lib/dist/lib/stringifyError'
99
import {
@@ -22,11 +22,12 @@ import {
2222
} from '../../collections'
2323
import { MethodContextAPI, MethodContext } from '../methodContext'
2424
import { wrapDefaultObject } from '@sofie-automation/corelib/dist/settings/objectWithOverrides'
25-
import { OrganizationId, StudioId } from '@sofie-automation/corelib/dist/dataModel/Ids'
25+
import { OrganizationId, PeripheralDeviceId, StudioId } from '@sofie-automation/corelib/dist/dataModel/Ids'
2626
import { logger } from '../../logging'
2727
import { DEFAULT_MINIMUM_TAKE_SPAN } from '@sofie-automation/shared-lib/dist/core/constants'
2828
import { UserPermissions } from '@sofie-automation/meteor-lib/dist/userPermissions'
2929
import { assertConnectionHasOneOfPermissions } from '../../security/auth'
30+
import { triggerWriteAccessBecauseNoCheckNecessary } from '../../security/lib/securityVerify'
3031

3132
const PERMISSIONS_FOR_MANAGE_STUDIOS: Array<keyof UserPermissions> = ['configure']
3233

@@ -119,6 +120,40 @@ class ServerStudiosAPI extends MethodContextAPI implements NewStudiosAPI {
119120
async removeStudio(studioId: StudioId) {
120121
return removeStudio(this, studioId)
121122
}
123+
124+
async assignConfigToPeripheralDevice(studioId: StudioId, configId: string, deviceId: PeripheralDeviceId | null) {
125+
triggerWriteAccessBecauseNoCheckNecessary()
126+
127+
// Unassign other uses
128+
await PeripheralDevices.updateAsync(
129+
{
130+
studioId: studioId,
131+
configId: configId,
132+
_id: { $ne: deviceId ?? protectString('') },
133+
},
134+
{
135+
$unset: {
136+
// nocommit check this
137+
studioId: 1,
138+
configId: 1,
139+
},
140+
},
141+
{
142+
multi: true,
143+
}
144+
)
145+
146+
if (deviceId) {
147+
// Set for the new one
148+
await PeripheralDevices.updateAsync(deviceId, {
149+
$set: {
150+
// nocommit check this
151+
studioId: studioId,
152+
configId: configId,
153+
},
154+
})
155+
}
156+
}
122157
}
123158
registerClassToMeteorMethods(StudiosAPIMethods, ServerStudiosAPI, false)
124159

meteor/server/collections/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export const PeripheralDevices = createAsyncOnlyMongoCollection<PeripheralDevice
141141
'connected',
142142
'connectionId',
143143
'token',
144+
'studioId', // nocommit ensure this is respected
144145
// 'settings' is allowed
145146
])
146147
},

meteor/server/publications/peripheralDeviceForDevice.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { Complete, assertNever, literal } from '@sofie-automation/corelib/dist/l
1414
import { MongoFieldSpecifierOnesStrict } from '@sofie-automation/corelib/dist/mongo'
1515
import {
1616
DBStudio,
17-
StudioDeviceSettings,
1817
StudioIngestDevice,
1918
StudioInputDevice,
2019
StudioPlayoutDevice,
@@ -44,13 +43,14 @@ const studioFieldsSpecifier = literal<MongoFieldSpecifierOnesStrict<Pick<DBStudi
4443
peripheralDeviceSettings: 1,
4544
})
4645

47-
type PeripheralDeviceFields = '_id' | 'category' | 'studioId' | 'secretSettings'
46+
type PeripheralDeviceFields = '_id' | 'category' | 'studioId' | 'configId' | 'secretSettings'
4847
const peripheralDeviceFieldsSpecifier = literal<
4948
MongoFieldSpecifierOnesStrict<Pick<PeripheralDevice, PeripheralDeviceFields>>
5049
>({
5150
_id: 1,
5251
category: 1,
5352
studioId: 1,
53+
configId: 1,
5454
secretSettings: 1,
5555
})
5656

@@ -65,11 +65,15 @@ export function convertPeripheralDeviceForGateway(
6565
let deviceSettings: PeripheralDeviceForDevice['deviceSettings'] = {}
6666

6767
if (studio) {
68-
const allDeviceSettingsInStudio = applyAndValidateOverrides(studio.peripheralDeviceSettings.deviceSettings).obj
69-
deviceSettings =
70-
Object.values<StudioDeviceSettings>(allDeviceSettingsInStudio).find(
71-
(v) => v.peripheralDeviceId === peripheralDevice._id
72-
)?.options ?? deviceSettings
68+
if (peripheralDevice.configId) {
69+
const allDeviceSettingsInStudio = applyAndValidateOverrides(
70+
studio.peripheralDeviceSettings.deviceSettings
71+
).obj
72+
deviceSettings = allDeviceSettingsInStudio[peripheralDevice.configId] ?? deviceSettings
73+
// Object.values<StudioDeviceSettings>(allDeviceSettingsInStudio).find(
74+
// (v) => v.peripheralDeviceId === peripheralDevice._id
75+
// )?.options ?? deviceSettings
76+
}
7377

7478
switch (peripheralDevice.category) {
7579
case PeripheralDeviceCategory.INGEST: {

packages/corelib/src/dataModel/PeripheralDevice.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ export interface PeripheralDevice {
2828
/** Name of the device (set by the device) */
2929
deviceName: string
3030

31+
// TODO: combine these into a single property, so that they will always be defined at the same times
3132
/** The studio this device is assigned to. Will be undefined for sub-devices */
3233
studioId?: StudioId
34+
/** The config object this device uses from the studio. Will be undefined for sub-devices */
35+
configId?: string
3336

3437
category: PeripheralDeviceCategory
3538
type: PeripheralDeviceType

packages/corelib/src/dataModel/Studio.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,11 @@ export interface StudioPlayoutDevice {
195195
}
196196

197197
export interface StudioDeviceSettings {
198-
/**
199-
* The id of the gateway this is assigned to
200-
* Future: This may be replaced with some other grouping or way of assigning devices
201-
*/
202-
peripheralDeviceId: PeripheralDeviceId | undefined
198+
// /**
199+
// * The id of the gateway this is assigned to
200+
// * Future: This may be replaced with some other grouping or way of assigning devices
201+
// */
202+
// peripheralDeviceId: PeripheralDeviceId | undefined
203203

204204
/**
205205
* User friendly name for the device

packages/meteor-lib/src/api/studios.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { StudioId } from '@sofie-automation/corelib/dist/dataModel/Ids'
1+
import { PeripheralDeviceId, StudioId } from '@sofie-automation/corelib/dist/dataModel/Ids'
22
import {
33
IStudioSettings,
44
MappingsExt,
@@ -9,11 +9,19 @@ import {
99
export interface NewStudiosAPI {
1010
insertStudio(): Promise<StudioId>
1111
removeStudio(studioId: StudioId): Promise<void>
12+
13+
assignConfigToPeripheralDevice(
14+
studioId: StudioId,
15+
configId: string,
16+
deviceId: PeripheralDeviceId | null
17+
): Promise<void>
1218
}
1319

1420
export enum StudiosAPIMethods {
1521
'insertStudio' = 'studio.insertStudio',
1622
'removeStudio' = 'studio.removeStudio',
23+
24+
'assignConfigToPeripheralDevice' = 'studio.assignConfigToPeripheralDevice',
1725
}
1826

1927
/**

0 commit comments

Comments
 (0)