Manage state from the webapp. Build all features there before mobile.

This commit is contained in:
mmcw-dev 2017-10-21 11:46:06 -04:00
parent 58f8d09775
commit 186b8324d1
5 changed files with 125 additions and 58 deletions

View File

@ -2,6 +2,11 @@
<html> <html>
<head> <head>
<title>intval 3</title> <title>intval 3</title>
<style>
html,body{
background: #212121;
}
</style>
</head> </head>
<body> <body>
<form method="POST" action="/frame"> <form method="POST" action="/frame">

View File

@ -4,7 +4,7 @@ const restify = require('restify')
const log = require('./lib/log')('main') const log = require('./lib/log')('main')
const fs = require('fs') const fs = require('fs')
const ble = require('./lib/blootstrap') //const ble = require('./lib/blootstrap')
const intval = require('./lib/intval') const intval = require('./lib/intval')
const PACKAGE = require('./package.json') const PACKAGE = require('./package.json')
@ -18,9 +18,13 @@ let app = restify.createServer({
}) })
function createServer () { function createServer () {
app.use(restify.plugins.queryParser())
app.use(restify.plugins.bodyParser({ mapParams: false }))
app.get('/', index) app.get('/', index)
app.get('/dir', rDir) app.get('/dir', rDir)
app.post('/dir', rDir) app.post('/dir', rDir)
app.get('/exposure', rExposure)
app.post('/exposure', rExposure)
app.get('/frame', rFrame) app.get('/frame', rFrame)
app.post('/frame', rFrame) app.post('/frame', rFrame)
app.get('/sequence', () => {}) app.get('/sequence', () => {})
@ -32,7 +36,82 @@ function createServer () {
} }
function rDir (req, res, next) { function rDir (req, res, next) {
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
}
res.send({ dir : dir })
return next()
}
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) {
intval.setExposure(exposure)
} else {
exposure = intval._state.frame.exposure
}
res.send({ exposure : exposure })
}
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
}
res.send({ delay : delay })
} }
function rFrame (req, res, next) { function rFrame (req, res, next) {
@ -48,22 +127,22 @@ function rStatus (req, res, next) {
} }
function index (req, res, next) { function index (req, res, next) {
fs.readFile(INDEXPATH, (err, data) => { fs.readFile(INDEXPATH, 'utf8', (err, data) => {
if (err) { if (err) {
return next(err) return next(err)
} }
res.end(data) res.end(data)
next() next()
}, 'utf8') })
} }
function init () { function init () {
createServer() createServer()
ble.on('data', (str) => { /*ble.on('data', (str) => {
console.log(str) console.log(str)
}) })*/
//intval.init()
} }
init() init()

View File

@ -38,17 +38,13 @@ const intval = {}
intval.init = function () { intval.init = function () {
intval._pin = {} intval._pin = {}
intval._state = { intval._state = {
dir : true, //forward
frame : { frame : {
dir : true, //forward
start : 0, //time frame started, timestamp start : 0, //time frame started, timestamp
active : false, //should frame be running active : false, //should frame be running
paused : false, paused : false,
time : 0, //length of frame, in ms exposure : 0, //length of frame exposure, in ms
delay : 0, //delay before start of frame, in ms delay : 0 //delay before start of frame, in ms
open : 250, //delay before pausing frame in open state
openBwd : 400,
closed : 100, //time that frame actually remains closed for
expected : 630 //expected length of frame, in ms
}, },
release : { release : {
time: 0, time: 0,
@ -60,7 +56,10 @@ intval.init = function () {
} }
} }
intval._frame = { intval._frame = {
open : 250, //delay before pausing frame in open state
openBwd : 400,
closed : 100, //time that frame actually remains closed for
expected : 630 //expected length of frame, in ms
} }
intval._release = { intval._release = {
min : 20, min : 20,
@ -171,13 +170,13 @@ intval._watchMicro = function (err, val) {
if (val === 0 && intval._state.frame.active) { if (val === 0 && intval._state.frame.active) {
if (!intval._state.micro.primed) { if (!intval._state.micro.primed) {
intval._state.micro.primed = true intval._state.micro.primed = true
intval._state.micro.time = NOW intval._state.micro.exposure = NOW
//log.info('Microswitch primed to stop motor') //log.info('Microswitch primed to stop motor')
} }
} else if (val === 1 && intval._state.frame.active) { } else if (val === 1 && intval._state.frame.active) {
if (intval._state.micro.primed && !intval._state.micro.paused) { if (intval._state.micro.primed && !intval._state.micro.paused) {
intval._state.micro.primed = false intval._state.micro.primed = false
intval._state.micro.time = 0 intval._state.micro.exposure = 0
//setTimeout( () => { //setTimeout( () => {
intval._stop() intval._stop()
//}, intval._microDelay) //}, intval._microDelay)
@ -190,7 +189,7 @@ intval._watchMicro = function (err, val) {
* *
* 1) If closed, start timer. * 1) If closed, start timer.
* 2) If opened, check timer AND * 2) If opened, check timer AND
* 3) If `press` (`now - intval._state.release.time`) greater than minimum and less than `intval._release.seq`, start frame * 3) If `press` (`now - intval._state.release.exposure`) greater than minimum and less than `intval._release.seq`, start frame
* 4) If `press` greater than `intval._release.seq`, start sequence * 4) If `press` greater than `intval._release.seq`, start sequence
* *
* Button + 10K ohm resistor * Button + 10K ohm resistor
@ -211,30 +210,30 @@ intval._watchRelease = function (err, val) {
if (val === 0) { if (val === 0) {
//closed //closed
if (intval._releaseClosedState(now)) { if (intval._releaseClosedState(now)) {
intval._state.release.time = now intval._state.release.exposure = now
intval._state.release.active = true //maybe unncecessary intval._state.release.active = true //maybe unncecessary
} }
} else if (val === 1) { } else if (val === 1) {
//opened //opened
if (intval._state.release.active) { if (intval._state.release.active) {
press = now - intval._state.release.time press = now - intval._state.release.exposure
if (press > intval._release.min && press < intval._release.seq) { if (press > intval._release.min && press < intval._release.seq) {
intval.frame() intval.frame()
} else if (press >= intval._release.seq) { } else if (press >= intval._release.seq) {
intval.sequence() intval.sequence()
} }
//log.info(`Release closed for ${press}ms`) //log.info(`Release closed for ${press}ms`)
intval._state.release.time = 0 intval._state.release.exposure = 0
intval._state.release.active = false intval._state.release.active = false
} }
} }
} }
intval._releaseClosedState = function (now) { intval._releaseClosedState = function (now) {
if (!intval._state.release.active && intval._state.release.time === 0) { if (!intval._state.release.active && intval._state.release.exposure === 0) {
return true return true
} }
if (intval._state.release.active && (now - intval._state.release.time) > (intval._release.seq * 10)) { if (intval._state.release.active && (now - intval._state.release.exposure) > (intval._release.seq * 10)) {
return true return true
} }
return false return false
@ -251,13 +250,15 @@ intval.setDir = function (val = true) {
if (typeof val !== 'boolean') { if (typeof val !== 'boolean') {
return log.warn('Direction must be represented as either true or false') return log.warn('Direction must be represented as either true or false')
} }
intval._state.dir = val intval._state.frame.dir = val
log.info('setDir', { direction : val ? 'forward' : 'backward' }) log.info('setDir', { direction : val ? 'forward' : 'backward' })
} }
intval.setTime = function (val = 0) {
intval._state.frame.time = val intval.setExposure = function (val = 0) {
intval._state.frame.exposure = val
log.info('setTime', { time : val }) log.info('setTime', { time : val })
} }
intval.setDelay = function (val = 0) { intval.setDelay = function (val = 0) {
intval._state.frame.delay = val intval._state.frame.delay = val
log.info('setDelay', { delay : val }) log.info('setDelay', { delay : val })
@ -270,14 +271,14 @@ intval.setDelay = function (val = 0) {
* *
*/ */
intval.frame = function (dir = null, time = null) { intval.frame = function (dir = null, time = null) {
if (dir === true || (dir === null && intval._state.dir === true) ) { if (dir === true || (dir === null && intval._state.frame.dir === true) ) {
dir = true dir = true
} else { } else {
dir = false dir = false
} }
if (time === null && intval._state.frame.time !== 0) { if (time === null && intval._state.frame.exposure !== 0) {
time = intval._state.frame.time time = intval._state.frame.exposure
} else if (time === null) { } else if (time === null) {
time = 0 //default speed time = 0 //default speed
} }
@ -297,20 +298,19 @@ intval.frame = function (dir = null, time = null) {
intval._state.frame.paused = true intval._state.frame.paused = true
if (dir) { if (dir) {
setTimeout(intval._pause, intval._state.frame.open) setTimeout(intval._pause, intval._frame.open)
//log.info('frame', { pausing : time + intval._state.frame.open }) //log.info('frame', { pausing : time + intval._frame.open })
setTimeout( () => { setTimeout( () => {
//log.info('frame', 'restarting')
intval._state.frame.paused = false intval._state.frame.paused = false
intval._startFwd() intval._startFwd()
}, time + intval._state.frame.closed) }, time + intval._frame.closed)
} else { } else {
setTimeout(intval._pause, intval._state.frame.openBwd) setTimeout(intval._pause, intval._frame.openBwd)
setTimeout( () => { setTimeout( () => {
//log.info('frame', 'restarting') //log.info('frame', 'restarting')
intval._state.frame.paused = false intval._state.frame.paused = false
intval._startBwd() intval._startBwd()
}, time + intval._state.frame.closed) }, time + intval._frame.closed)
} }
} }
} }

View File

@ -1,28 +1,8 @@
#blootstrap nginx conf #blootstrap nginx conf
#uncomment and modify following files for ssl
#server {
#listen 80;
#server_name my_project;
#return 301 https://$server_name$request_uri;
#}
server { server {
listen 80; listen 80;
#listen 443 ssl;
#ssl on;
#ssl_certificate {{SSL_CERT_PATH}};
#ssl_certificate_key {{SSL_KEY_PATH}};
#ssl_session_timeout 5m;
#ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
#ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
#ssl_prefer_server_ciphers on;
#server_name my_project;
location / { location / {
proxy_pass http://127.0.0.1:6699/; proxy_pass http://127.0.0.1:6699/;
@ -30,8 +10,8 @@ server {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
gzip on; gzip on;
gzip_comp_level 5; gzip_comp_level 5;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/json; gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/json;
} }
#uncomment for static file server #uncomment for static file server
#location /static/ { #location /static/ {
@ -40,8 +20,8 @@ server {
#access_log off; #access_log off;
#add_header Cache-Control "public"; #add_header Cache-Control "public";
#gzip on; #gzip on;
#gzip_comp_level 5; #gzip_comp_level 5;
#gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/json; #gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/json;
#use project location #use project location
#alias /var/node/intval3/static/; #alias /var/node/intval3/static/;
#} #}

3
tests/curl_endpoints.sh Normal file
View File

@ -0,0 +1,3 @@
#!/bin/bash
curl -H "Content-Type: application/json" -X POST -d '{"dir" : false }' http://localhost:6699/dir