Skip to content

Instantly share code, notes, and snippets.

@korayal
Created December 13, 2012 12:02
Show Gist options
  • Save korayal/4275983 to your computer and use it in GitHub Desktop.
Save korayal/4275983 to your computer and use it in GitHub Desktop.
var net = require("net");
var request = require('request');
var xml2js = require('xml2js');
// fill in here with your api key from http://www.notifymyandroid.com/account.jsp
var apikey = "";
// creating states
var HOTstate = 2, NORMALstate = 1, COLDstate = 0;
// setting the state to normal
var TEMPstate = NORMALstate;
var PORT = 1234;
VAR HOST = '0.0.0.0';
net.createServer(function(sock) {
sock.on('data', function(data) {
// assuming the data is "<TemperatureValue>\r\n"
var sensor = data.toString().replace("\r\n","");
console.log("We have a new data" + sensor);
if (sensor > 40 && TEMPstate != HOTstate){
notifyMA(apikey, "TemperatureApp", "Hot!", "It's getting Hot!", 4 );
TEMPstate = HOTstate;
}
else if (sensor < 0 && TEMPstate != COLDstate){
notifyMA(apikey, "TemperatureApp", "Cold!", "Winter is Coming!", 4 );
TEMPstate = COLDstate;
}
else if (TEMPstate != NORMALstate){
notifyMA(apikey, "TemperatureApp", "Normal", "Back to Normal!", 2 );
TEMPstate = NORMALstate;
}
});
sock.on('close', function() {
//logger.trace('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
});
}).listen(PORT, HOST);
function notifyMA(keystring, appname, eventtitle, descriptiontext, priorityvalue){
var parser = new xml2js.Parser();
// you can also use (http|https).request but this was easier for me
var r = request.post('http://www.notifymyandroid.com/publicapi/notify', {form : {apikey: keystring,
application: appname,
event: eventtitle,
description: descriptiontext,
priority: priorityvalue}}, function (error, response, body) {
if (!error && response.statusCode == 200) {
parser.parseString(body.toString(), function(err, result){
var success = result.nma.success[0].$;
// you can use these to manage your notification cases
console.log("response : " + success.code);
console.log("remaining : " + success.remaining);
console.log("resettimer : " + success.resettimer);
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment