Manage state from the webapp. Build all features there before mobile.
This commit is contained in:
parent
58f8d09775
commit
186b8324d1
|
@ -2,6 +2,11 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>intval 3</title>
|
||||
<style>
|
||||
html,body{
|
||||
background: #212121;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<form method="POST" action="/frame">
|
||||
|
|
91
index.js
91
index.js
|
@ -4,7 +4,7 @@ const restify = require('restify')
|
|||
const log = require('./lib/log')('main')
|
||||
const fs = require('fs')
|
||||
|
||||
const ble = require('./lib/blootstrap')
|
||||
//const ble = require('./lib/blootstrap')
|
||||
const intval = require('./lib/intval')
|
||||
|
||||
const PACKAGE = require('./package.json')
|
||||
|
@ -18,9 +18,13 @@ let app = restify.createServer({
|
|||
})
|
||||
|
||||
function createServer () {
|
||||
app.use(restify.plugins.queryParser())
|
||||
app.use(restify.plugins.bodyParser({ mapParams: false }))
|
||||
app.get('/', index)
|
||||
app.get('/dir', rDir)
|
||||
app.post('/dir', rDir)
|
||||
app.get('/exposure', rExposure)
|
||||
app.post('/exposure', rExposure)
|
||||
app.get('/frame', rFrame)
|
||||
app.post('/frame', rFrame)
|
||||
app.get('/sequence', () => {})
|
||||
|
@ -32,7 +36,82 @@ function createServer () {
|
|||
}
|
||||
|
||||
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) {
|
||||
|
@ -48,22 +127,22 @@ function rStatus (req, res, next) {
|
|||
}
|
||||
|
||||
function index (req, res, next) {
|
||||
fs.readFile(INDEXPATH, (err, data) => {
|
||||
fs.readFile(INDEXPATH, 'utf8', (err, data) => {
|
||||
if (err) {
|
||||
return next(err)
|
||||
}
|
||||
res.end(data)
|
||||
next()
|
||||
}, 'utf8')
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
function init () {
|
||||
createServer()
|
||||
|
||||
ble.on('data', (str) => {
|
||||
/*ble.on('data', (str) => {
|
||||
console.log(str)
|
||||
})
|
||||
})*/
|
||||
//intval.init()
|
||||
}
|
||||
|
||||
init()
|
||||
|
|
|
@ -38,17 +38,13 @@ const intval = {}
|
|||
intval.init = function () {
|
||||
intval._pin = {}
|
||||
intval._state = {
|
||||
dir : true, //forward
|
||||
frame : {
|
||||
dir : true, //forward
|
||||
start : 0, //time frame started, timestamp
|
||||
active : false, //should frame be running
|
||||
paused : false,
|
||||
time : 0, //length 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
|
||||
exposure : 0, //length of frame exposure, in ms
|
||||
delay : 0 //delay before start of frame, in ms
|
||||
},
|
||||
release : {
|
||||
time: 0,
|
||||
|
@ -60,7 +56,10 @@ intval.init = function () {
|
|||
}
|
||||
}
|
||||
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 = {
|
||||
min : 20,
|
||||
|
@ -171,13 +170,13 @@ intval._watchMicro = function (err, val) {
|
|||
if (val === 0 && intval._state.frame.active) {
|
||||
if (!intval._state.micro.primed) {
|
||||
intval._state.micro.primed = true
|
||||
intval._state.micro.time = NOW
|
||||
intval._state.micro.exposure = NOW
|
||||
//log.info('Microswitch primed to stop motor')
|
||||
}
|
||||
} else if (val === 1 && intval._state.frame.active) {
|
||||
if (intval._state.micro.primed && !intval._state.micro.paused) {
|
||||
intval._state.micro.primed = false
|
||||
intval._state.micro.time = 0
|
||||
intval._state.micro.exposure = 0
|
||||
//setTimeout( () => {
|
||||
intval._stop()
|
||||
//}, intval._microDelay)
|
||||
|
@ -190,7 +189,7 @@ intval._watchMicro = function (err, val) {
|
|||
*
|
||||
* 1) If closed, start timer.
|
||||
* 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
|
||||
*
|
||||
* Button + 10K ohm resistor
|
||||
|
@ -211,30 +210,30 @@ intval._watchRelease = function (err, val) {
|
|||
if (val === 0) {
|
||||
//closed
|
||||
if (intval._releaseClosedState(now)) {
|
||||
intval._state.release.time = now
|
||||
intval._state.release.exposure = now
|
||||
intval._state.release.active = true //maybe unncecessary
|
||||
}
|
||||
} else if (val === 1) {
|
||||
//opened
|
||||
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) {
|
||||
intval.frame()
|
||||
} else if (press >= intval._release.seq) {
|
||||
intval.sequence()
|
||||
}
|
||||
//log.info(`Release closed for ${press}ms`)
|
||||
intval._state.release.time = 0
|
||||
intval._state.release.exposure = 0
|
||||
intval._state.release.active = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
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 false
|
||||
|
@ -251,13 +250,15 @@ intval.setDir = function (val = true) {
|
|||
if (typeof val !== 'boolean') {
|
||||
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' })
|
||||
}
|
||||
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 })
|
||||
}
|
||||
|
||||
intval.setDelay = function (val = 0) {
|
||||
intval._state.frame.delay = val
|
||||
log.info('setDelay', { delay : val })
|
||||
|
@ -270,14 +271,14 @@ intval.setDelay = function (val = 0) {
|
|||
*
|
||||
*/
|
||||
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
|
||||
} else {
|
||||
dir = false
|
||||
}
|
||||
|
||||
if (time === null && intval._state.frame.time !== 0) {
|
||||
time = intval._state.frame.time
|
||||
if (time === null && intval._state.frame.exposure !== 0) {
|
||||
time = intval._state.frame.exposure
|
||||
} else if (time === null) {
|
||||
time = 0 //default speed
|
||||
}
|
||||
|
@ -297,20 +298,19 @@ intval.frame = function (dir = null, time = null) {
|
|||
intval._state.frame.paused = true
|
||||
|
||||
if (dir) {
|
||||
setTimeout(intval._pause, intval._state.frame.open)
|
||||
//log.info('frame', { pausing : time + intval._state.frame.open })
|
||||
setTimeout(intval._pause, intval._frame.open)
|
||||
//log.info('frame', { pausing : time + intval._frame.open })
|
||||
setTimeout( () => {
|
||||
//log.info('frame', 'restarting')
|
||||
intval._state.frame.paused = false
|
||||
intval._startFwd()
|
||||
}, time + intval._state.frame.closed)
|
||||
}, time + intval._frame.closed)
|
||||
} else {
|
||||
setTimeout(intval._pause, intval._state.frame.openBwd)
|
||||
setTimeout(intval._pause, intval._frame.openBwd)
|
||||
setTimeout( () => {
|
||||
//log.info('frame', 'restarting')
|
||||
intval._state.frame.paused = false
|
||||
intval._startBwd()
|
||||
}, time + intval._state.frame.closed)
|
||||
}, time + intval._frame.closed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
28
nginx.conf
28
nginx.conf
|
@ -1,28 +1,8 @@
|
|||
#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 {
|
||||
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 / {
|
||||
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-Real-IP $remote_addr;
|
||||
gzip on;
|
||||
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_comp_level 5;
|
||||
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
|
||||
#location /static/ {
|
||||
|
@ -40,8 +20,8 @@ server {
|
|||
#access_log off;
|
||||
#add_header Cache-Control "public";
|
||||
#gzip on;
|
||||
#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_comp_level 5;
|
||||
#gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/json;
|
||||
#use project location
|
||||
#alias /var/node/intval3/static/;
|
||||
#}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/bash
|
||||
|
||||
curl -H "Content-Type: application/json" -X POST -d '{"dir" : false }' http://localhost:6699/dir
|
Loading…
Reference in New Issue