web-remote/src/app/openlp.service.ts

116 lines
3.1 KiB
TypeScript
Raw Normal View History

import { Injectable, EventEmitter } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { URLSearchParams, Http } from '@angular/http';
import { Observable } from 'rxjs';
import { map, take } from 'rxjs/operators';
2018-08-22 20:04:41 +00:00
import { PluginDescription, State, Slide, ServiceItem } from './responses';
2018-08-27 21:01:02 +00:00
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;
};
@Injectable()
export class OpenLPService {
2018-08-27 21:01:02 +00:00
private apiURL: string;
public stateChanged$: EventEmitter<State>;
constructor(private http: HttpClient) {
2018-08-27 21:01:02 +00:00
let port: string;
if (environment.production) {
port = window.location.port;
}
else {
2018-08-27 21:01:02 +00:00
port = '4316';
}
this.apiURL = `http://localhost:${port}`;
this.stateChanged$ = new EventEmitter<State>();
let state: State = null;
const ws: WebSocket = new WebSocket('ws://localhost:4317/state');
ws.onmessage = (event) => {
const reader = new FileReader();
reader.onload = () => {
state = deserialize(JSON.parse(reader.result).results, State);
this.stateChanged$.emit(state);
};
reader.readAsText(event.data);
};
}
getItemSlides(): Observable<Slide[]> {
2018-08-20 12:25:52 +00:00
return this.http.get<Slide[]>(`${this.apiURL}/controller/live/text`);
}
getServiceItems(): Observable<ServiceItem[]> {
2018-08-20 12:25:52 +00:00
return this.http.get<ServiceItem[]>(`${this.apiURL}/service/list`);
}
2018-08-20 00:00:32 +00:00
getSearchablePlugins(): Observable<PluginDescription[]> {
return this.http.get<PluginDescription[]>(`${this.apiURL}/plugin/search`);
}
setServiceItem(id: number): Observable<any> {
2018-08-20 13:00:12 +00:00
return this.http.get(`${this.apiURL}/service/set?id=${id}`);
}
2018-08-20 13:00:12 +00:00
search(plugin, text): Observable<any> {
2018-08-20 12:16:15 +00:00
return this.http.get(`${this.apiURL}/${plugin}/search?q=${text}`);
}
2018-08-20 13:00:12 +00:00
setSlide(id): Observable<any> {
return this.http.get(`${this.apiURL}/controller/live/set?id=${id}`);
}
2018-08-20 13:00:12 +00:00
nextItem(): Observable<any> {
return this.http.get(`${this.apiURL}/service/next`);
}
2018-08-20 13:00:12 +00:00
previousItem(): Observable<any> {
return this.http.get(`${this.apiURL}/service/previous`);
}
2018-08-20 13:00:12 +00:00
nextSlide(): Observable<any> {
return this.http.get(`${this.apiURL}/controller/live/next`);
}
2018-08-20 13:00:12 +00:00
previousSlide(): Observable<any> {
return this.http.get(`${this.apiURL}/controller/live/previous`);
}
2018-08-20 13:00:12 +00:00
blankDisplay(): Observable<any> {
return this.http.get(`${this.apiURL}/display/blank`);
}
2018-08-20 13:00:12 +00:00
themeDisplay(): Observable<any> {
return this.http.get(`${this.apiURL}/display/theme`);
}
2018-08-20 13:00:12 +00:00
desktopDisplay(): Observable<any> {
return this.http.get(`${this.apiURL}/display/desktop`);
}
2018-08-20 13:00:12 +00:00
showDisplay(): Observable<any> {
return this.http.get(`${this.apiURL}/display/show`);
}
2018-08-20 13:00:12 +00:00
showAlert(text): Observable<any> {
return this.http.get(`${this.apiURL}/alert?text=${text}`);
}
2018-08-20 13:00:12 +00:00
sendItemLive(plugin, id): Observable<any> {
return this.http.get(`${this.apiURL}/${plugin}/live?id=${id}`);
}
2018-08-20 13:00:12 +00:00
addItemToService(plugin, id): Observable<any> {
return this.http.get(`${this.apiURL}/${plugin}/add?id=${id}`);
}
}