Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IN PROGRESS] Features - 2g data GET request #13

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions commandchain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Helper class for building chains in an easy to read way.

Args
commands
Array of command objects. Each object should have the following:
{
message: <String> message to send to the card,
patience: <Number> max milliseconds to wait for the command,
expected: <Array> expected result, will be compared with returned result to determine if call was successful.
}

You can then get and array of all messages, patiences, and expectations for passing to your chain

*/
function CommandChain(commands) {
this._commands = commands;
}

CommandChain.prototype.getMessages = function() {
return this._commands.map(function(obj) {
return obj.message;
});
};

CommandChain.prototype.getPatiences = function() {
return this._commands.map(function(obj) {
return obj.patience;
});
};

CommandChain.prototype.getExpected = function() {
return this._commands.map(function(obj) {
return obj.expected;
});
};

module.exports = CommandChain;
62 changes: 62 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var util = require('util');
var EventEmitter = require('events').EventEmitter;
var Packetizer = require('./packetizer.js');
var Postmaster = require('./postmaster.js');
var CommandChain = require('./commandchain.js');

var DEBUG = false; // Debug messages to the console

Expand Down Expand Up @@ -390,6 +391,67 @@ GPRS.prototype.sendSMS = function (number, message, callback) {
}
};

// perform a simple GET request to a web address
GPRS.prototype.requestGET = function(webAddress, callback) {
/*
Args
webAddress
String, the web address to perform GET request to.
callback
Callback function

Callback parameters
err
Error message or null
success
Boolean, true
*/

var self = this;

if(!webAddress) {
callback(new Error('Did not specify a webAddress'), null);
}

// AT+SAPBR=1,1 tells the module we are ready to send.
// note that in some cases you might need to send credentials
// first, including apn, username, and password.
// But in my testing with a T-Mobile phone+data SIM card,
// this was not needed.
var commands = new CommandChain([{
message: 'AT+SAPBR=1,1',
patience: 5000,
expected: ['AT+SAPBR=1,1', 'OK']
}, {
message: 'AT+HTTPINIT',
patience: 5000,
expected: ['AT+HTTPINIT', 'OK']
}, {
message: 'AT+HTTPPARA="CID",1',
patience: 5000,
expected: ['AT+HTTPPARA="CID",1', 'OK']
}, {
message: 'AT+HTTPPARA="URL","'+webAddress+'"',
patience: 5000,
expected: ['AT+HTTPPARA="URL","'+webAddress+'"', 'OK']
}, {
message: 'AT+HTTPACTION=0', // 0 = GET, 1 = POST
patience: 5000,
expected: ['AT+HTTPACTION=0']
}, {
message: 'AT+HTTPTERM', // terminate HTTP, requiring that we INIT next time
patience: 5000,
expected: ['AT+HTTPTERM']
}]);

self._chain(commands.getMessages(), commands.getPatiences(), commands.getExpected(), function(errr, data) {
if(errr) {
return callback(errr);
}
callback(null, true);
});

}
// Turn the module on or off by switching the power button (G3) electronically
GPRS.prototype.togglePower = function (callback) {
var self = this;
Expand Down