Skip to content

Commit

Permalink
chore: update to latest 1.51 server-integration
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarpl committed Nov 23, 2023
1 parent 6be4a7b commit 0e91479
Show file tree
Hide file tree
Showing 15 changed files with 347 additions and 277 deletions.
1 change: 0 additions & 1 deletion .vscode/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
tasks.json
settings.json
17 changes: 15 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@
{
"type": "node",
"request": "launch",
"name": "Launch Input Gateway",
"name": "Build & Run Input Gateway",
"preLaunchTask": "Input Gateway: build",
"program": "${workspaceFolder}/packages/input-gateway/dist/index.js",
"args": ["-id", "devxxsofie"],
"console": "integratedTerminal",
"outFiles": [
"${workspaceFolder}/packages/input-gateway/dist/**/*.js",
"${workspaceFolder}/packages/input-manager/dist/**/*.js"
]
},
{
"type": "node",
"request": "launch",
"name": "Run Input Gateway",
"program": "${workspaceFolder}/packages/input-gateway/dist/index.js",
"args": ["-id", "devxxsofie"],
"console": "integratedTerminal",
Expand All @@ -17,4 +30,4 @@
]
}
]
}
}
14 changes: 14 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"command": "yarn build",
"label": "Input Gateway: build",
"group": {
"kind": "build",
"isDefault": true
},
}
],
}
4 changes: 2 additions & 2 deletions packages/input-gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
],
"dependencies": {
"@sofie-automation/input-manager": "0.2.2-alpha.1",
"@sofie-automation/server-core-integration": "1.51.0-nightly-feat-input-gw-shift-20230731-125304-43763fe.0",
"@sofie-automation/shared-lib": "1.51.0-nightly-feat-input-gw-shift-20230731-125304-43763fe.0",
"@sofie-automation/server-core-integration": "1.51.0-nightly-release51-20231117-131706-17bb2e3.0",
"@sofie-automation/shared-lib": "1.51.0-nightly-release51-20231117-131706-17bb2e3.0",
"debug": "^4.3.1",
"eventemitter3": "^4.0.7",
"p-all": "^3.0.0",
Expand Down
57 changes: 34 additions & 23 deletions packages/input-gateway/src/coreHandler.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { CoreConnection, CoreOptions, DDPConnectorOptions } from '@sofie-automation/server-core-integration'
import {
CoreConnection,
CoreOptions,
DDPConnectorOptions,
PeripheralDevicePubSub,
PeripheralDevicePubSubCollectionsNames,
} from '@sofie-automation/server-core-integration'
import { StatusCode } from '@sofie-automation/shared-lib/dist/lib/status'
import _ from 'underscore'
import * as Winston from 'winston'
import { DeviceConfig } from './inputManagerHandler'
import fs from 'fs'
import { INPUT_DEVICE_CONFIG } from './configManifest'
import { PeripheralDeviceCommandId, PeripheralDeviceId } from '@sofie-automation/shared-lib/dist/core/model/Ids'
import { PeripheralDeviceForDevice } from '@sofie-automation/shared-lib/dist/core/model/peripheralDevice'
import {
PeripheralDeviceCategory,
PeripheralDeviceSubType,
Expand Down Expand Up @@ -105,10 +110,16 @@ export class CoreHandler {
this.logger.info('Core: Setting up subscriptions..')
this.logger.info('DeviceId: ' + this.core.deviceId)
await Promise.all([
this.core.autoSubscribe('peripheralDeviceForDevice', this.core.deviceId),
this.core.autoSubscribe('studioOfDevice', this.core.deviceId),
this.core.autoSubscribe('peripheralDeviceCommands', this.core.deviceId),
// @todo: subscribe to userInput
this.core.autoSubscribe(
PeripheralDevicePubSub.peripheralDeviceForDevice,
this.core.deviceId,
this._deviceOptions?.deviceToken
),
this.core.autoSubscribe(
PeripheralDevicePubSub.peripheralDeviceCommands,
this.core.deviceId,
this._deviceOptions?.deviceToken
),
])
this.logger.info('Core: Subscriptions are set up!')
if (this._observers.length) {
Expand All @@ -119,12 +130,12 @@ export class CoreHandler {
this._observers = []
}
// setup observers
const observer = this.core.observe('peripheralDeviceForDevice')
observer.added = (id: string) => {
this.onDeviceChanged(protectString(id))
const observer = this.core.observe(PeripheralDevicePubSubCollectionsNames.peripheralDeviceForDevice)
observer.added = (id) => {
this.onDeviceChanged(id)
}
observer.changed = (id1: string) => {
this.onDeviceChanged(protectString(id1))
observer.changed = (id) => {
this.onDeviceChanged(id)
}
this.setupObserverForPeripheralDeviceCommands(this)
return
Expand Down Expand Up @@ -211,7 +222,7 @@ export class CoreHandler {
}
onDeviceChanged(id: PeripheralDeviceId): void {
if (id === this.core.deviceId) {
const col = this.core.getCollection<PeripheralDeviceForDevice>('peripheralDeviceForDevice')
const col = this.core.getCollection(PeripheralDevicePubSubCollectionsNames.peripheralDeviceForDevice)
if (!col) throw new Error('collection "peripheralDeviceForDevice" not found!')

const device = col.findOne(id)
Expand Down Expand Up @@ -285,15 +296,15 @@ export class CoreHandler {
}
}
}
retireExecuteFunction(cmdId: string): void {
delete this._executedFunctions[cmdId]
retireExecuteFunction(cmdId: PeripheralDeviceCommandId): void {
delete this._executedFunctions[unprotectString(cmdId)]
}
setupObserverForPeripheralDeviceCommands(functionObject: CoreHandler): void {
const observer = functionObject.core.observe('peripheralDeviceCommands')
const observer = functionObject.core.observe(PeripheralDevicePubSubCollectionsNames.peripheralDeviceCommands)
functionObject.killProcess(0)
functionObject._observers.push(observer)
const addedChangedCommand = (id: PeripheralDeviceCommandId) => {
const cmds = functionObject.core.getCollection<PeripheralDeviceCommand>('peripheralDeviceCommands')
const cmds = functionObject.core.getCollection(PeripheralDevicePubSubCollectionsNames.peripheralDeviceCommands)
if (!cmds) throw Error('"peripheralDeviceCommands" collection not found!')
const cmd = cmds.findOne(id)
if (!cmd) throw Error('PeripheralCommand "' + id + '" not found!')
Expand All @@ -304,18 +315,18 @@ export class CoreHandler {
// console.log('not mine', cmd.deviceId, this.core.deviceId)
}
}
observer.added = (id: string) => {
addedChangedCommand(protectString(id))
observer.added = (id) => {
addedChangedCommand(id)
}
observer.changed = (id: string) => {
addedChangedCommand(protectString(id))
observer.changed = (id) => {
addedChangedCommand(id)
}
observer.removed = (id: string) => {
observer.removed = (id) => {
this.retireExecuteFunction(id)
}
const cmds = functionObject.core.getCollection('peripheralDeviceCommands')
const cmds = functionObject.core.getCollection(PeripheralDevicePubSubCollectionsNames.peripheralDeviceCommands)
if (!cmds) throw Error('"peripheralDeviceCommands" collection not found!')
;(cmds.find({}) as PeripheralDeviceCommand[]).forEach((cmd: PeripheralDeviceCommand) => {
cmds.find({}).forEach((cmd: PeripheralDeviceCommand) => {
if (cmd.deviceId === functionObject.core.deviceId) {
this.executeFunction(cmd, functionObject)
}
Expand Down
92 changes: 54 additions & 38 deletions packages/input-gateway/src/inputManagerHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ import {
TriggerEvent,
} from '@sofie-automation/input-manager'
import { interpollateTranslation, translateMessage } from './lib/translatableMessage'
import { protectString } from '@sofie-automation/shared-lib/dist/lib/protectedString'
import { ITranslatableMessage } from '@sofie-automation/shared-lib/dist/lib/translations'
import { Observer, SubscriptionId } from '@sofie-automation/server-core-integration'
import {
Observer,
SubscriptionId,
PeripheralDevicePubSub,
PeripheralDevicePubSubCollectionsNames,
} from '@sofie-automation/server-core-integration'
import { sleep } from '@sofie-automation/shared-lib/dist/lib/lib'
import PQueue from 'p-queue'
import { InputGatewaySettings } from './generated/options'
Expand Down Expand Up @@ -60,7 +64,7 @@ export class InputManagerHandler {
#shiftRegisters: number[] = []
#deviceTriggerActions: Record<string, Record<string, DeviceActionArguments>> = {}

#observers: Observer[] = []
#observers: Observer<any>[] = []
/** Set of deviceIds to check for triggers to send */
#devicesWithTriggersToSend = new Set<string>()

Expand Down Expand Up @@ -144,11 +148,16 @@ export class InputManagerHandler {
this.#inputManager = await this.#createInputManager(settings)

this.#triggersSubscriptionId = await this.#coreHandler.core.autoSubscribe(
'mountedTriggersForDevice',
PeripheralDevicePubSub.mountedTriggersForDevice,
this.#coreHandler.core.deviceId,
InputManagerHandler.getDeviceIds(settings),
this.#config.device.deviceToken
)
await this.#coreHandler.core.autoSubscribe(
PeripheralDevicePubSub.mountedTriggersForDevicePreview,
this.#coreHandler.core.deviceId,
InputManagerHandler.getDeviceIds(settings)
this.#config.device.deviceToken
)
await this.#coreHandler.core.autoSubscribe('mountedTriggersForDevicePreview', this.#coreHandler.core.deviceId)

this.#logger.info(`Subscribed to mountedTriggersForDevice: ${this.#triggersSubscriptionId}`)

Expand All @@ -163,9 +172,11 @@ export class InputManagerHandler {
.catch((err) => this.#logger.error(`Error in refreshMountedTriggers() on coreHandler.onConnected: ${err}`))
})

const mountedTriggersObserver = this.#coreHandler.core.observe('mountedTriggers')
const mountedTriggersObserver = this.#coreHandler.core.observe(
PeripheralDevicePubSubCollectionsNames.mountedTriggers
)
mountedTriggersObserver.added = (id, _obj) => {
this.#handleChangedMountedTrigger(protectString(id)).catch((err) =>
this.#handleChangedMountedTrigger(id).catch((err) =>
this.#logger.error(`Error in handleChangedMountedTrigger() on mountedTriggersObserver.added: ${err}`)
)
}
Expand All @@ -176,8 +187,8 @@ export class InputManagerHandler {
newFields: Partial<DeviceTriggerMountedAction>
) => {
const obj = this.#coreHandler.core
.getCollection<DeviceTriggerMountedAction>('mountedTriggers')
.findOne(protectString(id))
.getCollection(PeripheralDevicePubSubCollectionsNames.mountedTriggers)
.findOne(id)
if (!obj) return
if (
newFields['deviceId'] ||
Expand All @@ -189,13 +200,13 @@ export class InputManagerHandler {
oldFields.deviceId ?? obj.deviceId,
oldFields.deviceTriggerId ?? obj.deviceTriggerId
)
.then(async () => this.#handleChangedMountedTrigger(protectString(id)))
.then(async () => this.#handleChangedMountedTrigger(id))
.catch((err) => {
this.#logger.error(`Error in handleRemovedMountedTrigger() on mountedTriggersObserver.changed: ${err}`)
})
return
}
this.#handleChangedMountedTrigger(protectString(id)).catch((err) => {
this.#handleChangedMountedTrigger(id).catch((err) => {
this.#logger.error(`Error in handleChangedMountedTrigger() on mountedTriggersObserver.changed: ${err}`)
})
}
Expand All @@ -205,12 +216,16 @@ export class InputManagerHandler {
this.#logger.error(`Error in handleRemovedMountedTrigger() on mountedTriggersObserver.removed: ${err}`)
})
}
const triggersPreviewsObserver = this.#coreHandler.core.observe('mountedTriggersPreviews')
const triggersPreviewsObserver = this.#coreHandler.core.observe(
PeripheralDevicePubSubCollectionsNames.mountedTriggersPreviews
)
triggersPreviewsObserver.added = (id, obj) => {
const changedPreview = obj as PreviewWrappedAdLib
const mountedActions = this.#coreHandler.core.getCollection<DeviceTriggerMountedAction>('mountedTriggers').find({
actionId: changedPreview.actionId,
})
const mountedActions = this.#coreHandler.core
.getCollection(PeripheralDevicePubSubCollectionsNames.mountedTriggers)
.find({
actionId: changedPreview.actionId,
})
if (mountedActions.length === 0) {
this.#logger.error(`Could not find mounted action for PreviewAdlib: "${id}"`)
return
Expand All @@ -223,15 +238,17 @@ export class InputManagerHandler {
}
triggersPreviewsObserver.changed = (id, _old, _cleared, _new) => {
const changedPreview = this.#coreHandler.core
.getCollection<PreviewWrappedAdLib>('mountedTriggersPreviews')
.findOne(protectString(id))
.getCollection(PeripheralDevicePubSubCollectionsNames.mountedTriggersPreviews)
.findOne(id)
if (!changedPreview) {
this.#logger.error(`Could not find PreviewAdlib: "${id}"`)
return
}
const mountedActions = this.#coreHandler.core.getCollection('mountedTriggers').find({
actionId: changedPreview.actionId,
}) as DeviceTriggerMountedAction[]
const mountedActions = this.#coreHandler.core
.getCollection(PeripheralDevicePubSubCollectionsNames.mountedTriggers)
.find({
actionId: changedPreview.actionId,
})
if (mountedActions.length === 0) {
this.#logger.error(`Could not find mounted action for PreviewAdlib: "${changedPreview._id}"`)
return
Expand All @@ -244,9 +261,11 @@ export class InputManagerHandler {
}
triggersPreviewsObserver.removed = (_id, obj) => {
const changedPreview = obj as PreviewWrappedAdLib
const mountedActions = this.#coreHandler.core.getCollection('mountedTriggers').find({
actionId: changedPreview.actionId,
}) as DeviceTriggerMountedAction[]
const mountedActions = this.#coreHandler.core
.getCollection(PeripheralDevicePubSubCollectionsNames.mountedTriggers)
.find({
actionId: changedPreview.actionId,
})
if (mountedActions.length === 0) {
this.#logger.error(`Could not find mounted action for PreviewAdlib: "${changedPreview._id}"`)
return
Expand Down Expand Up @@ -300,9 +319,10 @@ export class InputManagerHandler {
this.#inputManager = await this.#createInputManager(settings)

this.#triggersSubscriptionId = await this.#coreHandler.core.autoSubscribe(
'mountedTriggersForDevice',
PeripheralDevicePubSub.mountedTriggersForDevice,
this.#coreHandler.core.deviceId,
InputManagerHandler.getDeviceIds(settings)
InputManagerHandler.getDeviceIds(settings),
this.#config.device.deviceToken
)

await this.#refreshMountedTriggers()
Expand All @@ -320,16 +340,12 @@ export class InputManagerHandler {
const endReplaceTransaction = this.#inputManager.beginFeedbackReplaceTransaction()

const mountedActions = this.#coreHandler.core
.getCollection('mountedTriggers')
.find({}) as DeviceTriggerMountedAction[]
.getCollection(PeripheralDevicePubSubCollectionsNames.mountedTriggers)
.find({})

for (const mountedTrigger of mountedActions) {
try {
await this.#handleChangedMountedTrigger(mountedTrigger._id)
} catch (err) {
this.#logger.error(`Error in #handleChangedMountedTrigger in #refreshMountedTriggers: ${err}`)
}
}
await Promise.allSettled(
mountedActions.map(async (mountedTrigger) => this.#handleChangedMountedTrigger(mountedTrigger._id))
)

await endReplaceTransaction()
}
Expand Down Expand Up @@ -506,7 +522,7 @@ export class InputManagerHandler {

async #handleChangedMountedTrigger(id: DeviceTriggerMountedActionId): Promise<void> {
const mountedTrigger = this.#coreHandler.core
.getCollection<DeviceTriggerMountedAction>('mountedTriggers')
.getCollection(PeripheralDevicePubSubCollectionsNames.mountedTriggers)
.findOne(id)
if (!this.#inputManager) return

Expand Down Expand Up @@ -608,11 +624,11 @@ export class InputManagerHandler {

if (actionId) {
const previewedAdlibs = this.#coreHandler.core
.getCollection('mountedTriggersPreviews')
.getCollection(PeripheralDevicePubSubCollectionsNames.mountedTriggersPreviews)
.find({
actionId: mountedTrigger?.actionId,
})
.reverse() as PreviewWrappedAdLib[]
.reverse()

if (previewedAdlibs.length > 0) {
tally = tally | Tally.PRESENT
Expand Down
Loading

0 comments on commit 0e91479

Please sign in to comment.