3 * Copyright (C) 2017-2018 "IoT.bzh"
4 * Author Sebastien Douheret <sebastien@iot.bzh>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import { Injectable, SecurityContext, isDevMode } from '@angular/core';
20 import { Observable } from 'rxjs/Observable';
21 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
23 import { XDSAgentService } from '../services/xdsagent.service';
25 import 'rxjs/add/observable/throw';
27 /* FIXME: syntax only compatible with TS>2.4.0
28 export enum StatusType {
30 NOT_INSTALLED = 'Not Installed',
31 INSTALLING = 'Installing',
32 UNINSTALLING = 'Un-installing',
33 INSTALLED = 'Installed'
36 export type StatusTypeEnum = 'Disable' | 'Not Installed' | 'Installing' | 'Un-installing' | 'Installed';
37 export const StatusType = {
39 NOT_INSTALLED: 'Not Installed',
40 INSTALLING: 'Installing',
41 UNINSTALLING: 'Un-installing',
42 INSTALLED: 'Installed',
45 export interface ISdk {
62 export interface ISdkManagementMsg {
76 export class SdkService {
77 public Sdks$: Observable<ISdk[]>;
78 public curSdk$: Observable<ISdk>;
80 private _sdksList = [];
81 private sdksSubject = <BehaviorSubject<ISdk[]>>new BehaviorSubject(this._sdksList);
82 private current: ISdk;
83 private curSdkSubject = <BehaviorSubject<ISdk>>new BehaviorSubject(this.current);
86 constructor(private xdsSvr: XDSAgentService) {
88 this.Sdks$ = this.sdksSubject.asObservable();
89 this.curSdk$ = this.curSdkSubject.asObservable();
91 this.xdsSvr.XdsConfig$.subscribe(cfg => {
92 if (!cfg || cfg.servers.length < 1) {
95 // FIXME support multiple server
96 // cfg.servers.forEach(svr => {
97 this.curServerID = cfg.servers[0].id;
98 this.xdsSvr.getSdks(this.curServerID).subscribe((sdks) => {
101 this._addUpdateSdk(s, true);
104 // TODO: get previous val from xds-config service / cookie
105 if (this._sdksList.length > 0) {
106 const installedSdks = this._sdksList.filter(sd => sd.status === StatusType.INSTALLED);
107 this.current = installedSdks.length > 0 ? installedSdks[0] : this._sdksList[0];
108 this.curSdkSubject.next(this.current);
111 this.sdksSubject.next(this._sdksList);
115 // Add listener on sdk creation, deletion and change events
116 this.xdsSvr.onSdkAdd().subscribe(sdk => this._addUpdateSdk(sdk));
117 this.xdsSvr.onSdkChange().subscribe(sdk => this._addUpdateSdk(sdk));
119 this.xdsSvr.onSdkRemove().subscribe(sdk => {
120 if (sdk.status !== StatusType.NOT_INSTALLED) {
121 /* tslint:disable:no-console */
122 console.log('Error: received event:sdk-remove with invalid status: sdk=', sdk);
129 public setCurrent(s: ISdk) {
133 public getCurrent(): ISdk {
137 public getCurrentId(): string {
138 if (this.current && this.current.id) {
139 return this.current.id;
144 public install(sdk: ISdk): Observable<ISdk> {
145 return this.xdsSvr.installSdk(this.curServerID, sdk.id);
148 public onInstall(): Observable<ISdkManagementMsg> {
149 return this.xdsSvr.onSdkManagement().filter(ev => ev.action === 'installing');
152 public onRemove(): Observable<ISdkManagementMsg> {
153 return this.xdsSvr.onSdkManagement().filter(ev => ev.action === 'removing');
156 public abortInstall(sdk: ISdk): Observable<ISdk> {
157 return this.xdsSvr.abortInstall(this.curServerID, sdk.id);
160 public remove(sdk: ISdk): Observable<ISdk> {
161 return this.xdsSvr.removeSdk(this.curServerID, sdk.id);
166 private _addUpdateSdk(sdk: ISdk, noNext?: boolean): ISdk {
168 // check if sdk already exists
169 const idx = this._sdksList.findIndex(s => s.id === sdk.id);
171 // Just update existing one
172 this._sdksList[idx] = sdk;
175 this._sdksList.push(sdk);
178 this._sdksList.sort((a, b) => {
179 if (a.name < b.name) {
182 if (a.name > b.name) {
190 this.sdksSubject.next(this._sdksList);
196 private _delSdk(sdk: ISdk) {
197 const idx = this._sdksList.findIndex(item => item.id === sdk.id);
200 /* tslint:disable:no-console */
201 console.log('Warning: Try to delete sdk unknown id: sdk=', sdk);
205 const delId = this._sdksList[idx].id;
206 this._sdksList.splice(idx, 1);
207 if (delId === this.current.id) {
208 this.setCurrent(this._sdksList[0]);
210 this.sdksSubject.next(this._sdksList);