Fixed 'Unknown sdkid' error in dashboard
[src/xds/xds-agent.git] / webapp / src / app / @core-xds / services / sdk.service.ts
index 181ad6f..f7e8c2f 100644 (file)
@@ -1,6 +1,6 @@
 /**
 * @license
-* Copyright (C) 2017 "IoT.bzh"
+* Copyright (C) 2017-2018 "IoT.bzh"
 * Author Sebastien Douheret <sebastien@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
@@ -62,6 +62,7 @@ export interface ISdk {
 export interface ISdkManagementMsg {
   cmdID: string;
   timestamp: string;
+  action: string;
   sdk: ISdk;
   stdout: string;
   stderr: string;
@@ -97,12 +98,13 @@ export class SdkService {
       this.xdsSvr.getSdks(this.curServerID).subscribe((sdks) => {
         this._sdksList = [];
         sdks.forEach(s => {
-          this._addSdk(s, true);
+          this._addUpdateSdk(s, true);
         });
 
         // TODO: get previous val from xds-config service / cookie
         if (this._sdksList.length > 0) {
-          this.current = this._sdksList[0];
+          const installedSdks = this._sdksList.filter(sd => sd.status === StatusType.INSTALLED);
+          this.current = installedSdks.length > 0 ? installedSdks[0] : this._sdksList[0];
           this.curSdkSubject.next(this.current);
         }
 
@@ -111,18 +113,17 @@ export class SdkService {
     });
 
     // Add listener on sdk creation, deletion and change events
-    this.xdsSvr.onSdkInstall().subscribe(evMgt => {
-      this._addSdk(evMgt.sdk);
-    });
-    this.xdsSvr.onSdkRemove().subscribe(evMgt => {
-      if (evMgt.sdk.status !== StatusType.NOT_INSTALLED) {
+    this.xdsSvr.onSdkAdd().subscribe(sdk => this._addUpdateSdk(sdk));
+    this.xdsSvr.onSdkChange().subscribe(sdk => this._addUpdateSdk(sdk));
+
+    this.xdsSvr.onSdkRemove().subscribe(sdk => {
+      if (sdk.status !== StatusType.NOT_INSTALLED) {
         /* tslint:disable:no-console */
-        console.log('Error: received event:sdk-remove with invalid status: evMgt=', evMgt);
+        console.log('Error: received event:sdk-remove with invalid status: sdk=', sdk);
         return;
       }
-      this._delSdk(evMgt.sdk);
+      this._delSdk(sdk);
     });
-
   }
 
   public setCurrent(s: ISdk) {
@@ -145,7 +146,11 @@ export class SdkService {
   }
 
   public onInstall(): Observable<ISdkManagementMsg> {
-    return this.xdsSvr.onSdkInstall();
+    return this.xdsSvr.onSdkManagement().filter(ev => ev.action === 'installing');
+  }
+
+  public onRemove(): Observable<ISdkManagementMsg> {
+    return this.xdsSvr.onSdkManagement().filter(ev => ev.action === 'removing');
   }
 
   public abortInstall(sdk: ISdk): Observable<ISdk> {
@@ -158,27 +163,28 @@ export class SdkService {
 
   /** Private **/
 
-  private _addSdk(sdk: ISdk, noNext?: boolean): ISdk {
+  private _addUpdateSdk(sdk: ISdk, noNext?: boolean): ISdk {
 
     // check if sdk already exists
     const idx = this._sdksList.findIndex(s => s.id === sdk.id);
     if (idx >= 0) {
+      // Just update existing one
       this._sdksList[idx] = sdk;
     } else {
       // add new sdk
       this._sdksList.push(sdk);
-    }
 
-    // sort sdk array
-    this._sdksList.sort((a, b) => {
-      if (a.name < b.name) {
-        return -1;
-      }
-      if (a.name > b.name) {
-        return 1;
-      }
-      return 0;
-    });
+      // sort sdk array
+      this._sdksList.sort((a, b) => {
+        if (a.name < b.name) {
+          return -1;
+        }
+        if (a.name > b.name) {
+          return 1;
+        }
+        return 0;
+      });
+    }
 
     if (!noNext) {
       this.sdksSubject.next(this._sdksList);