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

[NRY] added sim check #18

Open
wants to merge 1 commit 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: 25 additions & 13 deletions examples/gprs.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,34 @@ var message = 'Text from a Tessel!';
var gprs = gprslib.use(hardware);
gprs.on('ready', function() {
console.log('GPRS module connected to Tessel. Searching for network...')
// Give it 10 more seconds to connect to the network, then try to send an SMS
setTimeout(function() {
console.log('Sending', message, 'to', phoneNumber, '...');
// Send message
gprs.sendSMS(phoneNumber, message, function smsCallback(err, data) {
if (err) {
return console.log(err);
}
var success = data[0] !== -1;
console.log('Text sent:', success);
if (success) {
// If successful, log the number of the sent text
console.log('GPRS Module sent text #', data[0]);
}
gprs.getCIMI(function(err, CIMI){
console.log("got IMSI", CIMI);
gprs.getGSMStatus(function(err, gsm){
console.log("got gsm", gsm);
gprs.getGPRSStatus(function(err, gprs){
console.log("got gprs", gprs);
});
});
});
}, 10000);

// // Give it 10 more seconds to connect to the network, then try to send an SMS
// setTimeout(function() {
// console.log('Sending', message, 'to', phoneNumber, '...');
// // Send message
// gprs.sendSMS(phoneNumber, message, function smsCallback(err, data) {
// if (err) {
// return console.log(err);
// }
// var success = data[0] !== -1;
// console.log('Text sent:', success);
// if (success) {
// // If successful, log the number of the sent text
// console.log('GPRS Module sent text #', data[0]);
// }
// });
// }, 10000);
});

// Emit unsolicited messages beginning with...
Expand Down
58 changes: 40 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,19 @@ GPRS.prototype.answerCall = function (callback) {
});
};

// Send a series of back-to-back messages recursively and do something with the final result. Other results, if not of the form [`messages[n]`, 'OK'] error out and pass false to the callback. The arguments `messages` and `patience` must be of the same length. Like `_txrx`, this function is also useful for expanding the module's functionality.
// Send a series of back-to-back messages recursively and do something with the final result.
// Other results, if not of the form [`messages[n]`, 'OK'] error out and pass false to the
// callback. The arguments `messages` and `patience` must be of the same length. Like `_txrx`,
// this function is also useful for expanding the module's functionality.
GPRS.prototype._chain = function (messages, patiences, replies, callback) {
/*
mesages
An array of Strings to send as commands
patiences
An array of numbers; milliseconds to wait for each command to return
replies
An array of expected replies (arrays of strings). If any index is false-y, its reply simply must not error out.
An array of expected replies (arrays of strings). If any index is false-y, its
reply simply must not error out.
callback
Callback function. Args come from the last function in the chain.

Expand Down Expand Up @@ -196,29 +200,25 @@ GPRS.prototype._chain = function (messages, patiences, replies, callback) {
}
}
}
self.emit('_intermediate', correct);
// self.emit('_intermediate', correct);

if (correct) {
self._chain(messages.slice(1), patiences.slice(1), replies.slice(1), callback);
} else {
self.postmaster.forceClear();
if (callback) {
callback(new Error('Chain broke on ' + messages[0]), false);
}
}
};
// Still more to do in the chain
if (messages.length > 0) {
// if (messages.length > 0) {
var func = (messages.length === 1) ? callback:_intermediate;
if (DEBUG) {
console.log("_txrx sending", messages[0]);
}
self._txrx(messages[0], patiences[0], func, [[messages[0]], [replies[0][replies[0].length - 1]]]);
// If we have more to do before the base case, respond to the '_intermediate' event and keep going
if (func === _intermediate) {
self.once('_intermediate', function (correct) {
if (correct) {
self._chain(messages.slice(1), patiences.slice(1), replies.slice(1), callback);
} else {
self.postmaster.forceClear();
if (callback) {
callback(new Error('Chain broke on ' + messages[0]), false);
}
}
});
}
}
// }
}
};

Expand Down Expand Up @@ -299,6 +299,28 @@ GPRS.prototype._checkEmissions = function () {
});
};

GPRS.prototype.getCIMI = function(callback){
var self = this;
this._txrx('AT+CIMI', 10000, function (err, CIMI) {
callback(err, CIMI);
});
}

GPRS.prototype.getGSMStatus = function(callback){
var self = this;
this._txrx('AT+CREG?', 10000, function (err, status) {
callback(err, status);
});
}

GPRS.prototype.getGPRSStatus = function(callback){
var self = this;
this._txrx('AT+CGREG?', 10000, function (err, status) {
callback(err, status);
});
}


// Many unsolicited events are very useful to the user, such as when an SMS is received or a call is pending. This function configures the module to emit events that beign with a specific String. There is probably a better way to do this, though, so consider the function unstable and pull requests welcome.
GPRS.prototype.emitMe = function (beginnings) {
/*
Expand Down
5 changes: 3 additions & 2 deletions postmaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ Postmaster.prototype.send = function (message, patience, callback, alternate, de
temp(err, data);
}
};



// if we time out
var panic = setTimeout(function() {
self.removeListener('post', reply);
Expand All @@ -164,7 +165,7 @@ Postmaster.prototype.send = function (message, patience, callback, alternate, de
// if we get something

self.once('post', function(err, data) {
self.removeAllListeners('post'); // WORKAROUND: see bug https://github.com/tessel/runtime/issues/226
// self.removeAllListeners('post'); // WORKAROUND: see bug https://github.com/tessel/runtime/issues/226
clearTimeout(panic);
if (self.debug) {
console.log("postmaster replying", data);
Expand Down