improved opts processing

master
David Kebler 2018-02-14 19:03:20 -08:00
parent b6ef63e6bb
commit 8fb4dbd788
6 changed files with 42 additions and 25 deletions

View File

@ -1,11 +1,17 @@
module.exports = {
"ecmaFeatures": {
"modules": true,
"spread" : true,
"restParams" : true
},
"env": {
"es6": true,
"node": true,
"mocha": true
},
"parserOptions": {
"ecmaVersion": 2017
"ecmaVersion": 2017,
"sourceType": "module"
},
"extends": "eslint:recommended",
"rules": {

24
examples/device.mjs Normal file
View File

@ -0,0 +1,24 @@
/*
* i2c bus with both unix and tcp socket using defaults. For TCP that is host OS name and port 8080
*
*/
import Device from '../src/device-packet'
// const PATH = ''
;
(async () => {
let device = new Device({id:'an i2c device', address:0x27, bus:{host:'sbc'} })
device.reply = function (packet) {
console.log('for request ',packet._header)
console.log('bus response is ',packet.response)
}
console.log((await device.init()).scan)
process.kill(process.pid, 'SIGTERM')
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
process.kill(process.pid, 'SIGTERM')
})

View File

@ -1,8 +0,0 @@
let opts = {
dirname: __dirname + '/src',
// http://stackoverflow.com/questions/2078915/a-regular-expression-to-exclude-a-word-string
filter: /^(?!index)([^\.].*)\.js?$/,
recursive: false,
merge: true // remove or comment to have each file in /lib be a prop/key in library...see node-require-all
}
module.exports = require('@uci/require-all')(opts)

View File

@ -1,8 +1,8 @@
{
"name": "@uci/i2c",
"name": "@uci/i2c-device",
"version": "0.1.1",
"description": "Bus and Device Classes for I2C Interfacing",
"main": "index.js",
"description": "Device Classes for I2C Interfacing",
"main": "src/",
"scripts": {
"testw": "./node_modules/.bin/mocha --reporter list --recursive --watch",
"test": "istanbul cover ./node_modules/.bin/_mocha test/ --report lcovonly -- -R spec --recursive && codecov || true"
@ -23,9 +23,10 @@
"url": "https://github.com/uCOMmandIt/i2c/issues"
},
"homepage": "https://github.com/uCOMmandIt/i2c#readme",
"@std/esm": "cjs",
"dependencies": {
"@uci/require-all": "^2.x",
"i2c-bus": "^1.x"
"@std/esm": "^0.18.0",
"i2c-bus": "^1.x",
},
"devDependencies": {
"chai": "^3.5.0",

1
src/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/node_modules/

View File

@ -15,19 +15,12 @@ const LOG_OPTS = (id) => {
export default class Device extends Base {
constructor(opts) {
if (!opts.bus) opts.bus ={}
opts.bus.name = opts.bus.name || 'bus' //optional bus socket name
opts.sockets = opts.sockets ||''
// either device process instance runs on same host (use named pipe) or not (the host of bus must be given)
if (opts[opts.bus.name]) {
if (opts[opts.bus.name].host) {
opts.sockets = opts.sockets + opts.bus.name+'#c>t'
opts[opts.bus.name].port = opts[opts.bus.name].port || 1776
}
if (opts[opts.bus.name].path) opts.sockets = opts.bus.name+'#c>n'
} else {
opts[opts.bus.name] = { path : (process.env.SOCKETS_DIR || __dirname) + '/i2c-bus.sock' }
opts.sockets = opts.bus.name+'#c>t'
}
if (opts[opts.bus.name].host) opts[opts.bus.name].port = opts[opts.bus.name].port || 1776
else opts[opts.bus.name].path = opts[opts.bus.name].path || (process.env.SOCKETS_DIR || __dirname) + '/i2c-bus.sock'
opts.sockets = (opts.sockets || '') + opts.bus.name + '#c>' + ((opts[opts.bus.name].path) ? 'n' :'t')
console.log(opts)
super(opts)
log = logger.child(LOG_OPTS(this.id))