blob: 234335585941e1a70892c78d19da5f68c41d3718 [file] [log] [blame]
Radek Krejci5a1571a2018-02-16 13:45:53 +01001import { Injectable, OnInit } from '@angular/core';
Radek Krejci95bd14c2017-09-21 14:24:13 +02002import { Http, Response, RequestOptions, URLSearchParams } from '@angular/http';
3import { Observable } from 'rxjs/Observable';
4import 'rxjs/add/operator/catch';
5import 'rxjs/add/operator/map';
6import 'rxjs/add/operator/do';
7
Radek Krejci30ce1592018-03-01 14:44:14 +01008import { TreeService } from './tree.service';
Radek Krejci95bd14c2017-09-21 14:24:13 +02009import { Device } from '../inventory/device';
10import { Session } from './session';
11
12@Injectable()
Radek Krejci5a1571a2018-02-16 13:45:53 +010013export class SessionsService implements OnInit {
Radek Krejci95bd14c2017-09-21 14:24:13 +020014 public sessions: Session[];
Radek Krejci30ce1592018-03-01 14:44:14 +010015 public activeSession: string;
Radek Krejci95bd14c2017-09-21 14:24:13 +020016
Radek Krejci30ce1592018-03-01 14:44:14 +010017 constructor(private http: Http, private treeService: TreeService) {
Radek Krejci95bd14c2017-09-21 14:24:13 +020018 this.activeSession = localStorage.getItem('activeSession');
19 if (!this.activeSession) {
20 this.activeSession = "";
21 }
Radek Krejci5a1571a2018-02-16 13:45:53 +010022 }
23
24 ngOnInit(): void {
Radek Krejci95bd14c2017-09-21 14:24:13 +020025 this.checkSessions();
26 }
27
Radek Krejci77f77202017-11-03 15:33:50 +010028 storeData() {
29 localStorage.setItem('sessions', JSON.stringify(this.sessions));
30 }
31
Radek Krejci6e772b22018-01-25 13:28:57 +010032 loadData() {
33 this.sessions = JSON.parse(localStorage.getItem('sessions'));
Radek Krejcif23bf732018-03-09 11:34:56 +010034 if (!this.sessions) {
35 this.sessions = [];
36 }
Radek Krejci010475d2018-03-08 13:14:19 +010037 for (let session of this.sessions) {
38 /* fix links in modifications data to link the currently reloaded objects */
39 for (let mod in session.modifications) {
40 if ('data' in session.modifications[mod]) {
41 session.modifications[mod]['data'] = this.treeService.pathNode(session, mod);
42 }
43 }
44 }
Radek Krejci6e772b22018-01-25 13:28:57 +010045 }
46
Radek Krejci77f77202017-11-03 15:33:50 +010047 getActiveSession(key: string = this.activeSession): Session {
Radek Krejci6a2282f2018-02-19 13:01:23 +010048 if (key) {
49 for (let i = this.sessions.length; i > 0; i--) {
50 if (this.sessions[i - 1].key == key) {
51 return this.sessions[i - 1];
52 }
Radek Krejci903f94e2017-10-20 10:53:26 +020053 }
54 }
55 return null;
56 }
57
Radek Krejci77f77202017-11-03 15:33:50 +010058 changeActiveSession(key: string): Session {
59 if (!this.activeSession) {
60 return null;
61 }
62 let result = this.getActiveSession(key);
63 if (result) {
64 this.activeSession = key;
65 localStorage.setItem('activeSession', this.activeSession);
66 }
67 return result;
Radek Krejci903f94e2017-10-20 10:53:26 +020068 }
69
Radek Krejci010475d2018-03-08 13:14:19 +010070 checkSessionIndex(i: number) {
71 this.alive(this.sessions[i].key).then(resp => {
72 if (!resp['success']) {
73 if (this.activeSession && this.sessions[i].key == this.activeSession) {
74 /* active session is not alive - select new active session
75 * as the one on the left from the current one, if there
76 * is no one, choose the one on the right */
77 if (i > 0) {
78 this.activeSession = this.sessions[i - 1].key;
79 } else if (i + 1 < this.sessions.length) {
80 this.activeSession = this.sessions[i + 1].key;
81 } else {
82 this.activeSession = "";
83 }
84 localStorage.setItem('activeSession', this.activeSession);
85 }
86 this.sessions.splice(i, 1);
87 this.storeData();
88 }
89 });
90 }
91
92 checkSession(key: string) {
93 for (let i in this.sessions) {
94 if (this.sessions[i].key == key) {
95 this.checkSessionIndex(Number(i));
96 break;
97 }
98 }
99 }
100
Radek Krejci95bd14c2017-09-21 14:24:13 +0200101 checkSessions() {
Radek Krejci6e772b22018-01-25 13:28:57 +0100102 this.loadData();
Radek Krejcif23bf732018-03-09 11:34:56 +0100103 /* verify that the sessions are still active */
104 for (let i = this.sessions.length; i > 0; i--) {
105 this.checkSessionIndex(i - 1);
Radek Krejci95bd14c2017-09-21 14:24:13 +0200106 }
107 }
108
Radek Krejci30ce1592018-03-01 14:44:14 +0100109 collapse( activeSession, node = null ) {
110 if ( node ) {
111 delete node['children'];
112 activeSession.dataVisibility = 'mixed';
113 } else {
114 for ( let root of activeSession.data['children'] ) {
115 delete root['children'];
116 }
117 activeSession.dataVisibility = 'root';
118 }
119 this.treeService.updateHiddenFlags( activeSession );
120 this.storeData();
121 }
122
123 expand( activeSession, node, all: boolean ) {
124 node['loading'] = true;
125 this.rpcGetSubtree( activeSession.key, all, node['path'] ).subscribe( result => {
126 if ( result['success'] ) {
127 for ( let iter of result['data']['children'] ) {
128 this.treeService.setDirty( activeSession, iter );
129 }
130 node['children'] = result['data']['children'];
131 this.treeService.updateHiddenFlags( activeSession );
132 delete node['loading'];
133 this.storeData();
134 }
135 } );
136 }
137
Radek Krejci4d3896c2018-01-08 17:10:43 +0100138 checkValue(key: string, path: string, value: string): Observable<string[]> {
139 let params = new URLSearchParams();
140 params.set('key', key);
141 params.set('path', path);
142 params.set('value', value);
143 let options = new RequestOptions({ search: params });
Radek Krejci6e772b22018-01-25 13:28:57 +0100144 return this.http.get('/netopeer/session/schema/checkvalue', options)
Radek Krejci4d3896c2018-01-08 17:10:43 +0100145 .map((resp: Response) => resp.json())
146 .catch((err: Response | any) => Observable.throw(err));
147 }
148
Radek Krejci5a69fa32018-02-01 11:03:04 +0100149 private filterSchemas(node, schemas) {
150 if (node['deleted'] || (node['info']['type'] & 0x18)) {
151 /* ignore deleted nodes and nodes that can be instantiated multiple times */
152 return;
153 }
154 for (let index in schemas) {
155 if (!schemas[index]['config'] ||
156 (schemas[index]['name'] == node['info']['name'] && schemas[index]['module'] == node['info']['module'])) {
157 /* 1. read-only node */
158 /* 2. the node is already instantiated */
159 schemas.splice(index, 1);
160 }
161 }
162 }
163
Radek Krejci6e772b22018-01-25 13:28:57 +0100164 childrenSchemas(key: string, path: string, node = null) {
165 let params = new URLSearchParams();
166 params.set('key', key);
167 params.set('path', path);
168 params.set('relative', 'children');
169 let options = new RequestOptions({ search: params });
170 return this.http.get('/netopeer/session/schema', options)
171 .map((resp: Response) => {
172 let result = resp.json();
Radek Krejci010475d2018-03-08 13:14:19 +0100173 //console.log(result)
Radek Krejci6e772b22018-01-25 13:28:57 +0100174 if (result['success'] && node) {
Radek Krejci5a69fa32018-02-01 11:03:04 +0100175 if ('children' in node) {
176 for (let iter of node['children']) {
177 this.filterSchemas(iter, result['data']);
Radek Krejci6e772b22018-01-25 13:28:57 +0100178 }
Radek Krejci5a69fa32018-02-01 11:03:04 +0100179 }
180 if ('newChildren' in node) {
181 for (let iter of node['newChildren']) {
182 this.filterSchemas(iter, result['data']);
Radek Krejci6e772b22018-01-25 13:28:57 +0100183 }
184 }
185 }
186 if (result['success']) {
187 return result['data'];
188 } else {
189 return [];
190 }
191 }).toPromise();
192 }
193
Radek Krejcid0ce4cf2018-02-09 14:44:34 +0100194 schemaValues(key: string, path: string) {
195 let params = new URLSearchParams();
196 params.set('key', key);
197 params.set('path', path);
198 let options = new RequestOptions({ search: params });
199 return this.http.get('/netopeer/session/schema/values', options)
200 .map((resp: Response) => resp.json()).toPromise();
201 }
202
Radek Krejci6a2282f2018-02-19 13:01:23 +0100203 alive(key: string): Promise<string[]> {
Radek Krejci95bd14c2017-09-21 14:24:13 +0200204 let params = new URLSearchParams();
205 params.set('key', key);
206 let options = new RequestOptions({ search: params });
207 return this.http.get('/netopeer/session/alive', options)
Radek Krejci6a2282f2018-02-19 13:01:23 +0100208 .map((resp: Response) => resp.json()).toPromise();
Radek Krejci95bd14c2017-09-21 14:24:13 +0200209 }
210
211 getCpblts(key: string): Observable<string[]> {
212 let params = new URLSearchParams();
213 params.set('key', key);
214 let options = new RequestOptions({ search: params });
215 return this.http.get('/netopeer/session/capabilities', options)
216 .map((resp: Response) => resp.json())
217 .catch((err: Response | any) => Observable.throw(err));
218 }
219
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100220 setDirty(node) {
221 let activeSession = this.getActiveSession();
222 if (!activeSession.modifications) {
223 return;
224 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100225
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100226 if (node['path'] in activeSession.modifications) {
227 node['dirty'] = true;
228 if (activeSession.modifications[node['path']]['type'] == 'change') {
229 activeSession.modifications[node['path']]['original'] = node['value'];
230 }
231 node['value'] = activeSession.modifications[node['path']]['value'];
232 }
233 /* recursion */
234 if ('children' in node) {
235 for (let child of node['children']) {
236 this.setDirty(child);
237 }
238 }
239 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100240
Radek Krejcia1339602017-11-02 13:52:38 +0100241 rpcGetSubtree(key: string, all: boolean, path: string = ""): Observable<string[]> {
Radek Krejci2e578562017-10-17 11:11:13 +0200242 let params = new URLSearchParams();
243 params.set('key', key);
Radek Krejcia1339602017-11-02 13:52:38 +0100244 params.set('recursive', String(all));
245 if (path.length) {
246 params.set('path', path);
247 }
Radek Krejci2e578562017-10-17 11:11:13 +0200248 let options = new RequestOptions({ search: params });
249 return this.http.get('/netopeer/session/rpcGet', options)
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100250 .map((resp: Response) => {
Radek Krejci010475d2018-03-08 13:14:19 +0100251 console.log(resp);
252 let result = resp.json();
253 if (!result['success']) {
254 this.checkSession(key);
255 }
256 return result;
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100257 })
Radek Krejci2e578562017-10-17 11:11:13 +0200258 .catch((err: Response | any) => Observable.throw(err));
259 }
Radek Krejci30ce1592018-03-01 14:44:14 +0100260 rpcGet( activeSession: Session, all: boolean ) {
261 if ( activeSession.data ) {
262 if ( ( all && activeSession.dataVisibility == 'all' ) ||
263 ( !all && activeSession.dataVisibility == 'root' ) ) {
264 return;
265 }
266 }
267 activeSession.loading = true;
268 delete activeSession.data;
269 this.rpcGetSubtree( activeSession.key, all ).subscribe( result => {
270 if ( result['success'] ) {
271 for ( let iter of result['data'] ) {
272 this.treeService.setDirty( activeSession, iter );
273 }
274 activeSession.data = {};
275 activeSession.data['path'] = '/';
276 activeSession.data['info'] = {};
Radek Krejci010475d2018-03-08 13:14:19 +0100277 activeSession.data['info']['config'] = true;
Radek Krejci30ce1592018-03-01 14:44:14 +0100278 activeSession.data['info']['path'] = '/';
279 activeSession.data['children'] = result['data'];
280 if ( all ) {
281 activeSession.dataVisibility = 'all';
282 } else {
283 activeSession.dataVisibility = 'root';
284 }
285 }
286 activeSession.loading = false;
287 this.storeData();
288 } );
289 }
Radek Krejci2e578562017-10-17 11:11:13 +0200290
Radek Krejci62fec6a2018-02-06 10:24:09 +0100291 commit(key: string) {
292 let activeSession = this.getActiveSession(key);
293 let options = new RequestOptions({body: JSON.stringify({'key': key, 'modifications': activeSession.modifications})});
294 return this.http.post('/netopeer/session/commit', null, options)
295 .map((resp: Response) => resp.json()).toPromise();
296 }
297
Radek Krejci95bd14c2017-09-21 14:24:13 +0200298 close(key: string) {
299 let params = new URLSearchParams();
300 params.set('key', key);
301 let options = new RequestOptions({search: params});
302 return this.http.delete('/netopeer/session', options)
303 .map((resp: Response) => resp.json())
304 .do(resp => {
305 if (resp['success']) {
Radek Krejcicc355612018-02-14 08:55:01 +0100306 let index = this.sessions.findIndex((s: Session) => s.key == key);
307 this.sessions.splice(index, 1);
Radek Krejci95bd14c2017-09-21 14:24:13 +0200308 if (key == this.activeSession) {
Radek Krejcicc355612018-02-14 08:55:01 +0100309 if (index > 0) {
310 this.activeSession = this.sessions[index - 1].key;
311 } else if (this.sessions.length) {
Radek Krejci95bd14c2017-09-21 14:24:13 +0200312 this.activeSession = this.sessions[0].key;
313 } else {
314 this.activeSession = ""
315 }
316 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100317 this.storeData();
Radek Krejci95bd14c2017-09-21 14:24:13 +0200318 localStorage.setItem('activeSession', this.activeSession);
319 }
320 })
321 .catch((err: Response | any) => Observable.throw(err));
322 }
323
324 connect(dev: Device) {
Radek Krejcia6c8b412017-10-17 16:59:38 +0200325 let options = null; // = new RequestOptions({body: JSON.stringify({'id': dev.id})});
326 if (dev.id) {
327 options = new RequestOptions({body: JSON.stringify({'id': dev.id})});
328 } else {
329 options = new RequestOptions({body: JSON.stringify({'device': {'hostname': dev.hostname, 'port': dev.port, 'username': dev.username, 'password': dev.password}})});
330 }
Radek Krejci95bd14c2017-09-21 14:24:13 +0200331 return this.http.post('/netopeer/session', null, options)
332 .map((resp: Response) => resp.json())
333 .do(resp => {
334 if (resp['success']) {
Radek Krejcia1339602017-11-02 13:52:38 +0100335 this.sessions.push(new Session(resp['session-key'], dev));
Radek Krejci95bd14c2017-09-21 14:24:13 +0200336 this.activeSession = resp['session-key'];
Radek Krejci6e772b22018-01-25 13:28:57 +0100337 this.storeData();
Radek Krejci95bd14c2017-09-21 14:24:13 +0200338 localStorage.setItem('activeSession', this.activeSession);
339 }
340 })
341 .catch((err: Response | any) => Observable.throw(err))
342 }
343}