2017-08-22 01:11:07 +00:00
|
|
|
'use strict'
|
|
|
|
|
2017-08-22 01:23:48 +00:00
|
|
|
const restify = require('restify')
|
2017-09-26 03:25:34 +00:00
|
|
|
const log = require('./lib/log')('main')
|
2017-08-22 02:59:44 +00:00
|
|
|
const fs = require('fs')
|
2018-01-11 20:50:36 +00:00
|
|
|
const { exec } = require('child_process')
|
2017-08-28 12:49:47 +00:00
|
|
|
|
2017-12-11 19:38:46 +00:00
|
|
|
const BLE = require('./lib/ble')
|
2019-10-31 02:37:55 +00:00
|
|
|
const Intval = require('./lib/intval')
|
|
|
|
const intval = new Intval()
|
2019-10-31 02:32:52 +00:00
|
|
|
const Sequence = require('./lib/sequence')
|
|
|
|
const sequence = new Sequence(intval)
|
2017-08-22 01:23:48 +00:00
|
|
|
|
2017-08-22 02:59:44 +00:00
|
|
|
const PACKAGE = require('./package.json')
|
2017-08-22 01:11:07 +00:00
|
|
|
const PORT = process.env.PORT || 6699
|
2017-08-22 02:59:44 +00:00
|
|
|
const APPNAME = PACKAGE.name
|
2017-08-28 12:49:47 +00:00
|
|
|
const INDEXPATH = './app/www/index.html'
|
2017-08-22 01:23:48 +00:00
|
|
|
|
|
|
|
let app = restify.createServer({
|
|
|
|
name: APPNAME,
|
2018-01-05 22:31:00 +00:00
|
|
|
version: PACKAGE.version
|
2017-08-22 01:23:48 +00:00
|
|
|
})
|
|
|
|
|
2017-12-11 19:38:46 +00:00
|
|
|
let ble
|
|
|
|
|
2017-08-22 02:59:44 +00:00
|
|
|
function createServer () {
|
2017-10-21 15:46:06 +00:00
|
|
|
app.use(restify.plugins.queryParser())
|
|
|
|
app.use(restify.plugins.bodyParser({ mapParams: false }))
|
2017-10-21 16:08:12 +00:00
|
|
|
app.get( '/', index)
|
|
|
|
app.get( '/dir', rDir)
|
2017-10-20 17:58:01 +00:00
|
|
|
app.post('/dir', rDir)
|
2017-10-21 16:08:12 +00:00
|
|
|
app.get( '/exposure', rExposure)
|
2017-10-21 15:46:06 +00:00
|
|
|
app.post('/exposure', rExposure)
|
2017-12-21 03:15:40 +00:00
|
|
|
app.get( '/delay', rDelay)
|
|
|
|
app.post('/delay', rDelay)
|
2017-10-22 23:59:38 +00:00
|
|
|
app.get( '/counter', rCounter)
|
|
|
|
app.post('/counter', rCounter)
|
2017-10-21 16:08:12 +00:00
|
|
|
app.get( '/frame', rFrame)
|
2017-08-22 04:00:24 +00:00
|
|
|
app.post('/frame', rFrame)
|
2017-11-23 01:26:34 +00:00
|
|
|
app.get( '/sequence', rSequence)
|
|
|
|
app.post('/sequence', rSequence)
|
2018-02-07 23:38:19 +00:00
|
|
|
|
2017-10-21 16:08:12 +00:00
|
|
|
app.get( '/status', rStatus)
|
2018-02-08 20:31:48 +00:00
|
|
|
app.post('/reset', rReset)
|
2018-02-08 21:23:10 +00:00
|
|
|
app.post('/update', rUpdate)
|
|
|
|
app.post('/restart', rRestart)
|
2018-02-08 20:31:48 +00:00
|
|
|
|
|
|
|
|
2017-08-22 02:59:44 +00:00
|
|
|
app.listen(PORT, () => {
|
2017-09-26 03:25:34 +00:00
|
|
|
log.info('server', { name : APPNAME, port : PORT })
|
2017-08-22 02:59:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-12 18:09:13 +00:00
|
|
|
function createBLE () {
|
|
|
|
ble = new BLE(() => {
|
|
|
|
return intval.status()
|
|
|
|
})
|
|
|
|
ble.on('frame', bFrame)
|
2017-12-12 19:23:14 +00:00
|
|
|
ble.on('dir', bDir)
|
2017-12-13 21:04:24 +00:00
|
|
|
ble.on('exposure', bExposure)
|
2017-12-13 21:22:22 +00:00
|
|
|
ble.on('delay', bDelay)
|
2017-12-13 22:27:42 +00:00
|
|
|
ble.on('counter', bCounter)
|
2017-12-20 23:40:51 +00:00
|
|
|
ble.on('sequence', bSequence)
|
2018-02-08 20:31:48 +00:00
|
|
|
|
2017-12-20 23:40:51 +00:00
|
|
|
ble.on('reset', bReset)
|
2018-02-08 20:31:48 +00:00
|
|
|
ble.on('update', bUpdate)
|
|
|
|
ble.on('restart', bRestart)
|
2017-11-22 14:30:08 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 18:09:13 +00:00
|
|
|
//Restify functions
|
2017-10-20 17:58:01 +00:00
|
|
|
function rDir (req, res, next) {
|
2017-10-21 15:46:06 +00:00
|
|
|
let dir = true
|
|
|
|
let set = false
|
|
|
|
if (req.query && typeof req.query.dir !== 'undefined') {
|
|
|
|
if (typeof req.query.dir === 'string') {
|
|
|
|
dir = (req.query.dir === 'true')
|
|
|
|
} else {
|
|
|
|
dir = req.query.dir
|
|
|
|
}
|
|
|
|
set = true
|
2017-12-12 19:23:14 +00:00
|
|
|
} else if (req.body && typeof req.body.dir !== 'undefined') {
|
2017-10-21 15:46:06 +00:00
|
|
|
if (typeof req.body.dir === 'string') {
|
|
|
|
dir = (req.body.dir === 'true')
|
|
|
|
} else {
|
|
|
|
dir = req.body.dir
|
|
|
|
}
|
|
|
|
set = true
|
|
|
|
}
|
|
|
|
if (set) {
|
|
|
|
intval.setDir(dir)
|
|
|
|
} else {
|
|
|
|
dir = intval._state.frame.dir
|
|
|
|
}
|
2017-10-21 16:08:12 +00:00
|
|
|
log.info('/dir', { method: req.method, set : set, dir : dir})
|
2017-10-21 15:46:06 +00:00
|
|
|
res.send({ dir : dir })
|
|
|
|
return next()
|
|
|
|
}
|
2017-10-20 17:58:01 +00:00
|
|
|
|
2017-10-21 15:46:06 +00:00
|
|
|
function rExposure (req, res, next) {
|
|
|
|
let exposure = 0
|
|
|
|
let set = false
|
|
|
|
if (req.query && typeof req.query.exposure !== 'undefined') {
|
|
|
|
if (typeof req.query.exposure === 'string') {
|
|
|
|
exposure = parseInt(req.query.exposure)
|
|
|
|
} else {
|
|
|
|
exposure = req.query.exposure
|
|
|
|
}
|
|
|
|
set = true
|
2017-12-12 19:23:14 +00:00
|
|
|
} else if (req.body && typeof req.body.exposure !== 'undefined') {
|
2017-10-21 15:46:06 +00:00
|
|
|
if (typeof req.body.exposure === 'string') {
|
|
|
|
exposure = parseInt(req.body.exposure)
|
|
|
|
} else {
|
|
|
|
exposure = req.body.exposure
|
|
|
|
}
|
|
|
|
set = true
|
|
|
|
}
|
|
|
|
if (set) {
|
2017-10-23 04:37:44 +00:00
|
|
|
if (exposure <= intval._frame.expected) {
|
|
|
|
exposure = 0;
|
|
|
|
}
|
2017-10-21 15:46:06 +00:00
|
|
|
intval.setExposure(exposure)
|
|
|
|
} else {
|
|
|
|
exposure = intval._state.frame.exposure
|
|
|
|
}
|
2017-10-21 16:08:12 +00:00
|
|
|
log.info('/exposure', { method: req.method, set : set, exposure : exposure })
|
2017-10-21 15:46:06 +00:00
|
|
|
res.send({ exposure : exposure })
|
2017-10-21 16:08:12 +00:00
|
|
|
return next()
|
2017-10-21 15:46:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function rDelay (req, res, next) {
|
|
|
|
let delay = 0
|
|
|
|
let set = false
|
|
|
|
if (req.query && typeof req.query.delay !== 'undefined') {
|
|
|
|
if (typeof req.query.delay === 'string') {
|
|
|
|
delay = parseInt(req.query.delay)
|
|
|
|
} else {
|
|
|
|
delay = req.query.delay
|
|
|
|
}
|
|
|
|
set = true
|
2017-11-23 01:26:34 +00:00
|
|
|
}
|
2017-12-12 19:23:14 +00:00
|
|
|
if (req.body && typeof req.body.delay !== 'undefined') {
|
2017-10-21 15:46:06 +00:00
|
|
|
if (typeof req.body.delay === 'string') {
|
|
|
|
delay = parseInt(req.body.delay)
|
|
|
|
} else {
|
|
|
|
delay = req.body.delay
|
|
|
|
}
|
|
|
|
set = true
|
|
|
|
}
|
|
|
|
if (set) {
|
|
|
|
intval.setDelay(delay)
|
|
|
|
} else {
|
|
|
|
delay = intval._state.frame.delay
|
|
|
|
}
|
2017-10-21 16:08:12 +00:00
|
|
|
log.info('/delay', { method: req.method, set : set, delay : delay })
|
2017-10-21 15:46:06 +00:00
|
|
|
res.send({ delay : delay })
|
2017-10-21 16:08:12 +00:00
|
|
|
return next()
|
2017-10-20 17:58:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-22 23:59:38 +00:00
|
|
|
function rCounter (req, res, next) {
|
|
|
|
let counter = 0
|
|
|
|
let set = false
|
|
|
|
if (req.query && typeof req.query.counter !== 'undefined') {
|
|
|
|
if (typeof req.query.counter === 'string') {
|
|
|
|
counter = parseInt(req.query.counter)
|
|
|
|
} else {
|
|
|
|
counter = req.query.counter
|
|
|
|
}
|
|
|
|
set = true
|
|
|
|
}
|
|
|
|
if (req.body && typeof req.body.counter !== 'undefined') {
|
|
|
|
if (typeof req.body.counter !== 'string') {
|
|
|
|
counter = parseInt(req.body.counter)
|
|
|
|
} else {
|
|
|
|
counter = req.body.counter
|
|
|
|
}
|
|
|
|
set = true
|
|
|
|
}
|
|
|
|
if (set) {
|
|
|
|
intval.setCounter(counter)
|
|
|
|
} else {
|
|
|
|
counter = intval._state.counter
|
|
|
|
}
|
|
|
|
log.info('/counter', { method : req.method, set : set, counter : counter })
|
2017-11-22 16:31:08 +00:00
|
|
|
res.send({ counter : counter })
|
2017-10-22 23:59:38 +00:00
|
|
|
return next()
|
|
|
|
}
|
|
|
|
|
2019-10-31 02:32:52 +00:00
|
|
|
async function rFrame (req, res, next) {
|
2017-10-22 00:12:39 +00:00
|
|
|
let dir = true
|
|
|
|
let exposure = 0
|
|
|
|
if (intval._state.frame.dir !== true) {
|
|
|
|
dir = false
|
|
|
|
}
|
|
|
|
if (intval._state.frame.exposure !== 0) {
|
|
|
|
exposure = intval._state.frame.exposure
|
|
|
|
}
|
|
|
|
if (req.query && typeof req.query.dir !== 'undefined') {
|
|
|
|
if (typeof req.query.dir === 'string') {
|
|
|
|
dir = (req.query.dir === 'true')
|
|
|
|
} else {
|
|
|
|
dir = req.query.dir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (req.body && typeof req.body.dir !== 'undefined') {
|
|
|
|
if (typeof req.body.dir === 'string') {
|
|
|
|
dir = (req.body.dir === 'true')
|
|
|
|
} else {
|
|
|
|
dir = req.body.dir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (req.query && typeof req.query.exposure !== 'undefined') {
|
|
|
|
if (typeof req.query.exposure === 'string') {
|
|
|
|
exposure = parseInt(req.query.exposure)
|
|
|
|
} else {
|
|
|
|
exposure = req.query.exposure
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (req.body && typeof req.body.exposure !== 'undefined') {
|
|
|
|
if (typeof req.body.exposure === 'string') {
|
|
|
|
exposure = parseInt(req.body.exposure)
|
|
|
|
} else {
|
|
|
|
exposure = req.body.exposure
|
|
|
|
}
|
|
|
|
}
|
2017-11-23 01:26:34 +00:00
|
|
|
if (req.query && typeof req.query.delay !== 'undefined') {
|
|
|
|
if (typeof req.query.delay === 'string') {
|
|
|
|
delay = parseInt(req.query.delay)
|
|
|
|
} else {
|
|
|
|
delay = req.query.delay
|
|
|
|
}
|
|
|
|
}
|
2017-12-12 19:23:14 +00:00
|
|
|
if (req.body && typeof req.body.delay !== 'undefined') {
|
2017-11-23 01:26:34 +00:00
|
|
|
if (typeof req.body.delay === 'string') {
|
|
|
|
delay = parseInt(req.body.delay)
|
|
|
|
} else {
|
|
|
|
delay = req.body.delay
|
|
|
|
}
|
|
|
|
}
|
2017-10-22 00:12:39 +00:00
|
|
|
log.info('/frame', { method : req.method, dir : dir, exposure : exposure })
|
2019-10-31 02:32:52 +00:00
|
|
|
|
|
|
|
if (exposure < 30000) {
|
|
|
|
await intval.frame(dir, exposure)
|
|
|
|
} else {
|
|
|
|
intval.frame(dir, exposure)
|
|
|
|
res.send({ dir : dir, len : exposure, delaying : true })
|
2017-10-21 16:21:15 +00:00
|
|
|
return next()
|
2019-10-31 02:32:52 +00:00
|
|
|
}
|
2017-08-22 02:59:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function rStatus (req, res, next) {
|
2017-08-22 04:00:24 +00:00
|
|
|
const obj = intval.status()
|
2017-09-26 03:25:34 +00:00
|
|
|
res.send(obj)
|
2017-08-22 02:59:44 +00:00
|
|
|
return next()
|
|
|
|
}
|
|
|
|
|
2019-10-31 02:32:52 +00:00
|
|
|
async function rSequence (req, res, next) {
|
2017-11-23 01:26:34 +00:00
|
|
|
let dir = true
|
|
|
|
let exposure = 0
|
|
|
|
let delay = 0
|
2019-10-31 03:04:43 +00:00
|
|
|
let options = {}
|
2017-12-20 23:40:51 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-11-23 01:26:34 +00:00
|
|
|
if (req.query && typeof req.query.dir !== 'undefined') {
|
|
|
|
if (typeof req.query.dir === 'string') {
|
|
|
|
dir = (req.query.dir === 'true')
|
|
|
|
} else {
|
|
|
|
dir = req.query.dir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (req.body && typeof req.body.dir !== 'undefined') {
|
|
|
|
if (typeof req.body.dir === 'string') {
|
|
|
|
dir = (req.body.dir === 'true')
|
|
|
|
} else {
|
|
|
|
dir = req.body.dir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (req.query && typeof req.query.exposure !== 'undefined') {
|
|
|
|
if (typeof req.query.exposure === 'string') {
|
|
|
|
exposure = parseInt(req.query.exposure)
|
|
|
|
} else {
|
|
|
|
exposure = req.query.exposure
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (req.body && typeof req.body.exposure !== 'undefined') {
|
|
|
|
if (typeof req.body.exposure === 'string') {
|
|
|
|
exposure = parseInt(req.body.exposure)
|
|
|
|
} else {
|
|
|
|
exposure = req.body.exposure
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 03:10:36 +00:00
|
|
|
if (req.query && typeof req.query.delay !== 'undefined') {
|
|
|
|
if (typeof req.query.delay === 'string') {
|
|
|
|
delay = parseInt(req.query.delay)
|
|
|
|
} else {
|
|
|
|
delay = req.query.delay
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 03:04:43 +00:00
|
|
|
if (req.body && typeof req.body.delay !== 'undefined') {
|
2017-12-21 03:10:36 +00:00
|
|
|
if (typeof req.body.delay === 'string') {
|
|
|
|
delay = parseInt(req.body.delay)
|
|
|
|
} else {
|
|
|
|
delay = req.body.delay
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 03:04:43 +00:00
|
|
|
|
|
|
|
if (req.query && typeof req.query.len !== 'undefined') {
|
|
|
|
if (typeof req.query.len === 'string') {
|
|
|
|
options.len = parseInt(req.query.len)
|
|
|
|
} else {
|
|
|
|
options.len = req.query.len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (req.body && typeof req.body.len !== 'undefined') {
|
|
|
|
if (typeof req.body.len === 'string') {
|
|
|
|
options.len = parseInt(req.body.len)
|
|
|
|
} else {
|
|
|
|
options.len = req.body.len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.query && typeof req.query.multiple !== 'undefined') {
|
|
|
|
if (typeof req.query.multiple === 'string') {
|
|
|
|
options.multiple = parseInt(req.query.multiple)
|
|
|
|
} else {
|
|
|
|
options.multiple = req.query.multiple
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (req.body && typeof req.body.multiple !== 'undefined') {
|
|
|
|
if (typeof req.body.multiple === 'string') {
|
|
|
|
options.multiple = parseInt(req.body.multiple)
|
|
|
|
} else {
|
|
|
|
options.multiple = req.body.multiple
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 02:32:52 +00:00
|
|
|
if (intval._state.sequence && sequence.active) {
|
2017-12-21 03:39:20 +00:00
|
|
|
intval._state.sequence = false
|
2019-10-31 02:32:52 +00:00
|
|
|
sequence.stop()
|
2017-12-21 03:39:20 +00:00
|
|
|
res.send({ stopped : true })
|
2019-10-31 02:32:52 +00:00
|
|
|
|
2017-12-21 03:39:20 +00:00
|
|
|
return next()
|
2019-10-31 02:32:52 +00:00
|
|
|
|
2017-11-23 01:26:34 +00:00
|
|
|
} else {
|
2017-12-21 02:49:26 +00:00
|
|
|
intval._state.sequence = true
|
2019-10-31 03:04:43 +00:00
|
|
|
sequence.start(options)
|
2019-10-31 02:32:52 +00:00
|
|
|
res.send({ started : true })
|
2017-12-21 04:47:37 +00:00
|
|
|
|
2017-12-21 01:51:14 +00:00
|
|
|
return next()
|
2017-11-23 01:26:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 21:16:44 +00:00
|
|
|
function rReset (req, res, next) {
|
|
|
|
log.info(`/reset`, {time : +new Date()})
|
|
|
|
intval.reset()
|
|
|
|
setTimeout(() => {
|
2018-02-12 19:57:06 +00:00
|
|
|
const obj = intval.status()
|
|
|
|
res.send(obj)
|
2018-02-08 21:16:44 +00:00
|
|
|
return next()
|
|
|
|
}, 10)
|
|
|
|
}
|
|
|
|
|
2018-02-08 20:31:48 +00:00
|
|
|
function rUpdate (req, res, next) {
|
2018-02-08 21:02:18 +00:00
|
|
|
log.info(`/update`, { time : +new Date() })
|
2018-02-08 20:31:48 +00:00
|
|
|
exec('sh ./scripts/update.sh', (err, stdio, stderr) => {
|
2018-02-08 21:01:22 +00:00
|
|
|
if (err) {
|
|
|
|
log.error(err)
|
|
|
|
}
|
2018-02-08 21:23:10 +00:00
|
|
|
log.info(`/update`, { git : stdio })
|
2018-02-08 21:01:22 +00:00
|
|
|
res.send({ success : true, action : 'update', output : stdio })
|
2018-02-08 20:46:51 +00:00
|
|
|
res.end()
|
2018-02-08 20:50:21 +00:00
|
|
|
next()
|
|
|
|
setTimeout(() => {
|
|
|
|
process.exit(0)
|
|
|
|
}, 100)
|
2018-02-08 20:31:48 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function rRestart (req, res, next) {
|
2018-02-08 21:02:18 +00:00
|
|
|
log.info(`/restart`, { time : +new Date() })
|
2018-02-08 20:46:51 +00:00
|
|
|
res.send({ success : true, action : 'restart' })
|
|
|
|
res.end()
|
2018-02-08 20:50:21 +00:00
|
|
|
next()
|
|
|
|
setTimeout(() => {
|
|
|
|
process.exit(0)
|
|
|
|
}, 100)
|
2018-02-08 20:31:48 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 18:09:13 +00:00
|
|
|
//Ble functions
|
|
|
|
|
2019-10-31 02:32:52 +00:00
|
|
|
async function bFrame (obj, cb) {
|
2017-12-12 19:23:14 +00:00
|
|
|
let dir = true
|
|
|
|
let exposure = 0
|
2019-10-31 02:32:52 +00:00
|
|
|
|
|
|
|
if (sequence.active) {
|
|
|
|
return cb()
|
|
|
|
}
|
2017-12-12 19:23:14 +00:00
|
|
|
|
|
|
|
if (intval._state.frame.dir !== true) {
|
|
|
|
dir = false
|
|
|
|
}
|
2017-12-13 22:01:22 +00:00
|
|
|
if (intval._state.frame.exposure !== 0) {
|
|
|
|
exposure = intval._state.frame.exposure
|
|
|
|
}
|
2017-12-12 19:23:14 +00:00
|
|
|
if (typeof obj.dir !== 'undefined') {
|
|
|
|
if (typeof obj.dir === 'string') {
|
|
|
|
dir = (obj.dir === 'true')
|
|
|
|
} else {
|
|
|
|
dir = obj.dir
|
|
|
|
}
|
|
|
|
}
|
2017-12-13 21:04:24 +00:00
|
|
|
if (typeof obj.exposure !== 'undefined') {
|
|
|
|
if (typeof obj.exposure === 'string') {
|
|
|
|
exposure = parseInt(obj.exposure)
|
|
|
|
} else {
|
|
|
|
exposure = obj.exposure
|
|
|
|
}
|
2017-12-12 19:23:14 +00:00
|
|
|
}
|
|
|
|
log.info('frame', { method : 'ble', dir : dir, exposure : exposure })
|
2018-01-17 20:58:55 +00:00
|
|
|
|
|
|
|
if (exposure < 5000) {
|
2019-10-31 02:32:52 +00:00
|
|
|
await intval.frame(dir, exposure)
|
|
|
|
return cb()
|
2018-01-17 20:58:55 +00:00
|
|
|
} else {
|
2019-10-31 02:32:52 +00:00
|
|
|
intval.frame(dir, exposure)
|
2017-12-12 19:23:14 +00:00
|
|
|
return cb()
|
2018-01-17 20:58:55 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 23:40:14 +00:00
|
|
|
//setTimeout(cb, exposure === 0 ? 630 : exposure)
|
2017-12-12 18:09:13 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 19:23:14 +00:00
|
|
|
function bDir (obj, cb) {
|
|
|
|
let dir = true
|
|
|
|
let set = false
|
|
|
|
if (obj.dir !== 'undefined') {
|
|
|
|
if (typeof obj.dir === 'string') {
|
|
|
|
dir = (obj.dir === 'true')
|
|
|
|
} else {
|
|
|
|
dir = obj.dir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
intval.setDir(dir)
|
|
|
|
log.info('dir', { method: 'ble', dir : dir })
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
|
2017-12-13 21:04:24 +00:00
|
|
|
function bExposure (obj, cb) {
|
|
|
|
let exposure = 0
|
|
|
|
if (typeof obj.exposure !== 'undefined') {
|
|
|
|
if (typeof obj.exposure === 'string') {
|
|
|
|
exposure = parseInt(obj.exposure)
|
|
|
|
} else {
|
2017-12-13 21:25:23 +00:00
|
|
|
exposure = obj.exposure
|
2017-12-13 21:04:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
intval.setExposure(exposure)
|
|
|
|
log.info('exposure', { method: 'ble', exposure : exposure })
|
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
|
2017-12-13 21:22:22 +00:00
|
|
|
function bDelay (obj, cb) {
|
|
|
|
let delay = 0
|
|
|
|
let set = false
|
|
|
|
if (typeof obj.delay !== 'undefined') {
|
|
|
|
if (typeof obj.delay === 'string') {
|
|
|
|
delay = parseInt(obj.delay)
|
|
|
|
} else {
|
|
|
|
delay = obj.delay
|
|
|
|
}
|
|
|
|
set = true
|
|
|
|
}
|
|
|
|
intval.setDelay(delay)
|
|
|
|
log.info('delay', { method: 'ble', delay : delay })
|
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
|
2017-12-13 22:27:42 +00:00
|
|
|
function bCounter (obj, cb) {
|
|
|
|
let counter = 0
|
|
|
|
if (typeof obj.counter !== 'undefined') {
|
|
|
|
if (typeof obj.counter !== 'string') {
|
|
|
|
counter = parseInt(obj.counter)
|
|
|
|
} else {
|
|
|
|
counter = obj.counter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
intval.setCounter(counter)
|
|
|
|
log.info('counter', { method : 'ble', counter : counter })
|
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
|
2017-12-20 23:40:51 +00:00
|
|
|
function bSequence (obj, cb) {
|
|
|
|
let dir = true
|
|
|
|
let exposure = 0
|
|
|
|
let delay = 0
|
2019-10-31 03:04:43 +00:00
|
|
|
let options = {}
|
2017-12-20 23:40:51 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 03:04:43 +00:00
|
|
|
if (typeof obj.len !== 'undefined') {
|
|
|
|
if (typeof obj.len === 'string') {
|
|
|
|
options.len = parseInt(obj.len)
|
|
|
|
} else {
|
|
|
|
options.len = obj.len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (typeof obj.multiple !== 'undefined') {
|
|
|
|
if (typeof obj.multiple === 'string') {
|
|
|
|
options.multiple = parseInt(obj.multiple)
|
|
|
|
} else {
|
|
|
|
options.multiple = obj.multiple
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 02:49:26 +00:00
|
|
|
if (intval._state.sequence && sequence._state.active) {
|
2018-02-06 22:42:50 +00:00
|
|
|
//should not occur with single client
|
2018-01-01 05:52:29 +00:00
|
|
|
intval._state.sequence = false
|
2019-10-31 02:32:52 +00:00
|
|
|
sequence.stop()
|
2018-01-01 06:08:59 +00:00
|
|
|
log.info('sequence stop', { method : 'ble' })
|
2018-01-01 05:52:29 +00:00
|
|
|
return cb()
|
2017-12-20 23:40:51 +00:00
|
|
|
} else {
|
2017-12-21 02:49:26 +00:00
|
|
|
intval._state.sequence = true
|
2019-10-31 03:04:43 +00:00
|
|
|
sequence.start(options)
|
2019-10-31 02:32:52 +00:00
|
|
|
log.info('sequence start', { method : 'ble' })
|
2017-12-20 23:40:51 +00:00
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 20:58:55 +00:00
|
|
|
function bSequenceStop (obj, cb) {
|
2019-10-31 03:04:43 +00:00
|
|
|
if (intval._state.sequence && sequence.active) {
|
|
|
|
sequence.stop()
|
2018-02-06 22:42:50 +00:00
|
|
|
intval._state.sequence = false
|
|
|
|
log.info('sequence stop', { method : 'ble' })
|
|
|
|
return cb()
|
|
|
|
}
|
2018-01-17 20:58:55 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 23:40:51 +00:00
|
|
|
function bReset (obj, cb) {
|
2018-02-12 19:38:27 +00:00
|
|
|
log.info(`reset`, { method: 'ble' })
|
2017-12-20 23:40:51 +00:00
|
|
|
intval.reset()
|
|
|
|
setTimeout(cb, 10)
|
|
|
|
}
|
|
|
|
|
2018-02-08 20:31:48 +00:00
|
|
|
function bUpdate (obj, cb) {
|
2018-02-12 19:38:27 +00:00
|
|
|
log.info('update', { method : 'ble' })
|
2018-02-12 19:22:31 +00:00
|
|
|
exec('sh ./scripts/update.sh', (err, stdio, stderr) => {
|
2018-02-12 19:38:27 +00:00
|
|
|
if (err) {
|
|
|
|
log.error('update', err)
|
|
|
|
}
|
|
|
|
log.info('update', { stdio : stdio })
|
2018-02-12 19:22:31 +00:00
|
|
|
cb()
|
|
|
|
setTimeout(() => {
|
2018-02-08 20:31:48 +00:00
|
|
|
process.exit(0)
|
2018-02-12 19:22:31 +00:00
|
|
|
}, 20)
|
|
|
|
})
|
2018-02-08 20:31:48 +00:00
|
|
|
}
|
2019-10-31 02:32:52 +00:00
|
|
|
|
2018-02-08 20:31:48 +00:00
|
|
|
function bRestart (obj, cb) {
|
2018-02-12 19:38:27 +00:00
|
|
|
log.info('restart', { method : 'ble' })
|
2018-02-08 20:31:48 +00:00
|
|
|
cb()
|
|
|
|
setTimeout(() => {
|
|
|
|
process.exit(0)
|
|
|
|
}, 20)
|
|
|
|
}
|
|
|
|
|
2017-08-22 01:11:07 +00:00
|
|
|
function index (req, res, next) {
|
2017-10-21 15:46:06 +00:00
|
|
|
fs.readFile(INDEXPATH, 'utf8', (err, data) => {
|
2017-08-28 12:49:47 +00:00
|
|
|
if (err) {
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
res.end(data)
|
|
|
|
next()
|
2017-10-21 15:46:06 +00:00
|
|
|
})
|
2017-08-22 01:11:07 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 02:59:44 +00:00
|
|
|
function init () {
|
2017-12-12 18:09:13 +00:00
|
|
|
createServer()
|
|
|
|
createBLE()
|
2017-08-22 02:59:44 +00:00
|
|
|
}
|
2017-08-22 01:11:07 +00:00
|
|
|
|
2017-08-22 02:59:44 +00:00
|
|
|
init()
|