Files
WebApp/src/app/connect/domain/eeg-source.ts
T

53 lines
1.8 KiB
TypeScript

import { Observable, from, map, switchMap } from 'rxjs';
import { PreviewResponse, SourceLocator } from '../api/api.types';
import { fileToBase64 } from '../api/file-encoding';
import { SourceGateway } from '../api/source-gateway';
import { DataConnection, EncodedFile, SourceTable, encodeFiles } from './data-source';
/**
* EEG recordings (`.edf`). All files added together form one table with a
* row per recording: header data, an estimated background rhythm and the
* file itself, shaped for TEA's `EegEstudio`.
*/
export class EegConnection extends DataConnection {
readonly icon = 'monitor_heart';
readonly typeLabel = 'EEG (EDF)';
readonly encoded: EncodedFile[];
constructor(readonly files: File[]) {
super(files.length === 1 ? files[0].name : `${files.length} registros EEG`);
this.encoded = files.map((file) => new EncodedFile(file, fileToBase64));
}
override get singleTable(): boolean {
return true;
}
async payload(): Promise<Record<string, unknown>> {
return { files: await encodeFiles(this.encoded) };
}
protected discover(gateway: SourceGateway): Observable<SourceTable[]> {
// Reading the headers up front rejects files MNE can't open.
const table = new EegTable(this);
return table.preview(gateway, null).pipe(map(() => [table]));
}
}
export class EegTable extends SourceTable {
readonly icon = 'monitor_heart';
readonly kindLabel = 'Registros';
constructor(override readonly connection: EegConnection) {
super(connection, 'Registros');
}
preview(gateway: SourceGateway, target: string | null): Observable<PreviewResponse> {
return from(this.connection.payload()).pipe(switchMap((payload) => gateway.file.previewPayload('edf', payload, target)));
}
async locator(): Promise<SourceLocator> {
return { source_type: 'edf', payload: await this.connection.payload() };
}
}