0.1.23 streamline the commond bus function generation and call
parent
96472b71f8
commit
fff771ee17
|
@ -28,9 +28,15 @@ let options = {id:'i2c-client', useRootNS:true}
|
||||||
console.log('==================')
|
console.log('==================')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
await client.init()
|
console.log(await client.socketsInit())
|
||||||
|
|
||||||
|
client.on('connection:socket', async ev => {
|
||||||
|
if (ev.state ==='connected') {
|
||||||
|
console.log('connected continue')
|
||||||
await test.call(client,ADDRESS)
|
await test.call(client,ADDRESS)
|
||||||
process.kill(process.pid, 'SIGTERM')
|
process.kill(process.pid, 'SIGTERM')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
})().catch(err => {
|
})().catch(err => {
|
||||||
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
||||||
|
|
|
@ -27,6 +27,7 @@ export async function test () {
|
||||||
await this.send(packet)
|
await this.send(packet)
|
||||||
console.log('=============Device Address Scan Request ============')
|
console.log('=============Device Address Scan Request ============')
|
||||||
packet = {cmd:'scan'}
|
packet = {cmd:'scan'}
|
||||||
|
console.log(await this.send(packet))
|
||||||
let addresses = (await this.send(packet)).response
|
let addresses = (await this.send(packet)).response
|
||||||
console.log('addresses to check',addresses)
|
console.log('addresses to check',addresses)
|
||||||
console.log('=============Device ID scan request ============')
|
console.log('=============Device ID scan request ============')
|
||||||
|
@ -38,4 +39,7 @@ export async function test () {
|
||||||
await this.send(packet)
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@uci/i2c-bus",
|
"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",
|
"description": "I2c Bus Classes for Communication to I2C bus via socket or direct call",
|
||||||
"main": "src/bus",
|
"main": "src/bus",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
41
src/bus.js
41
src/bus.js
|
@ -32,13 +32,20 @@ class I2CBus extends Base {
|
||||||
this.i2cbus = i2c.open(this.busnum, () => {})
|
this.i2cbus = i2c.open(this.busnum, () => {})
|
||||||
this._funcs = bus_funcs
|
this._funcs = bus_funcs
|
||||||
this.bus = {}
|
this.bus = {}
|
||||||
this.addBusFuncs(this._funcs)
|
this.addBusFuncs()
|
||||||
this.addNamespace('bus')
|
this.addNamespace('bus')
|
||||||
this.bus.busFuncs = async (packet) => {
|
this.bus.busFuncs = async (packet) => {
|
||||||
packet.functions = this.busFuncs
|
packet.functions = this.busFuncs
|
||||||
packet.cmd='reply'
|
packet.cmd='reply'
|
||||||
return packet
|
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})
|
this._queue = new PQueue({concurrency: opts.concurrency || 1})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,16 +59,21 @@ class I2CBus extends Base {
|
||||||
|
|
||||||
get busFuncs() { return this._funcs}
|
get busFuncs() { return this._funcs}
|
||||||
|
|
||||||
addBusFuncs(funcs) {
|
addBusFuncs() {
|
||||||
for (let name in funcs) {
|
for (let alias in this.busFuncs) {
|
||||||
let func = funcs[name]
|
let func = this.busFuncs[alias]
|
||||||
this.addBusFunc(name,func)
|
this.addBusFunc(alias,func.name || alias, func.args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addBusFunc(name,func) {
|
addBusFunc(alias,name, args) {
|
||||||
this.bus[name] = busFunction.bind(this, func.name || name , func.args || [])
|
if (!name) name=alias
|
||||||
this._funcs[name]=func
|
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
|
} // end of Bus Packet Class
|
||||||
|
@ -93,7 +105,7 @@ const validateArg = function (arg,value) {
|
||||||
return valid
|
return valid
|
||||||
}
|
}
|
||||||
|
|
||||||
async function busFunction (func,args,packet) {
|
async function busFunction (name, func, args=[],packet) {
|
||||||
let argsV = []
|
let argsV = []
|
||||||
args.some(arg => {
|
args.some(arg => {
|
||||||
if (packet.args) {
|
if (packet.args) {
|
||||||
|
@ -101,20 +113,19 @@ async function busFunction (func,args,packet) {
|
||||||
if (argv != null) {
|
if (argv != null) {
|
||||||
if(validateArg(arg,argv)) argsV.push(argv)
|
if(validateArg(arg,argv)) argsV.push(argv)
|
||||||
else {
|
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
|
return true
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
packet.error = `missing argument, ${arg}, for function ${func}`
|
packet.error = `missing argument, ${arg}, for function ${name}`
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if (!packet.error) {
|
if (!packet.error) {
|
||||||
log.trace({msg:'adding to queue', function:func,arguments:argsV})
|
log.trace({msg:'adding to queue', function:name,arguments:argsV})
|
||||||
let busfunc = pify(this.i2cbus[func].bind(this.i2cbus,...argsV))
|
let [err,res] = await to(this._queue.add(() => func.call(this.i2cbus,...argsV)))
|
||||||
let [err,res] = await to(this._queue.add(busfunc))
|
if (err) packet.error = `error during call to ${name} with arguments ${JSON.stringify(packet.args)}: ${err}`
|
||||||
if (err) packet.error = `error during call to ${func} with arguments ${JSON.stringify(packet.args)}: ${err}`
|
|
||||||
else packet.response = res
|
else packet.response = res
|
||||||
}
|
}
|
||||||
packet.cmd = 'reply'
|
packet.cmd = 'reply'
|
||||||
|
|
Loading…
Reference in New Issue