Skip to content

Commit

Permalink
Improved error handling for emotiv devices
Browse files Browse the repository at this point in the history
  • Loading branch information
jdpigeon committed Dec 4, 2018
1 parent 2b86063 commit 4e8f27f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
4 changes: 1 addition & 3 deletions app/epics/deviceEpics.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,12 @@ const connectEpic = action$ =>
promise.then(
deviceInfo => deviceInfo,
error => {
console.error('connectEpic: ', error);
toast.error(`"Device Error: " ${error.toString()}`);
return null;
}
)
),
mergeMap(deviceInfo => {
if (deviceInfo) {
if (!isNil(deviceInfo.name)) {
return of(
setDeviceType(
deviceInfo.name.includes('Muse') ? DEVICES.MUSE : DEVICES.EMOTIV
Expand Down
47 changes: 24 additions & 23 deletions app/utils/eeg/emotiv.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
* an RxJS Observable of raw EEG data
*
*/
import { fromEvent } from "rxjs";
import { map, withLatestFrom, share } from "rxjs/operators";
import { addInfo, epoch, bandpassFilter } from "@neurosity/pipes";
import { toast } from "react-toastify";
import { parseEmotivSignalQuality } from "./pipes";
import { fromEvent } from 'rxjs';
import { map, withLatestFrom, share } from 'rxjs/operators';
import { addInfo, epoch, bandpassFilter } from '@neurosity/pipes';
import { toast } from 'react-toastify';
import { parseEmotivSignalQuality } from './pipes';
import {
USERNAME,
PASSWORD,
CLIENT_ID,
CLIENT_SECRET,
LICENSE_ID
} from "../../../keys";
import { EMOTIV_CHANNELS, PLOTTING_INTERVAL } from "../../constants/constants";
import Cortex from "./cortex";
} from '../../../keys';
import { EMOTIV_CHANNELS, PLOTTING_INTERVAL } from '../../constants/constants';
import Cortex from './cortex';

// Just returns the Cortex object from SDK
const verbose = process.env.LOG_LEVEL || 1;
Expand Down Expand Up @@ -44,34 +44,35 @@ export const connectToEmotiv = device =>
)
.then(() =>
client.createSession({
status: "active",
status: 'active',
headset: device.id
})
)
.then(
session => ({
name: session.headset.id,
samplingRate: session.headset.settings.eegRate,
channels: EMOTIV_CHANNELS
}),
err => toast("Device Error: ", err)
);
.then(session => ({
name: session.headset.id,
samplingRate: session.headset.settings.eegRate,
channels: EMOTIV_CHANNELS
}))
.catch(err => {
toast.error(`Couldn't connect to device ${device.id}`);
return err;
});

export const disconnectFromEmotiv = async () => {
const sessionStatus = await client.updateSession({ status: "close" });
const sessionStatus = await client.updateSession({ status: 'close' });
return sessionStatus;
};

// Returns an observable that will handle both connecting to Client and providing a source of EEG data
export const createRawEmotivObservable = async () => {
const subs = await client.subscribe({ streams: ["eeg", "dev"] });
if (!subs[0].eeg) throw new Error("failed to subscribe");
return fromEvent(client, "eeg").pipe(map(createEEGSample));
const subs = await client.subscribe({ streams: ['eeg', 'dev'] });
if (!subs[0].eeg) throw new Error('failed to subscribe');
return fromEvent(client, 'eeg').pipe(map(createEEGSample));
};

// Creates an observable that will epoch, filter, and add signal quality to EEG stream
export const createEmotivSignalQualityObservable = rawObservable => {
const signalQualityObservable = fromEvent(client, "dev");
const signalQualityObservable = fromEvent(client, 'dev');
const samplingRate = 128;
const channels = EMOTIV_CHANNELS;
const intervalSamples = (PLOTTING_INTERVAL * samplingRate) / 1000;
Expand All @@ -96,7 +97,7 @@ export const createEmotivSignalQualityObservable = rawObservable => {
};

export const injectEmotivMarker = (value, time) => {
client.injectMarker({ label: "event", value, time });
client.injectMarker({ label: 'event', value, time });
};

// ---------------------------------------------------------------------
Expand Down

0 comments on commit 4e8f27f

Please sign in to comment.