1.1.0 • Published 5 years ago

gpio-in-domapic v1.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

Gpio In Domapic

Handler to be used internally by Domapic Modules for controlling a gpio in \"in\" mode

Build status Coverage Status Quality Gate js-standard-style

NPM dependencies Last commit Last release

NPM downloads License


Intro

This package provides a Domapic handler for controlling a gpio in "in" mode using the onoff library internally. Passing to it a Domapic module instance, it will retrieve the module instance configuration defined when started the service, and will configure the gpio based on it.

Just define which are your module "options keys" for configuring the gpio, and the handler will automatically load the configuration. Or you can also set the options with fixed values programatically.

Installation

npm i gpio-in-domapic -save

Usage

new Gpio(domapicModule, [options[, configurationKeys]])

  • options <object> Object containing default values for options. Will apply these values if no configuration keys are provided.
    • debounceTimeout <boolean> Defines the debounce time for emitting gpio events.
  • configurationKeys <object> Object defining configuration keys from which the options will be loaded.
    • gpio <string> Key defining the configuration property in which the gpio number is defined. Default is gpio.
    • debounceTimeout <string> Key defining the configuration property in which the debounceTimeout option for the gpio is defined.
Instance
  • gpio.init() async method. Initializes the gpio retrieving configuration, etc.
  • gpio.status getter. Returns the current gpio status.
  • gpio.events getter. Returns gpio eventEmitter object. Read the events chapter for further info.
Statics
  • eventNames <object> Object containing gpio event names.

Events

Gpio instances emit events through an eventEmitter object exposed in the events getter. Event names are exposed in the Gpio static object eventNames. Available events are:

  • Gpio.eventNames.ON. Emitted when gpio status change to true
  • Gpio.eventNames.OFF. Emitted when gpio status change to false
  • Gpio.eventNames.CHANGE. Emitted whenever the gpio status changes. It sends the new status as first argument to subscribed listeners.
  • Gpio.eventNames.ERROR. Emitted whenever the gpio throws an error. It sends the error as first argument to subscribed listeners.

Example

In the next example, the gpio-in-domapic package is used to create a Domapic Module having an state for returning a door status, and emitting an event when the door status changes. It also allow users to decide the debounce time when starting the module.

const path = require('path')

const domapic = require('domapic-service')
const gpioIn = require('gpio-in-domapic')

domapic.createModule({
  packagePath: path.resolve(__dirname),
  customConfig: {
    gpio: {
      type: 'number',
      describe: 'Set gpio number for the door sensor'
    },
    debounce: {
      type: 'number',
      describe: 'Set debounce timeout for the door sensor',
      default: 1000
    }
  }
}).then(async dmpcModule => {
  const doorSensor = new gpioIn.Gpio(dmpcModule, {
  }, {
    debounceTimeout: 'debounce'
  })

  await dmpcModule.register({
    door: {
      description: 'Door status',
      data: {
        type: 'boolean'
      },
      state: {
        description: 'Returns current door status',
        handler: () => doorSensor.status
      },
      event: {
        description: 'Door status has changed'
      }
    }
  })

  await doorSensor.init()

  doorSensor.events.on(gpioIn.Gpio.eventNames.CHANGE, newValue => {
    dmpcModule.tracer.debug('Door status has changed', newValue)
    dmpcModule.events.emit('door', newValue)
  })

  return dmpcModule.start()
})

Now, the module can be started using the debounce option, which Gpio will use as debounceTimeout:

node server.js --gpio=18 --debounce=1500