uci-mcp/examples/ipc-test.mjs

63 lines
1.5 KiB
JavaScript

/*
* i2c bus unix socket and client in one for demo
*
*/
import Base from '../../uci-base/src/base'
const PATH = '/opt/sockets/mcp.sock'
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
;
(async () => {
let mcpclient = new Base({id:'mcpclient', sockets:'uc#c>n', uc:{path:PATH}})
await mcpclient.init()
console.log('=============sending============')
// let packet = {cmd:'chip.ack'}
// console.dir(packet)
// for (let i=0;i<20;i++){
// await delay(100)
// packet.num = i
// console.log('--------->',i)
// mcpclient.send(packet)
// }
let packet = {cmd:'chip.cfg'}
console.dir(packet)
mcpclient.send(packet)
await delay(1000)
// packet = {cmd:'pin.cfg', pin:2, cfg:'toggle_switch'}
packet = {cmd:'pin.cfg', pin:2}
console.dir(packet)
mcpclient.send(packet)
await delay(1000)
packet = {cmd:'pin.state.off', pin:2, }
console.dir(packet)
mcpclient.send(packet)
await delay(1000)
packet = {cmd:'pin.state.on', pin:2, }
console.dir(packet)
mcpclient.send(packet)
await delay(1000)
packet = {cmd:'pin.state.toggle', pin:2, }
console.dir(packet)
mcpclient.send(packet)
await delay(1000)
packet = {cmd:'pin.state.toggle', pin:2, }
console.dir(packet)
mcpclient.send(packet)
// packet = {cmd:'pin.cfg', pin:3}
// console.dir(packet)
// mcpclient.send(packet)
await delay(10000)
process.kill(process.pid, 'SIGTERM')
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})