/*-------------------------------------------------------------------------------------------------------------- * Copyright (c) insite-gmbh. All rights reserved. * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------------------------*/ import { Observable } from 'rxjs/Rx'; import { Injectable, EventEmitter } from '@angular/core'; import { Http, Headers } from '@angular/http'; import { InaxConfiguration, ProfileService, InaxSignalR, ConnectionState, IHubService, SignalrWindow} from '../../../@inax/common'; import { AlarmListChangeEvent, IHmiAlarmEntry } from './domain'; import { AlarmListSubscriber } from './domain/subscriber'; import { Subject } from "rxjs/Subject"; import { IResult } from './domain/Result'; import { InaxAlarmConfig } from './config/alarmConfig'; @Injectable() export class InaxAlarmService implements IHubService { private static _subscribers = new Array(); private static _hubProxy: any; private static _alarmControllerName:string = "AlarmManagement"; private static _callbackAvailable: boolean = false; get hubName():string{ return "AlarmHub"; } constructor(private _http: Http, private _configuration: InaxConfiguration, private _profileService: ProfileService, private _alarmConfig: InaxAlarmConfig) { } public get dataChangeIsAvailable():boolean{ return InaxAlarmService._callbackAvailable; } /*********************************************************************** * SIGNALR * ********************************************************************/ public registerEvents(signalR: InaxSignalR){ if(this._configuration.UseSignalR && this._configuration.EnabledSignalRHubs.indexOf(this.hubName) >= 0){ InaxAlarmService._hubProxy = signalR.getHubProxy(this.hubName); InaxAlarmService._hubProxy.on("alarmListChanged",() =>{ this._changeEvent.emit(new AlarmListChangeEvent()); }); InaxAlarmService._callbackAvailable = true; } } private _changeEvent: EventEmitter = new EventEmitter(); public get ChangeEvent(){return this._changeEvent;} /*********************************************************************** * WEB API * ********************************************************************/ public async numberOfAlarmsAsync(filter: string = ""):Promise { return await this.numberOfAlarms(filter).toPromise(); } public numberOfAlarms(filter: string = ""): Observable { let accessUrl = this._configuration.buildRestUrl(InaxAlarmService._alarmControllerName, "NumberOfAlarmEntries"); if(filter != null && filter.length > 0) accessUrl += "?filter=" + filter; let headers = this._profileService.addAuthorization(new Headers()); return this._http.get(accessUrl, { headers }).map(res => res.json()); } public async numberOfHistoricalAlarmsAsync(filter: string = ""):Promise { return await this.numberOfHistoricalAlarms(filter).toPromise(); } public numberOfHistoricalAlarms(filter: string = ""): Observable { let accessUrl = this._configuration.buildRestUrl(InaxAlarmService._alarmControllerName, "NumberOfHistoricalAlarmEntries"); if(filter != null && filter.length > 0) accessUrl += "?filter=" + filter; let headers = this._profileService.addAuthorization(new Headers()); return this._http.get(accessUrl, { headers }).map(res => res.json()); } public async alarmsAsync(filter: string = "", fromLcid:string = "", toLcid:string = "", skip:number = 0, take:number = 100, reverse:boolean = false, orderBy:string = ""):Promise> { return await this.alarms(filter,fromLcid,toLcid,skip,take,reverse,orderBy).toPromise(); } public alarms(filter: string = "", fromLcid:string = "", toLcid:string = "", skip:number = 0, take:number = 100, reverse:boolean = false, orderBy:string = ""): Observable> { let accessUrl = this._configuration.buildRestUrl(InaxAlarmService._alarmControllerName, "Alarms") + this.createQueryParams(filter,fromLcid,toLcid,skip,take,reverse,orderBy); let headers = this._profileService.addAuthorization(new Headers()); return this._http.get(accessUrl, { headers }).map(res => res.json()).map(x => { let res = x as IResult; return res != null ? this._alarmConfig.applyConfigSettingsOnAlarms(x.Entries) : []; }); } public async historyAsync(filter: string = "", fromLcid:string = "", toLcid:string = "", skip:number = 0, take:number = 100, reverse:boolean = false, orderBy:string = ""):Promise> { return await this.history(filter,fromLcid,toLcid,skip,take,reverse,orderBy).toPromise(); } public history(filter: string = "", fromLcid:string = "", toLcid:string = "", skip:number = 0, take:number = 100, reverse:boolean = false, orderBy:string = ""): Observable> { let accessUrl = this._configuration.buildRestUrl(InaxAlarmService._alarmControllerName, "AlarmHistory") + this.createQueryParams(filter,fromLcid,toLcid,skip,take,reverse,orderBy); let headers = this._profileService.addAuthorization(new Headers()); return this._http.get(accessUrl, { headers }).map(res => res.json()).map(x => { let res = x as IResult; return res != null ? this._alarmConfig.applyConfigSettingsOnAlarms(x.Entries) : []; }); } private createQueryParams(filter: string = "", fromLcid:string = "", toLcid:string = "", skip:number = 0, take:number = 100, reverse:boolean = false, orderBy:string = ""):string{ let queryParams = "?"; if(filter != null && filter.length > 0) queryParams += "filter=" + filter + " AND " + this._alarmConfig.createActiveAlarmFilterByConfigSettings(); else queryParams += this._alarmConfig.createActiveAlarmFilterByConfigSettings(); queryParams += "fromLcid=" + fromLcid + "&toLcid=" + toLcid + "&skip=" + skip + "&take=" + take + "&reverse=" + reverse; if(orderBy != null && orderBy.length > 0) queryParams += "&orderBy=" + orderBy; return queryParams; } private normalizeUri(uri: string):string{ return encodeURIComponent(uri); } }