emotiv-ts
Version:
A Typescript library that wraps the Cortex API functionalities to communicate with Emotiv headsets
81 lines (80 loc) • 3.58 kB
JavaScript
import { HeadsetService } from "./headset.service";
import { AuthenticationService } from "./authentication.service";
import { SessionService } from "./session.service";
import { EmotivError } from "../enums/internal/emotiv.error";
import WebSocket from 'websocket';
import { DataStreamService } from "./data-stream.service";
import { ProfileService } from "./profile.service";
export class EmotivService {
application;
socket;
authToken;
sessionId;
headsetId;
headsetService;
authenticationService;
sessionService;
dataStreamService;
constructor(socketUrl, application) {
// Create WebSocket
this.socket = new WebSocket.w3cwebsocket(socketUrl);
this.application = application;
// Initialize services
this.authenticationService = new AuthenticationService(this.socket);
this.sessionService = new SessionService(this.socket);
this.headsetService = new HeadsetService(this.socket);
this.dataStreamService = new DataStreamService(this.socket);
}
connect() {
return new Promise((resolve, reject) => {
this.socket.onopen = async () => {
await this.authenticationService.requestAccess(this.application)
.then(async (emotivAccess) => {
console.log(emotivAccess.message);
if (emotivAccess.isGranted) {
await this.authenticationService.authorize(this.application).then(authorisation => this.authToken = authorisation.token);
await this.headsetService.getHeadsets().then(headsetId => this.headsetId = headsetId);
await this.sessionService.createSession(this.authToken, this.headsetId).then(sessionId => this.sessionId = sessionId);
await this.authenticationService.getLicenseInfo();
resolve(() => {
console.log("Connected successfully.");
let sessionInfo = this.getSessionInfo();
// Shows the information about the session
console.debug("Session Info:", sessionInfo);
return sessionInfo;
});
}
else {
console.log(EmotivError.ACCESS_NOT_ACCEPTED);
throw new Error(EmotivError.ACCESS_NOT_ACCEPTED);
}
})
.catch(error => {
// Thrown an error if user is not logged in CortexUI
console.error(EmotivError.ACCESS_NOT_GRANTED, error);
throw new Error(EmotivError.ACCESS_NOT_GRANTED);
});
};
});
}
readData(streams, action) {
this.dataStreamService.subscribe(streams, action);
}
setupProfile(name, action) {
let profileService = new ProfileService(this.socket);
profileService.setupProfile(name, action).then(data => {
console.debug("SetupProfile data:", data);
return data;
});
}
async getSessionInfo() {
let deviceConnectionStatus;
await this.headsetService.controlDevice(this.headsetId).then(status => deviceConnectionStatus = status);
return {
"authToken": this.authToken,
"sessionId": this.sessionId,
"headsetId": this.headsetId,
"connectionStatus": JSON.parse(deviceConnectionStatus)['result']
};
}
}