8fa6ad2b7c03ff6eb10a622fed16ed0afece7673
[src/xds/xds-agent.git] / webapp / src / app / @core-xds / services / sdk.service.ts
1 /**
2 * @license
3 * Copyright (C) 2017 "IoT.bzh"
4 * Author Sebastien Douheret <sebastien@iot.bzh>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 import { Injectable, SecurityContext, isDevMode } from '@angular/core';
20 import { Observable } from 'rxjs/Observable';
21 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
22
23 import { XDSAgentService } from '../services/xdsagent.service';
24
25 import 'rxjs/add/observable/throw';
26
27 export interface ISdk {
28   id: string;
29   name: string;
30   description: string;
31   profile: string;
32   version: string;
33   arch: string;
34   path: string;
35   url: string;
36   status: string;
37   date: string;
38   size: string;
39   md5sum: string;
40   setupFile: string;
41   lastError: string;
42 }
43
44 export interface ISdkManagementMsg {
45   cmdID: string;
46   timestamp: string;
47   sdk: ISdk;
48   stdout: string;
49   stderr: string;
50   progress: number;
51   exited: boolean;
52   code: number;
53   error: string;
54 }
55
56 @Injectable()
57 export class SdkService {
58   public Sdks$: Observable<ISdk[]>;
59   public curSdk$: Observable<ISdk>;
60
61   private _sdksList = [];
62   private sdksSubject = <BehaviorSubject<ISdk[]>>new BehaviorSubject(this._sdksList);
63   private current: ISdk;
64   private curSdkSubject = <BehaviorSubject<ISdk>>new BehaviorSubject(this.current);
65   private curServerID;
66
67   constructor(private xdsSvr: XDSAgentService) {
68     this.current = null;
69     this.Sdks$ = this.sdksSubject.asObservable();
70     this.curSdk$ = this.curSdkSubject.asObservable();
71
72     this.xdsSvr.XdsConfig$.subscribe(cfg => {
73       if (!cfg || cfg.servers.length < 1) {
74         return;
75       }
76       // FIXME support multiple server
77       // cfg.servers.forEach(svr => {
78       this.curServerID = cfg.servers[0].id;
79       this.xdsSvr.getSdks(this.curServerID).subscribe((sdks) => {
80         this._sdksList = [];
81         sdks.forEach(s => {
82           this._addSdk(s, true);
83         });
84
85         // TODO: get previous val from xds-config service / cookie
86         if (this._sdksList.length > 0) {
87           this.current = this._sdksList[0];
88           this.curSdkSubject.next(this.current);
89         }
90
91         this.sdksSubject.next(this._sdksList);
92       });
93     });
94
95     // Add listener on sdk creation, deletion and change events
96     this.xdsSvr.onSdkInstall().subscribe(evMgt => {
97       this._addSdk(evMgt.sdk);
98     });
99     this.xdsSvr.onSdkRemove().subscribe(evMgt => {
100       if (evMgt.sdk.status !== 'Not Installed') {
101         /* tslint:disable:no-console */
102         console.log('Error: received event:sdk-remove with invalid status: evMgt=', evMgt);
103         return;
104       }
105       this._delSdk(evMgt.sdk);
106     });
107
108   }
109
110   public setCurrent(s: ISdk) {
111     this.current = s;
112   }
113
114   public getCurrent(): ISdk {
115     return this.current;
116   }
117
118   public getCurrentId(): string {
119     if (this.current && this.current.id) {
120       return this.current.id;
121     }
122     return '';
123   }
124
125   public install(sdk: ISdk): Observable<ISdk> {
126     return this.xdsSvr.installSdk(this.curServerID, sdk.id);
127   }
128
129   public onInstall(): Observable<ISdkManagementMsg> {
130     return this.xdsSvr.onSdkInstall();
131   }
132
133   public abortInstall(sdk: ISdk): Observable<ISdk> {
134     return this.xdsSvr.abortInstall(this.curServerID, sdk.id);
135   }
136
137   public remove(sdk: ISdk): Observable<ISdk> {
138     return this.xdsSvr.removeSdk(this.curServerID, sdk.id);
139   }
140
141   /** Private **/
142
143   private _addSdk(sdk: ISdk, noNext?: boolean): ISdk {
144
145     // add new sdk
146     this._sdksList.push(sdk);
147
148     // sort sdk array
149     this._sdksList.sort((a, b) => {
150       if (a.name < b.name) {
151         return -1;
152       }
153       if (a.name > b.name) {
154         return 1;
155       }
156       return 0;
157     });
158
159     if (!noNext) {
160       this.sdksSubject.next(this._sdksList);
161     }
162
163     return sdk;
164   }
165
166   private _delSdk(sdk: ISdk) {
167     const idx = this._sdksList.findIndex(item => item.id === sdk.id);
168     if (idx === -1) {
169       if (isDevMode) {
170         /* tslint:disable:no-console */
171         console.log('Warning: Try to delete sdk unknown id: sdk=', sdk);
172       }
173       return;
174     }
175     const delId = this._sdksList[idx].id;
176     this._sdksList.splice(idx, 1);
177     if (delId === this.current.id) {
178       this.setCurrent(this._sdksList[0]);
179     }
180     this.sdksSubject.next(this._sdksList);
181   }
182
183 }