uci-mcp/examples/17-test.js

60 lines
1.5 KiB
JavaScript

/*
* i2c bus unix socket and client in one for demo
*
*/
import Base from '../../uci-base/src/base'
import { spawn } from 'child_process'
const PATH = '/opt/sockets/mcp.sock'
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
const i2cbus = spawn('node',['-r', '@std/esm', './examples/bus'])
i2cbus.stdout.on('data', function(buf) {
console.log('[I2C BUS]', String(buf))
})
;
(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)
})