Skip to content

Commit 0c3b560

Browse files
author
AJ Keller
committed
Finished adding in the new parsers
1 parent d4f5fd7 commit 0c3b560

File tree

2 files changed

+71
-89
lines changed

2 files changed

+71
-89
lines changed

openBCIUtilities.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -884,31 +884,30 @@ function newSyncObject () {
884884
*/
885885
function transformRawDataPacketsToSample(o) {
886886

887-
if (o.rawDataPackets.byteLength !== k.OBCIPacketSize) return;
888887
let samples = [];
889-
const packetType = getRawPacketType(o.rawDataPacketBuffer[k.OBCIPacketPositionStopByte]);
890888
for (let i = 0; i < o.rawDataPackets.length; i++) {
891889
const rawDataPacket = o.rawDataPackets[i];
890+
const packetType = getRawPacketType(rawDataPacket[k.OBCIPacketPositionStopByte]);
892891
let sample;
893892
try {
894893
switch (packetType) {
895894
case k.OBCIStreamPacketStandardAccel:
896-
sample = parsePacketStandardAccel({
895+
sample = utilitiesModule.parsePacketStandardAccel({
897896
rawDataPacket,
898897
gains: o.gains,
899898
scale: o.scale
900899
});
901900
break;
902901
case k.OBCIStreamPacketStandardRawAux:
903-
sample = parsePacketStandardRawAux({
902+
sample = utilitiesModule.parsePacketStandardRawAux({
904903
rawDataPacket,
905904
gains: o.gains,
906905
scale: o.scale
907906
});
908907
break;
909908
case k.OBCIStreamPacketAccelTimeSyncSet:
910909
case k.OBCIStreamPacketAccelTimeSynced:
911-
sample = parsePacketTimeSyncedAccel({
910+
sample = utilitiesModule.parsePacketTimeSyncedAccel({
912911
rawDataPacket,
913912
gains: o.gains,
914913
timeOffset: o.timeOffset,
@@ -917,7 +916,7 @@ function transformRawDataPacketsToSample(o) {
917916
break;
918917
case k.OBCIStreamPacketRawAuxTimeSyncSet:
919918
case k.OBCIStreamPacketRawAuxTimeSynced:
920-
sample = parsePacketTimeSyncedRawAux({
919+
sample = utilitiesModule.parsePacketTimeSyncedRawAux({
921920
rawDataPacket,
922921
gains: o.gains,
923922
timeOffset: o.timeOffset
@@ -936,6 +935,7 @@ function transformRawDataPacketsToSample(o) {
936935
}
937936
}
938937

938+
return samples;
939939
}
940940

941941
/**

test/openBCIUtilities-test.js

Lines changed: 65 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
* Created by ajk on 12/15/15.
33
*/
44
'use strict';
5-
let bluebirdChecks = require('./bluebirdChecks');
6-
let openBCIUtilities = require('../openBCIUtilities');
7-
let chai = require('chai');
8-
let expect = chai.expect;
9-
let assert = chai.assert;
10-
let should = chai.should(); // eslint-disable-line no-unused-lets
11-
12-
let chaiAsPromised = require('chai-as-promised');
13-
let sinonChai = require('sinon-chai');
5+
const bluebirdChecks = require('./bluebirdChecks');
6+
const openBCIUtilities = require('../openBCIUtilities');
7+
const sinon = require('sinon');
8+
const chai = require('chai');
9+
const expect = chai.expect;
10+
const assert = chai.assert;
11+
const should = chai.should(); // eslint-disable-line no-unused-lets
12+
13+
const chaiAsPromised = require('chai-as-promised');
14+
const sinonChai = require('sinon-chai');
1415
chai.use(chaiAsPromised);
1516
chai.use(sinonChai);
16-
let bufferEqual = require('buffer-equal');
17+
const bufferEqual = require('buffer-equal');
1718

1819
let k = require('../openBCIConstants');
1920

@@ -1784,156 +1785,137 @@ describe('#extractRawDataPackets', function () {
17841785
* Test the function that routes raw packets for processing
17851786
*/
17861787
describe('#transformRawDataPacketsToSamples', function () {
1787-
var ourBoard;
1788-
var funcSpyTimeSyncSet, funcSpyTimeSyncedAccel, funcSpyTimeSyncedRawAux, funcSpyStandardRawAux, funcSpyStandardAccel;
1788+
var funcSpyTimeSyncedAccel, funcSpyTimeSyncedRawAux, funcSpyStandardRawAux, funcSpyStandardAccel;
17891789

17901790
before(function () {
1791-
ourBoard = new openBCIBoard.OpenBCIBoard({
1792-
verbose: true
1793-
});
17941791
// Put watchers on all functions
1795-
funcSpyStandardAccel = sinon.spy(ourBoard, '_processPacketStandardAccel');
1796-
funcSpyStandardRawAux = sinon.spy(ourBoard, '_processPacketStandardRawAux');
1797-
funcSpyTimeSyncSet = sinon.spy(ourBoard, '_processPacketTimeSyncSet');
1798-
funcSpyTimeSyncedAccel = sinon.spy(ourBoard, '_processPacketTimeSyncedAccel');
1799-
funcSpyTimeSyncedRawAux = sinon.spy(ourBoard, '_processPacketTimeSyncedRawAux');
1792+
funcSpyStandardAccel = sinon.spy(openBCIUtilities, 'parsePacketStandardAccel');
1793+
funcSpyStandardRawAux = sinon.spy(openBCIUtilities, 'parsePacketStandardRawAux');
1794+
funcSpyTimeSyncedAccel = sinon.spy(openBCIUtilities, 'parsePacketTimeSyncedAccel');
1795+
funcSpyTimeSyncedRawAux = sinon.spy(openBCIUtilities, 'parsePacketTimeSyncedRawAux');
18001796
});
18011797
beforeEach(function () {
18021798
funcSpyStandardAccel.reset();
18031799
funcSpyStandardRawAux.reset();
1804-
funcSpyTimeSyncSet.reset();
18051800
funcSpyTimeSyncedAccel.reset();
18061801
funcSpyTimeSyncedRawAux.reset();
1807-
1808-
ourBoard.sync.curSyncObj = openBCISample.newSyncObject();
18091802
});
18101803
after(function () {
18111804
// ourBoard = null
18121805
});
18131806
after(() => bluebirdChecks.noPendingPromises());
18141807

18151808
it('should process a standard packet', function () {
1816-
var buffer = openBCISample.samplePacket(0);
1809+
var buffer = openBCIUtilities.samplePacket(0);
18171810

18181811
// Call the function under test
1819-
ourBoard._processQualifiedPacket(buffer);
1812+
const samples = openBCIUtilities.transformRawDataPacketsToSample({
1813+
rawDataPackets: [buffer],
1814+
gains: defaultChannelSettingsArray
1815+
});
18201816

18211817
// Ensure that we extracted only one buffer
18221818
funcSpyStandardAccel.should.have.been.calledOnce;
1819+
expect(samples.length).to.equal(1);
1820+
});
1821+
1822+
it('should process a standard packet', function () {
1823+
// Call the function under test
1824+
const samples = openBCIUtilities.transformRawDataPacketsToSample({
1825+
rawDataPackets: [
1826+
openBCIUtilities.samplePacket(0),
1827+
openBCIUtilities.samplePacket(1),
1828+
openBCIUtilities.samplePacket(2)
1829+
],
1830+
gains: defaultChannelSettingsArray
1831+
});
1832+
1833+
// Ensure that we extracted only one buffer
1834+
funcSpyStandardAccel.should.have.been.calledThrice;
18231835
});
18241836
it('should process a standard packet with raw aux', function () {
1825-
var buffer = openBCISample.samplePacketStandardRawAux(0);
1837+
var buffer = openBCIUtilities.samplePacketStandardRawAux(0);
18261838

18271839
// Call the function under test
1828-
ourBoard._processQualifiedPacket(buffer);
1840+
openBCIUtilities.transformRawDataPacketsToSample({
1841+
rawDataPackets: [buffer]
1842+
});
18291843

18301844
// Ensure that we extracted only one buffer
18311845
funcSpyStandardRawAux.should.have.been.calledOnce;
18321846
});
18331847
it('should call nothing for a user defined packet type ', function () {
1834-
var buffer = openBCISample.samplePacketUserDefined();
1848+
var buffer = openBCIUtilities.samplePacketUserDefined();
18351849

18361850
// Call the function under test
1837-
ourBoard._processQualifiedPacket(buffer);
1851+
openBCIUtilities.transformRawDataPacketsToSample({
1852+
rawDataPackets: [buffer]
1853+
});
18381854

18391855
// Nothing should be called
18401856
funcSpyStandardAccel.should.not.have.been.called;
18411857
funcSpyStandardRawAux.should.not.have.been.called;
1842-
funcSpyTimeSyncSet.should.not.have.been.called;
18431858
funcSpyTimeSyncedAccel.should.not.have.been.called;
18441859
funcSpyTimeSyncedRawAux.should.not.have.been.called;
18451860
});
18461861
it('should process a time sync set packet with accel', function () {
1847-
var buffer = openBCISample.samplePacketAccelTimeSyncSet();
1862+
var buffer = openBCIUtilities.samplePacketAccelTimeSyncSet();
18481863

18491864
// Call the function under test
1850-
ourBoard._processQualifiedPacket(buffer);
1865+
openBCIUtilities.transformRawDataPacketsToSample({
1866+
rawDataPackets: [buffer]
1867+
});
18511868

1852-
// We should call to sync up
1853-
funcSpyTimeSyncSet.should.have.been.calledOnce;
1854-
funcSpyTimeSyncSet.should.have.been.calledWith(buffer);
18551869
// we should call to get a packet
18561870
funcSpyTimeSyncedAccel.should.have.been.calledOnce;
1857-
funcSpyTimeSyncedAccel.should.have.been.calledWith(buffer);
18581871
});
18591872
it('should process a time synced packet with accel', function () {
1860-
var buffer = openBCISample.samplePacketAccelTimeSynced(0);
1873+
var buffer = openBCIUtilities.samplePacketAccelTimeSynced(0);
18611874

18621875
// Call the function under test
1863-
ourBoard._processQualifiedPacket(buffer);
1876+
openBCIUtilities.transformRawDataPacketsToSample({
1877+
rawDataPackets: [buffer]
1878+
});
18641879

18651880
// Ensure that we extracted only one buffer
18661881
funcSpyTimeSyncedAccel.should.have.been.calledOnce;
18671882
});
18681883
it('should process a time sync set packet with raw aux', function () {
1869-
var buffer = openBCISample.samplePacketRawAuxTimeSyncSet(0);
1884+
var buffer = openBCIUtilities.samplePacketRawAuxTimeSyncSet(0);
18701885

18711886
// Call the function under test
1872-
ourBoard._processQualifiedPacket(buffer);
1887+
openBCIUtilities.transformRawDataPacketsToSample({
1888+
rawDataPackets: [buffer]
1889+
});
18731890

1874-
// We should call to sync up
1875-
funcSpyTimeSyncSet.should.have.been.calledOnce;
1876-
funcSpyTimeSyncSet.should.have.been.calledWith(buffer);
1877-
// we should call to get a packet
18781891
funcSpyTimeSyncedRawAux.should.have.been.calledOnce;
1879-
funcSpyTimeSyncedRawAux.should.have.been.calledWith(buffer);
18801892
});
18811893
it('should process a time synced packet with raw aux', function () {
1882-
var buffer = openBCISample.samplePacketRawAuxTimeSynced(0);
1894+
var buffer = openBCIUtilities.samplePacketRawAuxTimeSynced(0);
18831895

18841896
// Call the function under test
1885-
ourBoard._processQualifiedPacket(buffer);
1897+
openBCIUtilities.transformRawDataPacketsToSample({
1898+
rawDataPackets: [buffer]
1899+
});
18861900

18871901
// Ensure that we extracted only one buffer
18881902
funcSpyTimeSyncedRawAux.should.have.been.calledOnce;
18891903
});
18901904
it('should not identify any packet', function () {
1891-
var buffer = openBCISample.samplePacket(0);
1905+
var buffer = openBCIUtilities.samplePacket(0);
18921906

18931907
// Set the stop byte to some number not yet defined
18941908
buffer[k.OBCIPacketPositionStopByte] = 0xCF;
18951909

18961910
// Call the function under test
1897-
ourBoard._processDataBuffer(buffer);
1911+
openBCIUtilities.transformRawDataPacketsToSample({
1912+
rawDataPackets: [buffer]
1913+
});
18981914

18991915
// Nothing should be called
19001916
funcSpyStandardAccel.should.not.have.been.called;
19011917
funcSpyStandardRawAux.should.not.have.been.called;
1902-
funcSpyTimeSyncSet.should.not.have.been.called;
19031918
funcSpyTimeSyncedAccel.should.not.have.been.called;
19041919
funcSpyTimeSyncedRawAux.should.not.have.been.called;
19051920
});
1906-
it('should emit a dropped packet on dropped packet', function (done) {
1907-
// Set to default state
1908-
ourBoard.previousSampleNumber = -1;
1909-
var sampleNumber0 = openBCISample.samplePacket(0);
1910-
ourBoard.once('droppedPacket', () => {
1911-
done();
1912-
});
1913-
var sampleNumber2 = openBCISample.samplePacket(2);
1914-
// Call the function under test
1915-
ourBoard._processDataBuffer(sampleNumber0);
1916-
ourBoard._processDataBuffer(sampleNumber2);
1917-
});
1918-
it('should emit a dropped packet on dropped packet with edge', function (done) {
1919-
// Set to default state
1920-
var count = 0;
1921-
ourBoard.previousSampleNumber = 253;
1922-
var buf1 = openBCISample.samplePacket(254);
1923-
var countFunc = arr => {
1924-
count++;
1925-
};
1926-
ourBoard.on('droppedPacket', countFunc);
1927-
var buf2 = openBCISample.samplePacket(0);
1928-
var buf3 = openBCISample.samplePacket(1);
1929-
// Call the function under test
1930-
ourBoard._processDataBuffer(buf1);
1931-
ourBoard._processDataBuffer(buf2);
1932-
ourBoard._processDataBuffer(buf3);
1933-
setTimeout(() => {
1934-
ourBoard.removeListener('droppedPacket', countFunc);
1935-
expect(count).to.equal(1);
1936-
done();
1937-
}, 10);
1938-
});
19391921
});

0 commit comments

Comments
 (0)