1 import { Injectable, OnInit } from '@angular/core';
2 import { Http, Headers, RequestOptionsArgs, Response } from '@angular/http';
3 import { Location } from '@angular/common';
4 import { CookieService } from 'ngx-cookie';
5 import { Observable } from 'rxjs/Observable';
6 import { Subscriber } from 'rxjs/Subscriber';
7 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
9 // Import RxJs required methods
10 import 'rxjs/add/operator/map';
11 import 'rxjs/add/operator/catch';
12 import 'rxjs/add/observable/throw';
13 import 'rxjs/add/operator/mergeMap';
16 import { XDSAgentService, IXDSProjectConfig } from "../services/xdsagent.service";
17 import { AlertService, IAlert } from "../services/alert.service";
18 import { UtilsService } from "../services/utils.service";
20 export interface IConfig {
21 projectsRootDir: string;
22 //SEB projects: IProject[];
26 export class ConfigService {
28 public Conf$: Observable<IConfig>;
30 private confSubject: BehaviorSubject<IConfig>;
31 private confStore: IConfig;
32 // SEB cleanup private AgentConnectObs = null;
33 // SEB cleanup private stConnectObs = null;
35 constructor(private _window: Window,
36 private cookie: CookieService,
37 private xdsAgentSvr: XDSAgentService,
38 private alert: AlertService,
39 private utils: UtilsService,
42 this.confSubject = <BehaviorSubject<IConfig>>new BehaviorSubject(this.confStore);
43 this.Conf$ = this.confSubject.asObservable();
45 // force to load projects
51 // Try to retrieve previous config from cookie
52 let cookConf = this.cookie.getObject("xds-config");
53 if (cookConf != null) {
54 this.confStore = <IConfig>cookConf;
64 // Save config into cookie
67 this.confSubject.next(Object.assign({}, this.confStore));
69 // Don't save projects in cookies (too big!)
70 let cfg = Object.assign({}, this.confStore);
71 this.cookie.putObject("xds-config", cfg);
76 // Setup connection with local XDS agent
77 if (this.AgentConnectObs) {
79 this.AgentConnectObs.unsubscribe();
81 this.AgentConnectObs = null;
84 let cfg = this.confStore.xdsAgent;
85 this.AgentConnectObs = this.xdsAgentSvr.connect(cfg.retry, cfg.URL)
87 //console.log("Agent sts", sts);
88 // FIXME: load projects from local XDS Agent and
89 // not directly from local syncthing
90 this._loadProjectFromLocalST();
93 if (error.indexOf("XDS local Agent not responding") !== -1) {
94 let url_OS_Linux = "https://en.opensuse.org/LinuxAutomotive#Installation_AGL_XDS";
95 let url_OS_Other = "https://github.com/iotbzh/xds-agent#how-to-install-on-other-platform";
96 let msg = `<span><strong>` + error + `<br></strong>
97 You may need to install and execute XDS-Agent: <br>
98 On Linux machine <a href="` + url_OS_Linux + `" target="_blank"><span
99 class="fa fa-external-link"></span></a>
101 On Windows machine <a href="` + url_OS_Other + `" target="_blank"><span
102 class="fa fa-external-link"></span></a>
104 On MacOS machine <a href="` + url_OS_Other + `" target="_blank"><span
105 class="fa fa-external-link"></span></a>
107 this.alert.error(msg);
109 this.alert.error(error);
116 private _loadProjectFromLocalST() {
117 // Remove previous subscriber if existing
118 if (this.stConnectObs) {
120 this.stConnectObs.unsubscribe();
122 this.stConnectObs = null;
125 // FIXME: move this code and all logic about syncthing inside XDS Agent
126 // Setup connection with local SyncThing
127 let retry = this.confStore.localSThg.retry;
128 let url = this.confStore.localSThg.URL;
129 this.stConnectObs = this.stSvr.connect(retry, url).subscribe((sts) => {
130 this.confStore.localSThg.ID = sts.ID;
131 this.confStore.localSThg.tilde = sts.tilde;
132 if (this.confStore.projectsRootDir === "") {
133 this.confStore.projectsRootDir = sts.tilde;
136 // Rebuild projects definition from local and remote syncthing
137 this.confStore.projects = [];
139 this.xdsServerSvr.getProjects().subscribe(remotePrj => {
140 this.stSvr.getProjects().subscribe(localPrj => {
141 remotePrj.forEach(rPrj => {
142 let lPrj = localPrj.filter(item => item.id === rPrj.id);
143 if (lPrj.length > 0 || rPrj.type === ProjectType.NATIVE_PATHMAP) {
144 this._addProject(rPrj, true);
147 this.confSubject.next(Object.assign({}, this.confStore));
148 }), error => this.alert.error('Could not load initial state of local projects.');
149 }), error => this.alert.error('Could not load initial state of remote projects.');
152 if (error.indexOf("Syncthing local daemon not responding") !== -1) {
153 let msg = "<span><strong>" + error + "<br></strong>";
154 msg += "Please check that local XDS-Agent is running.<br>";
156 this.alert.error(msg);
158 this.alert.error(error);
163 set syncToolURL(url: string) {
164 this.confStore.localSThg.URL = url;
169 set projectsRootDir(p: string) {
171 if (p.charAt(0) === '~') {
172 p = this.confStore.localSThg.tilde + p.substring(1);
175 this.confStore.projectsRootDir = p;