Skip to content

Commit

Permalink
Merge pull request #328 from nrkno/feat/connection-mgmt-SOFIE-1152
Browse files Browse the repository at this point in the history
Refactor connection management
  • Loading branch information
nytamin authored Sep 20, 2024
2 parents 517f0ee + 3d5ee7e commit d182f1f
Show file tree
Hide file tree
Showing 31 changed files with 1,503 additions and 1,007 deletions.
122 changes: 25 additions & 97 deletions packages/quick-tsr/src/tsrHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
Datastore,
DeviceStatus,
SlowSentCommandInfo,
DeviceOptionsBase,
SlowFulfilledCommandInfo,
DeviceType,
CasparCGDevice,
Expand All @@ -17,23 +16,17 @@ import {
} from 'timeline-state-resolver'
import { ThreadedClass } from 'threadedclass'

import * as _ from 'underscore'
import { TSRSettings } from './index'
import { BaseRemoteDeviceIntegration } from 'timeline-state-resolver/dist/service/remoteDeviceInstance'

/**
* Represents a connection between Gateway and TSR
*/
export class TSRHandler {
private tsr!: Conductor

private _multiThreaded: boolean | null = null

// private _timeline: TSRTimeline
// private _mappings: Mappings

private _devices: { [deviceId: string]: BaseRemoteDeviceIntegration<DeviceOptionsBase<any>> } = {}

constructor() {
// nothing
}
Expand Down Expand Up @@ -76,6 +69,29 @@ export class TSRHandler {
// todo ?
})

this.tsr.connectionManager.on('connectionEvent:connectionChanged', (deviceId: string, status: DeviceStatus) => {
console.log(`Device ${deviceId} status changed: ${JSON.stringify(status)}`)
})
this.tsr.connectionManager.on(
'connectionEvent:slowSentCommand',
(_deviceId: string, _info: SlowSentCommandInfo) => {
// console.log(`Device ${device.deviceId} slow sent command: ${_info}`)
}
)
this.tsr.connectionManager.on(
'connectionEvent:slowFulfilledCommand',
(_deviceId: string, _info: SlowFulfilledCommandInfo) => {
// console.log(`Device ${device.deviceId} slow fulfilled command: ${_info}`)
}
)
this.tsr.connectionManager.on('connectionEvent:commandReport', (deviceId: string, command: any) => {
console.log(`Device ${deviceId} command: ${JSON.stringify(command)}`)
})
this.tsr.connectionManager.on('connectionEvent:debug', (deviceId: string, ...args: any[]) => {
const data = args.map((arg) => (typeof arg === 'object' ? JSON.stringify(arg) : arg))
console.log(`Device ${deviceId} debug: ${data}`)
})

await this.tsr.init()

// this._initialized = true
Expand All @@ -90,7 +106,7 @@ export class TSRHandler {
else return Promise.resolve()
}
async logMediaList(): Promise<void> {
for (const deviceContainer of this.tsr.getDevices()) {
for (const deviceContainer of this.tsr.connectionManager.getConnections()) {
if (deviceContainer.deviceType === DeviceType.CASPARCG) {
const device = deviceContainer.device as ThreadedClass<CasparCGDevice>

Expand Down Expand Up @@ -118,94 +134,6 @@ export class TSRHandler {
this.tsr.setDatastore(store)
}
public async setDevices(devices: { [deviceId: string]: DeviceOptionsAny }): Promise<void> {
const ps: Array<Promise<void>> = []

_.each(devices, (deviceOptions: DeviceOptionsAny, deviceId: string) => {
const oldDevice = this.tsr.getDevice(deviceId)

if (!oldDevice) {
if (deviceOptions.options) {
console.log('Initializing device: ' + deviceId)
ps.push(this._addDevice(deviceId, deviceOptions))
}
} else {
if (this._multiThreaded !== null && deviceOptions.isMultiThreaded === undefined) {
deviceOptions.isMultiThreaded = this._multiThreaded
}
if (deviceOptions.options) {
let anyChanged = false

// let oldOptions = (oldDevice.deviceOptions).options || {}

if (!_.isEqual(oldDevice.deviceOptions, deviceOptions)) {
anyChanged = true
}

if (anyChanged) {
console.log('Re-initializing device: ' + deviceId)
ps.push(this._removeDevice(deviceId).then(async () => this._addDevice(deviceId, deviceOptions)))
}
}
}
})

_.each(this.tsr.getDevices(), (oldDevice: BaseRemoteDeviceIntegration<DeviceOptionsBase<any>>) => {
const deviceId = oldDevice.deviceId
if (!devices[deviceId]) {
console.log('Un-initializing device: ' + deviceId)
ps.push(this._removeDevice(deviceId))
}
})

await Promise.all(ps)
}
private async _addDevice(deviceId: string, options: DeviceOptionsAny) {
// console.log('Adding device ' + deviceId)

if (!options.limitSlowSentCommand) options.limitSlowSentCommand = 40
if (!options.limitSlowFulfilledCommand) options.limitSlowFulfilledCommand = 100

try {
const device = await this.tsr.addDevice(deviceId, options)

this._devices[deviceId] = device

await device.device.on('connectionChanged', ((status: DeviceStatus) => {
console.log(`Device ${device.deviceId} status changed: ${JSON.stringify(status)}`)
}) as () => void)
await device.device.on('slowSentCommand', ((_info: SlowSentCommandInfo) => {
// console.log(`Device ${device.deviceId} slow sent command: ${_info}`)
}) as () => void)
await device.device.on('slowFulfilledCommand', ((_info: SlowFulfilledCommandInfo) => {
// console.log(`Device ${device.deviceId} slow fulfilled command: ${_info}`)
}) as () => void)
await device.device.on('commandReport', ((command: any) => {
console.log(`Device ${device.deviceId} command: ${JSON.stringify(command)}`)
}) as () => void)
await device.device.on('debug', (...args: any[]) => {
const data = args.map((arg) => (typeof arg === 'object' ? JSON.stringify(arg) : arg))
console.log(`Device ${device.deviceId} debug: ${data}`)
})
// also ask for the status now, and update:
// onConnectionChanged(await device.device.getStatus())
} catch (e) {
console.error(`Error when adding device "${deviceId}"`, e)
}
}
private async _removeDevice(deviceId: string) {
try {
await this.tsr.removeDevice(deviceId)
} catch (e) {
console.error('Error when removing tsr device: ' + e)
}

if (this._devices[deviceId]) {
try {
await this._devices[deviceId].device.terminate()
} catch (e) {
console.error('Error when removing device: ' + e)
}
}
delete this._devices[deviceId]
this.tsr.connectionManager.setConnections(devices)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ tsrConductor
.init()
.then(() => {
// Add devices to the TSR-conductor:
return tsrConductor.addDevice('casparcg0', {
type: DeviceType.CASPARCG,
options: {
host: 'localhost',
port: 5250,
return tsrConductor.connectionManager.setConnections({
casparcg0: {
type: DeviceType.CASPARCG,
options: {
host: 'localhost',
port: 5250,
},
},
})
})
Expand Down
12 changes: 7 additions & 5 deletions packages/timeline-state-resolver/examples/playVideoInCaspar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ tsr.on('debug', (deviceId, cmd) => console.log('debug', deviceId, cmd))
const a = async function () {
await tsr.init()

await tsr.addDevice('casparcg0', {
type: DeviceType.CASPARCG,
options: {
host: '127.0.0.1',
// port: 5250
tsr.connectionManager.setConnections({
casparcg0: {
type: DeviceType.CASPARCG,
options: {
host: '127.0.0.1',
// port: 5250
},
},
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ tsr.on('error', (e) => console.log('error', e))
const a = async function () {
await tsr.init()

await tsr.addDevice('casparcg0', {
type: DeviceType.CASPARCG,
options: {
host: '127.0.0.1',
// port: 5250
tsr.connectionManager.setConnections({
casparcg0: {
type: DeviceType.CASPARCG,
options: {
host: '127.0.0.1',
// port: 5250
},
},
})

Expand Down
13 changes: 10 additions & 3 deletions packages/timeline-state-resolver/src/__mocks__/osc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class UDPPort extends EventEmitter {
this.emit('ready')
}

send({ address }) {
send({ address }: { address: string }) {
orgSetTimeout(() => {
if (MockOSC.connectionIsGood) {
if (address === '/state/full') {
Expand All @@ -21,11 +21,18 @@ export class UDPPort extends EventEmitter {
value: `{ "channel": [{
"faderLevel": 0.75,
"pgmOn": false,
"pstOn": false
"pstOn": false,
"showChannel": true
}, {
"faderLevel": 0.75,
"pgmOn": false,
"pstOn": false
"pstOn": false,
"showChannel": true
}, {
"faderLevel": 0.75,
"pgmOn": false,
"pstOn": false,
"showChannel": true
}] }`,
},
],
Expand Down
Loading

0 comments on commit d182f1f

Please sign in to comment.