working out promise sequence of init tasks

master
David Kebler 2017-01-25 00:11:20 -08:00
parent 65e02f4fc6
commit d8172b092b
2 changed files with 76 additions and 35 deletions

View File

@ -18,39 +18,40 @@ class Interrupt extends EventEmitter {
this.num = pin_number;
this.hook = opts.hook // will be passed back with the emit
this.path = GPIO_ROOT_PATH + 'gpio' + this.num + '/'
this.edge = opts.edge ? opts.edge : 'falling'
this.delay = opts.delay ? opts.delay : 50
this.edge = opts.edge
this.delay = opts.delay
this.dbt = opts.debounce ? opts.debounce : 300
// this.poller = new Epoll(
// debounce((err, fd, events) => {
// if (err) { this.emit('error', err) } else { this.clear().then(this.emit('fired', this.hook)) }
// }, this.dbt, true)
// )
this.poller = new Epoll(
debounce((err, fd, events) => {
if (err) { this.emit('error', err) } else { this.clear().then(this.emit('fired', this.hook)) }
}, this.dbt, true)
)
}
init() {
let tasks = [
fs.writeFile(GPIO_ROOT_PATH + 'export', this.num).then(() => { console.log('exported', this.num) }),
pWaitFor(() => pathExists(this.path + 'direction')).then(_.pDelay(this.delay))
.then(() => { return fs.writeFile(this.path + 'direction', 'in').then(() => { console.log('direction', 'in') }) }),
fs.writeFile(this.path + 'edge', this.edge).then(() => { console.log('edge', this.edge) }),
fs.open(this.path + 'value', 'r+').then(fd => { this.valuefd = fd }),
this.clear().then(() => { return this.start() })
]
process.on('SIGINT', () => {
this.exit().then(() => console.log('\nunexported and closed')) // unexport on cntrl-c
})
return this.exit() // make sure pin is unexported
.then((resp) => {
console.log('ready to init', tasks)
return _.pSeries(tasks).then(console.log('finsihed tasks'))
return _.pSeries([
this.exit(),
this._export(this.num),
this._direction('in', this.delay),
() => this._edge(this.edge),
() => this._fd(),
() => this.clear().then(() => {
this.start()
return Promise.resolve('cleared and started')
})
])
}
_test(num) {
return Promise.resolve(num)
}
fire(name = 'fired') {
this.emit(name, this.hook)
}
@ -62,16 +63,20 @@ class Interrupt extends EventEmitter {
exit() {
return pathExists(this.path)
.then(() => {
return fs.writeFile(GPIO_ROOT_PATH + 'unexport', this.num)
})
.then(() => {
if (this.valuefd) {
this.stop()
return fs.close(this.valuefd)
.then((exists) => {
// console.log('exists', exists)
if (exists) {
return fs.writeFile(GPIO_ROOT_PATH + 'unexport', this.num)
.then(() => {
if (this.valuefd) {
this.stop()
return fs.close(this.valuefd)
}
return Promise.resolve('unexported - exited')
})
}
return Promise.resolve('already unexported')
})
.catch(err => { return Promise.resolve('already unexported') })
}
start() {
@ -82,6 +87,40 @@ class Interrupt extends EventEmitter {
this.poller.remove(this.valuefd).close();
}
_fd() {
return fs.open(this.path + 'value', 'r+')
.then(fd => {
this.valuefd = fd
return Promise.resolve('value fd set')
})
}
_export(num) {
return new Promise(function (resolve, reject) {
fs.writeFile(GPIO_ROOT_PATH + 'export', num)
.then(() => { return resolve('exported') })
.catch((err) => { return reject(err) })
})
}
_direction(dir = 'in', delay = 50) {
return pWaitFor(() => pathExists(this.path + 'direction'))
.then(_.pDelay(delay))
.then(() => {
return fs.writeFile(dir + 'direction', dir)
.then(() => {
return Promise.resolve('direction set')
})
})
}
_edge(edge = 'falling') {
return fs.writeFile(this.path + 'edge', edge)
.then(() => {
return Promise.resolve('edge set')
})
}
}
module.exports = {

View File

@ -15,13 +15,15 @@ let inter17 = new Interrupt(17, { hook: 'a hook to something to do' })
// console.log('Listener fired and returned:', hook)
// })
// inter17.init()
// .then(() => {
// console.log('initialzed, waiting for interrupt to fire')
// })
// .catch(err => console.log("returned error:", err))
//
// inter17.on('fired', hook => { counter(hook) })
inter17.init()
.then((resp) => {
console.log(resp)
console.log('initialzed, waiting for interrupt to fire')
})
.catch(err => console.log("returned error:", err))
//
// inter17.on('fired', hook => { counter(hook) })
let count = 0