Add sqlite3 and table logging functionality. Track all frames all the time.

This commit is contained in:
mmcwilliams 2017-11-23 09:19:43 -05:00
parent 37326534ed
commit 73a65d9021
3 changed files with 67 additions and 14 deletions

View File

@ -1,2 +1,35 @@
'use strict'
const os = require('os')
const sqlite3 = require('sqlite3').verbose()
const squel = require('squel')
const DB_FILE = os.homedir() + '/.intval3.db'
const db = new sqlite3.Database(DB_FILE)
class DB {
constructor () {
this._table = 'frames'
this.createTable()
}
createTable () {
const query = `CREATE TABLE
IF NOT EXISTS ${this._table} (
dir BOOLEAN,
exposure INTEGER,
start INTEGER,
stop INTEGER,
len INTEGER,
counter INTEGER
);`
db.run(query)
}
insert (obj) {
const query = squel.insert()
.into(this._table)
.setFields(obj) //dir, exposure, start, stop, len, counter
.toString()
db.run(query)
}
}
module.exports = new DB()

View File

@ -1,5 +1,6 @@
'use strict'
const db = require('../db')
const log = require('../log')('intval')
const storage = require('node-persist')
const fs = require('fs')
@ -93,6 +94,7 @@ intval._setState = function (data) {
paused : false,
exposure : 0, //length of frame exposure, in ms
delay : 0, //delay before start of frame, in ms
current : {}, //current settings
cb : () => {}
},
release : {
@ -178,16 +180,30 @@ intval._pause = function () {
*
*/
intval._stop = function () {
intval._pin.fwd.writeSync(0)
intval._pin.bwd.writeSync(0)
const entry = {}
const now = +new Date()
const len = now - intval._state.frame.start
intval._pin.fwd.writeSync(0)
intval._pin.bwd.writeSync(0)
log.info(`_stop`, { frame : len })
intval._pin.micro.unwatch()
intval._state.frame.active = false
if (intval._state.frame.cb) intval._state.frame.cb(len)
entry.start = intval._state.frame.start
entry.stop = now
entry.len = len
entry.dir = intval._state.current.dir
entry.exposure = intval._state.frame.current.exposure
entry.counter = intval._state.counter
db.insert(entry)
intval._state.frame.current = {}
}
/**
* Callback for watching relese switch state changes.
@ -326,34 +342,34 @@ intval.setCounter = function (val = 0) {
* Begin a single frame with set variables or defaults
*
* @param {?boolean} [dir="null"] (optional) Direction of the frame
* @param {?integer} [time="null"] (optional) Exposure time, 0 = minimum
* @param {?integer} [exposure="null"] (optional) Exposure time, 0 = minimum
*
*/
intval.frame = function (dir = null, time = null, cb = () => {}) {
intval.frame = function (dir = null, expsure = null, cb = () => {}) {
if (dir === true || (dir === null && intval._state.frame.dir === true) ) {
dir = true
} else {
dir = false
}
if (time === null && intval._state.frame.exposure !== 0) {
time = intval._state.frame.exposure
} else if (time === null) {
time = 0 //default speed
if (exposure === null && intval._state.frame.exposure !== 0) {
exposure = intval._state.frame.exposure
} else if (exposure === null) {
exposure = 0 //default speed
}
intval._state.frame.start = +new Date()
intval._state.frame.active = true
intval._pin.micro.watch(intval._watchMicro)
log.info('frame', {dir : dir ? 'forward' : 'backward', exposure : time})
log.info('frame', {dir : dir ? 'forward' : 'backward', exposure : exposure})
if (dir) {
intval._startFwd()
} else {
intval._startBwd()
}
if (time !== 0) {
if (exposure !== 0) {
intval._state.frame.paused = true
if (dir) {
setTimeout(intval._pause, intval._frame.open)
@ -361,14 +377,14 @@ intval.frame = function (dir = null, time = null, cb = () => {}) {
setTimeout( () => {
intval._state.frame.paused = false
intval._startFwd()
}, time + intval._frame.closed)
}, exposure + intval._frame.closed)
} else {
setTimeout(intval._pause, intval._frame.openBwd)
setTimeout( () => {
//log.info('frame', 'restarting')
intval._state.frame.paused = false
intval._startBwd()
}, time + intval._frame.closed)
}, exposure + intval._frame.closed)
}
}
if (dir) {
@ -384,6 +400,10 @@ intval.frame = function (dir = null, time = null, cb = () => {}) {
cb(len)
}
}
intval._state.frame.current = {
dir: dir,
exposure: time
}
}
/**
* Start a sequence of frames, using defaults or explicit instructions

View File

@ -9,7 +9,7 @@ apt-get install nodejs npm -y
npm install -g n
n 9.1.0
npm install -g npm@latest
npm install -g pm2
npm install -g pm2 node-gyp
echo "Installing bluetooth dependencies..."
apt-get install bluetooth bluez libbluetooth-dev libudev-dev -y