Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 1 | import { Injectable } from '@angular/core'; |
| 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 | 903f94e | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 12 | export class SessionsService{ |
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 | } |
| 21 | this.checkSessions(); |
| 22 | } |
| 23 | |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 24 | storeData() { |
| 25 | localStorage.setItem('sessions', JSON.stringify(this.sessions)); |
| 26 | } |
| 27 | |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 28 | loadData() { |
| 29 | this.sessions = JSON.parse(localStorage.getItem('sessions')); |
| 30 | } |
| 31 | |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 32 | getActiveSession(key: string = this.activeSession): Session { |
| 33 | if (!key) { |
Radek Krejci | 903f94e | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 34 | return null; |
| 35 | } |
| 36 | for (let i = this.sessions.length; i > 0; i--) { |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 37 | if (this.sessions[i - 1].key == key) { |
Radek Krejci | 903f94e | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 38 | return this.sessions[i - 1]; |
| 39 | } |
| 40 | } |
| 41 | return null; |
| 42 | } |
| 43 | |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 44 | 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 Krejci | 903f94e | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 56 | checkSessions() { |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 57 | this.loadData(); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 58 | 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 Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 84 | 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 Krejci | 4d3896c | 2018-01-08 17:10:43 +0100 | [diff] [blame] | 123 | 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 Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 129 | return this.http.get('/netopeer/session/schema/checkvalue', options) |
Radek Krejci | 4d3896c | 2018-01-08 17:10:43 +0100 | [diff] [blame] | 130 | .map((resp: Response) => resp.json()) |
| 131 | .catch((err: Response | any) => Observable.throw(err)); |
| 132 | } |
| 133 | |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 134 | 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 Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 169 | 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 Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 187 | setDirty(node) { |
| 188 | let activeSession = this.getActiveSession(); |
| 189 | if (!activeSession.modifications) { |
| 190 | return; |
| 191 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 192 | |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 193 | 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 Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 207 | |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 208 | rpcGetSubtree(key: string, all: boolean, path: string = ""): Observable<string[]> { |
Radek Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 209 | let params = new URLSearchParams(); |
| 210 | params.set('key', key); |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 211 | params.set('recursive', String(all)); |
| 212 | if (path.length) { |
| 213 | params.set('path', path); |
| 214 | } |
Radek Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 215 | let options = new RequestOptions({ search: params }); |
| 216 | return this.http.get('/netopeer/session/rpcGet', options) |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 217 | .map((resp: Response) => { |
Radek Krejci | f71867f | 2018-01-30 13:28:28 +0100 | [diff] [blame] | 218 | //console.log(resp); |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 219 | let result = resp.json(); |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 220 | 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 Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 233 | .catch((err: Response | any) => Observable.throw(err)); |
| 234 | } |
| 235 | |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 236 | 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 Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 252 | this.storeData(); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 253 | localStorage.setItem('activeSession', this.activeSession); |
| 254 | } |
| 255 | }) |
| 256 | .catch((err: Response | any) => Observable.throw(err)); |
| 257 | } |
| 258 | |
| 259 | connect(dev: Device) { |
Radek Krejci | a6c8b41 | 2017-10-17 16:59:38 +0200 | [diff] [blame] | 260 | 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 Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 266 | return this.http.post('/netopeer/session', null, options) |
| 267 | .map((resp: Response) => resp.json()) |
| 268 | .do(resp => { |
| 269 | if (resp['success']) { |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 270 | this.sessions.push(new Session(resp['session-key'], dev)); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 271 | this.activeSession = resp['session-key']; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 272 | this.storeData(); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 273 | localStorage.setItem('activeSession', this.activeSession); |
| 274 | } |
| 275 | }) |
| 276 | .catch((err: Response | any) => Observable.throw(err)) |
| 277 | } |
| 278 | } |