X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=webapp%2Fsrc%2Fapp%2F%40core-xds%2Fservices%2Fsdk.service.ts;h=f7e8c2f5e2e8fe300840adf9f96dde43cd35892a;hb=319bdec474f655f415609c76a8e3b50b1d0b9aa4;hp=8fa6ad2b7c03ff6eb10a622fed16ed0afece7673;hpb=45f6472d1e8ecad428da314a6d762143f033865d;p=src%2Fxds%2Fxds-agent.git diff --git a/webapp/src/app/@core-xds/services/sdk.service.ts b/webapp/src/app/@core-xds/services/sdk.service.ts index 8fa6ad2..f7e8c2f 100644 --- a/webapp/src/app/@core-xds/services/sdk.service.ts +++ b/webapp/src/app/@core-xds/services/sdk.service.ts @@ -1,6 +1,6 @@ /** * @license -* Copyright (C) 2017 "IoT.bzh" +* Copyright (C) 2017-2018 "IoT.bzh" * Author Sebastien Douheret * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,6 +24,24 @@ import { XDSAgentService } from '../services/xdsagent.service'; import 'rxjs/add/observable/throw'; +/* FIXME: syntax only compatible with TS>2.4.0 +export enum StatusType { + DISABLE = 'Disable', + NOT_INSTALLED = 'Not Installed', + INSTALLING = 'Installing', + UNINSTALLING = 'Un-installing', + INSTALLED = 'Installed' +} +*/ +export type StatusTypeEnum = 'Disable' | 'Not Installed' | 'Installing' | 'Un-installing' | 'Installed'; +export const StatusType = { + DISABLE: 'Disable', + NOT_INSTALLED: 'Not Installed', + INSTALLING: 'Installing', + UNINSTALLING: 'Un-installing', + INSTALLED: 'Installed', +}; + export interface ISdk { id: string; name: string; @@ -44,6 +62,7 @@ export interface ISdk { export interface ISdkManagementMsg { cmdID: string; timestamp: string; + action: string; sdk: ISdk; stdout: string; stderr: string; @@ -79,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); } @@ -93,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 !== '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) { @@ -127,7 +146,11 @@ export class SdkService { } public onInstall(): Observable { - return this.xdsSvr.onSdkInstall(); + return this.xdsSvr.onSdkManagement().filter(ev => ev.action === 'installing'); + } + + public onRemove(): Observable { + return this.xdsSvr.onSdkManagement().filter(ev => ev.action === 'removing'); } public abortInstall(sdk: ISdk): Observable { @@ -140,21 +163,28 @@ export class SdkService { /** Private **/ - private _addSdk(sdk: ISdk, noNext?: boolean): ISdk { - - // 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; - }); + 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; + }); + } if (!noNext) { this.sdksSubject.next(this._sdksList);