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 this.current = this._sdksList[0];
107 this.curSdkSubject.next(this.current);
110 this.sdksSubject.next(this._sdksList);
114 // Add listener on sdk creation, deletion and change events
115 this.xdsSvr.onSdkAdd().subscribe(sdk => this._addUpdateSdk(sdk));
116 this.xdsSvr.onSdkChange().subscribe(sdk => this._addUpdateSdk(sdk));
118 this.xdsSvr.onSdkRemove().subscribe(sdk => {
119 if (sdk.status !== StatusType.NOT_INSTALLED) {
120 /* tslint:disable:no-console */
121 console.log('Error: received event:sdk-remove with invalid status: sdk=', sdk);
128 public setCurrent(s: ISdk) {
132 public getCurrent(): ISdk {
136 public getCurrentId(): string {
137 if (this.current && this.current.id) {
138 return this.current.id;
143 public install(sdk: ISdk): Observable<ISdk> {
144 return this.xdsSvr.installSdk(this.curServerID, sdk.id);
147 public onInstall(): Observable<ISdkManagementMsg> {
148 return this.xdsSvr.onSdkManagement().filter(ev => ev.action === 'installing');
151 public onRemove(): Observable<ISdkManagementMsg> {
152 return this.xdsSvr.onSdkManagement().filter(ev => ev.action === 'removing');
155 public abortInstall(sdk: ISdk): Observable<ISdk> {
156 return this.xdsSvr.abortInstall(this.curServerID, sdk.id);
159 public remove(sdk: ISdk): Observable<ISdk> {
160 return this.xdsSvr.removeSdk(this.curServerID, sdk.id);
165 private _addUpdateSdk(sdk: ISdk, noNext?: boolean): ISdk {
167 // check if sdk already exists
168 const idx = this._sdksList.findIndex(s => s.id === sdk.id);
170 // Just update existing one
171 this._sdksList[idx] = sdk;
174 this._sdksList.push(sdk);
177 this._sdksList.sort((a, b) => {
178 if (a.name < b.name) {
181 if (a.name > b.name) {
189 this.sdksSubject.next(this._sdksList);
195 private _delSdk(sdk: ISdk) {
196 const idx = this._sdksList.findIndex(item => item.id === sdk.id);
199 /* tslint:disable:no-console */
200 console.log('Warning: Try to delete sdk unknown id: sdk=', sdk);
204 const delId = this._sdksList[idx].id;
205 this._sdksList.splice(idx, 1);
206 if (delId === this.current.id) {
207 this.setCurrent(this._sdksList[0]);
209 this.sdksSubject.next(this._sdksList);