From c218adb09ee731f4cd2046bf904455c26e4b9879 Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Wed, 17 Dec 2014 17:11:54 +0000 Subject: [PATCH] Separate the blinking out, ready to move to a separate module. --- src/lib/blink.coffee | 39 +++++++++++++++++++++++++++++++++++++++ src/utils.coffee | 41 ++++------------------------------------- 2 files changed, 43 insertions(+), 37 deletions(-) create mode 100644 src/lib/blink.coffee diff --git a/src/lib/blink.coffee b/src/lib/blink.coffee new file mode 100644 index 00000000..ba283af5 --- /dev/null +++ b/src/lib/blink.coffee @@ -0,0 +1,39 @@ +Promise = require 'bluebird' +fs = Promise.promisifyAll require 'fs' + +# Helps in blinking the LED from the given end point. +module.exports = exports = (ledFile) -> + blink = (ms = 200) -> + fs.writeFileAsync(ledFile, 1) + .delay(ms) + .then -> fs.writeFileAsync(ledFile, 0) + + blink.pattern = do -> + started = false + interval = null + timeout = null + # This function lets us have sensible param orders, + # and always have a timeout we can cancel. + delay = (ms, fn) -> + timeout = setTimeout(fn, ms) + start = -> + interval = setInterval(blink, 400) + delay 2000, -> + # Clear the blinks after 2 second + clearInterval(interval) + delay 2000, -> + # And then repeat again after another 2 seconds + start() + return { + start: -> + return false if started + started = true + start() + stop: -> + return false if not started + started = false + clearInterval(interval) + clearTimeout(timeout) + } + + return blink \ No newline at end of file diff --git a/src/utils.coffee b/src/utils.coffee index b3cd3aa9..9bcf9d44 100644 --- a/src/utils.coffee +++ b/src/utils.coffee @@ -5,6 +5,7 @@ config = require './config' mixpanel = require 'mixpanel' request = require './request' networkCheck = require './lib/network-check' +blink = require('./lib/blink')(config.ledFile) utils = exports @@ -59,41 +60,7 @@ exports.findIpAddrs = -> return ipAddr .filter(Boolean) -# Helps in blinking the LED from the given end point. -exports.blink = (ms = 200) -> - fs.writeFileAsync(config.ledFile, 1) - .delay(ms) - .then -> fs.writeFileAsync(config.ledFile, 0) - - -blinkPattern = do -> - started = false - interval = null - timeout = null - # This function lets us have sensible param orders, - # and always have a timeout we can cancel. - delay = (ms, fn) -> - timeout = setTimeout(fn, ms) - start = -> - interval = setInterval(utils.blink, 400) - delay 2000, -> - # Clear the blinks after 2 second - clearInterval(interval) - delay 2000, -> - # And then repeat again after another 2 seconds - start() - return { - start: -> - return false if started - started = true - start() - stop: -> - return false if not started - started = false - clearInterval(interval) - clearTimeout(timeout) - } - +exports.blink = blink exports.connectivityCheck = _.once -> networkCheck.monitorURL @@ -102,7 +69,7 @@ exports.connectivityCheck = _.once -> (connected) -> if connected console.log('Internet Connectivity: OK') - blinkPattern.stop() + blink.pattern.stop() else console.log('Waiting for connectivity...') - blinkPattern.start() \ No newline at end of file + blink.pattern.start() \ No newline at end of file