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() |
| 12 | export class SessionsService { |
| 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 | |
| 24 | checkSessions() { |
| 25 | this.sessions = JSON.parse(localStorage.getItem('sessions')); |
| 26 | if (!this.sessions) { |
| 27 | this.sessions = []; |
| 28 | } else { |
| 29 | /* verify that the sessions are still active */ |
| 30 | for (let i = this.sessions.length; i > 0; i--) { |
| 31 | this.alive(this.sessions[i - 1].key).subscribe(resp => { |
| 32 | if (!resp['success']) { |
| 33 | if (this.activeSession && this.sessions[i - 1].key == this.activeSession) { |
| 34 | /* active session is not alive - select new active session |
| 35 | * as the one on the left from the current one, if there |
| 36 | * is no one, choose the one on the right */ |
| 37 | if (i > 1) { |
| 38 | this.activeSession = this.sessions[i - 2].key; |
| 39 | } else if (this.sessions.length > i) { |
| 40 | this.activeSession = this.sessions[i].key; |
| 41 | } else { |
| 42 | this.activeSession = ""; |
| 43 | } |
| 44 | } |
| 45 | this.sessions.splice(i - 1, 1); |
| 46 | } |
| 47 | }); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | alive(key: string): Observable<string[]> { |
| 53 | let params = new URLSearchParams(); |
| 54 | params.set('key', key); |
| 55 | let options = new RequestOptions({ search: params }); |
| 56 | return this.http.get('/netopeer/session/alive', options) |
| 57 | .map((resp: Response) => resp.json()) |
| 58 | .catch((err: Response | any) => Observable.throw(err)); |
| 59 | } |
| 60 | |
| 61 | getCpblts(key: string): Observable<string[]> { |
| 62 | let params = new URLSearchParams(); |
| 63 | params.set('key', key); |
| 64 | let options = new RequestOptions({ search: params }); |
| 65 | return this.http.get('/netopeer/session/capabilities', options) |
| 66 | .map((resp: Response) => resp.json()) |
| 67 | .catch((err: Response | any) => Observable.throw(err)); |
| 68 | } |
| 69 | |
Radek Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 70 | rpcGet(key: string): Observable<string[]> { |
| 71 | let params = new URLSearchParams(); |
| 72 | params.set('key', key); |
| 73 | let options = new RequestOptions({ search: params }); |
| 74 | return this.http.get('/netopeer/session/rpcGet', options) |
| 75 | .map((resp: Response) => resp.json()) |
| 76 | .catch((err: Response | any) => Observable.throw(err)); |
| 77 | } |
| 78 | |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 79 | close(key: string) { |
| 80 | let params = new URLSearchParams(); |
| 81 | params.set('key', key); |
| 82 | let options = new RequestOptions({search: params}); |
| 83 | return this.http.delete('/netopeer/session', options) |
| 84 | .map((resp: Response) => resp.json()) |
| 85 | .do(resp => { |
| 86 | if (resp['success']) { |
| 87 | this.sessions.splice(this.sessions.findIndex((s: Session) => s.key == key), 1); |
| 88 | if (key == this.activeSession) { |
| 89 | if (this.sessions.length) { |
| 90 | this.activeSession = this.sessions[0].key; |
| 91 | } else { |
| 92 | this.activeSession = "" |
| 93 | } |
| 94 | } |
| 95 | localStorage.setItem('sessions', JSON.stringify(this.sessions)); |
| 96 | localStorage.setItem('activeSession', this.activeSession); |
| 97 | } |
| 98 | }) |
| 99 | .catch((err: Response | any) => Observable.throw(err)); |
| 100 | } |
| 101 | |
| 102 | connect(dev: Device) { |
Radek Krejci | a6c8b41 | 2017-10-17 16:59:38 +0200 | [diff] [blame^] | 103 | let options = null; // = new RequestOptions({body: JSON.stringify({'id': dev.id})}); |
| 104 | if (dev.id) { |
| 105 | options = new RequestOptions({body: JSON.stringify({'id': dev.id})}); |
| 106 | } else { |
| 107 | options = new RequestOptions({body: JSON.stringify({'device': {'hostname': dev.hostname, 'port': dev.port, 'username': dev.username, 'password': dev.password}})}); |
| 108 | } |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 109 | return this.http.post('/netopeer/session', null, options) |
| 110 | .map((resp: Response) => resp.json()) |
| 111 | .do(resp => { |
| 112 | if (resp['success']) { |
| 113 | this.sessions.push(new Session(resp['session-key'], dev)); |
| 114 | this.activeSession = resp['session-key']; |
| 115 | localStorage.setItem('sessions', JSON.stringify(this.sessions)); |
| 116 | localStorage.setItem('activeSession', this.activeSession); |
| 117 | } |
| 118 | }) |
| 119 | .catch((err: Response | any) => Observable.throw(err)) |
| 120 | } |
| 121 | } |