Begin mcopy-cli for a simple command line interface

This commit is contained in:
mmcwilliams 2019-02-24 08:13:47 -05:00
parent 73ae599cff
commit 56a83615a9
9 changed files with 2377 additions and 0 deletions

View File

@ -0,0 +1,11 @@
{
"name": "mscript",
"version": "1.0.0",
"description": "<a name=\"module_lib/mscript\"></a>",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

1
cli/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

59
cli/index.js Normal file
View File

@ -0,0 +1,59 @@
'use strict'
const program = require('commander')
const uuid = require('uuid')
const moment = require('moment')
const delay = require('delay')
//const intval = require('intval')
const arduino = require('arduino')
const mscript = require('mscript')
const dev = require('device')
let log
let readline
const cfg = require('../app/data/cfg.json')
const pkg = require('./package.json')
async function command () {
return new Promise ((resolve, reject) => {
return readline.question(`Input:`, (str) => {
log.info(str)
//interpret string
readline.close()
return resolve(true)
})
})
}
async function live () {
readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
})
log.info('Starting live control mode')
while (true) {
try {
await command()
} catch (err) {
log.error('Error executing command')
}
}
}
async function main (arg) {
log = require('log')(arg)
log.info('mcopy-cli')
}
program
.version(pkg.version)
.option('-l, --live', 'Live control mode')
.option('-f, --frames', 'Number of frames to capture with camera')
.option('-p, --pattern', 'Pattern of sequence to be repeated')
.option('-m, --mscript', 'Execute an mscript file')
.option('-q, --quiet', 'Suppresses all log messages')
.parse(process.argv)
main(program)

296
cli/lib/device/index.js Normal file
View File

@ -0,0 +1,296 @@
'use strict'
const dev = {}
dev.init = function () {
dev.listen()
}
dev.listen = function () {
ipcMain.on('profile', (event, arg) => {
log.info(`Saving profile ${arg.profile}`, 'SETTINGS', false, false)
settings.update('profile', arg.profile)
settings.save()
})
}
dev.enumerate = async function () {
let devices
try{
devices = await arduino.enumerate()
} catch (err) {
log.info(err, 'SERIAL', false, true)
await delay(1000)
return dev.all([])
}
log.info(`Found ${devices.length} USB devices`, 'SERIAL', true, true)
devices = dev.favor(devices)
return await dev.all(devices)
}
dev.favor = function (devices) {
const past = mcopy.settings.devices.filter(device => {
if (device.arduino) {
return device
}
}).map(device => {
return device.arduino
})
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
}
dev.distinguish = async function (device) {
let connectSuccess
let verifySuccess
let type
try {
connectSuccess = await arduino.connect('connect', device, true)
} catch (err) {
console.error(err)
return null
}
await delay(2000)
try {
verifySuccess = await arduino.verify()
} catch (err) {
console.error(err)
return null
}
log.info(`Verified ${device} as mcopy device`, 'SERIAL', true, true)
await delay(1000)
try {
type = await arduino.distinguish()
} catch (err) {
console.error(err)
return null
}
dev.remember('arduino', device, type)
log.info(`Determined ${device} to be ${type}`, 'SERIAL', true, true)
return type
}
dev.fakeProjector = async function () {
dev.connected.projector = '/dev/fake'
try {
await arduino.fakeConnect('projector')
} catch (err) {
console.error(err)
log.error(`Error connecting to fake PRONECTOR device`, 'SERIAL', true, true)
return false
}
log.info('Connected to fake PROJECTOR device', 'SERIAL', true, true)
return true
}
dev.fakeCamera = async function () {
dev.connected.camera = '/dev/fake'
try {
await arduino.fakeConnect('camera')
} catch (err) {
console.error(err)
log.error(`Error connecting to fake CAMERA device`, 'SERIAL', true, true)
return false
}
log.info('Connected to fake CAMERA device', 'SERIAL', true, true)
return true
}
dev.fakeLight = async function () {
dev.connected.light = '/dev/fake'
try {
await arduino.fakeConnect('light')
} catch (err) {
console.error(err)
log.error(`Error connecting to fake LIGHT device`, 'SERIAL', true, true)
return false
}
log.info('Connected to fake LIGHT device', 'SERIAL', true, true)
return true
}
dev.connectDevice = async function (device, type) {
let closeSuccess
let connectSuccess
try {
closeSuccess = await arduino.close()
} catch (err) {
console.error(err)
return false
}
if (type === 'projector') {
dev.connected.projector = device
try {
connectSuccess = await arduino.connect('projector', device, false)
} catch (err) {
console.error(err)
return false
}
log.info(`Connected to ${device} as PROJECTOR`, 'SERIAL', true, true)
} else if (type === 'camera') {
dev.connected.camera = device
try {
connectSuccess = await arduino.connect('camera', device, false)
} catch (err) {
console.error(err)
return false
}
log.info(`Connected to ${device} as CAMERA`, 'SERIAL', true, true)
} else if (type === 'light') {
dev.connected.light = device
try {
connectSuccess = await arduino.connect('light', device, false)
} catch (err) {
console.error(err)
return false
}
log.info(`Connected to ${device} as LIGHT`, 'SERIAL', true, true)
} else if (type === 'projector,light') {
dev.connected.projector = device
dev.connected.light = device
arduino.alias('light', device)
try{
connectSuccess = await arduino.connect('projector', device, false)
} catch (err) {
console.error(err)
return false
}
log.info(`Connected to ${device} as PROJECTOR + LIGHT`, 'SERIAL', true, true)
} else if (type === 'projector,camera,light') {
dev.connected.projector = device
dev.connected.camera = device
dev.connected.light = device
arduino.alias('camera', device)
arduino.alias('light', device)
try {
connectSuccess = await arduino.connect('projector', device, false)
} catch (err) {
console.error(err)
return false
}
log.info(`Connected to ${device} as PROJECTOR + CAMERA + LIGHT`, 'SERIAL', true, true)
} else if (type === 'projector,camera') {
dev.connected.projector = device
dev.connected.camera = device
arduino.alias('camera', device)
try {
connectSuccess = await arduino.connect('projector', device, false)
} catch (err) {
console.error(err)
return false
}
log.info(`Connected to ${device} as PROJECTOR`, 'SERIAL', true, true)
}
return connectSuccess
}
//Cases for 1 or 2 arduinos connected
dev.all = async function (devices) {
let c = {}
let p = {}
let l = {}
let type
let d
dev.connected = {
projector : false,
camera : false,
light : false
}
let checklist = []
for (let device of devices) {
try {
type = await dev.distinguish(device)
} catch (err) {
console.error(err)
return reject(err)
}
try {
await dev.connectDevice(device, type)
} catch (err) {
console.error(err)
return reject(err)
}
}
//done checking devices
if (!dev.connected.projector) {
await dev.fakeProjector()
}
p.arduino = dev.connected.projector
if (!dev.connected.camera) {
await dev.fakeCamera()
}
c.arduino = dev.connected.camera
if (mcopy.settings.camera.intval) {
c.intval = mcopy.settings.camera.intval
await delay(1000)
await cam.connectIntval(null, { connect : true, url : c.intval })
}
if (!dev.connected.light) {
await dev.fakeLight()
}
l.arduino = dev.connected.light
return dev.ready(p, c, l)
}
dev.remember = function (which, device, type) {
let deviceEntry
const match = mcopy.settings.devices.filter(dev => {
if (dev[which] && dev[which] === device) {
return dev
}
})
if (match.length === 0) {
deviceEntry = {
type : type
}
deviceEntry[which] = device
mcopy.settings.devices.push(deviceEntry)
settings.update('devices', mcopy.settings.devices)
settings.save()
}
};
dev.ready = function (projector, camera, light) {
mainWindow.webContents.send('ready', {
camera,
projector,
light,
profile: mcopy.settings.profile
})
settings.update('camera', camera)
settings.update('projector', projector)
settings.update('light', light)
settings.save()
return true
};
module.exports = dev

View File

@ -0,0 +1,11 @@
{
"name": "device",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

45
cli/lib/log/index.js Normal file
View File

@ -0,0 +1,45 @@
'use strict'
const winston = require('winston')
const path = require('path')
const fs = require('fs-extra')
const os = require('os')
const log = {}
log.file = function () {
let logPath = path.join(os.homedir(), `/.config/mcopy-cli/`)
if (process.platform === 'darwin') {
logPath = path.join(os.homedir(), `/Library/Logs/mcopy-cli/`)
} else if (process.platform === 'win32') {
logPath = path.join(os.homedir(), `/AppData/Roaming/mcopy-cli/`)
}
if (!fs.existsSync(logPath)) {
fs.mkdirSync(logPath)
}
return path.join(logPath, 'mcopy-cli.log')
}
log.time = 'MM/DD/YY-HH:mm:ss'
module.exports = function (arg) {
if (arg.quiet) {
log.transport = {
info : function () {},
warn : function () {},
error : function () {}
}
} else {
log.transport = winston.createLogger({
transports: [
new (winston.transports.Console)({
format: winston.format.simple()
}),
new (winston.transports.File)({
filename: log.file()
})
]
})
}
return log.transport
}

11
cli/lib/log/package.json Normal file
View File

@ -0,0 +1,11 @@
{
"name": "log",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

1909
cli/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

34
cli/package.json Normal file
View File

@ -0,0 +1,34 @@
{
"name": "mcopy-cli",
"version": "0.0.1",
"description": "CLI for controlling mcopy optical printer tools",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/sixteenmillimeter/mcopy.git"
},
"author": "mmcwilliams",
"license": "MIT",
"bugs": {
"url": "https://github.com/sixteenmillimeter/mcopy/issues"
},
"homepage": "https://github.com/sixteenmillimeter/mcopy#readme",
"dependencies": {
"arduino": "file:../app/lib/arduino",
"commander": "^2.19.0",
"delay": "file:../app/lib/delay",
"device": "file:lib/device",
"fs-extra": "^7.0.1",
"humanize-duration": "^3.17.0",
"intval": "file:../app/lib/intval",
"log": "file:lib/log",
"moment": "^2.24.0",
"mscript": "file:../app/lib/mscript",
"serialport": "^7.0.2",
"uuid": "^3.3.2",
"winston": "^3.2.1"
}
}