From f09799eb459c1c392be7165b956ebb51beeda902 Mon Sep 17 00:00:00 2001 From: jialiya Date: Fri, 4 Jul 2014 04:43:18 -0700 Subject: [PATCH] added sim check --- examples/gprs.js | 38 ++++++++++++++++++++----------- index.js | 58 +++++++++++++++++++++++++++++++++--------------- postmaster.js | 5 +++-- 3 files changed, 68 insertions(+), 33 deletions(-) diff --git a/examples/gprs.js b/examples/gprs.js index 9ce6054..fd362bc 100644 --- a/examples/gprs.js +++ b/examples/gprs.js @@ -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... diff --git a/index.js b/index.js index 5161d7b..3f69673 100644 --- a/index.js +++ b/index.js @@ -157,7 +157,10 @@ 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 @@ -165,7 +168,8 @@ GPRS.prototype._chain = function (messages, patiences, replies, callback) { 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. @@ -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); - } - } - }); - } - } + // } } }; @@ -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) { /* diff --git a/postmaster.js b/postmaster.js index 8fc0650..f7bd3dc 100644 --- a/postmaster.js +++ b/postmaster.js @@ -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); @@ -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);