Skip to content

Instantly share code, notes, and snippets.

@kurtpayne
Created June 23, 2012 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtpayne/2976944 to your computer and use it in GitHub Desktop.
Save kurtpayne/2976944 to your computer and use it in GitHub Desktop.
Text my wife when I leave work (onX)
// Initializing variables
var friend = { name : "WIFE",phoneNumber : "2225551234" } ;
var messageText = "I'm on my way home";
var action = "exit" /* leave */;
var location = { name : "work",latitude : "XX.123456",longitude : "-YY.123456" } ;
var time = "4:45 PM";
// End of variables initializing
console.log('Started script: Text ' + friend.name + ' \"' + messageText + '\" when I ' + action + ' ' + location.name + ' after ' + time );
// create a geo region for the trigger to take place at
var region = device.regions.createRegion({
latitude: parseFloat(location.latitude, 10),
longitude: parseFloat(location.longitude, 10),
name: location.name,
radius: 1000
});
// Parse a human readable time into epoch time
var parseTime = function (timeString) {
if (timeString === '') {
return null;
}
var time = timeString.match(/^(\d+)(:(\d\d))?\s*((a|(p))m?)?$/i);
if (time === null) {
return null;
}
var hours = parseInt(time[1], 10);
if (hours === 12 && !time[6]) {
hours = 0;
} else {
hours += (hours < 12 && time[6]) ? 12 : 0;
}
var d = new Date();
d.setHours(hours);
d.setMinutes(parseInt(time[3], 10) || 0);
d.setSeconds(0, 0);
return d;
};
// register a callback which sends a message when entering/exiting the region (depends on action)
region.on(action, function (){
var firstTime = parseTime(time);
if (firstTime === null) {
console.error('Invalid time format: ' + time + '. Expected: hh:mm or hh:mm AM/PM');
} else {
var now = new Date();
if (firstTime.getTime() < now.getTime()) {
console.log("Sending text message");
device.messaging.sendSms(
{
to: friend.phoneNumber,
body: messageText
},
function (err) {
if (err) {
console.error('Error sending text message: ' + JSON.stringify(err));
}
}
);
} else {
console.log("It's too early to send the text");
}
}
});
// start monitoring the region
device.regions.startMonitoring(region);
/*
//Testing
var locationSignal = { latitude: XX.123456, longitude : -YY.123456 };
region.emit('exit', locationSignal);
*/
console.log('Completed script: Text ' + friend.name + ' \"' + messageText + '\" when I ' + action + ' ' + location.name);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment