Last active
August 29, 2015 14:24
-
-
Save adelciotto/349101506b19131d1524 to your computer and use it in GitHub Desktop.
getup.js: A node script that reminds me to get off my ass during long sessions at the computer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ---------------------------------------------------------------------------- | |
* constants.js | |
* Author: Anthony Del Ciotto | |
* Date: 09/07/2015 | |
* ---------------------------------------------------------------------------- */ | |
module.exports = { | |
ROOT_PATH: require('path').dirname(require.main.filename), | |
DEFAULT_INITIAL_INTERVAL: 50 * (60 * 1000), // 50 mins in ms | |
DEFAULT_REPEAT_INTERVAL: 5 * (60 * 1000), // 5 mins in ms | |
DEFAULT_MAX_IDLE_TIME: 20 * 60, // 20 mins in secs | |
MAX_IGNORE_TIME: 20 * (60 * 1000), // 20 mins in ms | |
BATTERY_STAT_CMD: 'pmset -g batt | grep -m 1 -o \'AC\' || true', | |
DISPLAY_SLEEP_CMD: 'pmset displaysleepnow', | |
IDLE_TIME_CMD: 'ioreg -c IOHIDSystem | awk \'/HIDIdleTime/ {print $NF/1000000000; exit}\'' | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ---------------------------------------------------------------------------- | |
* data.js | |
* Description: This file contains all the possible message combinations for | |
* the notification. | |
* Author: Anthony Del Ciotto | |
* Date: 09/07/2015 | |
* ---------------------------------------------------------------------------- */ | |
module.exports = { | |
earlyMorning: [ | |
{ title: 'Go to bed', msg: 'It\'s very late' }, | |
{ title: 'Bit late ay?', msg: 'What are you doing up so late anyway?' }, | |
{ title: 'Go sleep!', msg: 'GO!' } | |
], | |
morning: [ | |
{ title: 'Good morning!', msg: 'Another beautiful day' }, | |
{ title: 'Have you eaten breakfast?', msg: 'The most important meal of the day' }, | |
{ title: 'Drink some water', msg: 'Best time to drink water is in the morning' } | |
], | |
afternoon: [ | |
{ title: 'Take oscar for a walk', msg: 'He needs the exercise....' }, | |
{ title: 'Go do some push-ups', msg: 'At least 30 you fat fuck' }, | |
{ title: 'Drink some water', msg: 'Or maybe a beer instead?' } | |
], | |
evening: [ | |
{ title: 'Walk around for a bit please', msg: 'Grab your phone and open spotify' }, | |
{ title: 'Is dinner ready yet?', msg: 'mmmmmmmmm' }, | |
{ title: 'Drink some water', msg: 'Do it cunt!' } | |
], | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ---------------------------------------------------------------------------- | |
* getup.js | |
* Description: A simple script that reminds me to get up off my ass after | |
* long coding sessions at the computer. | |
* Author: Anthony Del Ciotto | |
* Date: 09/07/2015 | |
* ---------------------------------------------------------------------------- */ | |
var exec = require('child_process').exec; | |
var fs = require('fs'); | |
var notifier = require('node-notifier'); | |
var util = require('util'); | |
var data = rootRequire('src/data'); | |
var constants = rootRequire('src/constants'); | |
var initialInterval, repeatInterval, maxIdleTime; | |
var intervalId; | |
var currentInterval; | |
var repeatCount = 0; | |
var maxRepeatCount = 0; | |
var displaySleeping = false; | |
var notIdle = false; | |
/** | |
* utility function to generate a random integer within a range | |
*/ | |
function getRandomInt(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
/** | |
* function to set a new interval time for the notifications | |
*/ | |
function setNewInterval(newInterval) { | |
if (currentInterval !== newInterval) { | |
clearInterval(intervalId); | |
intervalId = setInterval(onNotifierInterval, newInterval); | |
currentInterval = newInterval; | |
} | |
} | |
/** | |
* function to select some random text to fill the notification | |
* based on what time of day it is. | |
*/ | |
function getNotifyText() { | |
var hour = new Date().getHours(); | |
var obj; | |
// very early in the morning | |
if (hour >= 2 && hour <= 7) { | |
obj = data.earlyMorning[getRandomInt(0, data.earlyMorning.length - 1)]; | |
// morning | |
} else if (hour >= 8 && hour <= 11) { | |
obj = data.morning[getRandomInt(0, data.morning.length - 1)]; | |
// afternoon | |
} else if (hour >= 12 && hour <= 17) { | |
obj = data.afternoon[getRandomInt(0, data.afternoon.length - 1)]; | |
// evening | |
} else if (hour >= 18 && hour <= 1) { | |
obj = data.evening[getRandomInt(0, data.evening.length - 1)]; | |
} | |
return obj; | |
} | |
/** | |
* runs the 'terminal-notifier' program, sending a notification with | |
* a title, subtitle and body message | |
*/ | |
function osxNotify() { | |
var notifyText = getNotifyText(); | |
notifier.notify({ | |
title: notifyText.title, | |
message: notifyText.msg, | |
sound: true, | |
wait: true | |
}, function(err, res) { | |
if (err) { | |
throw err; | |
} | |
}); | |
} | |
function onNotifierInterval() { | |
exec(constants.BATTERY_STAT_CMD, function(err, stdout) { | |
if (err) { | |
throw err; | |
} | |
// if charging and not idle, annoy me | |
if (stdout !== '' && notIdle) { | |
if (repeatCount >= maxRepeatCount) { | |
exec(constants.DISPLAY_SLEEP_CMD); | |
repeatCount = 0; | |
displaySleeping = true; | |
} else if (!displaySleeping) { | |
osxNotify(); | |
setNewInterval(repeatInterval); | |
repeatCount++; | |
} | |
} | |
}); | |
} | |
function onNotifierClick(notifierObj, options) { | |
setNewInterval(initialInterval); | |
} | |
function onPollIdleInterval() { | |
exec(constants.IDLE_TIME_CMD, function(err, idleTime) { | |
if (err) { | |
throw err; | |
} | |
// check the user idle time, if it is greater than a certain | |
// threshhold then don't send a notification, I'm probably not | |
// at my desk... | |
if (idleTime <= maxIdleTime) { | |
if (displaySleeping) { | |
setNewInterval(initialInterval); | |
displaySleeping = false; | |
} | |
notIdle = true; | |
} else { | |
setNewInterval(initialInterval); | |
notIdle = false; | |
} | |
}); | |
} | |
function parseCmdArgs() { | |
var args = process.argv.slice(2); | |
initialInterval = (typeof args[0] === 'undefined' ? constants.DEFAULT_INITIAL_INTERVAL : parseInt(args[0])); | |
repeatInterval = (typeof args[1] === 'undefined' ? constants.DEFAULT_REPEAT_INTERVAL : parseInt(args[1])); | |
maxIdleTime = (typeof args[2] === 'undefined' ? constants.DEFAULT_MAX_IDLE_TIME : parseInt(args[2])); | |
maxRepeatCount = Math.floor(constants.MAX_IGNORE_TIME / repeatInterval); | |
} | |
/** | |
* program entry point | |
*/ | |
(function start() { | |
parseCmdArgs(); | |
notifier.on('click', onNotifierClick); | |
setInterval(onPollIdleInterval, 3000); | |
setNewInterval(initialInterval); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment