@@ -522,7 +522,6 @@ Wifi.prototype.searchToStream = function (o) {
522
522
} )
523
523
. catch ( ( err ) => {
524
524
reject ( err ) ;
525
- console . log ( err ) ;
526
525
} ) ;
527
526
} ) ;
528
527
this . searchStart ( ) . catch ( console . log ) ;
@@ -575,7 +574,7 @@ Wifi.prototype.syncSampleRate = function () {
575
574
this . _sampleRate = Number ( res . match ( numPattern ) [ 0 ] ) ;
576
575
resolve ( this . _sampleRate ) ;
577
576
} else {
578
- reject ( res ) ;
577
+ reject ( Error ( `syncSampleRate: MESSAGE: ${ res . message } ` ) ) ;
579
578
}
580
579
} )
581
580
. catch ( ( err ) => {
@@ -703,7 +702,7 @@ Wifi.prototype.syncRegisterSettings = function () {
703
702
return new Promise ( ( resolve , reject ) => {
704
703
this . write ( k . OBCIMiscQueryRegisterSettings )
705
704
. then ( ( res ) => {
706
- this . _rawDataPacketToSample . data = Buffer . from ( res ) ;
705
+ this . _rawDataPacketToSample . data = Buffer . from ( res . message ) ;
707
706
try {
708
707
obciUtils . syncChannelSettingsWithRawData ( this . _rawDataPacketToSample ) ;
709
708
resolve ( this . _rawDataPacketToSample . channelSettings ) ;
@@ -801,9 +800,9 @@ Wifi.prototype.streamStop = function () {
801
800
*/
802
801
Wifi . prototype . syncInfo = function ( o ) {
803
802
return this . get ( '/board' )
804
- . then ( ( info ) => {
803
+ . then ( ( res ) => {
805
804
try {
806
- info = JSON . parse ( info ) ;
805
+ const info = JSON . parse ( res . message ) ;
807
806
this . _boardConnected = info [ 'board_connected' ] ;
808
807
this . _numberOfChannels = info [ 'num_channels' ] ;
809
808
this . _boardType = info [ 'board_type' ] ;
@@ -827,14 +826,18 @@ Wifi.prototype.syncInfo = function (o) {
827
826
return Promise . reject ( err ) ;
828
827
}
829
828
} )
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
+ }
838
841
} )
839
842
. catch ( ( err ) => {
840
843
console . log ( err ) ;
@@ -861,7 +864,7 @@ Wifi.prototype.write = function (data) {
861
864
if ( this . options . debug ) obciDebug . debugBytes ( '>>>' , data ) ;
862
865
this . post ( '/command' , { 'command' : data . toString ( ) } )
863
866
. then ( ( res ) => {
864
- resolve ( res ) ;
867
+ resolve ( res . message ) ;
865
868
} )
866
869
. catch ( ( err ) => {
867
870
reject ( err ) ;
@@ -1090,56 +1093,64 @@ Wifi.prototype.wifiInitServer = function () {
1090
1093
1091
1094
/**
1092
1095
* 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
1094
1097
* @param cb {callback} Callback to know the response and everything is done. Can contain the message.
1095
1098
* @private
1096
1099
*/
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)}`);
1115
1105
}
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 } ` ) ) ;
1131
1121
} else {
1132
- if ( cb ) cb ( ) ;
1122
+ resolve ( {
1123
+ message : res . msg ,
1124
+ code : res . statusCode
1125
+ } ) ;
1133
1126
}
1134
1127
} ) ;
1135
1128
} ) ;
1136
1129
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
+ } ) ;
1141
1151
1142
- req . end ( ) ;
1152
+ req . end ( ) ;
1153
+ } ) ;
1143
1154
} ;
1144
1155
1145
1156
/**
@@ -1148,47 +1159,32 @@ Wifi.prototype._delete = function (host, path, cb) {
1148
1159
* @returns {Promise } - Resolves if gets a response from the client/server, rejects with error
1149
1160
*/
1150
1161
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 ) {
1151
1167
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'
1154
1173
} ;
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
- } ;
1167
1174
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 ) ;
1183
1179
} ) ;
1184
- } ) ;
1185
1180
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
+ } ) ;
1190
1185
1191
- req . end ( ) ;
1186
+ req . end ( ) ;
1187
+ } )
1192
1188
} ;
1193
1189
1194
1190
/**
@@ -1197,54 +1193,39 @@ Wifi.prototype._get = function (host, path, cb) {
1197
1193
* @returns {Promise } - Resolves if gets/with a response from the client/server, rejects with error
1198
1194
*/
1199
1195
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 ) {
1200
1201
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
1212
1211
}
1213
- } )
1214
- } ) ;
1215
- } ;
1212
+ } ;
1216
1213
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
+ } ) ;
1229
1219
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 ) ;
1237
1223
} ) ;
1238
- } ) ;
1239
1224
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 ( ) ;
1243
1228
} ) ;
1244
-
1245
- // write data to request body
1246
- req . write ( output ) ;
1247
- req . end ( ) ;
1248
1229
} ;
1249
1230
1250
1231
/**
@@ -1254,21 +1235,8 @@ Wifi.prototype._post = function (host, path, payload, cb) {
1254
1235
* @returns {Promise } - Resolves if gets a response from the client/server, rejects with error
1255
1236
*/
1256
1237
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 ) ;
1272
1240
} ;
1273
1241
1274
1242
module . exports = Wifi ;
0 commit comments