feat: node rpi demo (#3)
* uncomment nodespi * add in exports * fixed option * simplify build * adding spi options * more options * mark as experimental * add proxy transport * updated passive * use new transport * proxy bridge * passive for no transports, proxy otherwise * added logging * send buffer from bridge * remove too much logging * use bridge always
This commit is contained in:
Родитель
456061f546
Коммит
38471f57ea
|
@ -27,7 +27,7 @@ jobs:
|
|||
- uses: bahmutov/npm-install@v1
|
||||
- run: yarn prettier
|
||||
- run: yarn lint
|
||||
- run: yarn build:cli
|
||||
- run: yarn build
|
||||
- run: npx semantic-release
|
||||
if: ${{ github.ref == 'refs/heads/main' }}
|
||||
env:
|
||||
|
|
|
@ -11,11 +11,10 @@
|
|||
"scripts": {
|
||||
"lint": "eslint src/**/*.ts",
|
||||
"prettier": "prettier --write src/**/*.ts",
|
||||
"build:cli": "microbundle --no-compress",
|
||||
"watch:cli": "microbundle watch",
|
||||
"devtools": "yarn build:web && node ./dist/cli.js devtools",
|
||||
"build": "microbundle --no-compress",
|
||||
"watch": "microbundle watch",
|
||||
"publish:pi": "yarn build:cli && scp -r ../jacdac-ts/dist/* pi@192.168.0.131:/home/pi/nodejs/ && scp -r ./dist/* pi@192.168.0.131:/home/pi/nodejs/"
|
||||
"devtools": "yarn build:web && node ./dist/cli.js devtools",
|
||||
"publish:pi": "yarn build && scp -r ../jacdac-ts/dist/* pi@192.168.0.131:/home/pi/nodejs/ && scp -r ./dist/* pi@192.168.0.131:/home/pi/nodejs/"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
|
|
@ -6,6 +6,8 @@ import {
|
|||
PACKET_PROCESS,
|
||||
printPacket,
|
||||
serializeToTrace,
|
||||
createProxyBridge,
|
||||
toHex,
|
||||
} from "jacdac-ts"
|
||||
import { createTransports, TransportsOptions } from "./transports"
|
||||
|
||||
|
@ -46,8 +48,6 @@ export async function devToolsCommand(
|
|||
const tcpPort = 8082
|
||||
const listenHost = internet ? undefined : "127.0.0.1"
|
||||
|
||||
const transports = createTransports(options)
|
||||
|
||||
log(`Jacdac dev tools`)
|
||||
log(` dashboard: http://localhost:${port}`)
|
||||
log(` websocket: ws://localhost:${port}`)
|
||||
|
@ -60,6 +60,7 @@ export async function devToolsCommand(
|
|||
// start http server
|
||||
//debug(`starting proxy web server`)
|
||||
const clients: WebSocket[] = []
|
||||
|
||||
const server = http.createServer(function (req, res) {
|
||||
const parsedUrl = url.parse(req.url)
|
||||
const pathname = parsedUrl.pathname
|
||||
|
@ -73,19 +74,20 @@ export async function devToolsCommand(
|
|||
})
|
||||
|
||||
// passive bus to sniff packets
|
||||
const bridge = createProxyBridge(data =>
|
||||
clients.forEach(c => c.send(Buffer.from(data)))
|
||||
)
|
||||
const transports = createTransports(options)
|
||||
const bus = new JDBus(transports, {
|
||||
client: false,
|
||||
disableRoleManager: true,
|
||||
proxy: true,
|
||||
})
|
||||
bus.on(ERROR, e => error(e))
|
||||
bus.passive = transports.length === 0
|
||||
|
||||
bus.addBridge(bridge)
|
||||
const processPacket = (message: Buffer | Uint8Array, sender: string) => {
|
||||
const data = new Uint8Array(message)
|
||||
const pkt = Packet.fromBinary(data, bus.timestamp)
|
||||
pkt.sender = sender
|
||||
bus.processPacket(pkt)
|
||||
bridge.receiveFrameOrPacket(data, sender)
|
||||
}
|
||||
|
||||
function removeClient(client: WebSocket) {
|
||||
|
|
18
src/index.ts
18
src/index.ts
|
@ -23,9 +23,12 @@ async function mainCli() {
|
|||
|
||||
createCommand("stream")
|
||||
.option("--sensors", "stream sensors data")
|
||||
.option("-u, --usb", "listen to Jacdac over USB")
|
||||
.option("-s, --serial", "listen to Jacdac over SERIAL")
|
||||
//.option("-i, --spi", "listen to Jacdac over SPI")
|
||||
.option("-u, --usb", "listen to Jacdac over USB (requires usb)")
|
||||
.option(
|
||||
"-s, --serial",
|
||||
"listen to Jacdac over SERIAL (requires serialport)"
|
||||
)
|
||||
.option("-i, --spi", "listen to Jacdac over SPI (requires rpio, experimental)")
|
||||
.option("-p, --packets", "show all packets")
|
||||
.option("--devices <string>", "regular expression filter for devices")
|
||||
.option("--services <string>", "regular expression filter for services")
|
||||
|
@ -36,9 +39,12 @@ async function mainCli() {
|
|||
createCommand("devtools")
|
||||
.option("-p, --packets", "show all packets")
|
||||
.option("-w, --internet", "allow connections from non-localhost")
|
||||
.option("-u, --usb", "listen to Jacdac over USB")
|
||||
.option("-s, --serial", "listen to Jacdac over SERIAL")
|
||||
//.option("-i, --spi", "listen to Jacdac over SPI")
|
||||
.option("-u, --usb", "listen to Jacdac over USB (requires usb)")
|
||||
.option(
|
||||
"-s, --serial",
|
||||
"listen to Jacdac over SERIAL (requires serialport)"
|
||||
)
|
||||
.option("-i, --spi", "listen to Jacdac over SPI (requires rpio, experimental)")
|
||||
.action(devToolsCommand)
|
||||
|
||||
await program.parseAsync(process.argv)
|
||||
|
|
|
@ -2,6 +2,7 @@ import {
|
|||
createNodeUSBOptions,
|
||||
createNodeWebSerialTransport,
|
||||
createUSBTransport,
|
||||
createNodeSPITransport,
|
||||
Transport,
|
||||
} from "jacdac-ts"
|
||||
const log = console.log
|
||||
|
@ -12,6 +13,7 @@ export interface TransportsOptions {
|
|||
serial?: boolean
|
||||
ws?: boolean
|
||||
port?: number
|
||||
spi?: boolean
|
||||
}
|
||||
|
||||
export function createTransports(options: TransportsOptions) {
|
||||
|
@ -30,14 +32,12 @@ export function createTransports(options: TransportsOptions) {
|
|||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
transports.push(createNodeWebSerialTransport(require("serialport")))
|
||||
}
|
||||
/*
|
||||
if (options.spi) {
|
||||
log(`adding SPI transport`)
|
||||
log(`make sure to install the rpio package`)
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
transports.push(createNodeSPITransport(require("rpio")))
|
||||
}
|
||||
*/
|
||||
|
||||
return transports
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче