2018-08-19 20:37:53 +00:00
|
|
|
import { Injectable, EventEmitter } from '@angular/core';
|
2019-10-08 05:43:49 +00:00
|
|
|
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
2018-08-19 20:37:53 +00:00
|
|
|
import { Observable } from 'rxjs';
|
2019-10-08 05:43:49 +00:00
|
|
|
|
2019-11-07 18:02:26 +00:00
|
|
|
import {
|
|
|
|
PluginDescription,
|
|
|
|
State,
|
|
|
|
Slide,
|
|
|
|
ServiceItem,
|
2020-01-23 20:10:11 +00:00
|
|
|
Theme,
|
2019-11-07 18:02:26 +00:00
|
|
|
MainView,
|
|
|
|
SystemInformation,
|
|
|
|
Credentials,
|
|
|
|
AuthToken
|
|
|
|
} from './responses';
|
2018-08-27 21:01:02 +00:00
|
|
|
import { environment } from '../environments/environment';
|
2018-08-19 20:37:53 +00:00
|
|
|
|
2019-11-07 18:02:26 +00:00
|
|
|
|
2019-10-06 00:28:36 +00:00
|
|
|
const deserialize = (json, cls) => {
|
|
|
|
const inst = new cls();
|
|
|
|
for (const p in json) {
|
|
|
|
if (!json.hasOwnProperty(p)) {
|
|
|
|
continue;
|
2018-08-19 20:37:53 +00:00
|
|
|
}
|
2019-10-06 00:28:36 +00:00
|
|
|
inst[p] = json[p];
|
2018-08-19 20:37:53 +00:00
|
|
|
}
|
2019-10-06 00:28:36 +00:00
|
|
|
return inst;
|
|
|
|
};
|
2018-08-19 20:37:53 +00:00
|
|
|
|
2019-10-08 05:43:49 +00:00
|
|
|
const httpOptions = {
|
|
|
|
headers: new HttpHeaders({'Content-Type': 'application/json'})
|
|
|
|
};
|
|
|
|
|
2019-11-07 18:02:26 +00:00
|
|
|
|
2018-08-19 20:37:53 +00:00
|
|
|
@Injectable()
|
|
|
|
export class OpenLPService {
|
2018-08-27 21:01:02 +00:00
|
|
|
private apiURL: string;
|
2018-08-19 20:37:53 +00:00
|
|
|
public stateChanged$: EventEmitter<State>;
|
|
|
|
|
|
|
|
constructor(private http: HttpClient) {
|
2019-11-07 18:02:26 +00:00
|
|
|
const host = window.location.hostname;
|
2018-08-27 21:01:02 +00:00
|
|
|
let port: string;
|
|
|
|
if (environment.production) {
|
|
|
|
port = window.location.port;
|
2019-10-06 00:28:36 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-08-27 21:01:02 +00:00
|
|
|
port = '4316';
|
|
|
|
}
|
2019-11-09 16:26:56 +00:00
|
|
|
this.apiURL = `http://${host}:${port}/api/v2`;
|
2019-10-08 05:43:49 +00:00
|
|
|
|
2018-08-19 20:37:53 +00:00
|
|
|
this.stateChanged$ = new EventEmitter<State>();
|
2019-10-08 05:43:49 +00:00
|
|
|
this.retrieveSystemInformation().subscribe(info => {
|
2019-11-07 18:02:26 +00:00
|
|
|
const ws = new WebSocket(`ws://${host}:${info.websocket_port}/state`);
|
2019-10-08 05:43:49 +00:00
|
|
|
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);
|
2019-10-06 00:28:36 +00:00
|
|
|
};
|
2019-10-08 05:43:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setAuthToken(token: string): void {
|
|
|
|
httpOptions.headers = httpOptions.headers.set('Authorization', token);
|
|
|
|
}
|
|
|
|
|
|
|
|
retrieveSystemInformation(): Observable<SystemInformation> {
|
|
|
|
return this.http.get<SystemInformation>(`${this.apiURL}/core/system`, httpOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
getMainImage(): Observable<MainView> {
|
|
|
|
return this.http.get<MainView>(`${this.apiURL}/core/live-image`, httpOptions);
|
2018-08-19 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getItemSlides(): Observable<Slide[]> {
|
2019-10-08 05:43:49 +00:00
|
|
|
return this.http.get<Slide[]>(`${this.apiURL}/controller/live-item`, httpOptions);
|
2018-08-19 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getServiceItems(): Observable<ServiceItem[]> {
|
2019-11-09 16:26:56 +00:00
|
|
|
return this.http.get<ServiceItem[]>(`${this.apiURL}/service/items`, httpOptions);
|
2018-08-19 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 20:10:11 +00:00
|
|
|
getThemeLevel(): Observable<any> {
|
|
|
|
return this.http.get(`${this.apiURL}/controller/get_theme_level`, httpOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
getThemes(): Observable<Theme[]> {
|
|
|
|
return this.http.get<Theme[]>(`${this.apiURL}/controller/themes`, httpOptions);
|
|
|
|
}
|
|
|
|
|
2018-08-20 00:00:32 +00:00
|
|
|
getSearchablePlugins(): Observable<PluginDescription[]> {
|
2019-10-08 05:43:49 +00:00
|
|
|
return this.http.get<PluginDescription[]>(`${this.apiURL}/core/plugins`, httpOptions);
|
2018-08-19 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
2019-10-06 00:28:36 +00:00
|
|
|
setServiceItem(id: number): Observable<any> {
|
2019-11-09 16:26:56 +00:00
|
|
|
return this.http.post(`${this.apiURL}/service/show`, {'id': id}, httpOptions);
|
2018-08-19 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 13:00:12 +00:00
|
|
|
search(plugin, text): Observable<any> {
|
2019-10-08 05:43:49 +00:00
|
|
|
return this.http.get(`${this.apiURL}/plugins/${plugin}/search?text=${text}`, httpOptions);
|
2018-08-20 12:16:15 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 13:00:12 +00:00
|
|
|
setSlide(id): Observable<any> {
|
2019-10-08 05:43:49 +00:00
|
|
|
return this.http.post(`${this.apiURL}/controller/show`, {'id': id}, httpOptions);
|
2018-08-19 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 20:10:11 +00:00
|
|
|
setThemeLevel(level): Observable<any> {
|
|
|
|
return this.http.post(`${this.apiURL}/controller/set_theme_level`, {'level': level}, httpOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
setTheme(theme: string): Observable<any> {
|
|
|
|
return this.http.post(`${this.apiURL}/controller/theme_change`, {'theme': theme}, httpOptions);
|
|
|
|
}
|
|
|
|
|
2018-08-20 13:00:12 +00:00
|
|
|
nextItem(): Observable<any> {
|
2019-10-08 05:43:49 +00:00
|
|
|
return this.http.post(`${this.apiURL}/service/progress`, {'action': 'next'}, httpOptions);
|
2018-08-19 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 13:00:12 +00:00
|
|
|
previousItem(): Observable<any> {
|
2019-10-08 05:43:49 +00:00
|
|
|
return this.http.post(`${this.apiURL}/service/progress`, {'action': 'previous'}, httpOptions);
|
2018-08-19 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 13:00:12 +00:00
|
|
|
nextSlide(): Observable<any> {
|
2019-10-08 05:43:49 +00:00
|
|
|
return this.http.post(`${this.apiURL}/controller/progress`, {'action': 'next'}, httpOptions);
|
2018-08-19 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 13:00:12 +00:00
|
|
|
previousSlide(): Observable<any> {
|
2019-10-08 05:43:49 +00:00
|
|
|
return this.http.post(`${this.apiURL}/controller/progress`, {'action': 'previous'}, httpOptions);
|
2018-08-19 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 13:00:12 +00:00
|
|
|
blankDisplay(): Observable<any> {
|
2019-10-08 05:43:49 +00:00
|
|
|
return this.http.post(`${this.apiURL}/core/display`, {'display': 'blank'}, httpOptions);
|
2018-08-19 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 13:00:12 +00:00
|
|
|
themeDisplay(): Observable<any> {
|
2019-10-08 05:43:49 +00:00
|
|
|
return this.http.post(`${this.apiURL}/core/display`, {'display': 'theme'}, httpOptions);
|
2018-08-19 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 13:00:12 +00:00
|
|
|
desktopDisplay(): Observable<any> {
|
2019-10-08 05:43:49 +00:00
|
|
|
return this.http.post(`${this.apiURL}/core/display`, {'display': 'desktop'}, httpOptions);
|
2018-08-19 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 13:00:12 +00:00
|
|
|
showDisplay(): Observable<any> {
|
2019-10-08 05:43:49 +00:00
|
|
|
return this.http.post(`${this.apiURL}/core/display`, {'display': 'show'}, httpOptions);
|
2018-08-19 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 13:00:12 +00:00
|
|
|
showAlert(text): Observable<any> {
|
2019-10-08 05:43:49 +00:00
|
|
|
return this.http.post(`${this.apiURL}/plugins/alerts`, {'text': text}, httpOptions);
|
2018-08-19 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 13:00:12 +00:00
|
|
|
sendItemLive(plugin, id): Observable<any> {
|
2019-10-08 05:43:49 +00:00
|
|
|
return this.http.post(`${this.apiURL}/plugins/${plugin}/live`, {'id': id}, httpOptions);
|
2018-08-19 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 13:00:12 +00:00
|
|
|
addItemToService(plugin, id): Observable<any> {
|
2019-10-08 05:43:49 +00:00
|
|
|
return this.http.post(`${this.apiURL}/plugins/${plugin}/add`, {'id': id}, httpOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
login(credentials: Credentials): Observable<AuthToken> {
|
|
|
|
return this.http.post<AuthToken>(`${this.apiURL}/core/login`, credentials, httpOptions);
|
2018-08-19 20:37:53 +00:00
|
|
|
}
|
|
|
|
}
|