Promisify blink

This commit is contained in:
Petros Aggelatos 2013-12-23 04:22:54 +00:00 committed by Pablo Carranza Vélez
parent a8846b37b6
commit e5684a1c3a
2 changed files with 14 additions and 10 deletions

View File

@ -1,15 +1,17 @@
fs = require 'fs'
Promise = require 'bluebird'
fs = Promise.promisifyAll require 'fs'
utils = require './utils'
express = require 'express'
dockerode = require 'dockerode'
api = express()
LED_FILE = '/sys/class/leds/led0/brightness'
blink = (ms = 200, callback) ->
fs.writeFileSync(LED_FILE, 1)
setTimeout(->
fs.writeFile(LED_FILE, 0, callback)
, ms)
blink = (ms = 200) ->
fs.writeFileAsync(LED_FILE, 1)
.then(-> utils.delay(ms))
.then(-> fs.writeFileAsync(LED_FILE, 0))
api.post('/blink', (req, res) ->
interval = setInterval(blink, 400)

View File

@ -1,7 +1,7 @@
Promise = require('bluebird')
fs = Promise.promisifyAll(require('fs'))
os = require('os')
crypto = require('crypto')
Promise = require 'bluebird'
fs = Promise.promisifyAll require 'fs'
os = require 'os'
crypto = require 'crypto'
# Parses the output of /proc/cpuinfo to find the "Serial : 710abf21" line
# or the hostname if there isn't a serial number (when run in dev mode)
@ -16,3 +16,5 @@ exports.getDeviceUuid = ->
return crypto.createHash('sha1').update(serial, 'utf8').digest('hex')
)
exports.delay = (ms) -> new Promise (v) -> setTimeout(v, ms)