hb-rpi-tools 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -415,11 +415,17 @@ class Pigpio extends GpioClient {
415
415
  * for sending commands and receiving responses.
416
416
  * @throws `Error` - When connection fails.
417
417
  * @emits connect
418
+ * @emits ready
418
419
  */
419
420
  async connect () {
420
421
  await super.connect()
421
422
  await this.command(commands.HWVER)
422
423
  this.emit('message', 'connected to pigpio')
424
+
425
+ /** Emitted when client is ready to use the pigpio socket.
426
+ * @event PigpioClient#ready
427
+ */
428
+ this.emit('ready')
423
429
  }
424
430
 
425
431
  /** Subscribe to notifications for changed GPIO values.
@@ -689,14 +695,13 @@ class Pigpio extends GpioClient {
689
695
  const status = (await this.command(
690
696
  commands.SHELL, script.length, 0, script + '\0'
691
697
  )).status
692
- if (status === 32512) {
693
- throw new Error(
694
- `${this.commandName(commands.SHELL)}: ${script}: script not found`
695
- )
696
- } else if (status !== 0) {
697
- throw new Error(
698
- `${this.commandName(commands.SHELL)}: ${script}: exit status ${status / 256}`
699
- )
698
+ if (status !== 0) {
699
+ const message = status == null
700
+ ? 'timeout'
701
+ : status === 32512
702
+ ? 'script not found'
703
+ : `exit status ${status / 256}`
704
+ throw new Error(`${script}: ${message}`)
700
705
  }
701
706
  return status
702
707
  }
@@ -364,6 +364,7 @@ class Rgpio extends GpioClient {
364
364
  * for sending commands and receiving responses.
365
365
  * @throws `Error` - When connection fails.
366
366
  * @emits connect
367
+ * @emits ready
367
368
  */
368
369
  async connect () {
369
370
  await super.connect()
@@ -396,6 +397,11 @@ class Rgpio extends GpioClient {
396
397
  const result = await this.command(commands.GIC, { longs: [this.gpioChipHandle] })
397
398
  this.emit('message', `${result.name}: ${result.label}`)
398
399
  }
400
+
401
+ /** Emitted when client is ready to use the rgpio socket.
402
+ * @event RgpioClient#ready
403
+ */
404
+ this.emit('ready')
399
405
  }
400
406
 
401
407
  /** Subscribe to notifications for changed GPIO values.
@@ -702,14 +708,13 @@ class Rgpio extends GpioClient {
702
708
  const status = (await this.command(
703
709
  commands.SHELL, { longs: [script.length + 1], string: script }
704
710
  )).status
705
- if (status === 32512) {
706
- throw new Error(
707
- `${this.commandName(commands.SHELL)}: ${script}: script not found`
708
- )
709
- } else if (status !== 0) {
710
- throw new Error(
711
- `${this.commandName(commands.SHELL)}: ${script}: exit status ${status / 256}`
712
- )
711
+ if (status !== 0) {
712
+ const message = status == null
713
+ ? 'timeout'
714
+ : status === 32512
715
+ ? 'script not found'
716
+ : `exit status ${status / 256}`
717
+ throw new Error(`${script}: ${message}`)
713
718
  }
714
719
  return status
715
720
  }
package/lib/RpiTool.js CHANGED
@@ -15,7 +15,7 @@ const { b, u } = CommandLineTool
15
15
  const { UsageError } = CommandLineParser
16
16
 
17
17
  const usage = {
18
- rpi: `${b('rpi')} [${b('-hDV')}] [${b('-H')} ${u('hostname')}[${b(':')}${u('port')}]]] [${b('-U')} ${u('user')}] [${b('-P')} ${u('password')}] ${u('command')} [${u('argument')} ...]`,
18
+ rpi: `${b('rpi')} [${b('-hDV')}] [${b('-H')} ${u('hostname')}[${b(':')}${u('port')}]]] [${b('-U')} ${u('user')}] [${b('-P')} ${u('password')}] [${b('-t')} ${u('timeout')}] ${u('command')} [${u('argument')} ...]`,
19
19
  info: `${b('info')} [${b('-hns')}]`,
20
20
  state: `${b('state')} [${b('-hns')}]`,
21
21
  test: `${b('test')} [${b('-hns')}]`,
@@ -76,6 +76,10 @@ Parameters:
76
76
  Default is ${b('')} (empty).
77
77
  The password can also be set through the environment variable ${b('LG_PASS')}.
78
78
 
79
+ ${b('-t')} ${u('timeout')}, ${b('--timeout=')}${u('timeout')}
80
+ Set the command timeout in seconds.
81
+ Default is ${b('15')}.
82
+
79
83
  Commands:
80
84
  ${usage.info}
81
85
  ${description.info}
@@ -352,7 +356,8 @@ class RpiTool extends CommandLineTool {
352
356
  options: {
353
357
  host: process.env.LG_ADDR || process.env.PIGPIO_ADDR || 'localhost',
354
358
  user: process.env.LG_USER || 'homebridge-rpi',
355
- password: process.env.LG_PASS || ''
359
+ password: process.env.LG_PASS || '',
360
+ timeout: 15
356
361
  },
357
362
  port: process.env.LG_ADDR == null && process.env.PIGPIO_ADDR != null ? 8888 : 8889
358
363
  }
@@ -380,6 +385,9 @@ class RpiTool extends CommandLineTool {
380
385
  .option('P', 'password', (value) => {
381
386
  clargs.options.password = OptionParser.toString('password', value, true)
382
387
  })
388
+ .option('t', 'timeout', (value) => {
389
+ clargs.options.timeout = OptionParser.toInt('timeout', value, 1, 60, true)
390
+ })
383
391
  .parameter('command', (value) => {
384
392
  if (usage[value] == null || typeof this[value] !== 'function') {
385
393
  throw new UsageError(`${value}: unknown command`)
@@ -395,14 +403,7 @@ class RpiTool extends CommandLineTool {
395
403
  const { SystemInfo } = await import('hb-lib-tools/SystemInfo')
396
404
  let info
397
405
  if (['localhost', '127.0.0.1'].includes(this._clargs.options.host)) {
398
- const systemInfo = new SystemInfo()
399
- systemInfo
400
- .on('readFile', (fileName) => {
401
- this.debug('read file %s', fileName)
402
- })
403
- .on('exec', (cmd) => {
404
- this.debug('exec %s', cmd)
405
- })
406
+ const systemInfo = new SystemInfo({ logger: this })
406
407
  await systemInfo.init()
407
408
  if (!systemInfo.hwInfo.isRpi) {
408
409
  throw new Error('localhost: not a Rapsberry Pi')
@@ -441,7 +442,7 @@ class RpiTool extends CommandLineTool {
441
442
  return state
442
443
  }
443
444
 
444
- async _parseCommandArgs (...args) {
445
+ _parseCommandArgs (...args) {
445
446
  const parser = new CommandLineParser(this.pkgJson)
446
447
  const clargs = { options: {} }
447
448
  parser
@@ -525,7 +526,7 @@ class RpiTool extends CommandLineTool {
525
526
  .help('h', 'help', this.help)
526
527
  .remaining((list) => {
527
528
  for (const i in list) {
528
- const gpio = OptionParser.toInt(`gpio[${i}]`, list[i])
529
+ const gpio = OptionParser.toInt(`gpio[${i}]`, list[i], 0, 31, true)
529
530
  const bit = 1 << gpio
530
531
  this.debug('gpio %d, bit %d', gpio, bit)
531
532
  if ((bit & gpioMask) === 0) {
@@ -557,7 +558,7 @@ class RpiTool extends CommandLineTool {
557
558
  parser
558
559
  .help('h', 'help', this.help)
559
560
  .parameter('gpio', (value) => {
560
- clargs.gpio = OptionParser.toInt('gpio', value)
561
+ clargs.gpio = OptionParser.toInt('gpio', value, 0, 31, true)
561
562
  })
562
563
  .remaining((value) => {
563
564
  if (value.length > 1) {
@@ -589,14 +590,14 @@ class RpiTool extends CommandLineTool {
589
590
  parser
590
591
  .help('h', 'help', this.help)
591
592
  .parameter('gpio', (value) => {
592
- clargs.gpio = OptionParser.toInt('gpio', value)
593
+ clargs.gpio = OptionParser.toInt('gpio', value, 0, 31, true)
593
594
  })
594
595
  .remaining((value) => {
595
596
  if (value.length > 1) {
596
597
  throw new UsageError('too many parameters')
597
598
  }
598
599
  if (value.length === 1) {
599
- clargs.value = OptionParser.toInt('dutyCycle', value[0], 0, 100)
600
+ clargs.value = OptionParser.toInt('dutyCycle', value[0], 0, 100, true)
600
601
  }
601
602
  })
602
603
  .parse(...args)
@@ -626,16 +627,16 @@ class RpiTool extends CommandLineTool {
626
627
  parser
627
628
  .help('h', 'help', this.help)
628
629
  .parameter('gpio', (value) => {
629
- clargs.gpio = OptionParser.toInt('gpio', value)
630
+ clargs.gpio = OptionParser.toInt('gpio', value, 0, 31, true)
630
631
  })
631
632
  .remaining((value) => {
632
633
  if (value.length > 1) {
633
634
  throw new UsageError('too many parameters')
634
635
  }
635
636
  if (value.length === 1) {
636
- clargs.value = OptionParser.toInt('pulseWidth', value[0], 0)
637
+ clargs.value = OptionParser.toInt('pulseWidth', value[0], 0, 2500, true)
637
638
  if (clargs.value !== 0) {
638
- clargs.value = OptionParser.toInt('pulseWidth', value[0], 500, 2500)
639
+ clargs.value = OptionParser.toInt('pulseWidth', value[0], 500, 2500, true)
639
640
  }
640
641
  }
641
642
  })
@@ -664,7 +665,7 @@ class RpiTool extends CommandLineTool {
664
665
  parser
665
666
  .help('h', 'help', this.help)
666
667
  .parameter('gpio', (value) => {
667
- clargs.gpio = OptionParser.toInt('gpio', value)
668
+ clargs.gpio = OptionParser.toInt('gpio', value, 0, 31, true)
668
669
  })
669
670
  .parse(...args)
670
671
  const { gpioMask, model } = await this._getInfo()
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "hb-rpi-tools",
3
3
  "description": "Homebridge RPi Tools",
4
4
  "license": "Apache-2.0",
5
- "version": "1.0.1",
5
+ "version": "1.0.3",
6
6
  "keywords": [
7
7
  "raspberrypi",
8
8
  "raspberry",
@@ -25,10 +25,10 @@
25
25
  "rpi": "cli/rpi.js"
26
26
  },
27
27
  "engines": {
28
- "node": "^24||^22||^20"
28
+ "node": "^24||^22"
29
29
  },
30
30
  "dependencies": {
31
- "hb-lib-tools": "~2.2.18"
31
+ "hb-lib-tools": "~3.0.4"
32
32
  },
33
33
  "scripts": {
34
34
  "prepare": "standard && rm -rf out && jsdoc -c jsdoc.json",