Radek Krejci | 5a1571a | 2018-02-16 13:45:53 +0100 | [diff] [blame] | 1 | import { Injectable, OnInit } from '@angular/core'; |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 2 | import { Http, Response, RequestOptions, URLSearchParams } from '@angular/http'; |
| 3 | import { Observable } from 'rxjs/Observable'; |
| 4 | import 'rxjs/add/operator/catch'; |
| 5 | import 'rxjs/add/operator/map'; |
| 6 | import 'rxjs/add/operator/do'; |
| 7 | |
Radek Krejci | 30ce159 | 2018-03-01 14:44:14 +0100 | [diff] [blame] | 8 | import { TreeService } from './tree.service'; |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 9 | import { Device } from '../inventory/device'; |
| 10 | import { Session } from './session'; |
| 11 | |
| 12 | @Injectable() |
Radek Krejci | 5a1571a | 2018-02-16 13:45:53 +0100 | [diff] [blame] | 13 | export class SessionsService implements OnInit { |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 14 | public sessions: Session[]; |
Radek Krejci | 30ce159 | 2018-03-01 14:44:14 +0100 | [diff] [blame] | 15 | public activeSession: string; |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 16 | |
Radek Krejci | 30ce159 | 2018-03-01 14:44:14 +0100 | [diff] [blame] | 17 | constructor(private http: Http, private treeService: TreeService) { |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 18 | this.activeSession = localStorage.getItem('activeSession'); |
| 19 | if (!this.activeSession) { |
| 20 | this.activeSession = ""; |
| 21 | } |
Radek Krejci | 5a1571a | 2018-02-16 13:45:53 +0100 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | ngOnInit(): void { |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 25 | this.checkSessions(); |
| 26 | } |
| 27 | |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 28 | storeData() { |
| 29 | localStorage.setItem('sessions', JSON.stringify(this.sessions)); |
| 30 | } |
| 31 | |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 32 | loadData() { |
| 33 | this.sessions = JSON.parse(localStorage.getItem('sessions')); |
Radek Krejci | f23bf73 | 2018-03-09 11:34:56 +0100 | [diff] [blame] | 34 | if (!this.sessions) { |
| 35 | this.sessions = []; |
| 36 | } |
Radek Krejci | 010475d | 2018-03-08 13:14:19 +0100 | [diff] [blame] | 37 | 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 Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 47 | getActiveSession(key: string = this.activeSession): Session { |
Radek Krejci | 6a2282f | 2018-02-19 13:01:23 +0100 | [diff] [blame] | 48 | 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 Krejci | 903f94e | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | return null; |
| 56 | } |
| 57 | |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 58 | 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 Krejci | 903f94e | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Radek Krejci | 010475d | 2018-03-08 13:14:19 +0100 | [diff] [blame] | 70 | 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 Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 101 | checkSessions() { |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 102 | this.loadData(); |
Radek Krejci | f23bf73 | 2018-03-09 11:34:56 +0100 | [diff] [blame] | 103 | /* verify that the sessions are still active */ |
| 104 | for (let i = this.sessions.length; i > 0; i--) { |
| 105 | this.checkSessionIndex(i - 1); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Radek Krejci | 30ce159 | 2018-03-01 14:44:14 +0100 | [diff] [blame] | 109 | 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 Krejci | 4d3896c | 2018-01-08 17:10:43 +0100 | [diff] [blame] | 138 | 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 Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 144 | return this.http.get('/netopeer/session/schema/checkvalue', options) |
Radek Krejci | 4d3896c | 2018-01-08 17:10:43 +0100 | [diff] [blame] | 145 | .map((resp: Response) => resp.json()) |
| 146 | .catch((err: Response | any) => Observable.throw(err)); |
| 147 | } |
| 148 | |
Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 149 | 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 Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 164 | 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 Krejci | 010475d | 2018-03-08 13:14:19 +0100 | [diff] [blame] | 173 | //console.log(result) |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 174 | if (result['success'] && node) { |
Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 175 | if ('children' in node) { |
| 176 | for (let iter of node['children']) { |
| 177 | this.filterSchemas(iter, result['data']); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 178 | } |
Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 179 | } |
| 180 | if ('newChildren' in node) { |
| 181 | for (let iter of node['newChildren']) { |
| 182 | this.filterSchemas(iter, result['data']); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | } |
| 186 | if (result['success']) { |
| 187 | return result['data']; |
| 188 | } else { |
| 189 | return []; |
| 190 | } |
| 191 | }).toPromise(); |
| 192 | } |
| 193 | |
Radek Krejci | d0ce4cf | 2018-02-09 14:44:34 +0100 | [diff] [blame] | 194 | 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 Krejci | 6a2282f | 2018-02-19 13:01:23 +0100 | [diff] [blame] | 203 | alive(key: string): Promise<string[]> { |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 204 | 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 Krejci | 6a2282f | 2018-02-19 13:01:23 +0100 | [diff] [blame] | 208 | .map((resp: Response) => resp.json()).toPromise(); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 209 | } |
| 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 Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 220 | setDirty(node) { |
| 221 | let activeSession = this.getActiveSession(); |
| 222 | if (!activeSession.modifications) { |
| 223 | return; |
| 224 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 225 | |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 226 | 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 Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 240 | |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 241 | rpcGetSubtree(key: string, all: boolean, path: string = ""): Observable<string[]> { |
Radek Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 242 | let params = new URLSearchParams(); |
| 243 | params.set('key', key); |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 244 | params.set('recursive', String(all)); |
| 245 | if (path.length) { |
| 246 | params.set('path', path); |
| 247 | } |
Radek Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 248 | let options = new RequestOptions({ search: params }); |
| 249 | return this.http.get('/netopeer/session/rpcGet', options) |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 250 | .map((resp: Response) => { |
Radek Krejci | 010475d | 2018-03-08 13:14:19 +0100 | [diff] [blame] | 251 | console.log(resp); |
| 252 | let result = resp.json(); |
| 253 | if (!result['success']) { |
| 254 | this.checkSession(key); |
| 255 | } |
| 256 | return result; |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 257 | }) |
Radek Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 258 | .catch((err: Response | any) => Observable.throw(err)); |
| 259 | } |
Radek Krejci | 30ce159 | 2018-03-01 14:44:14 +0100 | [diff] [blame] | 260 | 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 Krejci | 010475d | 2018-03-08 13:14:19 +0100 | [diff] [blame] | 277 | activeSession.data['info']['config'] = true; |
Radek Krejci | 30ce159 | 2018-03-01 14:44:14 +0100 | [diff] [blame] | 278 | 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 Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 290 | |
Radek Krejci | 62fec6a | 2018-02-06 10:24:09 +0100 | [diff] [blame] | 291 | 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 Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 298 | 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 Krejci | cc35561 | 2018-02-14 08:55:01 +0100 | [diff] [blame] | 306 | let index = this.sessions.findIndex((s: Session) => s.key == key); |
| 307 | this.sessions.splice(index, 1); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 308 | if (key == this.activeSession) { |
Radek Krejci | cc35561 | 2018-02-14 08:55:01 +0100 | [diff] [blame] | 309 | if (index > 0) { |
| 310 | this.activeSession = this.sessions[index - 1].key; |
| 311 | } else if (this.sessions.length) { |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 312 | this.activeSession = this.sessions[0].key; |
| 313 | } else { |
| 314 | this.activeSession = "" |
| 315 | } |
| 316 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 317 | this.storeData(); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 318 | localStorage.setItem('activeSession', this.activeSession); |
| 319 | } |
| 320 | }) |
| 321 | .catch((err: Response | any) => Observable.throw(err)); |
| 322 | } |
| 323 | |
| 324 | connect(dev: Device) { |
Radek Krejci | a6c8b41 | 2017-10-17 16:59:38 +0200 | [diff] [blame] | 325 | 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 Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 331 | return this.http.post('/netopeer/session', null, options) |
| 332 | .map((resp: Response) => resp.json()) |
| 333 | .do(resp => { |
| 334 | if (resp['success']) { |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 335 | this.sessions.push(new Session(resp['session-key'], dev)); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 336 | this.activeSession = resp['session-key']; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 337 | this.storeData(); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 338 | localStorage.setItem('activeSession', this.activeSession); |
| 339 | } |
| 340 | }) |
| 341 | .catch((err: Response | any) => Observable.throw(err)) |
| 342 | } |
| 343 | } |