/*-------------------------------------------------------------------------------------------------------------- * 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 } from '@angular/core'; import { Http, Headers, Response } from '@angular/http'; import { InaxConfiguration, ProfileService, InaxSignalR, ConnectionState, IHubService, SignalrWindow} from '../../../@inax/common'; import { PropertyChangeEvent, MethodPropertyChangeEvent } from './domain'; import { IScriptEventProxy } from './domain/scriptEventProxy'; import { ScriptEventProxyFactory } from './internal/scriptEventProxyFactory'; import { Subject } from "rxjs/Subject"; @Injectable() export class InaxScriptService implements IHubService { private static _proxyFactory: ScriptEventProxyFactory; get hubName():string{ return "ScriptHub"; } constructor(private _http: Http, private _configuration: InaxConfiguration, private _profileService: ProfileService) { } /*********************************************************************** * SIGNALR * ********************************************************************/ public registerEvents(signalR: InaxSignalR) { if (this._configuration.UseSignalR && this._configuration.EnabledSignalRHubs.indexOf(this.hubName) >= 0) { InaxScriptService._proxyFactory = new ScriptEventProxyFactory(signalR.getHubProxy(this.hubName)); } } public create(scriptUnit: string, functionName: string): IScriptEventProxy { if (InaxScriptService._proxyFactory == null) throw "proxy not initialized"; return InaxScriptService._proxyFactory.create(scriptUnit); } public async executeInClientContextAsync(scriptUnit: string, functionName: string, parameters: Array): Promise { return await this.executeInClientContext(scriptUnit, functionName, parameters).toPromise(); } //Start a function on a ScriptUnit in the client context. public executeInClientContext(scriptUnit: string, functionName: string, parameters: Array): Observable { if (InaxScriptService._proxyFactory == null) throw "proxy not initialized"; return InaxScriptService._proxyFactory.invoke( scriptUnit, functionName, parameters, false ); } //invoke a script function on the server, and get events public invokeInClientContext(scriptUnit: string, functionName: string, parameters: Array): Observable { if (InaxScriptService._proxyFactory == null) throw "proxy not initialized"; // Now we just create our internal object so we can track this subject // in case someone else wants it too return InaxScriptService._proxyFactory.invoke(scriptUnit, functionName, parameters, true); } public setScriptPropertyInClientContext(scriptUnit: string, propertyName: string, propertyValue: any) { if (InaxScriptService._proxyFactory == null) throw "proxy not initialized"; // Now we just create our internal object so we can track this subject // in case someone else wants it too InaxScriptService._proxyFactory.setProperty(scriptUnit, propertyName, propertyValue); } /*********************************************************************** * WEB API * ********************************************************************/ //Get a property from the script from the server context public getScriptPropertyValueInServerContext(scriptUnit: string, propertyName: string): Observable { let accessUrl = this._configuration.buildRestUrl("Script", "GetScriptPropertyValue") + "?scriptUnit=" + scriptUnit + "&propertyName=" + propertyName; let headers = this._profileService.addAuthorization(new Headers()); return this._http.get(accessUrl, { headers }).map((res:Response) => this.mapToAny(res)); } //Set a property in the script in the server context public setScriptPropertyInServerContext(scriptUnit: string, propertyName: string, propertyValue: any): Observable { let accessUrl = this._configuration.buildRestUrl("Script", "SetScriptPropertyValue") + "?scriptUnit=" + scriptUnit + "&propertyName=" + propertyName; let headers = this._profileService.addAuthorization(new Headers()); return this._http.post(accessUrl, JSON.stringify(propertyValue), { headers }).map((res:Response) => res.ok); } //Start a function on a ScriptUnit in the server context (so no client property change) and did not wait for the result. public executeInServerContext(scriptUnit: string, functionName: string, parameters: Array): Observable { let accessUrl = this._configuration.buildRestUrl("Script", "RunScript") + "?scriptUnit=" + scriptUnit + "&function=" + functionName; let headers = this._profileService.addAuthorization(new Headers()); return this._http.post(accessUrl, JSON.stringify(parameters), { headers }).map((res:Response) => res.ok); } //invoke e script function on the server and return the result public invokeInServerContext(scriptUnit: string, functionName: string, parameters: Array): Observable { let accessUrl = this._configuration.buildRestUrl("Script", "InvokeScriptMethod") + "?scriptUnit=" + scriptUnit + "&function=" + functionName; let headers = this._profileService.addAuthorization(new Headers()); if (parameters != null && parameters.length > 0) { accessUrl += "¶meters="; for (var index = 0; index < arguments.length; index++) { accessUrl += arguments[index]; if (index + 1 < arguments.length) accessUrl += ","; } } return this._http.get(accessUrl, { headers }).map((res:Response) => this.mapToAny(res)); } private normalizeUri(uri: string): string { return encodeURIComponent(uri); } private mapToAny(res:Response):any{ try{ return res.json(); } catch(Exception){ return res.text(); } } }