2013-07-21 01:30:12 +00:00
|
|
|
fs = require('fs')
|
2013-07-20 02:57:51 +00:00
|
|
|
express = require('express')
|
2013-07-21 00:41:52 +00:00
|
|
|
async = require('async')
|
|
|
|
bootstrap = require('./bootstrap')
|
|
|
|
state = require('./state')
|
|
|
|
settings = require('./settings')
|
2013-07-21 01:30:12 +00:00
|
|
|
Application = require('./application')
|
|
|
|
|
|
|
|
console.log('Supervisor started..')
|
|
|
|
|
|
|
|
hakiApp = null
|
2013-07-20 02:57:51 +00:00
|
|
|
|
2013-07-21 00:41:52 +00:00
|
|
|
tasks = [
|
2013-07-18 10:40:35 +00:00
|
|
|
(callback) ->
|
2013-07-21 00:41:52 +00:00
|
|
|
if state.get('virgin')
|
2013-07-21 13:32:46 +00:00
|
|
|
console.log('Device is virgin. Bootstrapping')
|
2013-07-21 00:41:52 +00:00
|
|
|
handler = (error) ->
|
|
|
|
if error
|
2013-07-21 01:30:12 +00:00
|
|
|
console.log('Bootstrapping failed with error', error)
|
|
|
|
console.log('Trying again in 10s')
|
|
|
|
setTimeout((-> bootstrap(handler)), 10000)
|
2013-07-21 00:41:52 +00:00
|
|
|
else
|
2013-07-21 13:32:46 +00:00
|
|
|
console.log('Bootstrapping successful')
|
2013-07-21 00:41:52 +00:00
|
|
|
state.set('virgin', false)
|
|
|
|
callback()
|
|
|
|
bootstrap(handler)
|
2013-07-19 00:10:43 +00:00
|
|
|
else
|
2013-07-21 01:30:12 +00:00
|
|
|
console.log("Device isn't a virgin")
|
2013-07-21 00:41:52 +00:00
|
|
|
callback()
|
2013-07-21 01:30:12 +00:00
|
|
|
(callback) ->
|
|
|
|
hakiApp = new Application(state.get('gitUrl'), '/home/haki/hakiapp', 'haki')
|
|
|
|
if not state.get('appInitialised')
|
|
|
|
console.log('Initialising app..')
|
|
|
|
hakiApp.init((error) ->
|
|
|
|
if error then return callback(error)
|
|
|
|
state.set('appInitialised', true)
|
|
|
|
callback()
|
|
|
|
)
|
|
|
|
else
|
|
|
|
console.log('App already initialised')
|
|
|
|
callback()
|
|
|
|
(callback) ->
|
|
|
|
console.log('Fetching new code..')
|
|
|
|
hakiApp.update(callback)
|
|
|
|
(callback) ->
|
|
|
|
console.log('Starting the app..')
|
|
|
|
hakiApp.start(callback)
|
2013-07-19 00:10:43 +00:00
|
|
|
]
|
2013-07-18 20:34:00 +00:00
|
|
|
|
2013-07-21 00:41:52 +00:00
|
|
|
async.series(tasks, (error) ->
|
|
|
|
if error
|
2013-07-19 00:10:43 +00:00
|
|
|
console.error(error)
|
2013-07-21 01:30:12 +00:00
|
|
|
else
|
|
|
|
console.log('Everything is fine :)')
|
2013-07-19 00:10:43 +00:00
|
|
|
)
|
2013-07-20 02:57:51 +00:00
|
|
|
|
|
|
|
app = express()
|
|
|
|
|
|
|
|
app.post('/blink', (req, res) ->
|
2013-07-21 00:41:52 +00:00
|
|
|
state = 0
|
|
|
|
toggleLed = ->
|
|
|
|
state = (state + 1) % 2
|
|
|
|
fs.writeFileSync(settings.LED_FILE, state)
|
|
|
|
|
|
|
|
interval = setInterval(toggleLed, settings.BLINK_STEP)
|
|
|
|
setTimeout(->
|
|
|
|
clearInterval(interval)
|
2013-07-21 01:31:21 +00:00
|
|
|
fs.writeFileSync(settings.LED_FILE, 0)
|
2013-07-21 00:41:52 +00:00
|
|
|
res.send(200)
|
|
|
|
, 5000)
|
|
|
|
)
|
|
|
|
|
|
|
|
app.post('/update', (req, res) ->
|
2013-07-21 01:31:54 +00:00
|
|
|
hakiApp.update((error) ->
|
|
|
|
if error
|
|
|
|
res.send(500)
|
|
|
|
else
|
|
|
|
res.send(204)
|
|
|
|
)
|
2013-07-20 02:57:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
app.listen(80)
|