Skip to content

Instantly share code, notes, and snippets.

@matbee-eth
Created May 20, 2011 18:46
Show Gist options
  • Save matbee-eth/983519 to your computer and use it in GitHub Desktop.
Save matbee-eth/983519 to your computer and use it in GitHub Desktop.
SMSified nodejs script
/*
*
* SMS Number Classes
*
*/
function NumberClass (Address) {
var self = this;
//console.log(Address);
this._messages = [];
this._maxPerMinute = 10;
this._messagesRemaining = 10;
this._address = Address;
this.timer = function () {
setTimeout(function() {
self._messagesRemaining = self._maxPerMinute;
for (var i = 0; i < self._messages.length; i++) {
if (self._messages[i] == undefined) {
self._messages.splice(i, 1);
i--;
}
}
self.sendMessages();
console.log("------------LLOOOOPPP--------------");
self.doTimer();
},60000);
}
this.doTimer = function() {
self.timer();
}
this.timer();
}
NumberClass.prototype.addMessage = function (msg, recipient) {
//console.log(JSON.stringify(this))
this._messages.push({message: msg, address: recipient});
this.sendMessages();
}
NumberClass.prototype.sendMessages = function() {
if (this._messagesRemaining >= 1) {
var toDelete = [];
for (var i = 0; i < this._maxPerMinute; i++)
{
if (this._messages[i] != undefined) {
this.sendMessage(this._messages[i]);
delete this._messages[i];
//toDelete.push(i);
this._messagesRemaining--;
}
else {
delete this._messages[i];
//toDelete.push(i);
}
}
//for (var i = 0; i < toDelete.length; i++) {
// delete this._messages[toDelete[i]];
//}
}
}
NumberClass.prototype.sendMessage = function(msg) {
var sms = new smsified.SMSified('login', 'password');
//console.log(JSON.stringify(msg));
var options = {senderAddress: this._address, address: msg.address, message: msg.message};
sms.sendMessage(options, function(result) {
console.log(JSON.stringify(result));
});
}
NumberClass.prototype.hasRoom = function() {
if (this._messagesRemaining >= 1) {
return true;
}
else {
return false;
}
}
NumberClass.prototype.getCount = function() {
return this._messages.length;
}
/*
*
* SMS Central control.
*
*/
var SMSCenter = function() {
this._numbers = [];
}
SMSCenter.prototype.addNumber = function(numberClass) {
this._numbers.push(numberClass);
}
SMSCenter.prototype.addMessage = function(message) {
var addedMessage = false;
var lowestNumber = 20000;
var lowestNumberClass = null;
console.log(JSON.stringify(message));
for (var i = 0; i < this._numbers.length && !addedMessage; i++) {
//console.log(JSON.stringify(this._numbers[i]));
if (this._numbers[i].getCount() < lowestNumber) {
lowestNumber = this._numbers[i].getCount();
lowestNumberClass = this._numbers[i];
}
if (this._numbers[i].hasRoom()) {
this._numbers[i].addMessage(message.message, message.address);
//console.log("Added Message to: " + JSON.stringify(this._numbers[i]));
addedMessage = true;
}
}
if (!addedMessage) {
lowestNumberClass.addMessage(message);
}
}
var center = new SMSCenter();
var c = new NumberClass("518-------");
var b = new NumberClass("714-------");
center.addNumber(c);
center.addNumber(b);
center.addMessage({message: "1!", address: "289-------"});
center.addMessage({message: "2!", address: "289-------"});
center.addMessage({message: "3!", address: "289-------"});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment