blob: 8d28e24cf8c376de7c69c4e5c4ae28f3a5bbd6a0 [file] [log] [blame]
Radek Krejci95bd14c2017-09-21 14:24:13 +02001import { Injectable } from '@angular/core';
2import { 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
8import { Device } from '../inventory/device';
9import { Session } from './session';
10
11@Injectable()
Radek Krejci903f94e2017-10-20 10:53:26 +020012export class SessionsService{
Radek Krejci95bd14c2017-09-21 14:24:13 +020013 public sessions: Session[];
14 public activeSession;
15
16 constructor(private http: Http) {
17 this.activeSession = localStorage.getItem('activeSession');
18 if (!this.activeSession) {
19 this.activeSession = "";
20 }
21 this.checkSessions();
22 }
23
Radek Krejci77f77202017-11-03 15:33:50 +010024 storeData() {
25 localStorage.setItem('sessions', JSON.stringify(this.sessions));
26 }
27
Radek Krejci6e772b22018-01-25 13:28:57 +010028 loadData() {
29 this.sessions = JSON.parse(localStorage.getItem('sessions'));
30 }
31
Radek Krejci77f77202017-11-03 15:33:50 +010032 getActiveSession(key: string = this.activeSession): Session {
33 if (!key) {
Radek Krejci903f94e2017-10-20 10:53:26 +020034 return null;
35 }
36 for (let i = this.sessions.length; i > 0; i--) {
Radek Krejci77f77202017-11-03 15:33:50 +010037 if (this.sessions[i - 1].key == key) {
Radek Krejci903f94e2017-10-20 10:53:26 +020038 return this.sessions[i - 1];
39 }
40 }
41 return null;
42 }
43
Radek Krejci77f77202017-11-03 15:33:50 +010044 changeActiveSession(key: string): Session {
45 if (!this.activeSession) {
46 return null;
47 }
48 let result = this.getActiveSession(key);
49 if (result) {
50 this.activeSession = key;
51 localStorage.setItem('activeSession', this.activeSession);
52 }
53 return result;
Radek Krejci903f94e2017-10-20 10:53:26 +020054 }
55
Radek Krejci95bd14c2017-09-21 14:24:13 +020056 checkSessions() {
Radek Krejci6e772b22018-01-25 13:28:57 +010057 this.loadData();
Radek Krejci95bd14c2017-09-21 14:24:13 +020058 if (!this.sessions) {
59 this.sessions = [];
60 } else {
61 /* verify that the sessions are still active */
62 for (let i = this.sessions.length; i > 0; i--) {
63 this.alive(this.sessions[i - 1].key).subscribe(resp => {
64 if (!resp['success']) {
65 if (this.activeSession && this.sessions[i - 1].key == this.activeSession) {
66 /* active session is not alive - select new active session
67 * as the one on the left from the current one, if there
68 * is no one, choose the one on the right */
69 if (i > 1) {
70 this.activeSession = this.sessions[i - 2].key;
71 } else if (this.sessions.length > i) {
72 this.activeSession = this.sessions[i].key;
73 } else {
74 this.activeSession = "";
75 }
76 }
77 this.sessions.splice(i - 1, 1);
78 }
79 });
80 }
81 }
82 }
83
Radek Krejci4d3896c2018-01-08 17:10:43 +010084 checkValue(key: string, path: string, value: string): Observable<string[]> {
85 let params = new URLSearchParams();
86 params.set('key', key);
87 params.set('path', path);
88 params.set('value', value);
89 let options = new RequestOptions({ search: params });
Radek Krejci6e772b22018-01-25 13:28:57 +010090 return this.http.get('/netopeer/session/schema/checkvalue', options)
Radek Krejci4d3896c2018-01-08 17:10:43 +010091 .map((resp: Response) => resp.json())
92 .catch((err: Response | any) => Observable.throw(err));
93 }
94
Radek Krejci5a69fa32018-02-01 11:03:04 +010095 private filterSchemas(node, schemas) {
96 if (node['deleted'] || (node['info']['type'] & 0x18)) {
97 /* ignore deleted nodes and nodes that can be instantiated multiple times */
98 return;
99 }
100 for (let index in schemas) {
101 if (!schemas[index]['config'] ||
102 (schemas[index]['name'] == node['info']['name'] && schemas[index]['module'] == node['info']['module'])) {
103 /* 1. read-only node */
104 /* 2. the node is already instantiated */
105 schemas.splice(index, 1);
106 }
107 }
108 }
109
Radek Krejci6e772b22018-01-25 13:28:57 +0100110 childrenSchemas(key: string, path: string, node = null) {
111 let params = new URLSearchParams();
112 params.set('key', key);
113 params.set('path', path);
114 params.set('relative', 'children');
115 let options = new RequestOptions({ search: params });
116 return this.http.get('/netopeer/session/schema', options)
117 .map((resp: Response) => {
118 let result = resp.json();
119 console.log(result)
120 if (result['success'] && node) {
Radek Krejci5a69fa32018-02-01 11:03:04 +0100121 if ('children' in node) {
122 for (let iter of node['children']) {
123 this.filterSchemas(iter, result['data']);
Radek Krejci6e772b22018-01-25 13:28:57 +0100124 }
Radek Krejci5a69fa32018-02-01 11:03:04 +0100125 }
126 if ('newChildren' in node) {
127 for (let iter of node['newChildren']) {
128 this.filterSchemas(iter, result['data']);
Radek Krejci6e772b22018-01-25 13:28:57 +0100129 }
130 }
131 }
132 if (result['success']) {
133 return result['data'];
134 } else {
135 return [];
136 }
137 }).toPromise();
138 }
139
Radek Krejcid0ce4cf2018-02-09 14:44:34 +0100140 schemaValues(key: string, path: string) {
141 let params = new URLSearchParams();
142 params.set('key', key);
143 params.set('path', path);
144 let options = new RequestOptions({ search: params });
145 return this.http.get('/netopeer/session/schema/values', options)
146 .map((resp: Response) => resp.json()).toPromise();
147 }
148
Radek Krejci95bd14c2017-09-21 14:24:13 +0200149 alive(key: string): Observable<string[]> {
150 let params = new URLSearchParams();
151 params.set('key', key);
152 let options = new RequestOptions({ search: params });
153 return this.http.get('/netopeer/session/alive', options)
154 .map((resp: Response) => resp.json())
155 .catch((err: Response | any) => Observable.throw(err));
156 }
157
158 getCpblts(key: string): Observable<string[]> {
159 let params = new URLSearchParams();
160 params.set('key', key);
161 let options = new RequestOptions({ search: params });
162 return this.http.get('/netopeer/session/capabilities', options)
163 .map((resp: Response) => resp.json())
164 .catch((err: Response | any) => Observable.throw(err));
165 }
166
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100167 setDirty(node) {
168 let activeSession = this.getActiveSession();
169 if (!activeSession.modifications) {
170 return;
171 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100172
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100173 if (node['path'] in activeSession.modifications) {
174 node['dirty'] = true;
175 if (activeSession.modifications[node['path']]['type'] == 'change') {
176 activeSession.modifications[node['path']]['original'] = node['value'];
177 }
178 node['value'] = activeSession.modifications[node['path']]['value'];
179 }
180 /* recursion */
181 if ('children' in node) {
182 for (let child of node['children']) {
183 this.setDirty(child);
184 }
185 }
186 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100187
Radek Krejcia1339602017-11-02 13:52:38 +0100188 rpcGetSubtree(key: string, all: boolean, path: string = ""): Observable<string[]> {
Radek Krejci2e578562017-10-17 11:11:13 +0200189 let params = new URLSearchParams();
190 params.set('key', key);
Radek Krejcia1339602017-11-02 13:52:38 +0100191 params.set('recursive', String(all));
192 if (path.length) {
193 params.set('path', path);
194 }
Radek Krejci2e578562017-10-17 11:11:13 +0200195 let options = new RequestOptions({ search: params });
196 return this.http.get('/netopeer/session/rpcGet', options)
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100197 .map((resp: Response) => {
Radek Krejcif71867f2018-01-30 13:28:28 +0100198 //console.log(resp);
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100199 return resp.json();
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100200 })
Radek Krejci2e578562017-10-17 11:11:13 +0200201 .catch((err: Response | any) => Observable.throw(err));
202 }
203
Radek Krejci62fec6a2018-02-06 10:24:09 +0100204 commit(key: string) {
205 let activeSession = this.getActiveSession(key);
206 let options = new RequestOptions({body: JSON.stringify({'key': key, 'modifications': activeSession.modifications})});
207 return this.http.post('/netopeer/session/commit', null, options)
208 .map((resp: Response) => resp.json()).toPromise();
209 }
210
Radek Krejci95bd14c2017-09-21 14:24:13 +0200211 close(key: string) {
212 let params = new URLSearchParams();
213 params.set('key', key);
214 let options = new RequestOptions({search: params});
215 return this.http.delete('/netopeer/session', options)
216 .map((resp: Response) => resp.json())
217 .do(resp => {
218 if (resp['success']) {
Radek Krejcicc355612018-02-14 08:55:01 +0100219 let index = this.sessions.findIndex((s: Session) => s.key == key);
220 this.sessions.splice(index, 1);
Radek Krejci95bd14c2017-09-21 14:24:13 +0200221 if (key == this.activeSession) {
Radek Krejcicc355612018-02-14 08:55:01 +0100222 if (index > 0) {
223 this.activeSession = this.sessions[index - 1].key;
224 } else if (this.sessions.length) {
Radek Krejci95bd14c2017-09-21 14:24:13 +0200225 this.activeSession = this.sessions[0].key;
226 } else {
227 this.activeSession = ""
228 }
229 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100230 this.storeData();
Radek Krejci95bd14c2017-09-21 14:24:13 +0200231 localStorage.setItem('activeSession', this.activeSession);
232 }
233 })
234 .catch((err: Response | any) => Observable.throw(err));
235 }
236
237 connect(dev: Device) {
Radek Krejcia6c8b412017-10-17 16:59:38 +0200238 let options = null; // = new RequestOptions({body: JSON.stringify({'id': dev.id})});
239 if (dev.id) {
240 options = new RequestOptions({body: JSON.stringify({'id': dev.id})});
241 } else {
242 options = new RequestOptions({body: JSON.stringify({'device': {'hostname': dev.hostname, 'port': dev.port, 'username': dev.username, 'password': dev.password}})});
243 }
Radek Krejci95bd14c2017-09-21 14:24:13 +0200244 return this.http.post('/netopeer/session', null, options)
245 .map((resp: Response) => resp.json())
246 .do(resp => {
247 if (resp['success']) {
Radek Krejcia1339602017-11-02 13:52:38 +0100248 this.sessions.push(new Session(resp['session-key'], dev));
Radek Krejci95bd14c2017-09-21 14:24:13 +0200249 this.activeSession = resp['session-key'];
Radek Krejci6e772b22018-01-25 13:28:57 +0100250 this.storeData();
Radek Krejci95bd14c2017-09-21 14:24:13 +0200251 localStorage.setItem('activeSession', this.activeSession);
252 }
253 })
254 .catch((err: Response | any) => Observable.throw(err))
255 }
256}