2019-09-09 13:26:02 -07:00
|
|
|
|
|
|
|
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
|
|
|
|
|
2019-09-11 18:34:54 -07:00
|
|
|
export async function error (packet) {
|
|
|
|
console.log('======== error during socket/server processing')
|
|
|
|
if(packet.msg) console.log(packet.msg)
|
|
|
|
if(packet.error) console.log(packet.error)
|
|
|
|
if(packet._header.request) {
|
|
|
|
console.log('sent request was')
|
|
|
|
console.dir(packet._header.request)
|
|
|
|
}
|
|
|
|
console.log('==========')
|
|
|
|
}
|
|
|
|
|
2019-09-09 13:26:02 -07:00
|
|
|
export function reply (packet) {
|
|
|
|
let req = packet._header.request
|
2019-09-11 18:34:54 -07:00
|
|
|
if (req.cmd !=='busFuncs') console.log(`response from relays for ${req.cmd}:`,req.args, `was ${packet.response}`)
|
|
|
|
else {
|
|
|
|
console.log('==========Available Bus Functions=====')
|
|
|
|
console.dir(packet.functions)
|
|
|
|
console.log('===============')
|
|
|
|
}
|
2019-09-09 13:26:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function test (address) {
|
|
|
|
let packet
|
|
|
|
console.log('=============sending packets for i2c mcp23008 relay device ============')
|
2019-09-11 18:34:54 -07:00
|
|
|
console.log('setting all pins to outputs')
|
|
|
|
packet = {cmd:'write', args:{address:address,cmd: 0} }
|
2019-09-09 13:26:02 -07:00
|
|
|
console.dir(packet)
|
|
|
|
await this.send(packet)
|
|
|
|
packet = {cmd:'read', args:{address:address ,cmd:0} }
|
|
|
|
console.dir(packet)
|
|
|
|
await this.send(packet)
|
|
|
|
console.log('========= turn on each relay ============')
|
|
|
|
let byte = 0
|
|
|
|
for (let i = 0; i < 8 ; i++) {
|
|
|
|
byte += Math.pow(2,i)
|
|
|
|
packet = {cmd:'write', args:{address:address,cmd: 9, byte:byte} }
|
|
|
|
console.log(`==== relay ${i+1} on with byte: ${byte} ===`)
|
|
|
|
console.dir(packet)
|
2019-09-11 18:34:54 -07:00
|
|
|
this.send(packet)
|
2019-09-09 13:26:02 -07:00
|
|
|
packet = {cmd:'read', args:{address:address ,cmd:9} }
|
|
|
|
console.dir(packet)
|
2019-09-11 18:34:54 -07:00
|
|
|
this.send(packet)
|
2019-09-13 19:23:12 -07:00
|
|
|
await delay(300)
|
2019-09-09 13:26:02 -07:00
|
|
|
}
|
|
|
|
console.log('========= done each relay, clear (off) this ============')
|
|
|
|
packet = {cmd:'write', args:{address:address,cmd: 9, byte:0} }
|
|
|
|
console.dir(packet)
|
|
|
|
await this.send(packet)
|
|
|
|
packet = {cmd:'read', args:{address:address ,cmd:9} }
|
|
|
|
console.dir(packet)
|
|
|
|
await this.send(packet)
|
|
|
|
await delay(1000)
|
|
|
|
}
|