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 {
75 export class SdkService {
76 public Sdks$: Observable<ISdk[]>;
77 public curSdk$: Observable<ISdk>;
79 private _sdksList = [];
80 private sdksSubject = <BehaviorSubject<ISdk[]>>new BehaviorSubject(this._sdksList);
81 private current: ISdk;
82 private curSdkSubject = <BehaviorSubject<ISdk>>new BehaviorSubject(this.current);
85 constructor(private xdsSvr: XDSAgentService) {
87 this.Sdks$ = this.sdksSubject.asObservable();
88 this.curSdk$ = this.curSdkSubject.asObservable();
90 this.xdsSvr.XdsConfig$.subscribe(cfg => {
91 if (!cfg || cfg.servers.length < 1) {
94 // FIXME support multiple server
95 // cfg.servers.forEach(svr => {
96 this.curServerID = cfg.servers[0].id;
97 this.xdsSvr.getSdks(this.curServerID).subscribe((sdks) => {
100 this._addSdk(s, true);
103 // TODO: get previous val from xds-config service / cookie
104 if (this._sdksList.length > 0) {
105 this.current = this._sdksList[0];
106 this.curSdkSubject.next(this.current);
109 this.sdksSubject.next(this._sdksList);
113 // Add listener on sdk creation, deletion and change events
114 this.xdsSvr.onSdkInstall().subscribe(evMgt => {
115 this._addSdk(evMgt.sdk);
117 this.xdsSvr.onSdkRemove().subscribe(evMgt => {
118 if (evMgt.sdk.status !== StatusType.NOT_INSTALLED) {
119 /* tslint:disable:no-console */
120 console.log('Error: received event:sdk-remove with invalid status: evMgt=', evMgt);
123 this._delSdk(evMgt.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.onSdkInstall();
151 public abortInstall(sdk: ISdk): Observable<ISdk> {
152 return this.xdsSvr.abortInstall(this.curServerID, sdk.id);
155 public remove(sdk: ISdk): Observable<ISdk> {
156 return this.xdsSvr.removeSdk(this.curServerID, sdk.id);
161 private _addSdk(sdk: ISdk, noNext?: boolean): ISdk {
163 // check if sdk already exists
164 const idx = this._sdksList.findIndex(s => s.id === sdk.id);
166 this._sdksList[idx] = sdk;
169 this._sdksList.push(sdk);
173 this._sdksList.sort((a, b) => {
174 if (a.name < b.name) {
177 if (a.name > b.name) {
184 this.sdksSubject.next(this._sdksList);
190 private _delSdk(sdk: ISdk) {
191 const idx = this._sdksList.findIndex(item => item.id === sdk.id);
194 /* tslint:disable:no-console */
195 console.log('Warning: Try to delete sdk unknown id: sdk=', sdk);
199 const delId = this._sdksList[idx].id;
200 this._sdksList.splice(idx, 1);
201 if (delId === this.current.id) {
202 this.setCurrent(this._sdksList[0]);
204 this.sdksSubject.next(this._sdksList);