@@ -16,7 +16,6 @@ import {
1616 IMOSROReadyToAir ,
1717 IMOSROFullStory ,
1818 IConnectionConfig ,
19- IMOSDeviceConnectionOptions ,
2019 MosDevice ,
2120 IMOSListMachInfo ,
2221 IMOSString128 ,
@@ -40,12 +39,13 @@ import { MosGatewayConfig } from './generated/options'
4039import { MosDeviceConfig } from './generated/devices'
4140import { PeripheralDeviceForDevice } from '@sofie-automation/server-core-integration'
4241import _ = require( 'underscore' )
42+ import { MosStatusHandler } from './mosStatusHandler'
4343
44- export interface MosConfig {
44+ interface MosConfig {
4545 self : IConnectionConfig
4646 // devices: Array<IMOSDeviceConnectionOptions>
4747}
48- export type MosSubDeviceSettings = Record <
48+ type MosSubDeviceSettings = Record <
4949 string ,
5050 {
5151 type : ''
@@ -59,10 +59,13 @@ export type MosSubDeviceSettings = Record<
5959interface MosDeviceHandle {
6060 readonly deviceId : string
6161 readonly mosDevice : MosDevice
62- readonly deviceOptions : Readonly < IMOSDeviceConnectionOptions >
62+ readonly deviceOptions : Readonly < MosDeviceConfig >
6363
6464 // Once connected, a core handler is setup
6565 coreMosHandler ?: CoreMosDeviceHandler
66+
67+ // If writing back story/item status is enabled, the setup handler
68+ statusHandler ?: MosStatusHandler
6669}
6770
6871export class MosHandler {
@@ -147,11 +150,9 @@ export class MosHandler {
147150 return Promise . resolve ( )
148151 }
149152 }
150- setupObservers ( ) : void {
153+ private setupObservers ( ) : void {
151154 if ( this . _observers . length ) {
152- this . _observers . forEach ( ( obs ) => {
153- obs . stop ( )
154- } )
155+ this . _observers . forEach ( ( obs ) => obs . stop ( ) )
155156 this . _observers = [ ]
156157 }
157158 this . _logger . info ( 'Renewing observers' )
@@ -167,15 +168,9 @@ export class MosHandler {
167168 const deviceObserver = this . _coreHandler . core . observe (
168169 PeripheralDevicePubSubCollectionsNames . peripheralDeviceForDevice
169170 )
170- deviceObserver . added = ( ) => {
171- this . _deviceOptionsChanged ( )
172- }
173- deviceObserver . changed = ( ) => {
174- this . _deviceOptionsChanged ( )
175- }
176- deviceObserver . removed = ( ) => {
177- this . _deviceOptionsChanged ( )
178- }
171+ deviceObserver . added = ( ) => this . _deviceOptionsChanged ( )
172+ deviceObserver . changed = ( ) => this . _deviceOptionsChanged ( )
173+ deviceObserver . removed = ( ) => this . _deviceOptionsChanged ( )
179174 this . _observers . push ( deviceObserver )
180175
181176 this . _deviceOptionsChanged ( )
@@ -288,7 +283,6 @@ export class MosHandler {
288283
289284 if ( ! this . _coreHandler ) throw Error ( '_coreHandler is undefined!' )
290285
291- //@ts -expect-error this is not yet added to the official mos-connection
292286 const openMediaHotStandby = deviceEntry . deviceOptions . secondary ?. openMediaHotStandby || false
293287
294288 const coreMosHandler = await this . _coreHandler . registerMosDevice (
@@ -301,9 +295,18 @@ export class MosHandler {
301295
302296 deviceEntry . coreMosHandler = coreMosHandler
303297
298+ if ( deviceEntry . deviceOptions . statuses ?. enabled ) {
299+ if ( deviceEntry . statusHandler ) {
300+ deviceEntry . statusHandler . dispose ( )
301+ }
302+ deviceEntry . statusHandler = new MosStatusHandler (
303+ mosDevice ,
304+ coreMosHandler ,
305+ deviceEntry . deviceOptions . statuses
306+ )
307+ }
308+
304309 // Initial Status check:
305- const connectionStatus = mosDevice . getConnectionStatus ( )
306- coreMosHandler . onMosConnectionChanged ( connectionStatus ) // initial check
307310 // Profile 0: -------------------------------------------------
308311 mosDevice . onConnectionChange ( ( newStatus : IMOSConnectionStatus ) => {
309312 // MOSDevice >>>> Core
@@ -518,7 +521,7 @@ export class MosHandler {
518521
519522 if ( ! this . mos ) throw Error ( 'mos is undefined, call _initMosConnection first!' )
520523
521- const deviceOptions : IMOSDeviceConnectionOptions = JSON . parse ( JSON . stringify ( deviceOptions0 ) ) // deep clone
524+ const deviceOptions : MosDeviceConfig = JSON . parse ( JSON . stringify ( deviceOptions0 ) ) // deep clone
522525 deviceOptions . primary . timeout ||= DEFAULT_MOS_TIMEOUT_TIME
523526 deviceOptions . primary . heartbeatInterval ||= DEFAULT_MOS_HEARTBEAT_INTERVAL
524527
@@ -577,13 +580,21 @@ export class MosHandler {
577580 }
578581 }
579582 private async _removeDevice ( deviceId : string ) : Promise < void > {
580- const mosDevice = this . _allMosDevices . get ( deviceId ) ?. mosDevice
583+ const deviceEntry = this . _allMosDevices . get ( deviceId )
581584 this . _allMosDevices . delete ( deviceId )
582585
583- if ( mosDevice ) {
586+ if ( deviceEntry ) {
587+ const mosDevice = deviceEntry . mosDevice
588+
584589 // Cleanup the coreMosHandler from the device
585590 if ( this . _coreHandler ) await this . _coreHandler . unRegisterMosDevice ( mosDevice )
586591
592+ // Stop the status handler, if enabled
593+ if ( deviceEntry . statusHandler ) {
594+ deviceEntry . statusHandler . dispose ( )
595+ delete deviceEntry . statusHandler
596+ }
597+
587598 if ( ! this . mos ) {
588599 throw Error ( 'mos is undefined!' )
589600 }
0 commit comments