From 8fb4dbd78829e536771879bd3f7ff93fd727654a Mon Sep 17 00:00:00 2001 From: David Kebler Date: Wed, 14 Feb 2018 19:03:20 -0800 Subject: [PATCH] improved opts processing --- .eslintrc.js | 8 +++++++- examples/device.mjs | 24 ++++++++++++++++++++++++ index.js | 8 -------- package.json | 11 ++++++----- src/.gitignore | 1 + src/device-packet.mjs | 15 ++++----------- 6 files changed, 42 insertions(+), 25 deletions(-) create mode 100644 examples/device.mjs delete mode 100644 index.js create mode 100644 src/.gitignore diff --git a/.eslintrc.js b/.eslintrc.js index c11a1d5..2bed546 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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": { diff --git a/examples/device.mjs b/examples/device.mjs new file mode 100644 index 0000000..1ef3a35 --- /dev/null +++ b/examples/device.mjs @@ -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') +}) diff --git a/index.js b/index.js deleted file mode 100644 index f84fe28..0000000 --- a/index.js +++ /dev/null @@ -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) diff --git a/package.json b/package.json index 4a378fe..77460f3 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..2ccbe46 --- /dev/null +++ b/src/.gitignore @@ -0,0 +1 @@ +/node_modules/ diff --git a/src/device-packet.mjs b/src/device-packet.mjs index 79c3b75..bc6c9c7 100644 --- a/src/device-packet.mjs +++ b/src/device-packet.mjs @@ -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))