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')
|
2017-08-28 12:49:47 +00:00
|
|
|
|
2017-11-22 14:30:08 +00:00
|
|
|
const ble = require('./lib/ble')
|
2017-08-28 12:49:47 +00:00
|
|
|
const intval = require('./lib/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,
|
2017-08-22 02:59:44 +00:00
|
|
|
version: '0.0.1'
|
2017-08-22 01:23:48 +00:00
|
|
|
})
|
|
|
|
|
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-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-10-21 16:08:12 +00:00
|
|
|
app.get( '/sequence', () => {})
|
2017-08-22 04:00:24 +00:00
|
|
|
app.post('/sequence', () => {})
|
2017-10-23 17:33:05 +00:00
|
|
|
app.post('/reset', rReset)
|
2017-10-21 16:08:12 +00:00
|
|
|
app.get( '/status', rStatus)
|
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-11-22 14:30:08 +00:00
|
|
|
function bleBindings () {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
} else if (req.body && typeof req.body.dir !== 'udnefined') {
|
|
|
|
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
|
|
|
|
} else if (req.body && typeof req.body.exposure !== 'udnefined') {
|
|
|
|
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
|
|
|
|
} else if (req.body && typeof req.body.delay !== 'udnefined') {
|
|
|
|
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 })
|
|
|
|
req.send({ counter : counter })
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
|
2017-08-22 02:59:44 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.info('/frame', { method : req.method, dir : dir, exposure : exposure })
|
|
|
|
intval.frame(dir, exposure, (len) => {
|
|
|
|
res.send({ dir : dir, len : len})
|
2017-10-21 16:21:15 +00:00
|
|
|
return next()
|
2017-10-21 16:08:12 +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()
|
|
|
|
}
|
|
|
|
|
2017-10-24 00:29:52 +00:00
|
|
|
function rReset (req, res, next) {
|
2017-10-23 17:33:05 +00:00
|
|
|
intval.reset()
|
|
|
|
setTimeout(() => {
|
|
|
|
res.send(intval._state)
|
|
|
|
return next()
|
|
|
|
}, 10)
|
|
|
|
}
|
|
|
|
|
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 () {
|
|
|
|
createServer()
|
|
|
|
|
2017-10-21 15:46:06 +00:00
|
|
|
/*ble.on('data', (str) => {
|
2017-08-22 02:59:44 +00:00
|
|
|
console.log(str)
|
2017-10-21 15:46:06 +00:00
|
|
|
})*/
|
2017-10-21 15:47:55 +00:00
|
|
|
intval.init()
|
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()
|