090df7beabb39e1214e09e0399fe3ab0a3431cff
[src/xds/xds-agent.git] / webapp / src / app / services / config.service.ts
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';
8
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';
14
15
16 import { XDSAgentService, IXDSProjectConfig } from "../services/xdsagent.service";
17 import { AlertService, IAlert } from "../services/alert.service";
18 import { UtilsService } from "../services/utils.service";
19
20 export interface IConfig {
21     projectsRootDir: string;
22     //SEB projects: IProject[];
23 }
24
25 @Injectable()
26 export class ConfigService {
27
28     public Conf$: Observable<IConfig>;
29
30     private confSubject: BehaviorSubject<IConfig>;
31     private confStore: IConfig;
32     // SEB cleanup private AgentConnectObs = null;
33     // SEB cleanup private stConnectObs = null;
34
35     constructor(private _window: Window,
36         private cookie: CookieService,
37         private xdsAgentSvr: XDSAgentService,
38         private alert: AlertService,
39         private utils: UtilsService,
40     ) {
41         this.load();
42         this.confSubject = <BehaviorSubject<IConfig>>new BehaviorSubject(this.confStore);
43         this.Conf$ = this.confSubject.asObservable();
44
45         // force to load projects
46         this.loadProjects();
47     }
48
49     // Load config
50     load() {
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;
55         } else {
56             // Set default config
57             this.confStore = {
58                 projectsRootDir: "",
59                 //projects: []
60             };
61         }
62     }
63
64     // Save config into cookie
65     save() {
66         // Notify subscribers
67         this.confSubject.next(Object.assign({}, this.confStore));
68
69         // Don't save projects in cookies (too big!)
70         let cfg = Object.assign({}, this.confStore);
71         this.cookie.putObject("xds-config", cfg);
72     }
73
74     loadProjects() {
75         /* SEB
76         // Setup connection with local XDS agent
77         if (this.AgentConnectObs) {
78             try {
79                 this.AgentConnectObs.unsubscribe();
80             } catch (err) { }
81             this.AgentConnectObs = null;
82         }
83
84         let cfg = this.confStore.xdsAgent;
85         this.AgentConnectObs = this.xdsAgentSvr.connect(cfg.retry, cfg.URL)
86             .subscribe((sts) => {
87                 //console.log("Agent sts", sts);
88                 // FIXME: load projects from local XDS Agent and
89                 //  not directly from local syncthing
90                 this._loadProjectFromLocalST();
91
92             }, error => {
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>
100                         <br>
101                         On Windows machine <a href="` + url_OS_Other + `" target="_blank"><span
102                             class="fa fa-external-link"></span></a>
103                         <br>
104                         On MacOS machine <a href="` + url_OS_Other + `" target="_blank"><span
105                             class="fa fa-external-link"></span></a>
106                     `;
107                     this.alert.error(msg);
108                 } else {
109                     this.alert.error(error);
110                 }
111             });
112         */
113     }
114
115     /* SEB
116     private _loadProjectFromLocalST() {
117         // Remove previous subscriber if existing
118         if (this.stConnectObs) {
119             try {
120                 this.stConnectObs.unsubscribe();
121             } catch (err) { }
122             this.stConnectObs = null;
123         }
124
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;
134             }
135
136             // Rebuild projects definition from local and remote syncthing
137             this.confStore.projects = [];
138
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);
145                         }
146                     });
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.');
150
151         }, error => {
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>";
155                 msg += "</span>";
156                 this.alert.error(msg);
157             } else {
158                 this.alert.error(error);
159             }
160         });
161     }
162
163     set syncToolURL(url: string) {
164         this.confStore.localSThg.URL = url;
165         this.save();
166     }
167     */
168
169     set projectsRootDir(p: string) {
170         /* SEB
171         if (p.charAt(0) === '~') {
172             p = this.confStore.localSThg.tilde + p.substring(1);
173         }
174         */
175         this.confStore.projectsRootDir = p;
176         this.save();
177     }
178 }