Handler to be used internally by Domapic Modules for controlling a gpio in "out" mode
This package provides a Domapic handler for controlling a relay using the onoff library internally. Passing 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 relay, and the handler will automatically load the configuration. Or you can also set the options with fixed values programatically.
npm i gpio-out-domapic -save
options
<object>
Object containing default values for options. Will apply these values if no configuration keys are provided.initialStatus
<boolean>
Defines the initial status of the gpio. Default isfalse
.rememberLastStatus
<boolean>
Defines if the Gpio will save status to storage in order to remember the last one when it is restarted. Default isfalse
.invert
<boolean>
If true, the values read from or written to the GPIO should be inverted. Equivalent to theactiveLow
option of the onoff library.
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 isgpio
.initialStatus
<string>
Key defining the configuration property in which the initial status for the gpio is defined.rememberLastStatus
<string>
Key defining the configuration property in which the rememberLastStatus option is defined.invert
<string>
Key defining the configuration property in which the invert option is defined.
gpio.init()
async method
. Initializes the gpio retrieving configuration, etc.gpio.toggle()
async method
. Changes the relay status inverting the current value.gpio.setStatus(status)
async method
. Changes the relay status to the provided one.status
<boolean>
Defines the new status for the gpio.
gpio.status
getter
. Returns the current gpio status.
In the next example, the gpio-out-domapic
package is used to create a Domapic Module that controls a relay that can be toogled using the built-in api:
const path = require('path')
const domapic = require('domapic-service')
const gpioOut = require('gpio-out-domapic')
domapic.createModule({
packagePath: path.resolve(__dirname),
customConfig: {
status: {
type: 'boolean',
describe: 'Set initial status of the relay when module is started',
default: false
}
}
}).then(async dmpcModule => {
const relay = new gpioOut.Gpio(dmpcModule, {
rememberLastStatus: true
}, {
initialStatus: 'status'
})
await dmpcModule.register({
toggle: {
description: 'Toggle relay',
action: {
description: 'Toggle the relay status',
handler: async () => {
await relay.toggle()
return Promise.resolve()
}
}
}
})
await relay.init()
return dmpcModule.start()
})
Now, the module can be started using the status
option, which Gpio will use as initialStatus
:
node server.js --status=true