Newer
Older
import { Injectable, Inject } from '@angular/core';
import { HttpClient, HttpParams, HttpHeaders, HttpEvent, HttpErrorResponse } from '@angular/common/http';
import { ResponseContentType } from '@angular/http';
import { BehaviorSubject , Observable, of } from 'rxjs';
import { catchError, map, tap, switchMap } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class BackendService {
private lastBookletState = '';
private lastUnitResponses = '';
private lastUnitRestorePoint = '';
private itemplayers: {[filename: string]: string} = {};
constructor(
@Inject('SERVER_URL') private serverUrl: string,
private http: HttpClient) {}
private normaliseFileName(fn: string, ext: string): string {
fn = fn.toUpperCase();
ext = ext.toUpperCase();
if (ext.slice(0, 1) !== '.') {
ext = '.' + ext;
}
if (fn.slice(-(ext.length)) === ext) {
return fn;
} else {
return fn + ext;
}
}
// 888888888888888888888888888888888888888888888888888888888888888888
getBookletData(auth: Authorisation): Observable<BookletData | ServerError> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.http
.post<BookletData>(this.serverUrl + 'getBookletData.php', {au: auth}, httpOptions)
.pipe(
catchError(this.handleError)
);
}
// 888888888888888888888888888888888888888888888888888888888888888888
loadItemplayerOk(auth: Authorisation, unitDefinitionType: string): Observable<boolean> {
unitDefinitionType = this.normaliseFileName(unitDefinitionType, 'html');
if (this.itemplayers.hasOwnProperty(unitDefinitionType)) {
return of(true);
} else {
return this.getUnitResourceTxt(auth, unitDefinitionType)
.pipe(
switchMap(myData => {
if (myData instanceof ServerError) {
return of(false);
} else {
const unitDef = myData as string;
if (unitDef.length > 0) {
this.itemplayers[unitDefinitionType] = unitDef;
return of(true);
} else {
return of(false);
}
}
}));
}
}
// 888888888888888888888888888888888888888888888888888888888888888888
saveUnitReview(auth: Authorisation, unitDbId: number, priority: number,
categories: string, entry: string): Observable<boolean | ServerError> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.http
.post<boolean>(this.serverUrl + 'addUnitReview.php', {au: auth.toAuthString(), u: unitDbId,
p: priority, c: categories, e: entry}, httpOptions)
.pipe(
catchError(this.handleError)
);
}
// 888888888888888888888888888888888888888888888888888888888888888888
saveBookletReview(auth: Authorisation, priority: number, categories: string, entry: string): Observable<boolean | ServerError> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.http
.post<boolean>(this.serverUrl + 'addBookletReview.php', {au: auth.toAuthString(),
p: priority, c: categories, e: entry}, httpOptions)
.pipe(
catchError(this.handleError)
);
}
// 888888888888888888888888888888888888888888888888888888888888888888
getItemplayer(unitDefinitionType: string): string {
unitDefinitionType = this.normaliseFileName(unitDefinitionType, 'html');
if (this.itemplayers.hasOwnProperty(unitDefinitionType)) {
return this.itemplayers[unitDefinitionType];
} else {
return '';
}
}
// 888888888888888888888888888888888888888888888888888888888888888888
setBookletStatus(sessiontoken: string, state: {}): Observable<string | ServerError> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
if ((sessiontoken + JSON.stringify(state)) === this.lastBookletState) {
return new Observable(null);
} else {
this.lastBookletState = sessiontoken + JSON.stringify(state);
return this.http
.post<string>(this.serverUrl + 'setBookletStatus.php', {st: sessiontoken, state: state}, httpOptions)
.pipe(
catchError(this.handleError)
);
}
}
// 888888888888888888888888888888888888888888888888888888888888888888
getUnit(auth: Authorisation, unitid: string): Observable<UnitData | ServerError> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.http
.post<UnitData>(this.serverUrl + 'getUnit.php', {au: auth.toAuthString, u: unitid}, httpOptions)
.pipe(
catchError(this.handleError)
);
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
}
// 888888888888888888888888888888888888888888888888888888888888888888
getUnitResource(sessiontoken: string, resId: string): Observable<string | ServerError> {
const myHttpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
}),
responseType: 'arraybuffer' as 'json'
};
return this.http
.post<ArrayBuffer>(this.serverUrl + 'getUnitResource.php', {st: sessiontoken, r: resId}, myHttpOptions)
.pipe(
map((r: ArrayBuffer) => {
let str64 = '';
const alen = r.byteLength;
for (let i = 0; i < alen; i++) {
str64 += String.fromCharCode(r[i]);
}
return window.btoa(str64);
}),
catchError(this.handleError)
);
}
// 888888888888888888888888888888888888888888888888888888888888888888
getUnitResource64(sessiontoken: string, resId: string): Observable<string | ServerError> {
const myHttpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
}),
responseType: 'text' as 'json'
};
return this.http
.post<string>(this.serverUrl + 'getUnitResource64.php', {st: sessiontoken, r: resId}, myHttpOptions)
.pipe(
catchError(this.handleError)
);
}
// 888888888888888888888888888888888888888888888888888888888888888888
getUnitResourceTxt(auth: Authorisation, resId: string): Observable<string | ServerError> {
const myHttpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
}),
responseType: 'text' as 'json'
};
return this.http
.post<string>(this.serverUrl + 'getUnitResourceTxt.php', {au: auth.toAuthString, r: resId}, myHttpOptions)
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
.pipe(
catchError(this.handleError)
);
}
// 888888888888888888888888888888888888888888888888888888888888888888
// 888888888888888888888888888888888888888888888888888888888888888888
setUnitResponses(sessiontoken: string, unit: string, unitdata: string): Observable<string | ServerError> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
if ((sessiontoken + unit + JSON.stringify(unitdata)) === this.lastUnitResponses) {
return new Observable(null);
} else {
this.lastUnitResponses = sessiontoken + unit + JSON.stringify(unitdata);
// todo: store the response for evaluation
return this.http
.post<string>(this.serverUrl + 'setUnitResponses.php', {st: sessiontoken, u: unit, responses: unitdata}, httpOptions)
.pipe(
catchError(this.handleError)
);
}
}
// 888888888888888888888888888888888888888888888888888888888888888888
setUnitRestorePoint(sessiontoken: string, unit: string, unitdata: string): Observable<string | ServerError> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.http
.post<string>(this.serverUrl + 'setUnitRestorePoint.php', {st: sessiontoken, u: unit, restorepoint: unitdata}, httpOptions)
.pipe(
catchError(this.handleError)
);
}
// 888888888888888888888888888888888888888888888888888888888888888888
setUnitLog(sessiontoken: string, unit: string, unitdata: {}): Observable<string | ServerError> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.http
.post<string>(this.serverUrl + 'setUnitLog.php', {st: sessiontoken, u: unit, log: unitdata}, httpOptions)
.pipe(
catchError(this.handleError)
);
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
private handleError(errorObj: HttpErrorResponse): Observable<ServerError> {
const myreturn = new ServerError(errorObj.status, 'Fehler bei Datenübertragung');
if (errorObj.status === 401) {
myreturn.label = 'Fehler: Zugriff verweigert - bitte (neu) anmelden!';
} else if (errorObj.status === 503) {
myreturn.label = 'Fehler: Server meldet Datenbankproblem.';
} else if (errorObj.error instanceof ErrorEvent) {
myreturn.label = 'Fehler: ' + (<ErrorEvent>errorObj.error).message;
} else {
myreturn.label = 'Fehler: ' + errorObj.message;
}
return of(myreturn);
}
private handleErrorSimple(errorObj: HttpErrorResponse): Observable<boolean> {
const myreturn = new ServerError(errorObj.status, 'Fehler bei Datenübertragung');
console.log('handleErrorSimple: ' + errorObj);
return of(false);
}
}
// #############################################################################################
// class instead of interface to be able to use instanceof to check type
export class ServerError {
public code: number;
public label: string;
constructor(code: number, label: string) {
this.code = code;
this.label = label;
xml: string;
locked: boolean;
u: number;
}
export interface UnitData {
xml: string;
restorepoint: string;
status: {};
}
export interface ServerError {
code: number;
label: string;
}
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
export class Authorisation {
readonly personToken: string;
readonly bookletId: number;
static fromPersonTokenAndBookletId(personToken: string, bookletId: number): Authorisation {
return new Authorisation(personToken + '##' + bookletId.toString());
}
constructor(authString: string) {
if ((typeof authString !== 'string') || (authString.length === 0)) {
this.personToken = '';
this.bookletId = 0;
} else {
const retSplits = authString.split('##');
this.personToken = retSplits[0];
if (retSplits.length > 1) {
this.bookletId = +retSplits[1];
} else {
this.bookletId = 0;
}
}
}
toAuthString(): string {
return this.personToken + '##' + this.bookletId.toString();
}
}