uci-mcp/examples/17-test.mjs

51 lines
1.3 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}})
mcpclient.reply = function (packet) {
console.log('for request ',packet._header.request)
console.log('mcp status is ',packet.status)
}
await mcpclient.init()
console.log('=============sending============')
// const pins='2,3,4'
// const pins=3
// const pins='all'
// const pins=[1,3,5,7]
let packet = {cmd:'pin.cfg', pins:'all'}
await mcpclient.send(packet)
packet = {cmd:'pin.cfg', pins:'all', port:'B'}
await mcpclient.send(packet)
packet = {cmd:'pin.state.off', pins:'all' }
await mcpclient.send(packet)
packet = {cmd:'pin.state.off', pins:'all', port:'B' }
await mcpclient.send(packet)
packet = {cmd:'pin.state.on', pins:'2,7', port:'B' }
await mcpclient.send(packet)
packet = {cmd:'pin.state.on', pins:'3,6'}
await mcpclient.send(packet)
packet = {cmd:'pin.status', pins:'all'}
await mcpclient.send(packet)
packet = {cmd:'pin.status', pins:'all', port:'B'}
await mcpclient.send(packet)
process.kill(process.pid, 'SIGTERM')
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})