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 | |
| 8 | import { Device } from '../inventory/device'; |
| 9 | import { Session } from './session'; |
| 10 | |
| 11 | @Injectable() |
Radek Krejci | 5a1571a | 2018-02-16 13:45:53 +0100 | [diff] [blame^] | 12 | export class SessionsService implements OnInit { |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 13 | 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 Krejci | 5a1571a | 2018-02-16 13:45:53 +0100 | [diff] [blame^] | 21 | } |
| 22 | |
| 23 | ngOnInit(): void { |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 24 | this.checkSessions(); |
| 25 | } |
| 26 | |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 27 | storeData() { |
| 28 | localStorage.setItem('sessions', JSON.stringify(this.sessions)); |
| 29 | } |
| 30 | |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 31 | loadData() { |
| 32 | this.sessions = JSON.parse(localStorage.getItem('sessions')); |
| 33 | } |
| 34 | |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 35 | getActiveSession(key: string = this.activeSession): Session { |
| 36 | if (!key) { |
Radek Krejci | 903f94e | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 37 | return null; |
| 38 | } |
| 39 | for (let i = this.sessions.length; i > 0; i--) { |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 40 | if (this.sessions[i - 1].key == key) { |
Radek Krejci | 903f94e | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 41 | return this.sessions[i - 1]; |
| 42 | } |
| 43 | } |
| 44 | return null; |
| 45 | } |
| 46 | |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 47 | 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 Krejci | 903f94e | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 59 | checkSessions() { |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 60 | this.loadData(); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 61 | 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 Krejci | 4d3896c | 2018-01-08 17:10:43 +0100 | [diff] [blame] | 87 | 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 Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 93 | return this.http.get('/netopeer/session/schema/checkvalue', options) |
Radek Krejci | 4d3896c | 2018-01-08 17:10:43 +0100 | [diff] [blame] | 94 | .map((resp: Response) => resp.json()) |
| 95 | .catch((err: Response | any) => Observable.throw(err)); |
| 96 | } |
| 97 | |
Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 98 | 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 Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 113 | 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 Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 124 | if ('children' in node) { |
| 125 | for (let iter of node['children']) { |
| 126 | this.filterSchemas(iter, result['data']); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 127 | } |
Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 128 | } |
| 129 | if ('newChildren' in node) { |
| 130 | for (let iter of node['newChildren']) { |
| 131 | this.filterSchemas(iter, result['data']); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
| 135 | if (result['success']) { |
| 136 | return result['data']; |
| 137 | } else { |
| 138 | return []; |
| 139 | } |
| 140 | }).toPromise(); |
| 141 | } |
| 142 | |
Radek Krejci | d0ce4cf | 2018-02-09 14:44:34 +0100 | [diff] [blame] | 143 | 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 Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 152 | 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 Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 170 | setDirty(node) { |
| 171 | let activeSession = this.getActiveSession(); |
| 172 | if (!activeSession.modifications) { |
| 173 | return; |
| 174 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 175 | |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 176 | 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 Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 190 | |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 191 | rpcGetSubtree(key: string, all: boolean, path: string = ""): Observable<string[]> { |
Radek Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 192 | let params = new URLSearchParams(); |
| 193 | params.set('key', key); |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 194 | params.set('recursive', String(all)); |
| 195 | if (path.length) { |
| 196 | params.set('path', path); |
| 197 | } |
Radek Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 198 | let options = new RequestOptions({ search: params }); |
| 199 | return this.http.get('/netopeer/session/rpcGet', options) |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 200 | .map((resp: Response) => { |
Radek Krejci | f71867f | 2018-01-30 13:28:28 +0100 | [diff] [blame] | 201 | //console.log(resp); |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 202 | return resp.json(); |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 203 | }) |
Radek Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 204 | .catch((err: Response | any) => Observable.throw(err)); |
| 205 | } |
| 206 | |
Radek Krejci | 62fec6a | 2018-02-06 10:24:09 +0100 | [diff] [blame] | 207 | 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 Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 214 | 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 Krejci | cc35561 | 2018-02-14 08:55:01 +0100 | [diff] [blame] | 222 | let index = this.sessions.findIndex((s: Session) => s.key == key); |
| 223 | this.sessions.splice(index, 1); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 224 | if (key == this.activeSession) { |
Radek Krejci | cc35561 | 2018-02-14 08:55:01 +0100 | [diff] [blame] | 225 | if (index > 0) { |
| 226 | this.activeSession = this.sessions[index - 1].key; |
| 227 | } else if (this.sessions.length) { |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 228 | this.activeSession = this.sessions[0].key; |
| 229 | } else { |
| 230 | this.activeSession = "" |
| 231 | } |
| 232 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 233 | this.storeData(); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 234 | localStorage.setItem('activeSession', this.activeSession); |
| 235 | } |
| 236 | }) |
| 237 | .catch((err: Response | any) => Observable.throw(err)); |
| 238 | } |
| 239 | |
| 240 | connect(dev: Device) { |
Radek Krejci | a6c8b41 | 2017-10-17 16:59:38 +0200 | [diff] [blame] | 241 | 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 Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 247 | return this.http.post('/netopeer/session', null, options) |
| 248 | .map((resp: Response) => resp.json()) |
| 249 | .do(resp => { |
| 250 | if (resp['success']) { |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 251 | this.sessions.push(new Session(resp['session-key'], dev)); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 252 | this.activeSession = resp['session-key']; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 253 | this.storeData(); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 254 | localStorage.setItem('activeSession', this.activeSession); |
| 255 | } |
| 256 | }) |
| 257 | .catch((err: Response | any) => Observable.throw(err)) |
| 258 | } |
| 259 | } |