2018-02-01 19:52:47 +00:00
|
|
|
/* jshint esversion: 6, asi: true, strict: true*/
|
|
|
|
/* global require, setTimeout, process, console*/
|
|
|
|
|
2017-12-20 03:54:39 +00:00
|
|
|
'use strict'
|
2017-02-09 14:35:07 +00:00
|
|
|
|
2017-12-20 03:54:39 +00:00
|
|
|
const electron = require('electron')
|
2017-12-20 06:20:37 +00:00
|
|
|
const { Menu, MenuItem, ipcMain, BrowserWindow, app } = electron
|
2017-12-20 03:54:39 +00:00
|
|
|
const fs = require('fs')
|
2018-01-06 22:53:05 +00:00
|
|
|
const os = require('os')
|
2017-12-20 03:54:39 +00:00
|
|
|
const winston = require('winston')
|
|
|
|
const moment = require('moment')
|
|
|
|
const uuid = require('uuid')
|
|
|
|
const events = require('events')
|
2018-01-02 06:16:33 +00:00
|
|
|
const async = require('async')
|
2018-01-05 04:42:36 +00:00
|
|
|
const path = require('path')
|
2017-12-20 03:54:39 +00:00
|
|
|
const ee = new events.EventEmitter()
|
2018-02-01 19:52:47 +00:00
|
|
|
//const capture = require('./lib/capture')(ee)
|
2018-01-05 04:42:36 +00:00
|
|
|
const settings = require('./lib/settings')
|
2018-01-02 06:16:33 +00:00
|
|
|
|
2018-01-06 22:53:05 +00:00
|
|
|
const Server = require('./lib/server')
|
|
|
|
const Intval = require('./lib/intval')
|
|
|
|
|
2017-12-20 03:54:39 +00:00
|
|
|
const mcopy = {}
|
2018-02-01 19:52:47 +00:00
|
|
|
const log = {}
|
|
|
|
const proj = {}
|
|
|
|
const cam = {}
|
|
|
|
const light = {}
|
|
|
|
const dev = {}
|
2016-04-11 06:01:26 +00:00
|
|
|
|
2017-12-20 03:54:39 +00:00
|
|
|
let mainWindow
|
|
|
|
let mscript
|
|
|
|
let arduino
|
2018-01-06 22:53:05 +00:00
|
|
|
let intval
|
2017-12-20 03:54:39 +00:00
|
|
|
let projector
|
|
|
|
let camera
|
2018-01-05 04:42:36 +00:00
|
|
|
let server
|
|
|
|
let menu
|
2017-02-09 14:35:07 +00:00
|
|
|
|
2017-12-20 06:20:37 +00:00
|
|
|
//console.log(process.version)
|
2017-12-20 03:54:39 +00:00
|
|
|
|
2018-01-05 04:42:36 +00:00
|
|
|
mcopy.cfg = require('./data/cfg.json')
|
2018-01-21 23:53:15 +00:00
|
|
|
mcopy.settings = {}
|
2016-04-15 22:16:14 +00:00
|
|
|
|
2018-02-01 19:52:47 +00:00
|
|
|
dev.enumerate = function (err, devices) {
|
2016-04-19 01:26:45 +00:00
|
|
|
if (err) {
|
2017-12-20 06:20:37 +00:00
|
|
|
log.info(err, 'SERIAL', false, true)
|
2018-01-02 14:46:05 +00:00
|
|
|
setTimeout(() =>{
|
2018-02-01 19:52:47 +00:00
|
|
|
dev.all([])
|
2018-01-02 14:46:05 +00:00
|
|
|
}, 1000)
|
2016-04-19 01:26:45 +00:00
|
|
|
} else {
|
2018-02-01 19:52:47 +00:00
|
|
|
log.info(`Found ${devices.length} USB devices`, 'SERIAL', true, true)
|
|
|
|
devices = dev.favor(devices)
|
|
|
|
dev.all(devices)
|
2016-04-19 01:26:45 +00:00
|
|
|
}
|
2017-12-20 06:20:37 +00:00
|
|
|
}
|
2016-04-19 01:26:45 +00:00
|
|
|
|
2018-02-01 19:52:47 +00:00
|
|
|
dev.favor = function (devices) {
|
|
|
|
const past = mcopy.settings.devices.filter(device => {
|
|
|
|
if (device.arduino) {
|
|
|
|
return device
|
2018-02-01 02:46:03 +00:00
|
|
|
}
|
2018-02-01 19:52:47 +00:00
|
|
|
}).map(device => {
|
|
|
|
return device.arduino
|
2018-02-01 02:46:03 +00:00
|
|
|
})
|
|
|
|
if (past.length === 0) {
|
|
|
|
return devices
|
|
|
|
}
|
|
|
|
devices.sort((a, b) => {
|
|
|
|
if (past.indexOf(a) !== -1 && past.indexOf(b) === -1) {
|
|
|
|
return 1
|
|
|
|
} else if (past.indexOf(a) === -1 && past.indexOf(b) !== -1) {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
})
|
|
|
|
return devices
|
|
|
|
}
|
|
|
|
|
2018-02-01 19:52:47 +00:00
|
|
|
dev.distinguish = function (device, callback) {
|
2016-04-19 01:26:45 +00:00
|
|
|
var connectCb = function (err, device) {
|
|
|
|
if (err) {
|
2017-12-20 06:20:37 +00:00
|
|
|
return console.error(err)
|
2016-04-19 01:26:45 +00:00
|
|
|
}
|
|
|
|
setTimeout(function () {
|
2017-12-20 06:20:37 +00:00
|
|
|
arduino.verify(verifyCb)
|
2016-04-19 01:26:45 +00:00
|
|
|
}, 2000);
|
|
|
|
},
|
|
|
|
verifyCb = function (err, success) {
|
|
|
|
if (err) {
|
2017-12-20 06:20:37 +00:00
|
|
|
return console.error(err)
|
2016-04-19 01:26:45 +00:00
|
|
|
}
|
2018-01-02 06:16:33 +00:00
|
|
|
log.info(`Verified ${device} as mcopy device`, 'SERIAL', true, true)
|
2018-01-22 20:04:08 +00:00
|
|
|
|
2016-04-19 01:26:45 +00:00
|
|
|
setTimeout(function () {
|
|
|
|
arduino.distinguish(distinguishCb);
|
|
|
|
}, 1000);
|
|
|
|
},
|
|
|
|
distinguishCb = function (err, type) {
|
|
|
|
if (err) {
|
2017-12-20 06:20:37 +00:00
|
|
|
return console.error(err)
|
2016-04-19 01:26:45 +00:00
|
|
|
}
|
2018-02-01 19:52:47 +00:00
|
|
|
dev.remember('arduino', device, type)
|
2018-01-22 20:04:08 +00:00
|
|
|
|
2018-01-02 06:16:33 +00:00
|
|
|
log.info(`Determined ${device} to be ${type}`, 'SERIAL', true, true)
|
2016-04-19 01:26:45 +00:00
|
|
|
if (callback) { callback(err, type); }
|
|
|
|
}
|
2017-12-20 06:20:37 +00:00
|
|
|
arduino.connect('connect', device, true, connectCb)
|
2016-04-19 01:26:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//Cases for 1 or 2 arduinos connected
|
2018-02-01 19:52:47 +00:00
|
|
|
dev.all = function (devices) {
|
2018-01-05 04:42:36 +00:00
|
|
|
const connected = {
|
2018-01-02 06:16:33 +00:00
|
|
|
projector : false,
|
|
|
|
camera : false,
|
|
|
|
light : false
|
|
|
|
}
|
2018-01-05 04:42:36 +00:00
|
|
|
|
|
|
|
let checklist = []
|
2018-01-02 06:16:33 +00:00
|
|
|
var fakeProjector = function () {
|
|
|
|
connected.projector = '/dev/fake'
|
|
|
|
arduino.fakeConnect('projector', () => {
|
|
|
|
log.info('Connected to fake PROJECTOR device', 'SERIAL', true, true)
|
|
|
|
|
2017-12-20 06:20:37 +00:00
|
|
|
})
|
2018-01-02 06:16:33 +00:00
|
|
|
}
|
|
|
|
var fakeCamera = function () {
|
|
|
|
connected.camera = '/dev/fake'
|
|
|
|
arduino.fakeConnect('camera', () => {
|
|
|
|
log.info('Connected to fake CAMERA device', 'SERIAL', true, true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
var fakeLight = function () {
|
|
|
|
connected.light = '/dev/fake'
|
|
|
|
arduino.fakeConnect('light', () => {
|
|
|
|
log.info('Connected to fake LIGHT device', 'SERIAL', true, true)
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
var distinguishCb = function (device, type, cb) {
|
2017-12-20 06:20:37 +00:00
|
|
|
arduino.close(() => {
|
2016-04-19 01:52:38 +00:00
|
|
|
if (type === 'projector') {
|
2018-01-02 06:16:33 +00:00
|
|
|
connected.projector = device
|
|
|
|
arduino.connect('projector', device, false, () => {
|
|
|
|
log.info(`Connected to ${device} as PROJECTOR`, 'SERIAL', true, true)
|
|
|
|
cb()
|
|
|
|
})
|
2016-04-19 01:52:38 +00:00
|
|
|
} else if (type === 'camera') {
|
2018-01-02 06:16:33 +00:00
|
|
|
connected.camera = device
|
|
|
|
arduino.connect('camera', device, false, () => {
|
|
|
|
log.info(`Connected to ${device} as CAMERA`, 'SERIAL', true, true)
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
} else if (type === 'light') {
|
|
|
|
connected.light = device
|
|
|
|
arduino.connect('light', device, false, () => {
|
|
|
|
log.info(`Connected to ${device} as LIGHT`, 'SERIAL', true, true)
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
} else if (type === 'projector,light') {
|
|
|
|
connected.projector = device
|
|
|
|
connected.light = device
|
|
|
|
arduino.connect('projector', device, false, () => {
|
|
|
|
log.info(`Connected to ${device} as PROJECTOR + LIGHT`, 'SERIAL', true, true)
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
arduino.alias('light', device)
|
|
|
|
} else if (type === 'projector,camera,light') {
|
|
|
|
connected.projector = device
|
|
|
|
connected.camera = device
|
|
|
|
connected.light = device
|
|
|
|
arduino.connect('projector', device, false, () => {
|
|
|
|
log.info(`Connected to ${device} as PROJECTOR + CAMERA + LIGHT`, 'SERIAL', true, true)
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
arduino.alias('camera', device)
|
|
|
|
arduino.alias('light', device)
|
|
|
|
} else if (type === 'projector,camera') {
|
|
|
|
connected.projector = device
|
|
|
|
connected.camera = device
|
|
|
|
arduino.connect('projector', device, false, () => {
|
|
|
|
log.info(`Connected to ${device} as PROJECTOR`, 'SERIAL', true, true)
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
arduino.alias('camera', device)
|
|
|
|
} else {
|
|
|
|
cb()
|
2016-04-19 01:52:38 +00:00
|
|
|
}
|
2018-01-02 06:16:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
checklist = devices.map(device => {
|
|
|
|
return next => {
|
2018-02-01 19:52:47 +00:00
|
|
|
dev.distinguish(device, (err, type) => {
|
2018-01-02 06:16:33 +00:00
|
|
|
if (err) {
|
|
|
|
console.error(err)
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
distinguishCb(device, type, next)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2018-01-31 15:42:11 +00:00
|
|
|
|
2018-01-02 06:16:33 +00:00
|
|
|
async.series(checklist, () => {
|
|
|
|
//done checking devices
|
2018-02-01 19:52:47 +00:00
|
|
|
|
|
|
|
let c = {}
|
|
|
|
let p = {}
|
|
|
|
let l = {}
|
|
|
|
|
2018-01-02 06:16:33 +00:00
|
|
|
if (!connected.projector) {
|
|
|
|
fakeProjector()
|
|
|
|
}
|
2018-02-01 19:52:47 +00:00
|
|
|
p.arduino = connected.projector
|
2018-01-02 06:16:33 +00:00
|
|
|
if (!connected.camera) {
|
|
|
|
fakeCamera()
|
|
|
|
}
|
2018-02-01 19:52:47 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-01-02 06:16:33 +00:00
|
|
|
if (!connected.light) {
|
|
|
|
fakeLight()
|
|
|
|
}
|
2018-02-01 19:52:47 +00:00
|
|
|
l.arduino = connected.light
|
|
|
|
|
|
|
|
dev.ready(p, c, l)
|
2018-01-02 06:16:33 +00:00
|
|
|
})
|
2016-04-11 14:49:57 +00:00
|
|
|
};
|
2016-04-11 06:01:26 +00:00
|
|
|
|
2018-02-01 19:52:47 +00:00
|
|
|
dev.remember = function (which, device, type) {
|
2018-01-22 20:04:08 +00:00
|
|
|
let deviceEntry
|
2018-02-01 19:52:47 +00:00
|
|
|
const match = mcopy.settings.devices.filter(dev => {
|
|
|
|
if (dev[which] && dev[which] === device) {
|
2018-01-22 20:04:08 +00:00
|
|
|
return dev
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (match.length === 0) {
|
|
|
|
deviceEntry = {
|
|
|
|
type : type
|
|
|
|
}
|
2018-02-01 19:52:47 +00:00
|
|
|
deviceEntry[which] = device
|
2018-01-22 20:04:08 +00:00
|
|
|
mcopy.settings.devices.push(deviceEntry)
|
|
|
|
settings.update('devices', mcopy.settings.devices)
|
|
|
|
settings.save()
|
|
|
|
}
|
2018-02-01 19:52:47 +00:00
|
|
|
};
|
2018-01-22 20:04:08 +00:00
|
|
|
|
2018-02-01 19:52:47 +00:00
|
|
|
dev.ready = function (projector, camera, light) {
|
|
|
|
mainWindow.webContents.send('ready', {
|
|
|
|
camera: camera,
|
|
|
|
projector: projector,
|
|
|
|
light: light,
|
|
|
|
profile: mcopy.settings.profile
|
|
|
|
})
|
|
|
|
settings.update('camera', camera)
|
|
|
|
settings.update('projector', projector)
|
|
|
|
settings.update('light', light)
|
2018-01-05 04:42:36 +00:00
|
|
|
settings.save()
|
2016-04-19 03:44:51 +00:00
|
|
|
};
|
|
|
|
|
2016-04-11 14:49:57 +00:00
|
|
|
var createMenu = function () {
|
2018-01-05 04:42:36 +00:00
|
|
|
const template = require('./data/menu.json')
|
2017-12-20 06:20:37 +00:00
|
|
|
menu = Menu.buildFromTemplate(template)
|
|
|
|
Menu.setApplicationMenu(menu)
|
|
|
|
}
|
2016-04-11 06:01:26 +00:00
|
|
|
|
2016-04-11 14:49:57 +00:00
|
|
|
var createWindow = function () {
|
2016-04-17 21:55:23 +00:00
|
|
|
mainWindow = new BrowserWindow({
|
|
|
|
width: 800,
|
|
|
|
height: 600,
|
|
|
|
minWidth : 800,
|
|
|
|
minHeight : 600
|
2017-12-20 06:20:37 +00:00
|
|
|
})
|
|
|
|
mainWindow.loadURL('file://' + __dirname + '/index.html')
|
2018-02-01 19:52:47 +00:00
|
|
|
if (process.argv.indexOf('-d') !== -1 || process.argv.indexOf('--dev') !== -1) {
|
|
|
|
mainWindow.webContents.openDevTools()
|
|
|
|
}
|
2017-12-20 06:20:37 +00:00
|
|
|
mainWindow.on('closed', () => {
|
|
|
|
mainWindow = null
|
2018-01-05 04:42:36 +00:00
|
|
|
})
|
2016-04-11 06:01:26 +00:00
|
|
|
}
|
|
|
|
|
2016-04-17 21:55:23 +00:00
|
|
|
light.init = function () {
|
2017-12-20 06:20:37 +00:00
|
|
|
light.listen()
|
2018-02-01 19:52:47 +00:00
|
|
|
}
|
2016-04-17 21:55:23 +00:00
|
|
|
light.listen = function () {
|
2017-12-20 06:20:37 +00:00
|
|
|
ipcMain.on('light', (event, arg) => {
|
|
|
|
light.set(arg.rgb, arg.id)
|
|
|
|
event.returnValue = true
|
|
|
|
})
|
2018-02-01 19:52:47 +00:00
|
|
|
}
|
2016-04-17 21:55:23 +00:00
|
|
|
light.set = function (rgb, id) {
|
|
|
|
var str = rgb.join(',');
|
2018-01-02 06:16:33 +00:00
|
|
|
arduino.send('light', mcopy.cfg.arduino.cmd.light, (ms) => {
|
2017-12-20 06:20:37 +00:00
|
|
|
light.end(rgb, id, ms)
|
|
|
|
})
|
2018-01-02 06:16:33 +00:00
|
|
|
arduino.string('light', str)
|
2018-02-01 19:52:47 +00:00
|
|
|
}
|
2016-04-18 04:19:09 +00:00
|
|
|
light.end = function (rgb, id, ms) {
|
2017-12-20 06:20:37 +00:00
|
|
|
log.info('Light set to ' + rgb.join(','), 'LIGHT', true, true)
|
|
|
|
mainWindow.webContents.send('light', {rgb: rgb, id : id, ms: ms})
|
2018-02-01 19:52:47 +00:00
|
|
|
}
|
2016-04-13 04:06:19 +00:00
|
|
|
|
2016-04-18 02:22:18 +00:00
|
|
|
proj.state = {
|
|
|
|
dir : true //default dir
|
2018-02-01 19:52:47 +00:00
|
|
|
}
|
2016-04-18 02:22:18 +00:00
|
|
|
proj.init = function () {
|
2017-12-20 06:20:37 +00:00
|
|
|
proj.listen()
|
|
|
|
}
|
2016-04-18 02:22:18 +00:00
|
|
|
proj.set = function (dir, id) {
|
2017-12-20 06:20:37 +00:00
|
|
|
var cmd
|
2016-04-18 02:22:18 +00:00
|
|
|
if (dir) {
|
2017-12-20 06:20:37 +00:00
|
|
|
cmd = mcopy.cfg.arduino.cmd.proj_forward
|
2016-04-18 02:22:18 +00:00
|
|
|
} else {
|
2017-12-20 06:20:37 +00:00
|
|
|
cmd = mcopy.cfg.arduino.cmd.proj_backward
|
2016-04-18 02:22:18 +00:00
|
|
|
}
|
2017-12-20 06:20:37 +00:00
|
|
|
proj.state.dir = dir
|
|
|
|
arduino.send('projector', cmd, (ms) => {
|
|
|
|
proj.end(cmd, id, ms)
|
|
|
|
})
|
|
|
|
}
|
2016-04-18 02:22:18 +00:00
|
|
|
proj.move = function (frame, id) {
|
2017-12-20 06:20:37 +00:00
|
|
|
arduino.send('projector', mcopy.cfg.arduino.cmd.projector, (ms) => {
|
|
|
|
proj.end(mcopy.cfg.arduino.cmd.projector, id, ms)
|
|
|
|
})
|
|
|
|
}
|
2016-04-18 02:22:18 +00:00
|
|
|
proj.listen = function () {
|
2017-12-20 06:20:37 +00:00
|
|
|
ipcMain.on('proj', (event, arg) => {
|
2016-04-18 02:22:18 +00:00
|
|
|
if (typeof arg.dir !== 'undefined') {
|
2017-12-20 06:20:37 +00:00
|
|
|
proj.set(arg.dir, arg.id)
|
2016-04-18 02:22:18 +00:00
|
|
|
} else if (typeof arg.frame !== 'undefined') {
|
2017-12-20 06:20:37 +00:00
|
|
|
proj.move(arg.frame, arg.id)
|
2016-04-18 02:22:18 +00:00
|
|
|
}
|
2017-12-20 06:20:37 +00:00
|
|
|
event.returnValue = true
|
|
|
|
})
|
|
|
|
}
|
2016-04-18 03:02:22 +00:00
|
|
|
proj.end = function (cmd, id, ms) {
|
2017-12-20 06:20:37 +00:00
|
|
|
var message = ''
|
2016-04-18 02:22:18 +00:00
|
|
|
if (cmd === mcopy.cfg.arduino.cmd.proj_forward) {
|
2017-12-20 06:20:37 +00:00
|
|
|
message = 'Projector set to FORWARD'
|
2016-04-18 02:22:18 +00:00
|
|
|
} else if (cmd === mcopy.cfg.arduino.cmd.proj_backward) {
|
2017-12-20 06:20:37 +00:00
|
|
|
message = 'Projector set to BACKWARD'
|
2016-04-18 02:22:18 +00:00
|
|
|
} else if (cmd === mcopy.cfg.arduino.cmd.projector) {
|
2017-12-20 06:20:37 +00:00
|
|
|
message = 'Projector '
|
2016-04-18 02:22:18 +00:00
|
|
|
if (proj.state.dir) {
|
2017-12-20 06:20:37 +00:00
|
|
|
message += 'ADVANCED'
|
2016-04-18 02:22:18 +00:00
|
|
|
} else {
|
|
|
|
message += 'REWOUND'
|
|
|
|
}
|
2017-12-20 06:20:37 +00:00
|
|
|
message += ' 1 frame'
|
2016-04-18 02:22:18 +00:00
|
|
|
}
|
2017-12-20 06:20:37 +00:00
|
|
|
log.info(message, 'PROJECTOR', true, true)
|
|
|
|
mainWindow.webContents.send('proj', {cmd: cmd, id : id, ms: ms})
|
|
|
|
}
|
2016-04-18 03:02:22 +00:00
|
|
|
|
2018-02-01 19:52:47 +00:00
|
|
|
cam.intval = null
|
2016-04-18 03:02:22 +00:00
|
|
|
cam.state = {
|
|
|
|
dir : true //default dir
|
2017-12-20 06:20:37 +00:00
|
|
|
}
|
2016-04-18 03:02:22 +00:00
|
|
|
cam.init = function () {
|
2017-12-20 06:20:37 +00:00
|
|
|
cam.listen()
|
|
|
|
}
|
2016-04-18 03:02:22 +00:00
|
|
|
cam.set = function (dir, id) {
|
2018-01-06 22:53:05 +00:00
|
|
|
let cmd
|
2016-04-18 03:02:22 +00:00
|
|
|
if (dir) {
|
2017-12-20 06:20:37 +00:00
|
|
|
cmd = mcopy.cfg.arduino.cmd.cam_forward
|
2016-04-18 03:02:22 +00:00
|
|
|
} else {
|
2017-12-20 06:20:37 +00:00
|
|
|
cmd = mcopy.cfg.arduino.cmd.cam_backward
|
2016-04-18 03:02:22 +00:00
|
|
|
}
|
2017-12-20 06:20:37 +00:00
|
|
|
cam.state.dir = dir
|
2018-02-01 19:52:47 +00:00
|
|
|
|
|
|
|
if (cam.intval) {
|
|
|
|
cam.intval.setDir(dir, ms => {
|
|
|
|
cam.end(cmd, id, ms)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
arduino.send('camera', cmd, ms => {
|
|
|
|
cam.end(cmd, id, ms)
|
|
|
|
})
|
|
|
|
}
|
2017-12-20 06:20:37 +00:00
|
|
|
}
|
2018-01-05 04:42:36 +00:00
|
|
|
|
2016-04-18 03:02:22 +00:00
|
|
|
cam.move = function (frame, id) {
|
2018-01-06 22:53:05 +00:00
|
|
|
let cmd = mcopy.cfg.arduino.cmd.camera
|
2018-02-01 19:52:47 +00:00
|
|
|
if (cam.intval) {
|
|
|
|
cam.intval.move(ms => {
|
|
|
|
cam.end(cmd, id, ms)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
arduino.send('camera', cmd, ms => {
|
|
|
|
cam.end(cmd, id, ms)
|
|
|
|
})
|
|
|
|
}
|
2018-01-17 17:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cam.exposure = function (exposure, id) {
|
2018-02-01 19:52:47 +00:00
|
|
|
let cmd = 'E'
|
|
|
|
cam.intval.setDir('camera', exposure, ms => {
|
2018-01-06 22:53:05 +00:00
|
|
|
cam.end(cmd, id, ms)
|
2017-12-20 06:20:37 +00:00
|
|
|
})
|
|
|
|
}
|
2018-01-05 04:42:36 +00:00
|
|
|
|
2018-02-01 19:52:47 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 03:02:22 +00:00
|
|
|
cam.listen = function () {
|
2017-12-20 06:20:37 +00:00
|
|
|
ipcMain.on('cam', (event, arg) => {
|
2016-04-18 03:02:22 +00:00
|
|
|
if (typeof arg.dir !== 'undefined') {
|
2017-12-20 06:20:37 +00:00
|
|
|
cam.set(arg.dir, arg.id)
|
2016-04-18 03:02:22 +00:00
|
|
|
} else if (typeof arg.frame !== 'undefined') {
|
2017-12-20 06:20:37 +00:00
|
|
|
cam.move(arg.frame, arg.id)
|
2016-04-18 03:02:22 +00:00
|
|
|
}
|
2017-12-20 06:20:37 +00:00
|
|
|
event.returnValue = true
|
2018-01-31 15:42:11 +00:00
|
|
|
})
|
2018-02-01 19:52:47 +00:00
|
|
|
ipcMain.on('intval', cam.connectIntval)
|
2018-01-31 15:42:11 +00:00
|
|
|
}
|
2016-04-18 03:02:22 +00:00
|
|
|
cam.end = function (cmd, id, ms) {
|
2017-12-20 06:20:37 +00:00
|
|
|
var message = ''
|
2016-04-18 03:02:22 +00:00
|
|
|
if (cmd === mcopy.cfg.arduino.cmd.cam_forward) {
|
2017-12-20 06:20:37 +00:00
|
|
|
message = 'Camera set to FORWARD'
|
2016-04-18 03:02:22 +00:00
|
|
|
} else if (cmd === mcopy.cfg.arduino.cmd.cam_backward) {
|
2017-12-20 06:20:37 +00:00
|
|
|
message = 'Camera set to BACKWARD'
|
2016-04-18 03:02:22 +00:00
|
|
|
} else if (cmd === mcopy.cfg.arduino.cmd.camera) {
|
2017-12-20 06:20:37 +00:00
|
|
|
message = 'Camera '
|
2016-04-18 03:02:22 +00:00
|
|
|
if (cam.state.dir) {
|
2017-12-20 06:20:37 +00:00
|
|
|
message += 'ADVANCED'
|
2016-04-18 03:02:22 +00:00
|
|
|
} else {
|
|
|
|
message += 'REWOUND'
|
|
|
|
}
|
2017-12-20 06:20:37 +00:00
|
|
|
message += ' 1 frame'
|
2016-04-18 03:02:22 +00:00
|
|
|
}
|
2017-12-20 06:20:37 +00:00
|
|
|
log.info(message, 'CAMERA', true, true)
|
|
|
|
mainWindow.webContents.send('cam', {cmd: cmd, id : id, ms: ms})
|
2016-04-18 02:22:18 +00:00
|
|
|
};
|
|
|
|
|
2018-01-05 04:42:36 +00:00
|
|
|
log.file = function () {
|
2018-01-06 22:53:05 +00:00
|
|
|
let logPath = path.join(os.homedir(), `/.config/mcopy/`)
|
2018-01-05 14:17:41 +00:00
|
|
|
if (process.platform === 'darwin') {
|
2018-01-06 22:53:05 +00:00
|
|
|
logPath = path.join(os.homedir(), `/Library/Logs/mcopy/`)
|
2018-01-05 14:17:41 +00:00
|
|
|
} else if (process.platform === 'win32') {
|
2018-01-06 22:53:05 +00:00
|
|
|
logPath = path.join(os.homedir(), `/AppData/Roaming/mcopy/`)
|
|
|
|
}
|
|
|
|
if (!fs.existsSync(logPath)) {
|
|
|
|
fs.mkdirSync(logPath)
|
2018-01-05 04:42:36 +00:00
|
|
|
}
|
|
|
|
return path.join(logPath, 'mcopy.log')
|
|
|
|
}
|
2017-12-20 06:20:37 +00:00
|
|
|
log.time = 'MM/DD/YY-HH:mm:ss'
|
2016-04-13 03:08:56 +00:00
|
|
|
log.transport = new (winston.Logger)({
|
2016-04-13 02:55:51 +00:00
|
|
|
transports: [
|
|
|
|
new (winston.transports.Console)(),
|
2018-01-06 22:53:05 +00:00
|
|
|
new (winston.transports.File)({ filename: log.file() })
|
2016-04-13 02:55:51 +00:00
|
|
|
]
|
2017-12-20 06:20:37 +00:00
|
|
|
})
|
2016-04-13 02:51:47 +00:00
|
|
|
log.init = function () {
|
2017-12-20 06:20:37 +00:00
|
|
|
log.listen()
|
2018-01-05 04:42:36 +00:00
|
|
|
}
|
2016-04-13 03:08:56 +00:00
|
|
|
log.display = function (obj) {
|
2017-12-20 06:20:37 +00:00
|
|
|
mainWindow.webContents.send('log', obj)
|
2018-01-05 04:42:36 +00:00
|
|
|
}
|
2016-04-13 02:51:47 +00:00
|
|
|
log.listen = function () {
|
2017-12-20 06:20:37 +00:00
|
|
|
ipcMain.on('log', (event, arg) => {
|
|
|
|
log.transport.info('renderer', arg)
|
|
|
|
event.returnValue = true
|
|
|
|
})
|
2018-01-05 04:42:36 +00:00
|
|
|
}
|
2016-04-13 03:08:56 +00:00
|
|
|
log.info = function (action, service, status, display) {
|
|
|
|
var obj = {
|
|
|
|
time : moment().format(log.time),
|
|
|
|
action : action,
|
|
|
|
service : service,
|
|
|
|
status : status
|
2017-12-20 06:20:37 +00:00
|
|
|
}
|
|
|
|
log.transport.info('main', obj)
|
2016-04-13 03:08:56 +00:00
|
|
|
if (display) {
|
2017-12-20 06:20:37 +00:00
|
|
|
log.display(obj)
|
2016-04-13 03:08:56 +00:00
|
|
|
}
|
2018-01-05 04:42:36 +00:00
|
|
|
}
|
2018-02-01 19:52:47 +00:00
|
|
|
/*
|
2017-12-20 06:20:37 +00:00
|
|
|
var transfer = {}
|
2016-06-21 19:25:46 +00:00
|
|
|
|
|
|
|
transfer.init = function () {
|
2017-12-20 06:20:37 +00:00
|
|
|
transfer.listen()
|
2016-06-21 19:25:46 +00:00
|
|
|
};
|
|
|
|
transfer.listen = function () {
|
2017-12-20 06:20:37 +00:00
|
|
|
ipcMain.on('transfer', (event, arg) => {
|
2018-02-01 19:52:47 +00:00
|
|
|
let res = '';
|
2016-06-21 19:53:07 +00:00
|
|
|
//also turn on and off
|
2016-06-29 16:01:17 +00:00
|
|
|
if (arg.action === 'enable') {
|
2017-12-20 06:20:37 +00:00
|
|
|
capture.active = true
|
|
|
|
res = capture.active
|
2016-06-29 16:01:17 +00:00
|
|
|
} else if (arg.action === 'disable') {
|
2017-12-20 06:20:37 +00:00
|
|
|
capture.active = false
|
|
|
|
res = capture.active
|
2016-06-29 16:01:17 +00:00
|
|
|
} else if (arg.action === 'start') {
|
2017-12-20 06:20:37 +00:00
|
|
|
capture.start()
|
2016-06-21 19:53:07 +00:00
|
|
|
} else if (arg.action === 'end') {
|
2017-12-20 06:20:37 +00:00
|
|
|
res = capture.end()
|
2016-06-21 19:53:07 +00:00
|
|
|
}
|
2017-12-20 06:20:37 +00:00
|
|
|
event.returnValue = res
|
|
|
|
})
|
|
|
|
}
|
2018-02-01 19:52:47 +00:00
|
|
|
*/
|
2016-06-21 15:05:44 +00:00
|
|
|
var init = function () {
|
2018-01-05 04:42:36 +00:00
|
|
|
|
2017-12-20 06:20:37 +00:00
|
|
|
createWindow()
|
2018-02-01 19:52:47 +00:00
|
|
|
createMenu()
|
2017-12-20 06:20:37 +00:00
|
|
|
log.init()
|
|
|
|
light.init()
|
|
|
|
proj.init()
|
|
|
|
cam.init()
|
2016-06-21 15:05:44 +00:00
|
|
|
|
2018-01-05 04:42:36 +00:00
|
|
|
//transfer.init()
|
|
|
|
//capture.init()
|
2016-06-21 15:05:44 +00:00
|
|
|
|
2018-01-05 20:48:10 +00:00
|
|
|
arduino = require('./lib/arduino')(mcopy.cfg, ee)
|
2018-01-05 19:22:40 +00:00
|
|
|
mscript = require('./lib/mscript')
|
2018-01-21 23:53:15 +00:00
|
|
|
|
2016-06-21 15:05:44 +00:00
|
|
|
|
2018-01-21 23:53:15 +00:00
|
|
|
settings.restore()
|
|
|
|
mcopy.settings = settings.all()
|
2018-01-05 04:42:36 +00:00
|
|
|
|
2017-12-20 06:20:37 +00:00
|
|
|
setTimeout( () => {
|
2018-02-01 19:52:47 +00:00
|
|
|
arduino.enumerate(dev.enumerate)
|
2017-12-20 06:20:37 +00:00
|
|
|
}, 1000)
|
|
|
|
}
|
2016-06-21 15:05:44 +00:00
|
|
|
|
2017-12-20 06:20:37 +00:00
|
|
|
app.on('ready', init)
|
2016-06-21 15:05:44 +00:00
|
|
|
|
2017-12-20 06:20:37 +00:00
|
|
|
app.on('window-all-closed', () => {
|
2016-06-21 15:05:44 +00:00
|
|
|
//if (process.platform !== 'darwin') {
|
|
|
|
app.quit();
|
|
|
|
//}
|
|
|
|
});
|
|
|
|
|
2017-12-20 06:20:37 +00:00
|
|
|
app.on('activate', () => {
|
2016-06-21 15:05:44 +00:00
|
|
|
if (mainWindow === null) {
|
|
|
|
createWindow();
|
|
|
|
}
|
|
|
|
});
|
2018-02-01 19:52:47 +00:00
|
|
|
|
|
|
|
mcopy.relaunch = function () {
|
|
|
|
app.relaunch({args: process.argv.slice(1).concat(['--relaunch'])})
|
|
|
|
app.exit(0)
|
|
|
|
}
|