import { IMineMeldAPIService } from './minemeldapi' ; import { IMinemeldEventsService } from './events' ; import { IMinemeldSupervisorService } from './supervisor' ; export interface IMineMeldEngineStatusService { getStatus ( ) : angular . IPromise < IMineMeldEngineStatus > ; } export interface IMineMeldEngineStatus { statename : string ; } export class MineMeldEngineStatusService implements IMineMeldEngineStatusService { MineMeldAPIService : IMineMeldAPIService ; MinemeldEventsService : IMinemeldEventsService ; toastr : any ; $interval : angular . IIntervalService ; $rootScope : angular . IRootScopeService ; $q : angular . IQService ; MinemeldSupervisorService : IMinemeldSupervisorService ; currentStatusDeferred : ng . IDeferred < IMineMeldEngineStatus > ; statusSubscription : ( ) => void ; currentStatus : IMineMeldEngineStatus ; statusUpdatePromise : ng . IPromise < any > ; ngInject constructor ( MinemeldSupervisorService , MineMeldAPIService , MinemeldEventsService , toastr , $interval , $rootScope , $q ) { this . MineMeldAPIService = MineMeldAPIService ; this . MinemeldEventsService = MinemeldEventsService ; this . MinemeldSupervisorService = MinemeldSupervisorService ; this . toastr = toastr ; this . $interval = $interval ; this . $rootScope = $rootScope ; this . $q = $q ; this . currentStatusDeferred = this . $q . defer < IMineMeldEngineStatus > ( ) ; this . MineMeldAPIService . onLogin ( this . initStatusMonitor . bind ( this ) ) ; this . MineMeldAPIService . onLogout ( this . destroyStatusMonitor . bind ( this ) ) ; if ( this . MineMeldAPIService . isLoggedIn ( ) ) { this . initStatusMonitor ( ) ; } } public getStatus ( ) { if ( ! this . currentStatus ) { return this . currentStatusDeferred . promise ; } return this . $q . when ( this . currentStatus ) ; } private initStatusMonitor ( ) { if ( this . statusSubscription ) { return ; } this . updateEngineStatus ( ) ; } private destroyStatusMonitor ( ) { if ( this . statusSubscription ) { this . statusSubscription ( ) ; this . statusSubscription = undefined ; } if ( this . statusUpdatePromise ) { this . $interval . cancel ( this . statusUpdatePromise ) ; } this . currentStatus = undefined ; } private changeListener ( event , status ) { var broadcast ; broadcast = this . currentStatus . statename != status ; this . currentStatus . statename = status ; if ( broadcast ) { this . $rootScope . $broadcast ( 'mm-engine-status-changed' ) ; } } private updateEngineStatus ( ) { this . MinemeldSupervisorService . getStatus ( false ) . then ( ( result ) => { var oldStatename ; if ( this . currentStatus ) { oldStatename = this . currentStatus . statename ; } this . currentStatus = result . processes [ 'minemeld-engine' ] ; if ( ! this . statusSubscription ) { this . statusSubscription = this . $rootScope . $on ( 'mm-inner-engine-status-changed' , this . changeListener . bind ( this ) ) ; this . currentStatusDeferred . resolve ( this . currentStatus ) ; } if ( oldStatename != this . currentStatus . statename ) { this . $rootScope . $broadcast ( 'mm-engine-status-changed' ) ; } } , ( error ) => { console . log ( 'ERROR RETRIEVING MINEMELD ENGINE STATUS: ' + error . statusText ) ; throw error ; } ) . finally ( ( ) => { this . statusUpdatePromise = this . $interval ( this . updateEngineStatus . bind ( this ) , 1 * 60 * 1000 , 1 ) ; } ) ; } }