From 2c139f61937dd53e41d5884a9265c23a5fe11fbb Mon Sep 17 00:00:00 2001 From: Johan Nyman Date: Wed, 21 Apr 2021 09:52:59 +0200 Subject: [PATCH] fix: resend device statuses to core, on core reconnect --- src/coreHandler.ts | 2 +- src/mosHandler.ts | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/coreHandler.ts b/src/coreHandler.ts index 4ebf644..c6e1c55 100644 --- a/src/coreHandler.ts +++ b/src/coreHandler.ts @@ -152,7 +152,7 @@ export class CoreMosDeviceHandler { } onMosConnectionChanged (connectionStatus: IMOSConnectionStatus) { - let statusCode = P.StatusCode.UNKNOWN + let statusCode: P.StatusCode let messages: Array = [] if (connectionStatus.PrimaryConnected) { diff --git a/src/mosHandler.ts b/src/mosHandler.ts index 72bbf5a..4986d15 100644 --- a/src/mosHandler.ts +++ b/src/mosHandler.ts @@ -23,7 +23,7 @@ import { } from 'mos-connection' import * as _ from 'underscore' import * as Winston from 'winston' -import { CoreHandler } from './coreHandler' +import { CoreHandler, CoreMosDeviceHandler } from './coreHandler' import { CollectionObj } from '@sofie-automation/server-core-integration' // export interface MosOptions { @@ -68,7 +68,7 @@ export class MosHandler { public mosOptions: MosConfig public debugLogging: boolean = false - private allMosDevices: {[id: string]: IMOSDevice} = {} + private allMosDevices: {[id: string]: {mosDevice: IMOSDevice, coreMosHandler?: CoreMosDeviceHandler}} = {} private _ownMosDevices: {[deviceId: string]: MosDevice} = {} private _logger: Winston.LoggerInstance private _disposed: boolean = false @@ -109,7 +109,9 @@ export class MosHandler { }) .then(() => { this._coreHandler.onConnected(() => { + // This is called whenever a connection to Core has been (re-)established this.setupObservers() + this.sendStatusOfAllMosDevices() }) this.setupObservers() @@ -231,13 +233,15 @@ export class MosHandler { // a new connection to a device has been made this._logger.info('new mosConnection established: ' + mosDevice.idPrimary + ', ' + mosDevice.idSecondary) - this.allMosDevices[mosDevice.idPrimary] = mosDevice + this.allMosDevices[mosDevice.idPrimary] = { mosDevice: mosDevice } return this._coreHandler.registerMosDevice(mosDevice, this) .then((coreMosHandler) => { // this._logger.info('mosDevice registered -------------') // Setup message flow between the devices: + this.allMosDevices[mosDevice.idPrimary].coreMosHandler = coreMosHandler + // Initial Status check: let connectionStatus = mosDevice.getConnectionStatus() coreMosHandler.onMosConnectionChanged(connectionStatus) // initial check @@ -342,6 +346,14 @@ export class MosHandler { return }) } + private sendStatusOfAllMosDevices() { + // Send an update to Core of the status of all mos devices + for (const handler of Object.values(this.allMosDevices)) { + if (handler.coreMosHandler) { + handler.coreMosHandler.onMosConnectionChanged(handler.mosDevice.getConnectionStatus()) + } + } + } private getThisPeripheralDevice (): CollectionObj | undefined { let peripheralDevices = this._coreHandler.core.getCollection('peripheralDevices') return peripheralDevices.findOne(this._coreHandler.core.deviceId)