@@ -33,10 +33,12 @@ export class CoreHandler {
3333 core : CoreConnection | undefined
3434 logger : Winston . Logger
3535 public _observers : Array < Observer < any > > = [ ]
36+ public connectedToCore = false
3637 private _deviceOptions : DeviceConfig
3738 private _coreMosHandlers : Array < CoreMosDeviceHandler > = [ ]
3839 private _onConnected ?: ( ) => any
3940 private _isInitialized = false
41+ private _isDestroyed = false
4042 private _executedFunctions = new Set < PeripheralDeviceCommandId > ( )
4143 private _coreConfig ?: CoreConfig
4244 private _certificates ?: Buffer [ ]
@@ -54,10 +56,12 @@ export class CoreHandler {
5456
5557 this . core . onConnected ( ( ) => {
5658 this . logger . info ( 'Core Connected!' )
59+ this . connectedToCore = true
5760 if ( this . _isInitialized ) this . onConnectionRestored ( )
5861 } )
5962 this . core . onDisconnected ( ( ) => {
6063 this . logger . info ( 'Core Disconnected!' )
64+ this . connectedToCore = false
6165 } )
6266 this . core . onError ( ( err ) => {
6367 this . logger . error ( 'Core Error: ' + ( typeof err === 'string' ? err : err . message || err . toString ( ) ) )
@@ -74,31 +78,45 @@ export class CoreHandler {
7478 }
7579 await this . core . init ( ddpConfig )
7680
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-
8981 await this . setupSubscriptionsAndObservers ( )
9082
9183 this . _isInitialized = true
84+
85+ await this . updateCoreStatus ( )
86+ }
87+ getCoreStatus ( ) : {
88+ statusCode : StatusCode
89+ messages : string [ ]
90+ } {
91+ let statusCode = StatusCode . GOOD
92+ const messages : string [ ] = [ ]
93+
94+ if ( ! this . _isInitialized ) {
95+ statusCode = StatusCode . BAD
96+ messages . push ( 'Starting up...' )
97+ }
98+ if ( this . _isDestroyed ) {
99+ statusCode = StatusCode . FATAL
100+ messages . push ( 'Shut down' )
101+ }
102+ return {
103+ statusCode,
104+ messages,
105+ }
92106 }
107+ async updateCoreStatus ( ) : Promise < void > {
108+ if ( ! this . core ) throw Error ( 'core is undefined!' )
109+
110+ await this . core . setStatus ( this . getCoreStatus ( ) )
111+ }
112+
93113 async dispose ( ) : Promise < void > {
114+ this . _isDestroyed = true
94115 if ( ! this . core ) {
95116 throw Error ( 'core is undefined!' )
96117 }
97118
98- await this . core . setStatus ( {
99- statusCode : StatusCode . FATAL ,
100- messages : [ 'Shutting down' ] ,
101- } )
119+ await this . updateCoreStatus ( )
102120
103121 await Promise . all (
104122 this . _coreMosHandlers . map ( async ( cmh : CoreMosDeviceHandler ) => {
0 commit comments