import { BoxOverview } from './../../interfaces/boxes'; import { UserService } from './../../auth/user.service'; import { ExperimentContent, Experiment } from './../../interfaces/experiments'; import { AppConfig } from './../../app-config.interface'; import { APP_CONFIG } from './../../app-config'; import { Injectable, Inject } from '@angular/core'; import { Http, URLSearchParams } from '@angular/http'; import { Observable } from 'rxjs/Observable'; const LRU = require('lru'); import 'rxjs/add/operator/toPromise'; import 'rxjs/add/observable/throw'; import 'rxjs/add/operator/timeoutWith'; interface IAssignOptions { patientId: string; startTime: Date; endTime: Date; boxId: number; } @Injectable() export class BytefliesService { // timeout in milliseconds for HTTP requests timeout = 4000; public experiments; private cache; private bytefliesApiUrl; constructor( @Inject(APP_CONFIG) config: AppConfig, private http: Http, private user: UserService) { this.bytefliesApiUrl = config.apiEndpoint; this.cache = new LRU({ max: 10, // max elements in cache maxAge: 60 * 1000 // max age in milliseconds }); } getBoxes(experimentId: number): Promise { const token = this.user.privileges .find(privilege => privilege.experiment.id === experimentId).token.token; const url = `${this.bytefliesApiUrl}/box/overview/list?access_token=${token}`; return this.http.get(url) .timeoutWith(this.timeout, Observable.throw('the operation timed out')) .map(result => result.json()) .map(result => (result.content)) .map(content => content.map(box => { // patch GMT date strings to fit into JS's Date Object.keys(box.virtual_boxes).map(key => { const virtualbox = box.virtual_boxes[key]; this.patchDatestring(virtualbox.virtual_box.start_time); this.patchDatestring(virtualbox.virtual_box.end_time); virtualbox.recordings.map(recording => { this.patchDatestring(recording.device_recording.start_time); this.patchDatestring(recording.device_recording.end_time); }); }); return box; })) .catch(reason => { try { const result = reason.json(); if (result.status && result.status.code === 404) { return []; } else if (result.status && result.status.text) { return Observable.throw(result.status.text); } else { return Observable.throw('Something went wrong.'); } } catch (e) { return Observable.throw(reason); } }).toPromise(); } assignPatient(options: IAssignOptions, experimentId: number) { const token = this.user.privileges .find(privilege => privilege.experiment.id === experimentId) .token.token; const url = `${this.bytefliesApiUrl}/box/reassign?access_token=${token}`; // transform startTime and endTime to UTC const startTime = new Date(options.startTime); const timezoneOffset = new Date().getTimezoneOffset() * 60 * 1000; startTime.setTime(startTime.getTime() + timezoneOffset); console.log('converted startTime', startTime.toUTCString()); console.log('ASSIGNING', options); const body = { company_specific_id: options.patientId, box_allocation_id: options.boxId, start_time: startTime, // end_time: options.endTime, }; // return Promise.resolve(); return this.http.post(url, body) .timeoutWith(this.timeout, Observable.throw('The operation timed out')) .map(response => response.json().content as Object[]) .catch(reason => { try { const result = reason.json(); if (result.status && result.status.code === 409 && result.content.conflict) { return Observable.throw(result.error.conflict); } else if (result.status && result.status.text) { return Observable.throw(result.status.text); } else { return Observable.throw('Something went wrong.'); } } catch (e) { return Observable.throw(reason); } }).toPromise(); } getExperiments(ownerId: number): Promise { if (!this.user.privileges) { console.log('no privileges!'); return Promise.reject(new Error('No privileges found')); } const experiments = this.user.privileges.map(privilege => privilege.experiment); return Promise.resolve(experiments); } patchDatestring(time: { value: string }) { if (!!time && !!time.value) { // to fix dates in safari etc: time.value = time.value.split('.')[0]; time.value = time.value.split('-').join('/'); // to indicate GMT: time.value += ' GMT'; } } }