Migration to AGL gerrit (update go import)
[src/xds/xds-agent.git] / webapp / src / app / @core-xds / services / xdsagent.service.ts
index 06ca557..27e14cf 100644 (file)
@@ -1,3 +1,21 @@
+/**
+* @license
+* Copyright (C) 2017-2018 "IoT.bzh"
+* Author Sebastien Douheret <sebastien@iot.bzh>
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*   http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
 import { Injectable, Inject, isDevMode } from '@angular/core';
 import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
 import { DOCUMENT } from '@angular/common';
@@ -7,7 +25,7 @@ import { BehaviorSubject } from 'rxjs/BehaviorSubject';
 import * as io from 'socket.io-client';
 
 import { AlertService } from './alert.service';
-import { ISdk } from './sdk.service';
+import { ISdk, ISdkManagementMsg } from './sdk.service';
 import { ProjectType, ProjectTypeEnum } from './project.service';
 
 // Import RxJs required methods
@@ -16,7 +34,7 @@ import 'rxjs/add/operator/catch';
 import 'rxjs/add/observable/throw';
 import 'rxjs/add/operator/mergeMap';
 import 'rxjs/add/observable/of';
-import 'rxjs/add/operator/retryWhen';
+import { ErrorObservable } from 'rxjs/observable/ErrorObservable';
 
 
 export interface IXDSConfigProject {
@@ -115,6 +133,9 @@ export class XDSAgentService {
   protected projectDel$ = new Subject<IXDSProjectConfig>();
   protected projectChange$ = new Subject<IXDSProjectConfig>();
 
+  protected sdkInstall$ = new Subject<ISdkManagementMsg>();
+  protected sdkRemove$ = new Subject<ISdkManagementMsg>();
+
   private baseUrl: string;
   private wsUrl: string;
   private httpSessionID: string;
@@ -231,7 +252,7 @@ export class XDSAgentService {
     this.socket.on('event:project-add', (ev) => {
       if (ev && ev.data && ev.data.id) {
         this.projectAdd$.next(Object.assign({}, ev.data));
-        if (ev.sessionID !== this.httpSessionID && ev.data.label) {
+        if (ev.sessionID !== '' && ev.sessionID !== this.httpSessionID && ev.data.label) {
           this.alert.info('Project "' + ev.data.label + '" has been added by another tool.');
         }
       } else if (isDevMode) {
@@ -243,7 +264,7 @@ export class XDSAgentService {
     this.socket.on('event:project-delete', (ev) => {
       if (ev && ev.data && ev.data.id) {
         this.projectDel$.next(Object.assign({}, ev.data));
-        if (ev.sessionID !== this.httpSessionID && ev.data.label) {
+        if (ev.sessionID !== '' && ev.sessionID !== this.httpSessionID && ev.data.label) {
           this.alert.info('Project "' + ev.data.label + '" has been deleted by another tool.');
         }
       } else if (isDevMode) {
@@ -255,10 +276,36 @@ export class XDSAgentService {
       if (ev && ev.data) {
         this.projectChange$.next(Object.assign({}, ev.data));
       } else if (isDevMode) {
-        console.log('Warning: received event:project-state-change with unknown data: ev=', ev);
+        console.log('Warning: received event:project-state-change with unkn220own data: ev=', ev);
+      }
+    });
+
+    this.socket.on('event:sdk-install', (ev) => {
+      if (ev && ev.data && ev.data.sdk) {
+        const evt = <ISdkManagementMsg>ev.data;
+        this.sdkInstall$.next(Object.assign({}, evt));
+
+        if (ev.sessionID !== '' && ev.sessionID !== this.httpSessionID && evt.sdk.name) {
+          this.alert.info('SDK "' + evt.sdk.name + '" has been installed by another tool.');
+        }
+      } else if (isDevMode) {
+        /* tslint:disable:no-console */
+        console.log('Warning: received event:sdk-install with unknown data: ev=', ev);
       }
     });
 
+    this.socket.on('event:sdk-remove', (ev) => {
+      if (ev && ev.data && ev.data.sdk) {
+        const evt = <ISdkManagementMsg>ev.data;
+        this.sdkRemove$.next(Object.assign({}, evt));
+
+        if (ev.sessionID !== '' && ev.sessionID !== this.httpSessionID && evt.sdk.name) {
+          this.alert.info('SDK "' + evt.sdk.name + '" has been removed by another tool.');
+        }
+      } else if (isDevMode) {
+        console.log('Warning: received event:sdk-remove with unknown data: ev=', ev);
+      }
+    });
   }
 
   /**
@@ -276,6 +323,14 @@ export class XDSAgentService {
     return this.projectChange$.asObservable();
   }
 
+  onSdkInstall(): Observable<ISdkManagementMsg> {
+    return this.sdkInstall$.asObservable();
+  }
+
+  onSdkRemove(): Observable<ISdkManagementMsg> {
+    return this.sdkRemove$.asObservable();
+  }
+
   /**
   ** Misc / Version
   ***/
@@ -337,10 +392,22 @@ export class XDSAgentService {
     if (!svr || !svr.connected) {
       return Observable.of([]);
     }
-
     return this._get(svr.partialUrl + '/sdks');
   }
 
+  installSdk(serverID: string, id: string, filename?: string, force?: boolean): Observable<ISdk> {
+    return this._post(this._getServerUrl(serverID) + '/sdks', { id: id, filename: filename, force: force });
+  }
+
+  abortInstall(serverID: string, id: string): Observable<ISdk> {
+    return this._post(this._getServerUrl(serverID) + '/sdks/abortinstall', { id: id });
+  }
+
+  removeSdk(serverID: string, id: string): Observable<ISdk> {
+    return this._delete(this._getServerUrl(serverID) + '/sdks/' + id);
+  }
+
+
   /***
   ** Projects
   ***/
@@ -402,6 +469,17 @@ export class XDSAgentService {
     return svr[0];
   }
 
+  private _getServerUrl(serverID: string): string | ErrorObservable {
+    const svr = this._getServer(serverID);
+    if (!svr || !svr.connected) {
+      if (isDevMode) {
+        console.log('ERROR: XDS Server unknown: serverID=' + serverID);
+      }
+      return Observable.throw('Cannot identify XDS Server');
+    }
+    return svr.partialUrl;
+  }
+
   private _attachAuthHeaders(options?: any) {
     options = options || {};
     const headers = options.headers || new HttpHeaders();