Sequence logic ready for testing

This commit is contained in:
mmcw-dev 2017-12-20 18:40:51 -05:00
parent 2a2d71ec73
commit e6070465e5
2 changed files with 163 additions and 7 deletions

View File

@ -50,6 +50,8 @@ function createBLE () {
ble.on('exposure', bExposure) ble.on('exposure', bExposure)
ble.on('delay', bDelay) ble.on('delay', bDelay)
ble.on('counter', bCounter) ble.on('counter', bCounter)
ble.on('sequence', bSequence)
ble.on('reset', bReset)
} }
//Restify functions //Restify functions
@ -246,6 +248,17 @@ function rSequence (req, res, next) {
let dir = true let dir = true
let exposure = 0 let exposure = 0
let delay = 0 let delay = 0
if (intval._state.frame.dir !== true) {
dir = false
}
if (intval._state.frame.exposure !== 0) {
exposure = intval._state.frame.exposure
}
if (intval._state.frame.delay !== 0) {
delay = intval._state.frame.delay
}
if (req.query && typeof req.query.dir !== 'undefined') { if (req.query && typeof req.query.dir !== 'undefined') {
if (typeof req.query.dir === 'string') { if (typeof req.query.dir === 'string') {
dir = (req.query.dir === 'true') dir = (req.query.dir === 'true')
@ -274,13 +287,24 @@ function rSequence (req, res, next) {
exposure = req.body.exposure exposure = req.body.exposure
} }
} }
if (sequence.active) { if (sequence._state.active) {
return sequence.stop(() => { return sequence.stop(() => {
res.send({ stopped : true }) res.send({ stopped : true })
return next() return next()
}) })
} else { } else {
return sequence.start({}, (seq) => { return sequence.start({
loop : [ (next) => {
intval.frame(dir, exposure, (len) => {
next()
}),
}, (next) => {
setTimeout(() => {
next()
}, delay)
}
]
}, (seq) => {
res.send(seq) res.send(seq)
return next() return next()
}) })
@ -379,6 +403,64 @@ function bCounter (obj, cb) {
return cb() return cb()
} }
function bSequence (obj, cb) {
let dir = true
let exposure = 0
let delay = 0
if (intval._state.frame.dir !== true) {
dir = false
}
if (intval._state.frame.exposure !== 0) {
exposure = intval._state.frame.exposure
}
if (intval._state.frame.delay !== 0) {
delay = intval._state.frame.delay
}
if (typeof obj.dir !== 'undefined') {
if (typeof obj.dir === 'string') {
dir = (obj.dir === 'true')
} else {
dir = obj.dir
}
}
if (typeof obj.exposure !== 'undefined') {
if (typeof obj.exposure === 'string') {
exposure = parseInt(obj.exposure)
} else {
exposure = obj.exposure
}
}
if (sequence._state.active) {
return sequence.stop(() => {
return cb()
})
} else {
console.time('sequence time')
sequence.start({
loop : [ (next) => {
intval.frame(dir, exposure, (len) => {
next()
}),
}, (next) => {
setTimeout(() => {
next()
}, delay)
}
]
}, (seq) => {
console.timeEnd('sequence time')
})
return cb()
}
}
function bReset (obj, cb) {
intval.reset()
setTimeout(cb, 10)
}
function index (req, res, next) { function index (req, res, next) {
fs.readFile(INDEXPATH, 'utf8', (err, data) => { fs.readFile(INDEXPATH, 'utf8', (err, data) => {
if (err) { if (err) {

View File

@ -1,17 +1,91 @@
'use strict' 'use strict'
const sequence = { const log = require('../log')('seq')
/** Object sequence features */
const sequence = {};
sequence._state = {
arr : [],
active : false, active : false,
paused : false,
frame: false, frame: false,
delay : false delay : false,
count : 0,
stop : null
}
sequence._loop = {
arr : [],
count : 0,
max : 0
} }
sequence.start = function (options, cb) { sequence.start = function (options, cb) {
sequence.active = true sequence._state.active = true
sequence._state.count = 0
if (options.arr) {
sequence._state.arr = options.arr
}
if (options.loop) {
sequence._loop.arr = options.loop
sequence._loop.count = 0
}
if (options.maxLoop) {
sequence._loop.max = options.maxLoop
} else {
sequence._loop.max = 0
}
sequence.step()
} }
sequence.stop = function (cb) { sequence.stop = function (next) {
sequence.active = false sequence._state.active = false
sequence._state.count = 0
sequence._loop.count = 0
sequence._state.stop()
next()
}
sequence.pause = function () {
sequence._state.paused = true
}
sequence.resume = function () {
sequence._state.paused = false
sequence.step()
}
sequence.step = function () {
if (sequence._state.active && !sequence._state.paused) {
if (sequence._state.arr.length > 0 && typeof sequence._state.arr[sequence._state.count] !== 'undefined') {
return sequence._state.arr[sequence._state.count](() => {
sequence._state.count++
sequence.step()
})
} else if (sequence._loop.arr.length > 0 && typeof sequence._loop.arr[sequence._state.count] !== 'undefined') {
if (sequence._state.count > sequence._loop.arr.length) {
sequence._state.count = 0
sequence._loop.count++
}
if (sequence._loop.count > sequence._loop.max) {
return sequence.stop()
}
return sequence._loop.arr[sequence._state.count](() => {
sequence._state.count++
})
} else{
return sequence.stop()
}
} else if (sequence._state.paused) {
log.info('Sequence paused', { loop : sequence._loop.count, count : sequence._state.count })
} else if (!sequence._state.active) {
log.info('Sequence stopped', { loop : sequence._loop.count, count : sequence._state.count })
}
} }
module.exports = sequence module.exports = sequence