Skip to content

Commit 065a88f

Browse files
author
AJ Keller
authored
Merge pull request #21 from OpenBCI/development
Development
2 parents 8e631be + 7788d0d commit 065a88f

File tree

4 files changed

+122
-148
lines changed

4 files changed

+122
-148
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# v0.4.1
2+
3+
### Bug Fixes
4+
5+
* DELETE, GET, and POST would resolve even if code was not equal to 200
6+
17
# v0.4.0
28

39
### New Features

examples/getStreaming/getStreaming.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* do `npm install`
1010
* then `npm start`
1111
*/
12-
let debug = true; // Pretty print any bytes in and out... it's amazing...
12+
let debug = false; // Pretty print any bytes in and out... it's amazing...
1313
let verbose = true; // Adds verbosity to functions
14-
const protocol = 'udp'; // or 'udp'
14+
const protocol = 'tcp'; // or 'udp'
1515

1616
const k = require('openbci-utilities').Constants;
1717
let Wifi = require('../../openBCIWifi');

openBCIWifi.js

Lines changed: 113 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,6 @@ Wifi.prototype.searchToStream = function (o) {
522522
})
523523
.catch((err) => {
524524
reject(err);
525-
console.log(err);
526525
});
527526
});
528527
this.searchStart().catch(console.log);
@@ -575,7 +574,7 @@ Wifi.prototype.syncSampleRate = function () {
575574
this._sampleRate = Number(res.match(numPattern)[0]);
576575
resolve(this._sampleRate);
577576
} else {
578-
reject(res);
577+
reject(Error(`syncSampleRate: MESSAGE:${res.message}`));
579578
}
580579
})
581580
.catch((err) => {
@@ -703,7 +702,7 @@ Wifi.prototype.syncRegisterSettings = function () {
703702
return new Promise((resolve, reject) => {
704703
this.write(k.OBCIMiscQueryRegisterSettings)
705704
.then((res) => {
706-
this._rawDataPacketToSample.data = Buffer.from(res);
705+
this._rawDataPacketToSample.data = Buffer.from(res.message);
707706
try {
708707
obciUtils.syncChannelSettingsWithRawData(this._rawDataPacketToSample);
709708
resolve(this._rawDataPacketToSample.channelSettings);
@@ -801,9 +800,9 @@ Wifi.prototype.streamStop = function () {
801800
*/
802801
Wifi.prototype.syncInfo = function (o) {
803802
return this.get('/board')
804-
.then((info) => {
803+
.then((res) => {
805804
try {
806-
info = JSON.parse(info);
805+
const info = JSON.parse(res.message);
807806
this._boardConnected = info['board_connected'];
808807
this._numberOfChannels = info['num_channels'];
809808
this._boardType = info['board_type'];
@@ -827,14 +826,18 @@ Wifi.prototype.syncInfo = function (o) {
827826
return Promise.reject(err);
828827
}
829828
})
830-
.then((allInfo) => {
831-
allInfo = JSON.parse(allInfo);
832-
this._shieldName = allInfo.name;
833-
this._macAddress = allInfo.mac;
834-
this._version = allInfo.version;
835-
this._latency = allInfo.latency;
836-
this._allInfo = allInfo;
837-
return Promise.resolve(this._boardInfo);
829+
.then((res) => {
830+
try {
831+
const allInfo = JSON.parse(res.message);
832+
this._shieldName = allInfo.name;
833+
this._macAddress = allInfo.mac;
834+
this._version = allInfo.version;
835+
this._latency = allInfo.latency;
836+
this._allInfo = allInfo;
837+
return Promise.resolve(this._boardInfo);
838+
} catch (err) {
839+
return Promise.reject(err);
840+
}
838841
})
839842
.catch((err) => {
840843
console.log(err);
@@ -861,7 +864,7 @@ Wifi.prototype.write = function (data) {
861864
if (this.options.debug) obciDebug.debugBytes('>>>', data);
862865
this.post('/command', {'command': data.toString()})
863866
.then((res) => {
864-
resolve(res);
867+
resolve(res.message);
865868
})
866869
.catch((err) => {
867870
reject(err);
@@ -1090,56 +1093,64 @@ Wifi.prototype.wifiInitServer = function () {
10901093

10911094
/**
10921095
* Used to process a response from either a GET, POST, or DELETE.
1093-
* @param res {String} The response from the server or client
1096+
* @param res {Object} The response from the server or client
10941097
* @param cb {callback} Callback to know the response and everything is done. Can contain the message.
10951098
* @private
10961099
*/
1097-
Wifi.prototype._processResponse = function (res, cb) {
1098-
if (this.options.verbose) {
1099-
console.log(`STATUS: ${res.statusCode}`);
1100-
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
1101-
}
1102-
res.setEncoding('utf8');
1103-
let msg = '';
1104-
res.on('data', (chunk) => {
1105-
if (this.options.verbose) console.log(`BODY: ${chunk}`);
1106-
msg += chunk.toString();
1107-
});
1108-
res.once('end', () => {
1109-
if (this.options.verbose) console.log('No more data in response.');
1110-
this.emit('res', msg);
1111-
if (res.statusCode !== 200) {
1112-
if (cb) cb(msg);
1113-
} else {
1114-
if (cb) cb();
1100+
Wifi.prototype._processResponse = function (res) {
1101+
return new Promise((resolve, reject) => {
1102+
if (this.options.verbose) {
1103+
console.log(`STATUS: ${res.statusCode}`);
1104+
// console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
11151105
}
1116-
});
1117-
};
1118-
1119-
Wifi.prototype._delete = function (host, path, cb) {
1120-
const options = {
1121-
host: host,
1122-
port: 80,
1123-
path: path,
1124-
method: 'DELETE'
1125-
};
1126-
1127-
const req = http.request(options, (res) => {
1128-
this._processResponse(res, (err) => {
1129-
if (err) {
1130-
if (cb) cb(err);
1106+
let processResponseTimeout = setTimeout(() => {
1107+
reject(Error('Timeout waiting for response'));
1108+
}, 2000);
1109+
res.setEncoding('utf8');
1110+
let msg = '';
1111+
res.on('data', (chunk) => {
1112+
msg += chunk.toString();
1113+
});
1114+
res.once('end', () => {
1115+
clearTimeout(processResponseTimeout);
1116+
if (this.options.verbose) console.log('No more data in response.');
1117+
res['msg'] = msg;
1118+
if (this.options.verbose) console.log(`BODY: ${msg}`);
1119+
if (res.statusCode !== 200) {
1120+
reject(Error(`ERROR: CODE: ${res.statusCode} MESSAGE: ${res.msg}`));
11311121
} else {
1132-
if (cb) cb();
1122+
resolve({
1123+
message: res.msg,
1124+
code: res.statusCode
1125+
});
11331126
}
11341127
});
11351128
});
11361129

1137-
req.once('error', (e) => {
1138-
if (this.options.verbose) console.log(`DELETE problem with request: ${e.message}`);
1139-
if (cb) cb(e);
1140-
});
1130+
};
1131+
1132+
Wifi.prototype._delete = function (host, path) {
1133+
return new Promise((resolve, reject) => {
1134+
const options = {
1135+
host: host,
1136+
port: 80,
1137+
path: path,
1138+
method: 'DELETE'
1139+
};
1140+
1141+
const req = http.request(options, (res) => {
1142+
this._processResponse(res)
1143+
.then(resolve)
1144+
.catch(reject);
1145+
});
1146+
1147+
req.once('error', (e) => {
1148+
if (this.options.verbose) console.log(`DELETE problem with request: ${e.message}`);
1149+
reject(e);
1150+
});
11411151

1142-
req.end();
1152+
req.end();
1153+
});
11431154
};
11441155

11451156
/**
@@ -1148,47 +1159,32 @@ Wifi.prototype._delete = function (host, path, cb) {
11481159
* @returns {Promise} - Resolves if gets a response from the client/server, rejects with error
11491160
*/
11501161
Wifi.prototype.delete = function (path) {
1162+
if (this.options.verbose) console.log(`-> DELETE: ${this._ipAddress}${path}`);
1163+
return this._delete(this._ipAddress, path);
1164+
};
1165+
1166+
Wifi.prototype._get = function (host, path) {
11511167
return new Promise((resolve, reject) => {
1152-
const resFunc = (res) => {
1153-
resolve(res);
1168+
const options = {
1169+
host: host,
1170+
port: 80,
1171+
path: path,
1172+
method: 'GET'
11541173
};
1155-
this.once('res', resFunc);
1156-
if (this.options.verbose) console.log(`-> DELETE: ${this._ipAddress}${path}`);
1157-
this._delete(this._ipAddress, path, (err) => {
1158-
if (err) {
1159-
if (this.options.verbose) {
1160-
this.removeListener('res', resFunc);
1161-
reject(err);
1162-
}
1163-
}
1164-
})
1165-
});
1166-
};
11671174

1168-
Wifi.prototype._get = function (host, path, cb) {
1169-
const options = {
1170-
host: host,
1171-
port: 80,
1172-
path: path,
1173-
method: 'GET'
1174-
};
1175-
1176-
const req = http.request(options, (res) => {
1177-
this._processResponse(res, (err) => {
1178-
if (err) {
1179-
if (cb) cb(err);
1180-
} else {
1181-
if (cb) cb();
1182-
}
1175+
const req = http.request(options, (res) => {
1176+
this._processResponse(res)
1177+
.then(resolve)
1178+
.catch(reject);
11831179
});
1184-
});
11851180

1186-
req.once('error', (e) => {
1187-
if (this.options.verbose) console.log(`problem with request: ${e.message}`);
1188-
if (cb) cb(e);
1189-
});
1181+
req.once('error', (e) => {
1182+
if (this.options.verbose) console.log(`GET problem with request: ${e.message}`);
1183+
reject(e);
1184+
});
11901185

1191-
req.end();
1186+
req.end();
1187+
})
11921188
};
11931189

11941190
/**
@@ -1197,54 +1193,39 @@ Wifi.prototype._get = function (host, path, cb) {
11971193
* @returns {Promise} - Resolves if gets/with a response from the client/server, rejects with error
11981194
*/
11991195
Wifi.prototype.get = function (path) {
1196+
if (this.options.verbose) console.log(`-> GET: ${this._ipAddress}${path}`);
1197+
return this._get(this._ipAddress, path);
1198+
};
1199+
1200+
Wifi.prototype._post = function (host, path, payload) {
12001201
return new Promise((resolve, reject) => {
1201-
const resFunc = (res) => {
1202-
resolve(res);
1203-
};
1204-
this.once('res', resFunc);
1205-
if (this.options.verbose) console.log(`-> GET: ${this._ipAddress}${path}`);
1206-
this._get(this._ipAddress, path, (err) => {
1207-
if (err) {
1208-
if (this.options.verbose) {
1209-
this.removeListener('res', resFunc);
1210-
reject(err);
1211-
}
1202+
const output = JSON.stringify(payload);
1203+
const options = {
1204+
host: host,
1205+
port: 80,
1206+
path: path,
1207+
method: 'POST',
1208+
headers: {
1209+
'Content-Type': 'application/json',
1210+
'Content-Length': output.length
12121211
}
1213-
})
1214-
});
1215-
};
1212+
};
12161213

1217-
Wifi.prototype._post = function (host, path, payload, cb) {
1218-
const output = JSON.stringify(payload);
1219-
const options = {
1220-
host: host,
1221-
port: 80,
1222-
path: path,
1223-
method: 'POST',
1224-
headers: {
1225-
'Content-Type': 'application/json',
1226-
'Content-Length': output.length
1227-
}
1228-
};
1214+
const req = http.request(options, (res) => {
1215+
this._processResponse(res)
1216+
.then(resolve)
1217+
.catch(reject);
1218+
});
12291219

1230-
const req = http.request(options, (res) => {
1231-
this._processResponse(res, (err) => {
1232-
if (err) {
1233-
if (cb) cb.call(this, err);
1234-
} else {
1235-
if (cb) cb.call(this);
1236-
}
1220+
req.once('error', (e) => {
1221+
if (this.options.verbose) console.log(`problem with request: ${e.message}`);
1222+
reject(e);
12371223
});
1238-
});
12391224

1240-
req.once('error', (e) => {
1241-
if (this.options.verbose) console.log(`problem with request: ${e.message}`);
1242-
if (cb) cb.call(this, e);
1225+
// write data to request body
1226+
req.write(output);
1227+
req.end();
12431228
});
1244-
1245-
// write data to request body
1246-
req.write(output);
1247-
req.end();
12481229
};
12491230

12501231
/**
@@ -1254,21 +1235,8 @@ Wifi.prototype._post = function (host, path, payload, cb) {
12541235
* @returns {Promise} - Resolves if gets a response from the client/server, rejects with error
12551236
*/
12561237
Wifi.prototype.post = function (path, payload) {
1257-
return new Promise((resolve, reject) => {
1258-
const resFunc = (res) => {
1259-
resolve(res);
1260-
};
1261-
this.once('res', resFunc);
1262-
if (this.options.verbose) console.log(`-> POST: ${this._ipAddress}${path} ${JSON.stringify(payload)}`);
1263-
this._post(this._ipAddress, path, payload, (err) => {
1264-
if (err) {
1265-
if (this.options.verbose) {
1266-
this.removeListener('res', resFunc);
1267-
reject(err);
1268-
}
1269-
}
1270-
})
1271-
});
1238+
if (this.options.verbose) console.log(`-> POST: ${this._ipAddress}${path} ${JSON.stringify(payload)}`);
1239+
return this._post(this._ipAddress, path, payload);
12721240
};
12731241

12741242
module.exports = Wifi;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openbci-wifi",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"description": "The official SDK for the OpenBCI/Push The World Wifi Shield connected to either OpenBCI Cytons or Ganglions.",
55
"main": "openBCIWifi.js",
66
"scripts": {

0 commit comments

Comments
 (0)