diff --git a/Dockerfile.x86 b/Dockerfile.x86 index 2eea78cd..5f4effdf 100644 --- a/Dockerfile.x86 +++ b/Dockerfile.x86 @@ -6,6 +6,7 @@ WORKDIR /app ENV SUPERVISOR_IMAGE resin/x86-supervisor ENV CONFIG_MOUNT_POINT /boot/config.json +ENV LED_FILE /dev/null RUN rm -rf node_modules RUN npm install --unsafe-perm --production diff --git a/package.json b/package.json index 1b1f5467..ea0dc76b 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "knex": "~0.5.1", "lodash": "~2.4.1", "mixpanel": "0.0.20", + "ping": "0.1.8", "pubnub": "~3.6.4", "request": "~2.22.0", "resin-platform-api": "git+ssh://git@bitbucket.org:rulemotion/resin-platform-api.git#v0.2.3", diff --git a/src/api.coffee b/src/api.coffee index cc80672f..aa242acd 100644 --- a/src/api.coffee +++ b/src/api.coffee @@ -7,16 +7,9 @@ supervisor = require './supervisor-update' api = express() -LED_FILE = '/sys/class/leds/led0/brightness' - -blink = (ms = 200) -> - fs.writeFileAsync(LED_FILE, 1) - .delay(ms) - .then -> fs.writeFileAsync(LED_FILE, 0) - api.post '/v1/blink', (req, res) -> utils.mixpanelTrack('Device blink') - interval = setInterval(blink, 400) + interval = setInterval(utils.blink, 400) setTimeout(-> clearInterval(interval) , 15000) diff --git a/src/app.coffee b/src/app.coffee index 8d38447d..e944c27d 100644 --- a/src/app.coffee +++ b/src/app.coffee @@ -8,11 +8,37 @@ bootstrap = require './bootstrap' utils.mixpanelTrack('Supervisor start') +connectivityState = true # Used to prevent multiple messages when disconnected + +ensureConnected = (continuous=false) -> + utils.checkConnectivity() + .then (connected) -> + if not connected + if connectivityState + console.log('Waiting for connectivity...') + connectivityState = false + interval = setInterval(utils.blink,200) + Promise.delay(1000) + .then -> + # Clear the blinks after 1 second + clearInterval(interval) + ensureConnected(continuous) + else + if not connectivityState + console.log('Internet Connectivity: OK') + connectivityState = true + if continuous + setTimeout(-> + ensureConnected(continuous) + , 10 * 1000) # Every 10 seconds perform this check. + + knex('config').select('value').where(key: 'uuid').then ([uuid]) -> if not uuid?.value console.log('New device detected. Bootstrapping..') - utils.mixpanelTrack('Device bootstrap') - bootstrap() + ensureConnected().then -> + utils.mixpanelTrack('Device bootstrap') + bootstrap() else uuid.value .then (uuid) -> @@ -67,3 +93,6 @@ knex('config').select('value').where(key: 'uuid').then ([uuid]) -> setInterval(updateIpAddr, 5 * 60 * 1000) # Every 5 mins updateIpAddr() + console.log('Starting connectivity check..') + ensureConnected(true) + diff --git a/src/config.coffee b/src/config.coffee index 4081a8b1..590b6aee 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -8,6 +8,7 @@ module.exports = config = dockerSocket: process.env.DOCKER_SOCKET ? '/run/docker.sock' localImage: process.env.SUPERVISOR_IMAGE ? 'resin/rpi-supervisor' configMountPoint: process.env.CONFIG_MOUNT_POINT ? '/mnt/mmcblk0p1/config.json' + ledFile: process.env.LED_FILE ? '/sys/class/leds/led0/brightness' config.remoteImage = config.registryEndpoint + '/' + config.localImage diff --git a/src/utils.coffee b/src/utils.coffee index 0749512c..9045f322 100644 --- a/src/utils.coffee +++ b/src/utils.coffee @@ -3,6 +3,7 @@ _ = require 'lodash' fs = Promise.promisifyAll require 'fs' config = require './config' mixpanel = require 'mixpanel' +ping = require 'ping' # Parses package.json and returns resin-supervisor's version exports.getSupervisorVersion = -> @@ -46,3 +47,17 @@ exports.findIpAddrs = -> prevLine = line 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) + +# Helps in checking connectivity by pinging the given site. +exports.checkConnectivity = (host = '8.8.8.8') -> + ping.sys.promise_probe(host, + timeout: 1 + extra: ["-c 1"] + ).then (res) -> + return res.alive