From 471ee2df1d85d5680887876602de90d2abcb067e Mon Sep 17 00:00:00 2001 From: David Kebler Date: Thu, 15 Aug 2019 14:05:24 -0700 Subject: [PATCH] 0.1.20 add default named pipe and tcp socket/server return init errors add ack package function to call bus ac --- examples/example.js | 38 ++++++++++++++++++++++++++++++++++++++ examples/ipc-scan.js | 24 ------------------------ examples/tcp-scan.js | 24 ------------------------ nodemon.json | 3 +++ nodemon.json.sav | 4 ++++ package.json | 19 +++++++++---------- src/device.js | 25 ++++++++++++++----------- 7 files changed, 68 insertions(+), 69 deletions(-) create mode 100644 examples/example.js delete mode 100644 examples/ipc-scan.js delete mode 100644 examples/tcp-scan.js create mode 100644 nodemon.json create mode 100644 nodemon.json.sav diff --git a/examples/example.js b/examples/example.js new file mode 100644 index 0000000..3332191 --- /dev/null +++ b/examples/example.js @@ -0,0 +1,38 @@ +/* +* +*/ +import Device from '../src/device' + +const ADDRESS = process.env.ADDRESS || 0x27 +const TIMEOUT = 15 + + ; +(async () => { + + let device = new Device({id:'i2c-device', useRootNS:true, initTimeout:TIMEOUT, address:ADDRESS, bus:{host:process.env.HOST, port:process.env.PORT}}) + + device.on('status',err => { + console.log('STATUS EMITTED\n', err) + }) + + device.reply = function (packet) { + console.log('=====bus reply response====') + console.log('to request:',packet._header.request) + console.log('was:',packet.response) + console.log('===============') + } + + let res = await device.init() + if (res.error) { + console.log('errors during init') + process.kill(process.pid, 'SIGTERM') + } + + console.log('all connected good proceed with some commands to bus') + await device.bus.write(9,255) + device.bus.write(9,0) + +})().catch(err => { + console.error('FATAL: UNABLE TO START SYSTEM!\n',err) + process.kill(process.pid, 'SIGTERM') +}) diff --git a/examples/ipc-scan.js b/examples/ipc-scan.js deleted file mode 100644 index 1729da6..0000000 --- a/examples/ipc-scan.js +++ /dev/null @@ -1,24 +0,0 @@ -/* -* i2c bus with named pipe socket - run on same machine/host as bus -* -*/ -import Device from '../src/device' -// const PATH = '' - - ; -(async () => { - - let device = new Device({id:'an i2c device', useRootNS:true, address:0x27}) - - 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/examples/tcp-scan.js b/examples/tcp-scan.js deleted file mode 100644 index f5bf51f..0000000 --- a/examples/tcp-scan.js +++ /dev/null @@ -1,24 +0,0 @@ -/* -* 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' -// const PATH = '' - - ; -(async () => { - - let device = new Device({id:'an i2c device', useRootNS:true, address:0x27, bus:{host:'sbc'} }) - - device.reply = function (packet) { - console.log('for request ',packet._header.request) - console.log('bus response is ',packet.response) - } - - console.log('return from await scan', (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/nodemon.json b/nodemon.json new file mode 100644 index 0000000..f7a9f9d --- /dev/null +++ b/nodemon.json @@ -0,0 +1,3 @@ +{ + "ignoreRoot": [".git"] +} diff --git a/nodemon.json.sav b/nodemon.json.sav new file mode 100644 index 0000000..46168ff --- /dev/null +++ b/nodemon.json.sav @@ -0,0 +1,4 @@ +{ + "ignoreRoot": [".git"], + "watch": ["node_modules/@uci/*/src","node_modules/@uci-utils/*/src","src","examples"] +} diff --git a/package.json b/package.json index bf603a7..1048918 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "name": "@uci/i2c-device", - "version": "0.1.17", + "version": "0.1.20", "description": "Device Classes for I2C Interfacing", "main": "src/device", "scripts": { - "tscan": "node -r esm examples/tcp-scan || true", - "iscan": "node -r esm examples/ipc-scan || true", + "example": "node -r esm examples/example --preserve-symlinks || true", + "example:dev": "UCI_ENV=dev ./node_modules/.bin/nodemon -r esm examples/example --preserve-symlinks || true" , "testw": "./node_modules/.bin/mocha --reporter list --recursive --watch", "test": "istanbul cover ./node_modules/.bin/_mocha test/ --report lcovonly -- -R spec --recursive && codecov || true" }, @@ -26,15 +26,14 @@ }, "homepage": "https://github.com/uCOMmandIt/i2c#readme", "dependencies": { - "@uci/base": "^0.1.21", - "@uci-utils/logger": "0.0.14" + "@uci/base": "^0.1.30", + "@uci-utils/logger": "0.0.15" }, "devDependencies": { - "chai": "^4.1.2", + "chai": "^4.2.0", "chai-as-promised": "^7.1.1", - "codecov": "^3.0.2", - "esm": "^3.0.38", - "istanbul": "^0.4.5", - "mocha": "^5.2.0" + "esm": "^3.2.25", + "mocha": "^6.2.0", + "nodemon": "^1.19.2" } } diff --git a/src/device.js b/src/device.js index bc2312b..ea47a1f 100644 --- a/src/device.js +++ b/src/device.js @@ -5,16 +5,18 @@ let log = {} class I2CDevice extends Base { constructor(opts) { + opts.ndevice = opts.ndevice || {} + opts.tdevice = opts.tdevice || {} + opts.ndevice.path = opts.path || 'i2c-device' + opts.tdevice.port = opts.port || 1777 opts.bus = opts.bus || {} - if (opts.bus.host || opts.host) { - (opts.bus.host = opts.host || opts.bus.host), - (opts.bus.port = opts.bus.port || opts.port || 1776) - opts.sockets = (opts.sockets ? opts.sockets + ',' : '') + 'bus#c>t' - } else { - opts.bus.path = opts.bus.path || opts.path || 'i2c-bus' - opts.sockets = (opts.sockets ? opts.sockets + ',' : '') + 'bus#c>n' - } + if (opts.bus.host) opts.bus.port = opts.bus.port || 1776 + else opts.bus.path = opts.bus.path || 'i2c-bus' super(opts) + this.addSocket('bus','c',opts.bus.host ? 't':'n',opts.bus) + this.addSocket('device:tcp','s','t',opts.tdevice) + this.addSocket('device:np','s','n',opts.ndevice) + log = logger({ file: 'src/device-packet.js', class: 'Device', @@ -25,12 +27,13 @@ class I2CDevice extends Base { this.address = +opts.address // make sure any passed number is number not a string this.channel = +opts.channel // if using TAC9546A channel number on which device is attached this.bus = this.bindFuncs(commands) - this.addNamespace('bus','s') // give remote packet access to bus commands. ack will superceed base ack + this._s.ack = async (packet) => { return Object.assign(packet,await this.bus.ack()) } // give socket access to bus.ack } async init() { - await super.init() - let res = await this.bus.ack() + let res = await super.init() + if (res.errors) return {error: res.errors, msg:'socket initialization', } + res = await this.bus.ack() let socket = this.getSocket('bus').opts let connection = socket.path || socket.host + ':' + socket.port if (!res.ack) {