Skip to content

Commit

Permalink
Improve monitor output and log to usual file
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerivec committed Nov 8, 2024
1 parent 4ba7b11 commit fd1f0b4
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/commands/monitor/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Command } from '@oclif/core'

import { SLStatus } from 'zigbee-herdsman/dist/adapter/ember/enums.js'

import { logger } from '../../index.js'
import { getPortConf } from '../../utils/port.js'
import { Transport, TransportEvent } from '../../utils/transport.js'
Expand All @@ -9,6 +11,8 @@ export default class Monitor extends Command {
static override description = 'Monitor the chosen port in the console.'
static override examples = ['<%= config.bin %> <%= command.id %>']

private logBuffer: Buffer = Buffer.alloc(0)

public async run(): Promise<void> {
const portConf = await getPortConf()
logger.debug(`Using port conf: ${JSON.stringify(portConf)}`)
Expand All @@ -28,7 +32,7 @@ export default class Monitor extends Command {
logger.info(`Started monitoring. Press any key to stop.`)

transport.on(TransportEvent.FAILED, () => this.exit(1))
transport.on(TransportEvent.DATA, (data) => process.stdout.write(data))
transport.on(TransportEvent.DATA, this.onTransportData.bind(this))

process.stdin.setRawMode(true)
process.stdin.resume()
Expand All @@ -42,4 +46,36 @@ export default class Monitor extends Command {

return this.exit(0)
}

private onTransportData(received: Buffer): void {
// concat received to previous to ensure lines are outputted properly
let data = Buffer.concat([this.logBuffer, received])
let position: number

while ((position = data.indexOf('\r\n')) !== -1) {
// take everything up to '\r\n' (excluded)
const line = data.subarray(0, position)

// skip blank lines
if (line.length > 0) {
let asciiLine = line.toString('ascii')
// append SLStatus at end of line if detected hex for it
// - "Join network complete: 0x18"
// - "Join network start: 0x0"
// XXX: format seems pretty standard throughout the SDK, but this might create some false matches (hence leaving the hex too)
const endStatusMatch = asciiLine.match(/ (0x\d+)$/)

if (endStatusMatch) {
asciiLine += ` (${SLStatus[Number.parseInt(endStatusMatch[1], 16)]})`
}

logger.info(asciiLine)
}

// remove the line from internal buffer (set below), this time include '\r\n'
data = data.subarray(position + 2)
}

this.logBuffer = data
}
}

0 comments on commit fd1f0b4

Please sign in to comment.