balena-supervisor/app.coffee

82 lines
1.7 KiB
CoffeeScript
Raw Normal View History

fs = require('fs')
express = require('express')
2013-07-21 00:41:52 +00:00
async = require('async')
bootstrap = require('./bootstrap')
state = require('./state')
settings = require('./settings')
Application = require('./application')
console.log('Supervisor started..')
hakiApp = null
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')
console.log('Device is virgin. Boostrapping')
2013-07-21 00:41:52 +00:00
handler = (error) ->
if error
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
state.set('virgin', false)
callback()
bootstrap(handler)
2013-07-19 00:10:43 +00:00
else
console.log("Device isn't a virgin")
2013-07-21 00:41:52 +00:00
callback()
(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-21 00:41:52 +00:00
async.series(tasks, (error) ->
if error
2013-07-19 00:10:43 +00:00
console.error(error)
else
console.log('Everything is fine :)')
2013-07-19 00:10:43 +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)
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)
)
)
app.listen(80)