import { Injectable, EventEmitter } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Observable } from 'rxjs'; import { PluginDescription, State, Slide, ServiceItem, Theme, 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', 'Access-Control-Allow-Origin': '*' }) }; @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/v2`; this.stateChanged$ = new EventEmitter(); this.retrieveSystemInformation().subscribe(info => { const ws = new WebSocket(`ws://${host}:${info.websocket_port}`); 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); } getSearchablePlugins(): Observable { return this.http.get(`${this.apiURL}/core/plugins`, httpOptions); } search(plugin, text): Observable { return this.http.get(`${this.apiURL}/plugins/${plugin}/search?text=${text}`, httpOptions); } getSearchOptions(plugin): Observable { return this.http.get(`${this.apiURL}/plugins/${plugin}/search-options`, httpOptions); } setSearchOption(plugin, option): Observable { return this.http.post(`${this.apiURL}/plugins/${plugin}/search-options`, {'option': option}, httpOptions); } getServiceItems(): Observable { return this.http.get(`${this.apiURL}/service/items`, httpOptions); } setServiceItem(id: any): Observable { return this.http.post(`${this.apiURL}/service/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); } getServiceItem(): Observable { return this.http.get(`${this.apiURL}/controller/live-item`, httpOptions); } getNotes(): Observable { return this.http.get(`${this.apiURL}/controller/notes`, httpOptions); } setSlide(id: any): Observable { return this.http.post(`${this.apiURL}/controller/show`, {'id': id}, 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); } getThemeLevel(): Observable { return this.http.get(`${this.apiURL}/controller/theme-level`, httpOptions); } getThemes(): Observable { return this.http.get(`${this.apiURL}/controller/themes`, httpOptions); } setThemeLevel(level): Observable { return this.http.post(`${this.apiURL}/controller/theme-level`, {'level': level}, httpOptions); } getTheme(): Observable { return this.http.get(`${this.apiURL}/controller/theme`, httpOptions); } setTheme(theme: string): Observable { return this.http.post(`${this.apiURL}/controller/theme`, {'theme': theme}, 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); } }