0.1.23 streamline the commond bus function generation and call

master
David Kebler 2019-12-18 18:09:44 -08:00
parent 96472b71f8
commit fff771ee17
4 changed files with 40 additions and 19 deletions

View File

@ -28,9 +28,15 @@ let options = {id:'i2c-client', useRootNS:true}
console.log('==================')
}
})
await client.init()
await test.call(client,ADDRESS)
process.kill(process.pid, 'SIGTERM')
console.log(await client.socketsInit())
client.on('connection:socket', async ev => {
if (ev.state ==='connected') {
console.log('connected continue')
await test.call(client,ADDRESS)
process.kill(process.pid, 'SIGTERM')
}
})
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)

View File

@ -27,6 +27,7 @@ export async function test () {
await this.send(packet)
console.log('=============Device Address Scan Request ============')
packet = {cmd:'scan'}
console.log(await this.send(packet))
let addresses = (await this.send(packet)).response
console.log('addresses to check',addresses)
console.log('=============Device ID scan request ============')
@ -38,4 +39,7 @@ export async function test () {
await this.send(packet)
}
}
console.log('=============Sending Write to mcp at 39 ============')
packet= {cmd:'write', args:{'address':39,'cmd':9,'byte':128}}
await this.send(packet)
}

View File

@ -1,6 +1,6 @@
{
"name": "@uci/i2c-bus",
"version": "0.1.22",
"version": "0.1.23",
"description": "I2c Bus Classes for Communication to I2C bus via socket or direct call",
"main": "src/bus",
"scripts": {

View File

@ -32,13 +32,20 @@ class I2CBus extends Base {
this.i2cbus = i2c.open(this.busnum, () => {})
this._funcs = bus_funcs
this.bus = {}
this.addBusFuncs(this._funcs)
this.addBusFuncs()
this.addNamespace('bus')
this.bus.busFuncs = async (packet) => {
packet.functions = this.busFuncs
packet.cmd='reply'
return packet
}
this.bus.ack = async function (){
//
let bus = await this.bus.scan()
if (bus.error) return bus
let res = { cmd:'reply', ack: true, scan:bus.response}
return res
},
this._queue = new PQueue({concurrency: opts.concurrency || 1})
}
@ -52,16 +59,21 @@ class I2CBus extends Base {
get busFuncs() { return this._funcs}
addBusFuncs(funcs) {
for (let name in funcs) {
let func = funcs[name]
this.addBusFunc(name,func)
addBusFuncs() {
for (let alias in this.busFuncs) {
let func = this.busFuncs[alias]
this.addBusFunc(alias,func.name || alias, func.args)
}
}
addBusFunc(name,func) {
this.bus[name] = busFunction.bind(this, func.name || name , func.args || [])
this._funcs[name]=func
addBusFunc(alias,name, args) {
if (!name) name=alias
if (Array.isArray(name)) {
args=name
name=alias
}
console.log(alias,name,args)
this.bus[alias] = busFunction.bind(this, alias, pify(this.i2cbus[name]), args ||[])
}
} // end of Bus Packet Class
@ -93,7 +105,7 @@ const validateArg = function (arg,value) {
return valid
}
async function busFunction (func,args,packet) {
async function busFunction (name, func, args=[],packet) {
let argsV = []
args.some(arg => {
if (packet.args) {
@ -101,20 +113,19 @@ async function busFunction (func,args,packet) {
if (argv != null) {
if(validateArg(arg,argv)) argsV.push(argv)
else {
packet.error = `argument ${arg} has an invalide value ${argv} for function ${func}`
packet.error = `argument ${arg} has an invalid value ${argv} for function ${name}`
return true
}
} else {
packet.error = `missing argument, ${arg}, for function ${func}`
packet.error = `missing argument, ${arg}, for function ${name}`
return true
}
}
})
if (!packet.error) {
log.trace({msg:'adding to queue', function:func,arguments:argsV})
let busfunc = pify(this.i2cbus[func].bind(this.i2cbus,...argsV))
let [err,res] = await to(this._queue.add(busfunc))
if (err) packet.error = `error during call to ${func} with arguments ${JSON.stringify(packet.args)}: ${err}`
log.trace({msg:'adding to queue', function:name,arguments:argsV})
let [err,res] = await to(this._queue.add(() => func.call(this.i2cbus,...argsV)))
if (err) packet.error = `error during call to ${name} with arguments ${JSON.stringify(packet.args)}: ${err}`
else packet.response = res
}
packet.cmd = 'reply'