2013-12-23 04:22:54 +00:00
|
|
|
Promise = require 'bluebird'
|
|
|
|
fs = Promise.promisifyAll require 'fs'
|
2013-12-23 04:31:33 +00:00
|
|
|
url = require 'url'
|
|
|
|
knex = require './db'
|
2013-12-23 04:22:54 +00:00
|
|
|
utils = require './utils'
|
2013-12-14 05:18:20 +00:00
|
|
|
express = require 'express'
|
2013-12-23 04:31:33 +00:00
|
|
|
request = Promise.promisify require 'request'
|
2013-12-14 05:18:20 +00:00
|
|
|
|
|
|
|
api = express()
|
|
|
|
|
|
|
|
LED_FILE = '/sys/class/leds/led0/brightness'
|
2013-12-23 04:22:54 +00:00
|
|
|
|
|
|
|
blink = (ms = 200) ->
|
|
|
|
fs.writeFileAsync(LED_FILE, 1)
|
|
|
|
.then(-> utils.delay(ms))
|
|
|
|
.then(-> fs.writeFileAsync(LED_FILE, 0))
|
2013-12-14 05:18:20 +00:00
|
|
|
|
2013-12-23 04:29:48 +00:00
|
|
|
api.post('/v1/blink', (req, res) ->
|
2013-12-14 05:18:20 +00:00
|
|
|
interval = setInterval(blink, 400)
|
2013-12-17 06:04:53 +00:00
|
|
|
setTimeout(->
|
|
|
|
clearInterval(interval)
|
|
|
|
, 5000)
|
2013-12-14 05:18:20 +00:00
|
|
|
res.send(200)
|
|
|
|
)
|
|
|
|
|
2013-12-23 04:29:48 +00:00
|
|
|
api.post('/v1/update', (req, res) ->
|
2013-12-23 04:31:33 +00:00
|
|
|
res.send(204)
|
|
|
|
Promise.all([
|
|
|
|
knex('config').select('value').where(key: 'apiKey')
|
|
|
|
knex('config').select('value').where(key: 'uuid')
|
|
|
|
]).then(([[apiKey], [uuid]]) ->
|
|
|
|
apiKey = apiKey.value
|
|
|
|
uuid = uuid.value
|
|
|
|
request(
|
|
|
|
method: 'GET'
|
|
|
|
url: url.resolve(process.env.API_ENDPOINT, "/ewa/application?$filter=device/uuid eq '#{uuid}'&apikey=#{apiKey}")
|
|
|
|
json: true
|
|
|
|
).spread((request, body) ->
|
|
|
|
for app in body.d
|
|
|
|
console.log("Got application", app)
|
|
|
|
)
|
|
|
|
)
|
2013-12-14 05:18:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
module.exports = api
|