refactored for changes to uci-base e5059368a9b8e655a7332b032330ff47aa0acbf0

master
David Kebler 2018-02-04 14:19:44 -08:00
parent a5e52274b0
commit bc75af050b
7 changed files with 37 additions and 28 deletions

View File

@ -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()

View File

@ -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)

View File

@ -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 => {

View File

@ -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)

View File

@ -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 => {

View File

@ -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"
}
}

View File

@ -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) {