blob: fc850c468235223ce525e4a56f70397c81867201 [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
8import { Device } from '../inventory/device';
9import { Session } from './session';
10
11@Injectable()
Radek Krejci5a1571a2018-02-16 13:45:53 +010012export class SessionsService implements OnInit {
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 }
Radek Krejci5a1571a2018-02-16 13:45:53 +010021 }
22
23 ngOnInit(): void {
Radek Krejci95bd14c2017-09-21 14:24:13 +020024 this.checkSessions();
25 }
26
Radek Krejci77f77202017-11-03 15:33:50 +010027 storeData() {
28 localStorage.setItem('sessions', JSON.stringify(this.sessions));
29 }
30
Radek Krejci6e772b22018-01-25 13:28:57 +010031 loadData() {
32 this.sessions = JSON.parse(localStorage.getItem('sessions'));
33 }
34
Radek Krejci77f77202017-11-03 15:33:50 +010035 getActiveSession(key: string = this.activeSession): Session {
36 if (!key) {
Radek Krejci903f94e2017-10-20 10:53:26 +020037 return null;
38 }
39 for (let i = this.sessions.length; i > 0; i--) {
Radek Krejci77f77202017-11-03 15:33:50 +010040 if (this.sessions[i - 1].key == key) {
Radek Krejci903f94e2017-10-20 10:53:26 +020041 return this.sessions[i - 1];
42 }
43 }
44 return null;
45 }
46
Radek Krejci77f77202017-11-03 15:33:50 +010047 changeActiveSession(key: string): Session {
48 if (!this.activeSession) {
49 return null;
50 }
51 let result = this.getActiveSession(key);
52 if (result) {
53 this.activeSession = key;
54 localStorage.setItem('activeSession', this.activeSession);
55 }
56 return result;
Radek Krejci903f94e2017-10-20 10:53:26 +020057 }
58
Radek Krejci95bd14c2017-09-21 14:24:13 +020059 checkSessions() {
Radek Krejci6e772b22018-01-25 13:28:57 +010060 this.loadData();
Radek Krejci95bd14c2017-09-21 14:24:13 +020061 if (!this.sessions) {
62 this.sessions = [];
63 } else {
64 /* verify that the sessions are still active */
65 for (let i = this.sessions.length; i > 0; i--) {
66 this.alive(this.sessions[i - 1].key).subscribe(resp => {
67 if (!resp['success']) {
68 if (this.activeSession && this.sessions[i - 1].key == this.activeSession) {
69 /* active session is not alive - select new active session
70 * as the one on the left from the current one, if there
71 * is no one, choose the one on the right */
72 if (i > 1) {
73 this.activeSession = this.sessions[i - 2].key;
74 } else if (this.sessions.length > i) {
75 this.activeSession = this.sessions[i].key;
76 } else {
77 this.activeSession = "";
78 }
79 }
80 this.sessions.splice(i - 1, 1);
81 }
82 });
83 }
84 }
85 }
86
Radek Krejci4d3896c2018-01-08 17:10:43 +010087 checkValue(key: string, path: string, value: string): Observable<string[]> {
88 let params = new URLSearchParams();
89 params.set('key', key);
90 params.set('path', path);
91 params.set('value', value);
92 let options = new RequestOptions({ search: params });
Radek Krejci6e772b22018-01-25 13:28:57 +010093 return this.http.get('/netopeer/session/schema/checkvalue', options)
Radek Krejci4d3896c2018-01-08 17:10:43 +010094 .map((resp: Response) => resp.json())
95 .catch((err: Response | any) => Observable.throw(err));
96 }
97
Radek Krejci5a69fa32018-02-01 11:03:04 +010098 private filterSchemas(node, schemas) {
99 if (node['deleted'] || (node['info']['type'] & 0x18)) {
100 /* ignore deleted nodes and nodes that can be instantiated multiple times */
101 return;
102 }
103 for (let index in schemas) {
104 if (!schemas[index]['config'] ||
105 (schemas[index]['name'] == node['info']['name'] && schemas[index]['module'] == node['info']['module'])) {
106 /* 1. read-only node */
107 /* 2. the node is already instantiated */
108 schemas.splice(index, 1);
109 }
110 }
111 }
112
Radek Krejci6e772b22018-01-25 13:28:57 +0100113 childrenSchemas(key: string, path: string, node = null) {
114 let params = new URLSearchParams();
115 params.set('key', key);
116 params.set('path', path);
117 params.set('relative', 'children');
118 let options = new RequestOptions({ search: params });
119 return this.http.get('/netopeer/session/schema', options)
120 .map((resp: Response) => {
121 let result = resp.json();
122 console.log(result)
123 if (result['success'] && node) {
Radek Krejci5a69fa32018-02-01 11:03:04 +0100124 if ('children' in node) {
125 for (let iter of node['children']) {
126 this.filterSchemas(iter, result['data']);
Radek Krejci6e772b22018-01-25 13:28:57 +0100127 }
Radek Krejci5a69fa32018-02-01 11:03:04 +0100128 }
129 if ('newChildren' in node) {
130 for (let iter of node['newChildren']) {
131 this.filterSchemas(iter, result['data']);
Radek Krejci6e772b22018-01-25 13:28:57 +0100132 }
133 }
134 }
135 if (result['success']) {
136 return result['data'];
137 } else {
138 return [];
139 }
140 }).toPromise();
141 }
142
Radek Krejcid0ce4cf2018-02-09 14:44:34 +0100143 schemaValues(key: string, path: string) {
144 let params = new URLSearchParams();
145 params.set('key', key);
146 params.set('path', path);
147 let options = new RequestOptions({ search: params });
148 return this.http.get('/netopeer/session/schema/values', options)
149 .map((resp: Response) => resp.json()).toPromise();
150 }
151
Radek Krejci95bd14c2017-09-21 14:24:13 +0200152 alive(key: string): Observable<string[]> {
153 let params = new URLSearchParams();
154 params.set('key', key);
155 let options = new RequestOptions({ search: params });
156 return this.http.get('/netopeer/session/alive', options)
157 .map((resp: Response) => resp.json())
158 .catch((err: Response | any) => Observable.throw(err));
159 }
160
161 getCpblts(key: string): Observable<string[]> {
162 let params = new URLSearchParams();
163 params.set('key', key);
164 let options = new RequestOptions({ search: params });
165 return this.http.get('/netopeer/session/capabilities', options)
166 .map((resp: Response) => resp.json())
167 .catch((err: Response | any) => Observable.throw(err));
168 }
169
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100170 setDirty(node) {
171 let activeSession = this.getActiveSession();
172 if (!activeSession.modifications) {
173 return;
174 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100175
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100176 if (node['path'] in activeSession.modifications) {
177 node['dirty'] = true;
178 if (activeSession.modifications[node['path']]['type'] == 'change') {
179 activeSession.modifications[node['path']]['original'] = node['value'];
180 }
181 node['value'] = activeSession.modifications[node['path']]['value'];
182 }
183 /* recursion */
184 if ('children' in node) {
185 for (let child of node['children']) {
186 this.setDirty(child);
187 }
188 }
189 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100190
Radek Krejcia1339602017-11-02 13:52:38 +0100191 rpcGetSubtree(key: string, all: boolean, path: string = ""): Observable<string[]> {
Radek Krejci2e578562017-10-17 11:11:13 +0200192 let params = new URLSearchParams();
193 params.set('key', key);
Radek Krejcia1339602017-11-02 13:52:38 +0100194 params.set('recursive', String(all));
195 if (path.length) {
196 params.set('path', path);
197 }
Radek Krejci2e578562017-10-17 11:11:13 +0200198 let options = new RequestOptions({ search: params });
199 return this.http.get('/netopeer/session/rpcGet', options)
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100200 .map((resp: Response) => {
Radek Krejcif71867f2018-01-30 13:28:28 +0100201 //console.log(resp);
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100202 return resp.json();
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100203 })
Radek Krejci2e578562017-10-17 11:11:13 +0200204 .catch((err: Response | any) => Observable.throw(err));
205 }
206
Radek Krejci62fec6a2018-02-06 10:24:09 +0100207 commit(key: string) {
208 let activeSession = this.getActiveSession(key);
209 let options = new RequestOptions({body: JSON.stringify({'key': key, 'modifications': activeSession.modifications})});
210 return this.http.post('/netopeer/session/commit', null, options)
211 .map((resp: Response) => resp.json()).toPromise();
212 }
213
Radek Krejci95bd14c2017-09-21 14:24:13 +0200214 close(key: string) {
215 let params = new URLSearchParams();
216 params.set('key', key);
217 let options = new RequestOptions({search: params});
218 return this.http.delete('/netopeer/session', options)
219 .map((resp: Response) => resp.json())
220 .do(resp => {
221 if (resp['success']) {
Radek Krejcicc355612018-02-14 08:55:01 +0100222 let index = this.sessions.findIndex((s: Session) => s.key == key);
223 this.sessions.splice(index, 1);
Radek Krejci95bd14c2017-09-21 14:24:13 +0200224 if (key == this.activeSession) {
Radek Krejcicc355612018-02-14 08:55:01 +0100225 if (index > 0) {
226 this.activeSession = this.sessions[index - 1].key;
227 } else if (this.sessions.length) {
Radek Krejci95bd14c2017-09-21 14:24:13 +0200228 this.activeSession = this.sessions[0].key;
229 } else {
230 this.activeSession = ""
231 }
232 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100233 this.storeData();
Radek Krejci95bd14c2017-09-21 14:24:13 +0200234 localStorage.setItem('activeSession', this.activeSession);
235 }
236 })
237 .catch((err: Response | any) => Observable.throw(err));
238 }
239
240 connect(dev: Device) {
Radek Krejcia6c8b412017-10-17 16:59:38 +0200241 let options = null; // = new RequestOptions({body: JSON.stringify({'id': dev.id})});
242 if (dev.id) {
243 options = new RequestOptions({body: JSON.stringify({'id': dev.id})});
244 } else {
245 options = new RequestOptions({body: JSON.stringify({'device': {'hostname': dev.hostname, 'port': dev.port, 'username': dev.username, 'password': dev.password}})});
246 }
Radek Krejci95bd14c2017-09-21 14:24:13 +0200247 return this.http.post('/netopeer/session', null, options)
248 .map((resp: Response) => resp.json())
249 .do(resp => {
250 if (resp['success']) {
Radek Krejcia1339602017-11-02 13:52:38 +0100251 this.sessions.push(new Session(resp['session-key'], dev));
Radek Krejci95bd14c2017-09-21 14:24:13 +0200252 this.activeSession = resp['session-key'];
Radek Krejci6e772b22018-01-25 13:28:57 +0100253 this.storeData();
Radek Krejci95bd14c2017-09-21 14:24:13 +0200254 localStorage.setItem('activeSession', this.activeSession);
255 }
256 })
257 .catch((err: Response | any) => Observable.throw(err))
258 }
259}