Save devices state

Restore state
Switch to use intval as camera
This commit is contained in:
mmcwilliams 2018-02-01 14:52:47 -05:00
parent db47c4b524
commit 34a7d37b7b
4 changed files with 237 additions and 143 deletions

View File

@ -1,20 +1,16 @@
{ {
"version" : "2.0.0", "version" : "2.0.0",
"ext_port" : 1111, "ext_port" : 1111,
"arduino" : {
"baud" : 57600,
"board" : "uno",
"serialDelay" : 20,
"sequenceDelay" : 100,
"profiles" : { "profiles" : {
"mcopy" : { "mcopy" : {
"label" : "Default mcopy profile",
"cam" : { "cam" : {
"time" : 630, "time" : 850,
"delay" : 50, "delay" : 50,
"momentary" : 0 "momentary" : 0
}, },
"proj" : { "proj" : {
"time" : 1300, "time" : 1400,
"delay" : 50, "delay" : 50,
"momentary" : 0 "momentary" : 0
}, },
@ -23,7 +19,25 @@
"after" : 0 "after" : 0
} }
}, },
"jk103" : {
"label" : "JK103",
"cam" : {
"time" : 600,
"delay" : 50,
"momentary" : 240
},
"proj" : {
"time" : 950,
"delay" : 50,
"momentary" : 240
},
"black" : {
"before" : 0,
"after" : 0
}
},
"jk_original" : { "jk_original" : {
"label" : "JK45 profile",
"cam" : { "cam" : {
"time" : 750, "time" : 750,
"delay" : 50, "delay" : 50,
@ -39,25 +53,22 @@
"after" : 250 "after" : 250
} }
}, },
"jk103" : { "intval3" : {
"label" : "INTVAL3",
"cam" : { "cam" : {
"time" : 600, "time" : 630,
"delay" : 50, "delay" : 50,
"momentary" : 240 "momentary" : 0
},
"proj" : {
"time" : 950,
"delay" : 50,
"momentary" : 240
},
"black" : {
"before" : 0,
"after" : 0
} }
} }
}, },
"arduino" : {
"baud" : 57600,
"board" : "uno",
"serialDelay" : 20,
"sequenceDelay" : 100,
"cam" : { "cam" : {
"time" : 750, "time" : 850,
"delay" : 50, "delay" : 50,
"momentary" : 300 "momentary" : 300
}, },

View File

@ -12,6 +12,10 @@
padding: 6px 12px; padding: 6px 12px;
font-size: 21px; font-size: 21px;
min-width: 300px; min-width: 300px;
&.active{
border-color: @SELECTED;
color: @SELECTED;
}
} }
button{ button{
margin-top: -1px; margin-top: -1px;

View File

@ -3,40 +3,65 @@ var devices = {};
devices.init = function () { devices.init = function () {
'use strict'; 'use strict';
devices.listen(); devices.listen();;
devices.profiles();
gui.overlay(true); gui.overlay(true);
gui.spinner(true, 'Checking for connected devices...'); gui.spinner(true, 'Checking for connected devices...');
}; };
devices.profiles = function () {
const keys = Object.keys(mcopy.cfg.profiles);
const elem = $('#profile')
let opt;
elem.empty();
for (let key of keys) {
opt = $('<option>');
opt.val(key);
opt.text(mcopy.cfg.profiles[key].label);
elem.append(opt);
}
elem.on('change', (t) => {
devices.profile($('#profile').val());
})
devices.profile(keys[0]);
};
devices.profile = function (profile) {
log.info(`Changed configuration profile to "${profile}"`, 'DEVICES', true, true);
const p = mcopy.cfg.profiles[profile];
const keys = Object.keys(p);
for (let key of keys) {
mcopy.cfg[key] = keys[key]
}
};
devices.listen = function () { devices.listen = function () {
'use strict'; 'use strict';
ipcRenderer.on('ready', devices.ready); ipcRenderer.on('ready', devices.ready);
ipcRenderer.on('intval', devices.intvalCb); ipcRenderer.on('intval', devices.intvalCb);
}; };
devices.ready = function (event, arg) { devices.ready = function (event, arg) {
'use strict'; 'use strict';
console.dir(arg)
let opt; let opt;
let devs = []; let devs = [];
let notify = 'Connected to ' let notify = 'Connected to ';
gui.spinner(false); gui.spinner(false);
gui.overlay(false); gui.overlay(false);
for (let i in arg) { for (let i in arg) {
if (arg[i] !== '/dev/fake') { devs.push(arg[i].arduino);
devs.push(arg[i]); if (arg[i].arduino && arg[i].arduino !== '/dev/fake') {
if (notify === 'Connected to ') { if (notify === 'Connected to ') {
notify += arg[i] + ' ' notify += arg[i].arduino + ' '
} else { } else {
notify += '& ' + arg[i] notify += `& ${arg[i].arduino}`
} }
} }
opt = $('<option>'); opt = $('<option>');
opt.val(arg[i]); opt.val(`ARDUINO_${arg[i].arduino}`);
opt.text(arg[i]); opt.text(arg[i].arduino);
$(`#${i}_device`).empty(); $(`#${i}_device`).empty();
$(`#${i}_device`).append(opt); $(`#${i}_device`).append(opt);
if (notify !== 'Connected to ') {
gui.notify()
} }
if (notify !== 'Connected to ') {
gui.notify('DEVICES', notify)
} }
if (devs.length > 0) { if (devs.length > 0) {
$('#devices').empty(); $('#devices').empty();
@ -59,7 +84,7 @@ devices.intval = function () {
url : url url : url
} }
if ( url !== '' && typeof url !== 'undefined') { if ( url !== '' && typeof url !== 'undefined') {
proceed = confirm(`Are you sure you want to connect to INTVAL3 ${url}?`) proceed = confirm(`Are you sure you want to connect to INTVAL3 @ ${url}?`)
} else { } else {
alert('Cannot connect to INTVAL3 url as entered.') alert('Cannot connect to INTVAL3 url as entered.')
} }
@ -70,17 +95,28 @@ devices.intval = function () {
ipcRenderer.send('intval', obj) ipcRenderer.send('intval', obj)
} else { } else {
$('#camera_type_arduino').prop('checked', 'checked'); $('#camera_type_arduino').prop('checked', 'checked');
$('#intval').removeClass('active');
} }
}; };
devices.intvalCb = function (evt, args) { devices.intvalCb = function (evt, args) {
'use strict'; 'use strict';
let state;
gui.spinner(false); gui.spinner(false);
gui.overlay(false); gui.overlay(false);
if (args.connected && args.connected === true) { if (args.connected && args.connected === true) {
//success state //success state
state = JSON.parse(args.state);
mcopy.state.camera.pos = state.counter;
mcopy.state.camera.direction = state.frame.dir;
$('#intval').val(args.url);
$('#intval').addClass('active');
$('#camera_type_intval').prop('checked', 'checked');
gui.notify('DEVICES', `Connected to INTVAL3 @ ${args.url}`)
gui.updateState()
} else { } else {
$('#camera_type_arduino').prop('checked', 'checked'); $('#camera_type_arduino').prop('checked', 'checked');
$('#intval').removeClass('active');
} }
}; };

View File

@ -1,3 +1,6 @@
/* jshint esversion: 6, asi: true, strict: true*/
/* global require, setTimeout, process, console*/
'use strict' 'use strict'
const electron = require('electron') const electron = require('electron')
@ -11,13 +14,18 @@ const events = require('events')
const async = require('async') const async = require('async')
const path = require('path') const path = require('path')
const ee = new events.EventEmitter() const ee = new events.EventEmitter()
const capture = require('./lib/capture')(ee) //const capture = require('./lib/capture')(ee)
const settings = require('./lib/settings') const settings = require('./lib/settings')
const Server = require('./lib/server') const Server = require('./lib/server')
const Intval = require('./lib/intval') const Intval = require('./lib/intval')
const mcopy = {} const mcopy = {}
const log = {}
const proj = {}
const cam = {}
const light = {}
const dev = {}
let mainWindow let mainWindow
let mscript let mscript
@ -27,35 +35,32 @@ let projector
let camera let camera
let server let server
let menu let menu
let log = {}
//console.log(process.version) //console.log(process.version)
mcopy.cfg = require('./data/cfg.json') mcopy.cfg = require('./data/cfg.json')
mcopy.settings = {} mcopy.settings = {}
var enumerateDevices = function (err, devices) { dev.enumerate = function (err, devices) {
if (err) { if (err) {
log.info(err, 'SERIAL', false, true) log.info(err, 'SERIAL', false, true)
setTimeout(() =>{ setTimeout(() =>{
distinguishDevices([]) dev.all([])
}, 1000) }, 1000)
} else { } else {
log.info('Found ' + devices.length + ' USB devices', 'SERIAL', true, true) log.info(`Found ${devices.length} USB devices`, 'SERIAL', true, true)
console.dir(devices) devices = dev.favor(devices)
devices = favorDevices(devices) dev.all(devices)
console.dir(devices)
distinguishDevices(devices)
} }
} }
var favorDevices = function (devices) { dev.favor = function (devices) {
const past = mcopy.settings.devices.filter(dev => { const past = mcopy.settings.devices.filter(device => {
if (dev.arduino) { if (device.arduino) {
return dev return device
} }
}).map(dev => { }).map(device => {
return dev.arduino return device.arduino
}) })
if (past.length === 0) { if (past.length === 0) {
return devices return devices
@ -71,7 +76,7 @@ var favorDevices = function (devices) {
return devices return devices
} }
var distinguishDevice = function (device, callback) { dev.distinguish = function (device, callback) {
var connectCb = function (err, device) { var connectCb = function (err, device) {
if (err) { if (err) {
return console.error(err) return console.error(err)
@ -94,7 +99,7 @@ var distinguishDevice = function (device, callback) {
if (err) { if (err) {
return console.error(err) return console.error(err)
} }
rememberDevice(device, type) dev.remember('arduino', device, type)
log.info(`Determined ${device} to be ${type}`, 'SERIAL', true, true) log.info(`Determined ${device} to be ${type}`, 'SERIAL', true, true)
if (callback) { callback(err, type); } if (callback) { callback(err, type); }
@ -103,7 +108,7 @@ var distinguishDevice = function (device, callback) {
}; };
//Cases for 1 or 2 arduinos connected //Cases for 1 or 2 arduinos connected
var distinguishDevices = function (devices) { dev.all = function (devices) {
const connected = { const connected = {
projector : false, projector : false,
camera : false, camera : false,
@ -185,7 +190,7 @@ var distinguishDevices = function (devices) {
checklist = devices.map(device => { checklist = devices.map(device => {
return next => { return next => {
distinguishDevice(device, (err, type) => { dev.distinguish(device, (err, type) => {
if (err) { if (err) {
console.error(err) console.error(err)
return next() return next()
@ -197,42 +202,65 @@ var distinguishDevices = function (devices) {
async.series(checklist, () => { async.series(checklist, () => {
//done checking devices //done checking devices
let c = {}
let p = {}
let l = {}
if (!connected.projector) { if (!connected.projector) {
fakeProjector() fakeProjector()
} }
p.arduino = connected.projector
if (!connected.camera) { if (!connected.camera) {
fakeCamera() fakeCamera()
} }
c.arduino = connected.camera
if (mcopy.settings.camera.intval) {
c.intval = mcopy.settings.camera.intval
console.dir(mcopy.settings.camera)
setTimeout(() => {
cam.connectIntval(null, { connect : true, url : c.intval })
}, 1000)
}
if (!connected.light) { if (!connected.light) {
fakeLight() fakeLight()
} }
devicesReady(connected.projector, connected.camera, connected.light) l.arduino = connected.light
dev.ready(p, c, l)
}) })
}; };
var rememberDevice = function (device, type) { dev.remember = function (which, device, type) {
let deviceEntry let deviceEntry
let match = mcopy.settings.devices.filter(dev => { const match = mcopy.settings.devices.filter(dev => {
if (dev.arduino && dev.arduino === device) { if (dev[which] && dev[which] === device) {
return dev return dev
} }
}) })
if (match.length === 0) { if (match.length === 0) {
deviceEntry = { deviceEntry = {
arduino : device,
type : type type : type
} }
deviceEntry[which] = device
mcopy.settings.devices.push(deviceEntry) mcopy.settings.devices.push(deviceEntry)
settings.update('devices', mcopy.settings.devices) settings.update('devices', mcopy.settings.devices)
settings.save() settings.save()
} }
} };
var devicesReady = function (projector, camera, light) { dev.ready = function (projector, camera, light) {
mainWindow.webContents.send('ready', {camera: camera, projector: projector, light: light }) mainWindow.webContents.send('ready', {
settings.update('camera', { arduino : camera }) camera: camera,
settings.update('projector', { arduino : projector }) projector: projector,
settings.update('light', { arduino : light }) light: light,
profile: mcopy.settings.profile
})
settings.update('camera', camera)
settings.update('projector', projector)
settings.update('light', light)
settings.save() settings.save()
}; };
@ -250,38 +278,38 @@ var createWindow = function () {
minHeight : 600 minHeight : 600
}) })
mainWindow.loadURL('file://' + __dirname + '/index.html') mainWindow.loadURL('file://' + __dirname + '/index.html')
//mainWindow.webContents.openDevTools() if (process.argv.indexOf('-d') !== -1 || process.argv.indexOf('--dev') !== -1) {
mainWindow.webContents.openDevTools()
}
mainWindow.on('closed', () => { mainWindow.on('closed', () => {
mainWindow = null mainWindow = null
}) })
} }
var light = {}
light.init = function () { light.init = function () {
light.listen() light.listen()
}; }
light.listen = function () { light.listen = function () {
ipcMain.on('light', (event, arg) => { ipcMain.on('light', (event, arg) => {
light.set(arg.rgb, arg.id) light.set(arg.rgb, arg.id)
event.returnValue = true event.returnValue = true
}) })
}; }
light.set = function (rgb, id) { light.set = function (rgb, id) {
var str = rgb.join(','); var str = rgb.join(',');
arduino.send('light', mcopy.cfg.arduino.cmd.light, (ms) => { arduino.send('light', mcopy.cfg.arduino.cmd.light, (ms) => {
light.end(rgb, id, ms) light.end(rgb, id, ms)
}) })
arduino.string('light', str) arduino.string('light', str)
}; }
light.end = function (rgb, id, ms) { light.end = function (rgb, id, ms) {
log.info('Light set to ' + rgb.join(','), 'LIGHT', true, true) log.info('Light set to ' + rgb.join(','), 'LIGHT', true, true)
mainWindow.webContents.send('light', {rgb: rgb, id : id, ms: ms}) mainWindow.webContents.send('light', {rgb: rgb, id : id, ms: ms})
}; }
var proj = {}
proj.state = { proj.state = {
dir : true //default dir dir : true //default dir
}; }
proj.init = function () { proj.init = function () {
proj.listen() proj.listen()
} }
@ -331,9 +359,7 @@ proj.end = function (cmd, id, ms) {
mainWindow.webContents.send('proj', {cmd: cmd, id : id, ms: ms}) mainWindow.webContents.send('proj', {cmd: cmd, id : id, ms: ms})
} }
var cam = { cam.intval = null
intval : null
}
cam.state = { cam.state = {
dir : true //default dir dir : true //default dir
} }
@ -348,34 +374,61 @@ cam.set = function (dir, id) {
cmd = mcopy.cfg.arduino.cmd.cam_backward cmd = mcopy.cfg.arduino.cmd.cam_backward
} }
cam.state.dir = dir cam.state.dir = dir
arduino.send('camera', cmd, (ms) => {
if (cam.intval) {
cam.intval.setDir(dir, ms => {
cam.end(cmd, id, ms) cam.end(cmd, id, ms)
}) })
/* } else {
intval.setDir('camera', dir, (ms) => { arduino.send('camera', cmd, ms => {
cam.end(cmd, id, ms) cam.end(cmd, id, ms)
}) })
*/ }
} }
cam.move = function (frame, id) { cam.move = function (frame, id) {
let cmd = mcopy.cfg.arduino.cmd.camera let cmd = mcopy.cfg.arduino.cmd.camera
arduino.send('camera', cmd, (ms) => { if (cam.intval) {
cam.intval.move(ms => {
cam.end(cmd, id, ms) cam.end(cmd, id, ms)
}) })
/* } else {
intval.move('camera', (ms) => { arduino.send('camera', cmd, ms => {
cam.end(cmd, id, ms) cam.end(cmd, id, ms)
}) })
*/ }
} }
cam.exposure = function (exposure, id) { cam.exposure = function (exposure, id) {
intval.setDir('camera', exposure, (ms) => { let cmd = 'E'
cam.intval.setDir('camera', exposure, ms => {
cam.end(cmd, id, ms) cam.end(cmd, id, ms)
}) })
} }
cam.connectIntval = function (event, arg) {
if (arg.connect) {
cam.intval = new Intval(arg.url)
cam.intval.connect((err, ms, state) => {
if (err) {
mainWindow.webContents.send('intval', { connected : false })
log.info(`Cannot connect to ${arg.url}`, 'INTVAL', true, true)
cam.intval = null
delete cam.intval
} else {
mainWindow.webContents.send('intval', { connected : true, url : arg.url, state : state })
log.info(`Connected to INTVAL3 @ ${arg.url}`, 'INTVAL', true, true)
settings.update('camera', { intval : arg.url })
settings.save()
dev.remember('intval', arg.url, 'camera')
}
})
} else if (arg.disconnect) {
cam.intval = null
}
}
cam.listen = function () { cam.listen = function () {
ipcMain.on('cam', (event, arg) => { ipcMain.on('cam', (event, arg) => {
if (typeof arg.dir !== 'undefined') { if (typeof arg.dir !== 'undefined') {
@ -385,22 +438,7 @@ cam.listen = function () {
} }
event.returnValue = true event.returnValue = true
}) })
ipcMain.on('intval', (event, arg) => { ipcMain.on('intval', cam.connectIntval)
if (arg.connect) {
cam.intval = new Intval(arg.url)
cam.intval.connect((err, ms, state) => {
if (err) {
mainWindow.webContents.send('intval', { connected : false })
log.info(`Cannot connect to ${arg.url}`, 'INTVAL', true, true)
cam.intval = null
} else {
log.info()
}
})
} else if (arg.disconnect) {
cam.intval = null
}
})
} }
cam.end = function (cmd, id, ms) { cam.end = function (cmd, id, ms) {
var message = '' var message = ''
@ -464,7 +502,7 @@ log.info = function (action, service, status, display) {
log.display(obj) log.display(obj)
} }
} }
/*
var transfer = {} var transfer = {}
transfer.init = function () { transfer.init = function () {
@ -472,7 +510,7 @@ transfer.init = function () {
}; };
transfer.listen = function () { transfer.listen = function () {
ipcMain.on('transfer', (event, arg) => { ipcMain.on('transfer', (event, arg) => {
var res = ''; let res = '';
//also turn on and off //also turn on and off
if (arg.action === 'enable') { if (arg.action === 'enable') {
capture.active = true capture.active = true
@ -488,11 +526,11 @@ transfer.listen = function () {
event.returnValue = res event.returnValue = res
}) })
} }
*/
var init = function () { var init = function () {
createWindow() createWindow()
//createMenu() createMenu()
log.init() log.init()
light.init() light.init()
proj.init() proj.init()
@ -509,7 +547,7 @@ var init = function () {
mcopy.settings = settings.all() mcopy.settings = settings.all()
setTimeout( () => { setTimeout( () => {
arduino.enumerate(enumerateDevices) arduino.enumerate(dev.enumerate)
}, 1000) }, 1000)
} }
@ -526,3 +564,8 @@ app.on('activate', () => {
createWindow(); createWindow();
} }
}); });
mcopy.relaunch = function () {
app.relaunch({args: process.argv.slice(1).concat(['--relaunch'])})
app.exit(0)
}