UNPKG

emotiv-ts

Version:

A Typescript library that wraps the Cortex API functionalities to communicate with Emotiv headsets

61 lines (60 loc) 2.6 kB
import { EmotivService } from "./emotiv.service"; import { RecordService } from "./record.service"; import { AuthenticationService } from "./authentication.service"; import { SessionService } from "./session.service"; import { Requests } from "../enums/internal/emotiv.requests"; import { InjectMarker } from "../models/requests/markers/inject-marker"; export class MarkerService { socket; emotivService; recordService; constructor(socket, application) { this.socket = socket; this.emotivService = new EmotivService(this.socket.url, application); this.recordService = new RecordService(this.socket); } addMarker() { let context = this; this.socket.onopen = async () => { await this.emotivService.connect(); let recordName = 'test_marker'; await this.recordService.startRecord(recordName); let numberOfMarker = 10; for (let numMarker = 0; numMarker < numberOfMarker; numMarker++) { setTimeout(async function () { let markerLabel = `marker_number_${numMarker}`; let markerTime = Date.now(); let marker = { label: markerLabel, value: "test", port: "Software", time: markerTime }; await context.injectMarker(marker.label, marker.value, marker.port, marker.time); }, 3000); } //await thisStopRecord.stopRecord(thisStopRecord.authToken, thisStopRecord.sessionId, recordName) }; } injectMarker(label, value, port, time) { let context = this; let authToken = AuthenticationService.getAuthToken(); let sessionId = SessionService.getSessionId(); let injectMarkerRequest = new InjectMarker(authToken, sessionId, time, value, label, port); return new Promise(function (resolve, reject) { context.socket.send(JSON.stringify(injectMarkerRequest)); context.socket.onmessage = (message) => { try { if (JSON.parse(message.data)['id'] == Requests.INJECT_MARKER_REQUEST) { console.log('INJECT MARKER RESULT --------------------------------'); console.log(message.data); resolve(message.data); } } catch (error) { console.error(error); } }; }); } }