From bc75af050bb2a5cd9cf8911c07a6c74514620ea3 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Sun, 4 Feb 2018 14:19:44 -0800 Subject: [PATCH] refactored for changes to uci-base e5059368a9b8e655a7332b032330ff47aa0acbf0 --- examples/bus.mjs | 9 +++------ examples/ipc-relay.mjs | 4 ++-- examples/ipc-scan.mjs | 4 ++-- examples/tcp-relay.mjs | 2 +- examples/tcp-scan.mjs | 2 +- package.json | 5 ++++- src/bus-packet.mjs | 39 ++++++++++++++++++++++++--------------- 7 files changed, 37 insertions(+), 28 deletions(-) diff --git a/examples/bus.mjs b/examples/bus.mjs index e93b7f2..cd99d8f 100644 --- a/examples/bus.mjs +++ b/examples/bus.mjs @@ -2,16 +2,13 @@ * i2c bus with both unix and tcp socket using defaults. For TCP that is host OS name and port 8080 * */ - import Bus from '../src/bus-packet' +const PATH = '/opt/uci/unix.sock' -const delay = time => new Promise(res=>setTimeout(()=>res(),time)) - -; + ; (async () => { - // let i2cbus = new Bus({id:'i2c-bus', log:true}) - let i2cbus = new Bus({id:'i2c-bus', sockets:'us,ts', log:true}) + let i2cbus = new Bus({id:'i2c-bus', us:{path:PATH} }) await i2cbus.init() diff --git a/examples/ipc-relay.mjs b/examples/ipc-relay.mjs index e610a86..ce75f87 100644 --- a/examples/ipc-relay.mjs +++ b/examples/ipc-relay.mjs @@ -3,7 +3,7 @@ * */ -const PATH = '/opt/uci/uci-base/src/unix.sock' +const PATH = '/opt/uci/unix.sock' import Base from '../../uci-base/src/base' // import Base from '@uci/base' @@ -12,7 +12,7 @@ const delay = time => new Promise(res=>setTimeout(()=>res(),time)) ; (async () => { - let relays = new Base({sockets:'uc', path:PATH}) + let relays = new Base({id:'pipe-i2c-client', sockets:'uc#c>n', uc:{path:PATH}}) relays.reply = function (packet) { // console.log(packet.bus) diff --git a/examples/ipc-scan.mjs b/examples/ipc-scan.mjs index 0d6d8ed..ea31714 100644 --- a/examples/ipc-scan.mjs +++ b/examples/ipc-scan.mjs @@ -5,13 +5,13 @@ import Base from '../../uci-base/src/base' -const PATH = '/opt/uci/uci-base/src/unix.sock' +const PATH = '/opt/uci/unix.sock' const delay = time => new Promise(res=>setTimeout(()=>res(),time)) ; (async () => { - let scanner = new Base({id:'pipe-i2c-client', sockets:'uc', path:PATH}) + let scanner = new Base({id:'pipe-i2c-client', sockets:'uc#c>n', uc:{path:PATH}}) scanner.reply = function (packet) { let addresses = packet.response.map(device => { diff --git a/examples/tcp-relay.mjs b/examples/tcp-relay.mjs index c9c236a..ded5dc6 100644 --- a/examples/tcp-relay.mjs +++ b/examples/tcp-relay.mjs @@ -12,7 +12,7 @@ const delay = time => new Promise(res=>setTimeout(()=>res(),time)) ; (async () => { - let relays = new Base({id:'tcp-i2c-client', sockets:'tc', host:HOST, log:true}) + let relays = new Base({id:'tcp-i2c-client', sockets:'tc#c>t', tc:{host:HOST}}) relays.reply = function (packet) { // console.log(packet.bus) diff --git a/examples/tcp-scan.mjs b/examples/tcp-scan.mjs index c4c859e..ff7a082 100644 --- a/examples/tcp-scan.mjs +++ b/examples/tcp-scan.mjs @@ -8,7 +8,7 @@ const delay = time => new Promise(res=>setTimeout(()=>res(),time)) ; (async () => { - let scanner = new Base({id:'tcp-i2c-client', sockets:'tc', host:HOST}) + let scanner = new Base({id:'tcp-i2c-client', sockets:'tc#c>t', tc:{host:HOST}}) scanner.reply = function (packet) { let addresses = packet.response.map(device => { diff --git a/package.json b/package.json index ff0ac31..9f60185 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,8 @@ "description": "I2c Bus Classes for Communication to I2C bus via socket or direct call", "main": "src/bus", "scripts": { + "bus": "node_modules/.bin/nodemon --require @std/esm --watch ../ examples/bus", + "bus2": "node_modules/.bin/nodemon --require @std/esm examples/bus", "test": "mocha -r @std/esm test/*.test.mjs", "testw": "mocha -r @std/esm test/*.test.mjs --watch --recurse --watch-extensions mjs", "testci": "istanbul cover ./node_modules/.bin/_mocha test/ --report lcovonly -- -R spec --recursive && codecov || true" @@ -36,6 +38,7 @@ "chai-as-promised": "^6.0.0", "codecov": "^1.0.1", "istanbul": "^0.4.5", - "mocha": "^3.2.0" + "mocha": "^3.2.0", + "nodemon": "^1.14.12" } } diff --git a/src/bus-packet.mjs b/src/bus-packet.mjs index a64865d..2853f32 100644 --- a/src/bus-packet.mjs +++ b/src/bus-packet.mjs @@ -6,33 +6,40 @@ import Base from '../../uci-base/src/base' export default class Bus extends Base { constructor(opts) { - opts.sockets = opts.sockets || 'us' + console.log(opts) + opts.sockets = opts.sockets || 'us#s>n,ts#s>t' super(opts) this.busnum = opts.busnum || 1 this.i2cbus = i2c.open(this.busnum, () => {}) - this.funcs = bus_funcs - this.registerPacketProcessor.bind(this)(process) + // console.log(' ',this._packetProcess) + // this._packetProcess = packetProcess + // console.log(' ',this._packetProcess) + this.bus = bus_funcs // this.init = this.init.bind(this) } async init(){ + this.amendSocketProcessing(bus_funcs) await super.init() } + // TODO see if default processing of base can handle this now + async _packetProcess (sname,packet){ + if (!packet.cmd) return {error: 'no cmd: key in packet', packet: packet } + if (this.bus[packet.cmd]) { + // let res = validateArgs(packet) // handle with before hook + // if (res.error) return res.error + packet.cmd_sent = packet.cmd + packet.response = await this.bus[packet.cmd].bind(this)(packet.args) + packet.cmd = 'reply' + return packet + } else return {error: 'no i2c bus function available for packet command', packet: packet } + } + } // end of Bus Packet Class // replace base processor with one for i2c bus functions -const process = async function (packet){ - if (!packet.cmd) return {error: 'no cmd: key in packet', packet: packet } - if (this.context.funcs[packet.cmd]) { - // let res = validateArgs(packet) - // if (res.error) return res.error - packet.cmd_sent = packet.cmd - packet.response = await this.context.funcs[packet.cmd].bind(this.context)(packet.args) - packet.cmd = 'reply' - return packet - } else return {error: 'no i2c bus function available for packet command', packet: packet } -} + const validateArgs = function (packet) { let missing = [] @@ -69,7 +76,9 @@ const validateArgs = function (packet) { const bus_funcs = { - scan: function () { return pify(this.i2cbus.scan).bind(this.i2cbus)() }, + scan: function () { + // console.log(this) + return pify(this.i2cbus.scan).bind(this.i2cbus)() }, close: function () { return pify(this.i2cbus.close).bind(this.i2cbus)() }, readRaw: function (args) {