X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=webapp%2Fsrc%2Fapp%2F%40core-xds%2Fservices%2Fsdk.service.ts;h=8fa6ad2b7c03ff6eb10a622fed16ed0afece7673;hb=45f6472d1e8ecad428da314a6d762143f033865d;hp=2d54d4e8d9026950671603f809a0494bff392b6a;hpb=a85f3ef5017e7e1406476194cd5f3e848a3718f9;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 2d54d4e..8fa6ad2 100644 --- a/webapp/src/app/@core-xds/services/sdk.service.ts +++ b/webapp/src/app/@core-xds/services/sdk.service.ts @@ -16,7 +16,7 @@ * limitations under the License. */ -import { Injectable, SecurityContext } from '@angular/core'; +import { Injectable, SecurityContext, isDevMode } from '@angular/core'; import { Observable } from 'rxjs/Observable'; import { BehaviorSubject } from 'rxjs/BehaviorSubject'; @@ -25,60 +25,159 @@ import { XDSAgentService } from '../services/xdsagent.service'; import 'rxjs/add/observable/throw'; export interface ISdk { - id: string; - profile: string; - version: string; - arch: number; - path: string; + id: string; + name: string; + description: string; + profile: string; + version: string; + arch: string; + path: string; + url: string; + status: string; + date: string; + size: string; + md5sum: string; + setupFile: string; + lastError: string; +} + +export interface ISdkManagementMsg { + cmdID: string; + timestamp: string; + sdk: ISdk; + stdout: string; + stderr: string; + progress: number; + exited: boolean; + code: number; + error: string; } @Injectable() export class SdkService { - public Sdks$: Observable; - - private _sdksList = []; - private current: ISdk; - private sdksSubject = >new BehaviorSubject(this._sdksList); - - constructor(private xdsSvr: XDSAgentService) { - this.current = null; - this.Sdks$ = this.sdksSubject.asObservable(); - - this.xdsSvr.XdsConfig$.subscribe(cfg => { - if (!cfg || cfg.servers.length < 1) { - return; - } - // FIXME support multiple server - // cfg.servers.forEach(svr => { - this.xdsSvr.getSdks(cfg.servers[0].id).subscribe((s) => { - this._sdksList = s; - this.sdksSubject.next(s); - }); - }); - } + public Sdks$: Observable; + public curSdk$: Observable; - public setCurrent(s: ISdk) { - this.current = s; - } + private _sdksList = []; + private sdksSubject = >new BehaviorSubject(this._sdksList); + private current: ISdk; + private curSdkSubject = >new BehaviorSubject(this.current); + private curServerID; - public getCurrent(): ISdk { - return this.current; - } + constructor(private xdsSvr: XDSAgentService) { + this.current = null; + this.Sdks$ = this.sdksSubject.asObservable(); + this.curSdk$ = this.curSdkSubject.asObservable(); - public getCurrentId(): string { - if (this.current && this.current.id) { - return this.current.id; + this.xdsSvr.XdsConfig$.subscribe(cfg => { + if (!cfg || cfg.servers.length < 1) { + return; + } + // FIXME support multiple server + // cfg.servers.forEach(svr => { + this.curServerID = cfg.servers[0].id; + this.xdsSvr.getSdks(this.curServerID).subscribe((sdks) => { + this._sdksList = []; + sdks.forEach(s => { + this._addSdk(s, true); + }); + + // TODO: get previous val from xds-config service / cookie + if (this._sdksList.length > 0) { + this.current = this._sdksList[0]; + this.curSdkSubject.next(this.current); } - return ''; + + this.sdksSubject.next(this._sdksList); + }); + }); + + // 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') { + /* tslint:disable:no-console */ + console.log('Error: received event:sdk-remove with invalid status: evMgt=', evMgt); + return; + } + this._delSdk(evMgt.sdk); + }); + + } + + public setCurrent(s: ISdk) { + this.current = s; + } + + public getCurrent(): ISdk { + return this.current; + } + + public getCurrentId(): string { + if (this.current && this.current.id) { + return this.current.id; } + return ''; + } + + public install(sdk: ISdk): Observable { + return this.xdsSvr.installSdk(this.curServerID, sdk.id); + } + + public onInstall(): Observable { + return this.xdsSvr.onSdkInstall(); + } + + public abortInstall(sdk: ISdk): Observable { + return this.xdsSvr.abortInstall(this.curServerID, sdk.id); + } + + public remove(sdk: ISdk): Observable { + return this.xdsSvr.removeSdk(this.curServerID, sdk.id); + } + + /** Private **/ - public add(sdk: ISdk): Observable { - // TODO SEB - return Observable.throw('Not implement yet'); + 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; + }); + + if (!noNext) { + this.sdksSubject.next(this._sdksList); } - public delete(sdk: ISdk): Observable { - // TODO SEB - return Observable.throw('Not implement yet'); + return sdk; + } + + private _delSdk(sdk: ISdk) { + const idx = this._sdksList.findIndex(item => item.id === sdk.id); + if (idx === -1) { + if (isDevMode) { + /* tslint:disable:no-console */ + console.log('Warning: Try to delete sdk unknown id: sdk=', sdk); + } + return; + } + const delId = this._sdksList[idx].id; + this._sdksList.splice(idx, 1); + if (delId === this.current.id) { + this.setCurrent(this._sdksList[0]); } + this.sdksSubject.next(this._sdksList); + } + }