blob: 68fa136473038a105cfffb2994ebcadb805cb2ba [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 Krejci6e772b22018-01-25 13:28:57 +010084 createModificationsRecord(path) {
85 let activeSession = this.getActiveSession();
86 if (!activeSession.modifications) {
87 activeSession.modifications = {};
88 }
89
90 if (!(path in activeSession.modifications)) {
91 activeSession.modifications[path] = {};
92 }
93 return activeSession.modifications[path];
94 }
95
96 getModificationsRecord(path) {
97 let activeSession = this.getActiveSession();
98 if (!activeSession.modifications) {
99 return null;
100 }
101
102 if (!(path in activeSession.modifications)) {
103 return null;
104 }
105 return activeSession.modifications[path];
106 }
107
108 removeModificationsRecord(path = null) {
109 let activeSession = this.getActiveSession();
110 if (!activeSession.modifications) {
111 return;
112 }
113
114 if (path && (path in activeSession.modifications)) {
115 delete activeSession.modifications[path];
116 }
117
118 if (!Object.keys(activeSession.modifications).length) {
119 delete activeSession.modifications;
120 }
121 }
122
Radek Krejci4d3896c2018-01-08 17:10:43 +0100123 checkValue(key: string, path: string, value: string): Observable<string[]> {
124 let params = new URLSearchParams();
125 params.set('key', key);
126 params.set('path', path);
127 params.set('value', value);
128 let options = new RequestOptions({ search: params });
Radek Krejci6e772b22018-01-25 13:28:57 +0100129 return this.http.get('/netopeer/session/schema/checkvalue', options)
Radek Krejci4d3896c2018-01-08 17:10:43 +0100130 .map((resp: Response) => resp.json())
131 .catch((err: Response | any) => Observable.throw(err));
132 }
133
Radek Krejci6e772b22018-01-25 13:28:57 +0100134 childrenSchemas(key: string, path: string, node = null) {
135 let params = new URLSearchParams();
136 params.set('key', key);
137 params.set('path', path);
138 params.set('relative', 'children');
139 let options = new RequestOptions({ search: params });
140 return this.http.get('/netopeer/session/schema', options)
141 .map((resp: Response) => {
142 let result = resp.json();
143 console.log(result)
144 if (result['success'] && node) {
145 for (let iter of node['children']) {
146 if (iter['deleted'] || (iter['info']['type'] & 0x18)) {
147 /* ignore deleted nodes and nodes that can be instantiated multiple times */
148 continue;
149 }
150 for (let index in result['data']) {
151 if (!result['data'][index]['config'] ||
152 (result['data'][index]['name'] == iter['info']['name'] && result['data'][index]['module'] == iter['info']['module'])) {
153 /* 1. read-only node */
154 /* 2. the node is already instantiated */
155 result['data'].splice(index, 1);
156 break;
157 }
158 }
159 }
160 }
161 if (result['success']) {
162 return result['data'];
163 } else {
164 return [];
165 }
166 }).toPromise();
167 }
168
Radek Krejci95bd14c2017-09-21 14:24:13 +0200169 alive(key: string): Observable<string[]> {
170 let params = new URLSearchParams();
171 params.set('key', key);
172 let options = new RequestOptions({ search: params });
173 return this.http.get('/netopeer/session/alive', options)
174 .map((resp: Response) => resp.json())
175 .catch((err: Response | any) => Observable.throw(err));
176 }
177
178 getCpblts(key: string): Observable<string[]> {
179 let params = new URLSearchParams();
180 params.set('key', key);
181 let options = new RequestOptions({ search: params });
182 return this.http.get('/netopeer/session/capabilities', options)
183 .map((resp: Response) => resp.json())
184 .catch((err: Response | any) => Observable.throw(err));
185 }
186
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100187 setDirty(node) {
188 let activeSession = this.getActiveSession();
189 if (!activeSession.modifications) {
190 return;
191 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100192
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100193 if (node['path'] in activeSession.modifications) {
194 node['dirty'] = true;
195 if (activeSession.modifications[node['path']]['type'] == 'change') {
196 activeSession.modifications[node['path']]['original'] = node['value'];
197 }
198 node['value'] = activeSession.modifications[node['path']]['value'];
199 }
200 /* recursion */
201 if ('children' in node) {
202 for (let child of node['children']) {
203 this.setDirty(child);
204 }
205 }
206 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100207
Radek Krejcia1339602017-11-02 13:52:38 +0100208 rpcGetSubtree(key: string, all: boolean, path: string = ""): Observable<string[]> {
Radek Krejci2e578562017-10-17 11:11:13 +0200209 let params = new URLSearchParams();
210 params.set('key', key);
Radek Krejcia1339602017-11-02 13:52:38 +0100211 params.set('recursive', String(all));
212 if (path.length) {
213 params.set('path', path);
214 }
Radek Krejci2e578562017-10-17 11:11:13 +0200215 let options = new RequestOptions({ search: params });
216 return this.http.get('/netopeer/session/rpcGet', options)
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100217 .map((resp: Response) => {
Radek Krejcif71867f2018-01-30 13:28:28 +0100218 //console.log(resp);
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100219 let result = resp.json();
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100220 if (result['success']) {
221 if (path.length) {
222 for (let iter of result['data']['children']) {
223 this.setDirty(iter);
224 }
225 } else {
226 for (let iter of result['data']) {
227 this.setDirty(iter);
228 }
229 }
230 }
231 return result;
232 })
Radek Krejci2e578562017-10-17 11:11:13 +0200233 .catch((err: Response | any) => Observable.throw(err));
234 }
235
Radek Krejci95bd14c2017-09-21 14:24:13 +0200236 close(key: string) {
237 let params = new URLSearchParams();
238 params.set('key', key);
239 let options = new RequestOptions({search: params});
240 return this.http.delete('/netopeer/session', options)
241 .map((resp: Response) => resp.json())
242 .do(resp => {
243 if (resp['success']) {
244 this.sessions.splice(this.sessions.findIndex((s: Session) => s.key == key), 1);
245 if (key == this.activeSession) {
246 if (this.sessions.length) {
247 this.activeSession = this.sessions[0].key;
248 } else {
249 this.activeSession = ""
250 }
251 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100252 this.storeData();
Radek Krejci95bd14c2017-09-21 14:24:13 +0200253 localStorage.setItem('activeSession', this.activeSession);
254 }
255 })
256 .catch((err: Response | any) => Observable.throw(err));
257 }
258
259 connect(dev: Device) {
Radek Krejcia6c8b412017-10-17 16:59:38 +0200260 let options = null; // = new RequestOptions({body: JSON.stringify({'id': dev.id})});
261 if (dev.id) {
262 options = new RequestOptions({body: JSON.stringify({'id': dev.id})});
263 } else {
264 options = new RequestOptions({body: JSON.stringify({'device': {'hostname': dev.hostname, 'port': dev.port, 'username': dev.username, 'password': dev.password}})});
265 }
Radek Krejci95bd14c2017-09-21 14:24:13 +0200266 return this.http.post('/netopeer/session', null, options)
267 .map((resp: Response) => resp.json())
268 .do(resp => {
269 if (resp['success']) {
Radek Krejcia1339602017-11-02 13:52:38 +0100270 this.sessions.push(new Session(resp['session-key'], dev));
Radek Krejci95bd14c2017-09-21 14:24:13 +0200271 this.activeSession = resp['session-key'];
Radek Krejci6e772b22018-01-25 13:28:57 +0100272 this.storeData();
Radek Krejci95bd14c2017-09-21 14:24:13 +0200273 localStorage.setItem('activeSession', this.activeSession);
274 }
275 })
276 .catch((err: Response | any) => Observable.throw(err))
277 }
278}