Skip to content

Commit

Permalink
adding brainflowerror class to nodejs
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Parfenov <a1994ndrey@gmail.com>
  • Loading branch information
Andrey1994 committed Aug 16, 2023
1 parent 9325613 commit 6724fb0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run_unix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ jobs:
- name: Synthetic Node
run: |
cd $GITHUB_WORKSPACE/nodejs_package/brainflow
node run example
npm run example
# deploy started
- name: Install AWS CLI
run: sudo -H python3 -m pip install awscli==1.21.10
Expand Down
9 changes: 5 additions & 4 deletions .github/workflows/run_windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,6 @@ jobs:
uses: julia-actions/setup-julia@v1
with:
version: 1.3.1
# Unit testing
- name: Run unit tests
run: .\build\tests\Release\brainflow_tests.exe
shell: cmd
# BoardController testing
- name: Run Julia Tests
run: |
Expand Down Expand Up @@ -294,6 +290,11 @@ jobs:
cd %GITHUB_WORKSPACE%\rust_package\brainflow
cargo run --example=eeg_metrics
shell: cmd
- name: Synthetic Node
run: |
cd %GITHUB_WORKSPACE%\nodejs_package\brainflow
npm run example
shell: cmd
# Start Deploy Stage
- name: Install Python AWS Tools
run: |
Expand Down
75 changes: 38 additions & 37 deletions nodejs_package/brainflow/board_shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
BrainFlowPresets,
BrainFlowExitCodes,
LogLevels,
BrainFlowError,
} from './brainflow.types';
import { BoardControllerCLikeFunctions as CLike, BoardControllerFunctions } from './functions.types';

Expand Down Expand Up @@ -103,7 +104,7 @@ class BoardControllerDLL extends BoardControllerFunctions {
case 'linux':
return `${this.libPath}/libBoardController.so`;
default:
throw new Error(`OS ${platform} is not supported.`);
throw new BrainFlowError(BrainFlowExitCodes.GENERAL_ERROR, `OS ${platform} is not supported.`);
}
}

Expand All @@ -113,7 +114,7 @@ class BoardControllerDLL extends BoardControllerFunctions {
return lib;
} catch (err) {
console.error(err);
throw new Error(`${'Could not load BoardController DLL - path://'}${this.dllPath}`);
throw new BrainFlowError(BrainFlowExitCodes.GENERAL_ERROR, `${'Could not load BoardController DLL - path://'}${this.dllPath}`);
}
}
}
Expand All @@ -139,72 +140,72 @@ export class BoardShim {
const prepared = [0];
const res = this.boardController.isPrepared(prepared, this.boardId, this.inputJson);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not check prepared state');
throw new BrainFlowError(res, 'Could not check prepared state');
}
return !!prepared[0];
}

public addStreamer(streamerParams: string, preset = BrainFlowPresets.DEFAULT_PRESET): void {
const res = this.boardController.addStreamer(streamerParams, preset, this.boardId, this.inputJson);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not add streamer');
throw new BrainFlowError(res, 'Could not add streamer');
}
}

public deleteStreamer(streamerParams: string, preset = BrainFlowPresets.DEFAULT_PRESET): void {
const res = this.boardController.deleteStreamer(streamerParams, preset, this.boardId, this.inputJson);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not delete streamer');
throw new BrainFlowError(res, 'Could not delete streamer');
}
}

public insertMarker(value: number, preset = BrainFlowPresets.DEFAULT_PRESET): void {
const res = this.boardController.insertMarker(value, preset, this.boardId, this.inputJson);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not insert marker');
throw new BrainFlowError(res, 'Could not insert marker');
}
}

public setLogLevel(logLevel: LogLevels): void {
const res = this.boardController.setLogLevelBoardController(logLevel);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not set log level properly');
throw new BrainFlowError(res, 'Could not set log level properly');
}
}

public setLogFile(file: string): void {
const res = this.boardController.setLogFileBoardController(file);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not redirect to log file');
throw new BrainFlowError(res, 'Could not redirect to log file');
}
}

public logMessage(logLevel: LogLevels, message: string): void {
const res = this.boardController.logMessageBoardController(logLevel, message);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not writte message');
throw new BrainFlowError(res, 'Could not writte message');
}
}

public prepareSession(): void {
const res = this.boardController.prepareSession(this.boardId, this.inputJson);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not prepare session');
throw new BrainFlowError(res, 'Could not prepare session');
}
}

public startStream(numSamples = 1800 * 250, streamerParams = null): void {
const res = this.boardController.startStream(numSamples, streamerParams, this.boardId, this.inputJson);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not start stream');
throw new BrainFlowError(res, 'Could not start stream');
}
}

public getBoardDataCount(preset = BrainFlowPresets.DEFAULT_PRESET): number {
const dataSize = [0];
const res = this.boardController.getBoardDataCount(preset, dataSize, this.boardId, this.inputJson);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get board data count');
throw new BrainFlowError(res, 'Could not get board data count');
}
return dataSize[0];
}
Expand All @@ -219,7 +220,7 @@ export class BoardShim {
const dataArr = [...new Array(packageLength * dataSize).fill(0)];
const res = this.boardController.getBoardData(dataSize, preset, dataArr, this.boardId, this.inputJson);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get board data');
throw new BrainFlowError(res, 'Could not get board data');
}
return _.chunk(dataArr, dataSize);
}
Expand All @@ -237,7 +238,7 @@ export class BoardShim {
this.inputJson,
);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get current board data');
throw new BrainFlowError(res, 'Could not get current board data');
}
if (!currentSize.length) {
return null;
Expand All @@ -248,29 +249,29 @@ export class BoardShim {
public releaseAllSessions(): void {
const res = this.boardController.releaseAllSessions();
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not release all sessions');
throw new BrainFlowError(res, 'Could not release all sessions');
}
}

public releaseSession(): void {
const res = this.boardController.releaseSession(this.boardId, this.inputJson);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not release session');
throw new BrainFlowError(res, 'Could not release session');
}
}

public stopStream(): void {
const res = this.boardController.stopStream(this.boardId, this.inputJson);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not stop stream');
throw new BrainFlowError(res, 'Could not stop stream');
}
}

public getNumRows(boardId: BoardIds, preset = BrainFlowPresets.DEFAULT_PRESET): number {
const numRows = [0];
const res = this.boardController.getNumRows(boardId, preset, numRows);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get num rows');
throw new BrainFlowError(res, 'Could not get num rows');
}
return numRows[0];
}
Expand All @@ -279,7 +280,7 @@ export class BoardShim {
const value = [0];
const res = this.boardController.getPackageNumChannel(boardId, preset, value);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get such data from this device');
throw new BrainFlowError(res, 'Could not get such data from this device');
}
return value[0];
}
Expand All @@ -288,7 +289,7 @@ export class BoardShim {
const value = [0];
const res = this.boardController.getTimestampChannel(boardId, preset, value);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get such data from this device');
throw new BrainFlowError(res, 'Could not get such data from this device');
}
return value[0];
}
Expand All @@ -297,7 +298,7 @@ export class BoardShim {
const value = [0];
const res = this.boardController.getMarkerChannel(boardId, preset, value);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get such data from this device');
throw new BrainFlowError(res, 'Could not get such data from this device');
}
return value[0];
}
Expand All @@ -306,7 +307,7 @@ export class BoardShim {
const value = [0];
const res = this.boardController.getBatteryChannel(boardId, preset, value);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get such data from this device');
throw new BrainFlowError(res, 'Could not get such data from this device');
}
return value[0];
}
Expand All @@ -315,7 +316,7 @@ export class BoardShim {
const samplingRate = [0];
const res = this.boardController.getSamplingRate(boardId, preset, samplingRate);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get sampling rate');
throw new BrainFlowError(res, 'Could not get sampling rate');
}
return samplingRate[0];
}
Expand All @@ -325,7 +326,7 @@ export class BoardShim {
const eegChannels = [...new Array(512).fill(0)];
const res = this.boardController.getEegChannels(boardId, preset, eegChannels, numChannels);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get board info');
throw new BrainFlowError(res, 'Could not get board info');
}
return eegChannels.slice(0, numChannels[0]);
}
Expand All @@ -335,7 +336,7 @@ export class BoardShim {
const сhannels = [...new Array(512).fill(0)];
const res = this.boardController.getExgChannels(boardId, preset, сhannels, numChannels);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get board info');
throw new BrainFlowError(res, 'Could not get board info');
}
return сhannels.slice(0, numChannels[0]);
}
Expand All @@ -345,7 +346,7 @@ export class BoardShim {
const сhannels = [...new Array(512).fill(0)];
const res = this.boardController.getEmgChannels(boardId, preset, сhannels, numChannels);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get board info');
throw new BrainFlowError(res, 'Could not get board info');
}
return сhannels.slice(0, numChannels[0]);
}
Expand All @@ -355,7 +356,7 @@ export class BoardShim {
const сhannels = [...new Array(512).fill(0)];
const res = this.boardController.getEcgChannels(boardId, preset, сhannels, numChannels);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get board info');
throw new BrainFlowError(res, 'Could not get board info');
}
return сhannels.slice(0, numChannels[0]);
}
Expand All @@ -365,7 +366,7 @@ export class BoardShim {
const сhannels = [...new Array(512).fill(0)];
const res = this.boardController.getEogChannels(boardId, preset, сhannels, numChannels);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get board info');
throw new BrainFlowError(res, 'Could not get board info');
}
return сhannels.slice(0, numChannels[0]);
}
Expand All @@ -375,7 +376,7 @@ export class BoardShim {
const сhannels = [...new Array(512).fill(0)];
const res = this.boardController.getPpgChannels(boardId, preset, сhannels, numChannels);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get board info');
throw new BrainFlowError(res, 'Could not get board info');
}
return сhannels.slice(0, numChannels[0]);
}
Expand All @@ -385,7 +386,7 @@ export class BoardShim {
const сhannels = [...new Array(512).fill(0)];
const res = this.boardController.getEdaChannels(boardId, preset, сhannels, numChannels);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get board info');
throw new BrainFlowError(res, 'Could not get board info');
}
return сhannels.slice(0, numChannels[0]);
}
Expand All @@ -395,7 +396,7 @@ export class BoardShim {
const сhannels = [...new Array(512).fill(0)];
const res = this.boardController.getAccelChannels(boardId, preset, сhannels, numChannels);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get board info');
throw new BrainFlowError(res, 'Could not get board info');
}
return сhannels.slice(0, numChannels[0]);
}
Expand All @@ -405,7 +406,7 @@ export class BoardShim {
const сhannels = [...new Array(512).fill(0)];
const res = this.boardController.getAnalogChannels(boardId, preset, сhannels, numChannels);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get board info');
throw new BrainFlowError(res, 'Could not get board info');
}
return сhannels.slice(0, numChannels[0]);
}
Expand All @@ -415,7 +416,7 @@ export class BoardShim {
const сhannels = [...new Array(512).fill(0)];
const res = this.boardController.getGyroChannels(boardId, preset, сhannels, numChannels);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get board info');
throw new BrainFlowError(res, 'Could not get board info');
}
return сhannels.slice(0, numChannels[0]);
}
Expand All @@ -425,7 +426,7 @@ export class BoardShim {
const сhannels = [...new Array(512).fill(0)];
const res = this.boardController.getOtherChannels(boardId, preset, сhannels, numChannels);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get board info');
throw new BrainFlowError(res, 'Could not get board info');
}
return сhannels.slice(0, numChannels[0]);
}
Expand All @@ -435,7 +436,7 @@ export class BoardShim {
const сhannels = [...new Array(512).fill(0)];
const res = this.boardController.getTemperatureChannels(boardId, preset, сhannels, numChannels);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get board info');
throw new BrainFlowError(res, 'Could not get board info');
}
return сhannels.slice(0, numChannels[0]);
}
Expand All @@ -445,7 +446,7 @@ export class BoardShim {
const сhannels = [...new Array(512).fill(0)];
const res = this.boardController.getResistanceChannels(boardId, preset, сhannels, numChannels);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get board info');
throw new BrainFlowError(res, 'Could not get board info');
}
return сhannels.slice(0, numChannels[0]);
}
Expand All @@ -455,7 +456,7 @@ export class BoardShim {
const сhannels = [...new Array(512).fill(0)];
const res = this.boardController.getMagnetometerChannels(boardId, preset, сhannels, numChannels);
if (res !== BrainFlowExitCodes.STATUS_OK) {
throw new Error('Could not get board info');
throw new BrainFlowError(res, 'Could not get board info');
}
return сhannels.slice(0, numChannels[0]);
}
Expand Down
12 changes: 12 additions & 0 deletions nodejs_package/brainflow/brainflow.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
export class BrainFlowError extends Error {
__proto__ = Error

public exitCode: number;

constructor(errorCode: number, msg: string) {
super("Error code is: " + errorCode + " " + msg);
this.exitCode = errorCode;
Object.setPrototypeOf(this, BrainFlowError.prototype);
}
}

// --------------------------- //
// I. BoardShim enums
// --------------------------- //
Expand Down
Loading

0 comments on commit 6724fb0

Please sign in to comment.