@@ -10,6 +10,7 @@ import {
1010 stringifyError ,
1111 PeripheralDevicePubSub ,
1212 PeripheralDevicePubSubCollectionsNames ,
13+ ICoreHandler ,
1314} from '@sofie-automation/server-core-integration'
1415import * as Winston from 'winston'
1516
@@ -29,14 +30,16 @@ export interface CoreConfig {
2930/**
3031 * Represents a connection between mos-integration and Core
3132 */
32- export class CoreHandler {
33+ export class CoreHandler implements ICoreHandler {
3334 core : CoreConnection | undefined
3435 logger : Winston . Logger
3536 public _observers : Array < Observer < any > > = [ ]
37+ public connectedToCore = false
3638 private _deviceOptions : DeviceConfig
3739 private _coreMosHandlers : Array < CoreMosDeviceHandler > = [ ]
3840 private _onConnected ?: ( ) => any
3941 private _isInitialized = false
42+ private _isDestroyed = false
4043 private _executedFunctions = new Set < PeripheralDeviceCommandId > ( )
4144 private _coreConfig ?: CoreConfig
4245 private _certificates ?: Buffer [ ]
@@ -54,10 +57,12 @@ export class CoreHandler {
5457
5558 this . core . onConnected ( ( ) => {
5659 this . logger . info ( 'Core Connected!' )
60+ this . connectedToCore = true
5761 if ( this . _isInitialized ) this . onConnectionRestored ( )
5862 } )
5963 this . core . onDisconnected ( ( ) => {
6064 this . logger . info ( 'Core Disconnected!' )
65+ this . connectedToCore = false
6166 } )
6267 this . core . onError ( ( err ) => {
6368 this . logger . error ( 'Core Error: ' + ( typeof err === 'string' ? err : err . message || err . toString ( ) ) )
@@ -74,31 +79,45 @@ export class CoreHandler {
7479 }
7580 await this . core . init ( ddpConfig )
7681
77- if ( ! this . core ) {
78- throw Error ( 'core is undefined!' )
79- }
80-
81- this . core
82- . setStatus ( {
83- statusCode : StatusCode . GOOD ,
84- // messages: []
85- } )
86- . catch ( ( e ) => this . logger . warn ( 'Error when setting status:' + e ) )
87- // nothing
88-
8982 await this . setupSubscriptionsAndObservers ( )
9083
9184 this . _isInitialized = true
85+
86+ await this . updateCoreStatus ( )
87+ }
88+ getCoreStatus ( ) : {
89+ statusCode : StatusCode
90+ messages : string [ ]
91+ } {
92+ let statusCode = StatusCode . GOOD
93+ const messages : string [ ] = [ ]
94+
95+ if ( ! this . _isInitialized ) {
96+ statusCode = StatusCode . BAD
97+ messages . push ( 'Starting up...' )
98+ }
99+ if ( this . _isDestroyed ) {
100+ statusCode = StatusCode . FATAL
101+ messages . push ( 'Shut down' )
102+ }
103+ return {
104+ statusCode,
105+ messages,
106+ }
92107 }
108+ async updateCoreStatus ( ) : Promise < void > {
109+ if ( ! this . core ) throw Error ( 'core is undefined!' )
110+
111+ await this . core . setStatus ( this . getCoreStatus ( ) )
112+ }
113+
93114 async dispose ( ) : Promise < void > {
115+ this . _isDestroyed = true
94116 if ( ! this . core ) {
95117 throw Error ( 'core is undefined!' )
96118 }
97119
98- await this . core . setStatus ( {
99- statusCode : StatusCode . FATAL ,
100- messages : [ 'Shutting down' ] ,
101- } )
120+ await this . updateCoreStatus ( )
102121
103122 await Promise . all (
104123 this . _coreMosHandlers . map ( async ( cmh : CoreMosDeviceHandler ) => {
0 commit comments