import { Injectable, EventEmitter } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Observable } from 'rxjs'; import { PluginDescription, State, Slide, ServiceItem, MainView, SystemInformation, Credentials, AuthToken } from './responses'; import { environment } from '../environments/environment'; const deserialize = (json, cls) => { const inst = new cls(); for (const p in json) { if (!json.hasOwnProperty(p)) { continue; } inst[p] = json[p]; } return inst; }; const httpOptions = { headers: new HttpHeaders({'Content-Type': 'application/json'}) }; @Injectable() export class OpenLPService { private apiURL: string; public stateChanged$: EventEmitter; constructor(private http: HttpClient) { const host = window.location.hostname; let port: string; if (environment.production) { port = window.location.port; } else { port = '4316'; } this.apiURL = `http://${host}:${port}/api/v1`; this.stateChanged$ = new EventEmitter(); this.retrieveSystemInformation().subscribe(info => { const ws = new WebSocket(`ws://${host}:${info.websocket_port}/state`); ws.onmessage = (event) => { const reader = new FileReader(); reader.onload = () => { const state = deserialize(JSON.parse(reader.result as string).results, State); this.stateChanged$.emit(state); }; reader.readAsText(event.data); }; }); } setAuthToken(token: string): void { httpOptions.headers = httpOptions.headers.set('Authorization', token); } retrieveSystemInformation(): Observable { return this.http.get(`${this.apiURL}/core/system`, httpOptions); } getMainImage(): Observable { return this.http.get(`${this.apiURL}/core/live-image`, httpOptions); } getItemSlides(): Observable { return this.http.get(`${this.apiURL}/controller/live-item`, httpOptions); } getServiceItems(): Observable { return this.http.get(`${this.apiURL}/service/list`, httpOptions); } getSearchablePlugins(): Observable { return this.http.get(`${this.apiURL}/core/plugins`, httpOptions); } setServiceItem(id: number): Observable { return this.http.get(`${this.apiURL}/service/set?id=${id}`); } search(plugin, text): Observable { return this.http.get(`${this.apiURL}/plugins/${plugin}/search?text=${text}`, httpOptions); } setSlide(id): Observable { return this.http.post(`${this.apiURL}/controller/show`, {'id': id}, httpOptions); } nextItem(): Observable { return this.http.post(`${this.apiURL}/service/progress`, {'action': 'next'}, httpOptions); } previousItem(): Observable { return this.http.post(`${this.apiURL}/service/progress`, {'action': 'previous'}, httpOptions); } nextSlide(): Observable { return this.http.post(`${this.apiURL}/controller/progress`, {'action': 'next'}, httpOptions); } previousSlide(): Observable { return this.http.post(`${this.apiURL}/controller/progress`, {'action': 'previous'}, httpOptions); } blankDisplay(): Observable { return this.http.post(`${this.apiURL}/core/display`, {'display': 'blank'}, httpOptions); } themeDisplay(): Observable { return this.http.post(`${this.apiURL}/core/display`, {'display': 'theme'}, httpOptions); } desktopDisplay(): Observable { return this.http.post(`${this.apiURL}/core/display`, {'display': 'desktop'}, httpOptions); } showDisplay(): Observable { return this.http.post(`${this.apiURL}/core/display`, {'display': 'show'}, httpOptions); } showAlert(text): Observable { return this.http.post(`${this.apiURL}/plugins/alerts`, {'text': text}, httpOptions); } sendItemLive(plugin, id): Observable { return this.http.post(`${this.apiURL}/plugins/${plugin}/live`, {'id': id}, httpOptions); } addItemToService(plugin, id): Observable { return this.http.post(`${this.apiURL}/plugins/${plugin}/add`, {'id': id}, httpOptions); } login(credentials: Credentials): Observable { return this.http.post(`${this.apiURL}/core/login`, credentials, httpOptions); } }