3 * Copyright (C) 2017 "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 export interface ISdk {
44 export interface ISdkManagementMsg {
57 export class SdkService {
58 public Sdks$: Observable<ISdk[]>;
59 public curSdk$: Observable<ISdk>;
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);
67 constructor(private xdsSvr: XDSAgentService) {
69 this.Sdks$ = this.sdksSubject.asObservable();
70 this.curSdk$ = this.curSdkSubject.asObservable();
72 this.xdsSvr.XdsConfig$.subscribe(cfg => {
73 if (!cfg || cfg.servers.length < 1) {
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) => {
82 this._addSdk(s, true);
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);
91 this.sdksSubject.next(this._sdksList);
95 // Add listener on sdk creation, deletion and change events
96 this.xdsSvr.onSdkInstall().subscribe(evMgt => {
97 this._addSdk(evMgt.sdk);
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);
105 this._delSdk(evMgt.sdk);
110 public setCurrent(s: ISdk) {
114 public getCurrent(): ISdk {
118 public getCurrentId(): string {
119 if (this.current && this.current.id) {
120 return this.current.id;
125 public install(sdk: ISdk): Observable<ISdk> {
126 return this.xdsSvr.installSdk(this.curServerID, sdk.id);
129 public onInstall(): Observable<ISdkManagementMsg> {
130 return this.xdsSvr.onSdkInstall();
133 public abortInstall(sdk: ISdk): Observable<ISdk> {
134 return this.xdsSvr.abortInstall(this.curServerID, sdk.id);
137 public remove(sdk: ISdk): Observable<ISdk> {
138 return this.xdsSvr.removeSdk(this.curServerID, sdk.id);
143 private _addSdk(sdk: ISdk, noNext?: boolean): ISdk {
146 this._sdksList.push(sdk);
149 this._sdksList.sort((a, b) => {
150 if (a.name < b.name) {
153 if (a.name > b.name) {
160 this.sdksSubject.next(this._sdksList);
166 private _delSdk(sdk: ISdk) {
167 const idx = this._sdksList.findIndex(item => item.id === sdk.id);
170 /* tslint:disable:no-console */
171 console.log('Warning: Try to delete sdk unknown id: sdk=', sdk);
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]);
180 this.sdksSubject.next(this._sdksList);