From 615586173d1e2df2c525d76c7b06521a3648d524 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Wed, 7 Feb 2018 10:41:56 -0800 Subject: [PATCH] better socket default port (1776) and path (using SOCKETS_DIR environment variable added logger --- examples/bus.mjs | 2 +- examples/ipc-relay.mjs | 2 +- examples/ipc-scan.mjs | 2 +- examples/tcp-relay.mjs | 2 +- examples/tcp-scan.mjs | 2 +- package.json | 5 +++-- src/bus-packet.mjs | 26 +++++++++++++++++--------- 7 files changed, 25 insertions(+), 16 deletions(-) diff --git a/examples/bus.mjs b/examples/bus.mjs index cd99d8f..d4156af 100644 --- a/examples/bus.mjs +++ b/examples/bus.mjs @@ -8,7 +8,7 @@ const PATH = '/opt/uci/unix.sock' ; (async () => { - let i2cbus = new Bus({id:'i2c-bus', us:{path:PATH} }) + let i2cbus = new Bus({id:'i2c-bus'}) await i2cbus.init() diff --git a/examples/ipc-relay.mjs b/examples/ipc-relay.mjs index ce75f87..0cd4be7 100644 --- a/examples/ipc-relay.mjs +++ b/examples/ipc-relay.mjs @@ -3,7 +3,7 @@ * */ -const PATH = '/opt/uci/unix.sock' +const PATH = '/opt/sockets/i2c-bus.sock' import Base from '../../uci-base/src/base' // import Base from '@uci/base' diff --git a/examples/ipc-scan.mjs b/examples/ipc-scan.mjs index ea31714..59c92db 100644 --- a/examples/ipc-scan.mjs +++ b/examples/ipc-scan.mjs @@ -5,7 +5,7 @@ import Base from '../../uci-base/src/base' -const PATH = '/opt/uci/unix.sock' +const PATH = '/opt/sockets/i2c-bus.sock' const delay = time => new Promise(res=>setTimeout(()=>res(),time)) ; diff --git a/examples/tcp-relay.mjs b/examples/tcp-relay.mjs index ded5dc6..daa1ee9 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#c>t', tc:{host:HOST}}) + let relays = new Base({id:'tcp-i2c-client', sockets:'tc#c>t', tc:{port: 1776, host:HOST}}) relays.reply = function (packet) { // console.log(packet.bus) diff --git a/examples/tcp-scan.mjs b/examples/tcp-scan.mjs index ff7a082..d2b5b4f 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#c>t', tc:{host:HOST}}) + let scanner = new Base({id:'tcp-i2c-client', sockets:'tc#c>t', tc:{port: 1776, host:HOST}}) scanner.reply = function (packet) { let addresses = packet.response.map(device => { diff --git a/package.json b/package.json index 9f60185..97dd28a 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,9 @@ "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", + "busa": "node_modules/.bin/nodemon --require @std/esm --watch ../ examples/bus", + "bus": "SOCKET_DIR=/opt/sockets node_modules/.bin/nodemon --require @std/esm examples/bus", + "busl": "DEBUG=true SOCKETS_DIR=/opt/sockets 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" diff --git a/src/bus-packet.mjs b/src/bus-packet.mjs index 2853f32..13d6940 100644 --- a/src/bus-packet.mjs +++ b/src/bus-packet.mjs @@ -4,22 +4,32 @@ import pify from 'pify' // import Base from '@uci/base' import Base from '../../uci-base/src/base' +import logger from '../../uci-logger/src/logger' +let log = {} +const LOG_OPTS = { + repo:'uci-i2c-bus', + npm:'@uci/i2c-bus', + file:'src/bus-packet.mjs', + class:'Bus', + id:this.id, + instance_created:new Date().getTime() +} + export default class Bus extends Base { constructor(opts) { - console.log(opts) + log = logger.child(LOG_OPTS) opts.sockets = opts.sockets || 'us#s>n,ts#s>t' + opts.ts = opts.ts || { port:1776 } + opts.us = opts.us || {path: (process.env.SOCKETS_DIR || __dirname) + '/i2c-bus.sock'} + log.info({opts:opts},'create bus with these opts') super(opts) this.busnum = opts.busnum || 1 this.i2cbus = i2c.open(this.busnum, () => {}) - // 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() } @@ -69,16 +79,14 @@ const validateArgs = function (packet) { } if (missing.length > 0) { - return {error: `following arguments are missing ${missing}`, packet: packet } + return {error: `following bus arguments are missing ${missing}`, packet: packet } } return {} } const bus_funcs = { - scan: function () { - // console.log(this) - return pify(this.i2cbus.scan).bind(this.i2cbus)() }, + scan: function () { return pify(this.i2cbus.scan).bind(this.i2cbus)() }, close: function () { return pify(this.i2cbus.close).bind(this.i2cbus)() }, readRaw: function (args) {