Commit d07b5863 authored by Peter Müller's avatar Peter Müller

New setup promise chain to avoid overlapping initialisations and unhandled rejection errors, v1.0.4

parent d2972b2f
Pipeline #141 passed with stages
in 26 seconds
## v1.0.4 2017-06-10
- New setup promise chain to avoid overlapping initialisations and unhandled rejection errors
## v1.0.3 2017-06-04
- Added German localizations
......
......@@ -22,6 +22,7 @@ Feel free to submit more localized [device-config-schema-xx.coffee](https://git.
## Installation
You can simply install this plugin via the frontend.
After the installation you have to restart pimatic.
Alternatively you can add it manually to the `config.json` of pimatic:
......
......@@ -26,7 +26,7 @@
"pcf8574.coffee",
"README.md"
],
"version": "1.0.3",
"version": "1.0.4",
"homepage": "https://git.cryhost.de/crycode/pimatic-pcf8574",
"license": "GPL-2.0",
"repository": {
......
......@@ -75,6 +75,9 @@ module.exports = (env) ->
@pollingInterval = null
# Promise chain for setup
@setupChain = Promise.resolve()
# parse and check the address
@address = parseInt @config.address
assert @address >= 0 and @address <= 255, 'The address must be between 0x00 (0) and 0xFF (255)'
......@@ -145,17 +148,27 @@ module.exports = (env) ->
assert 7 >= @config.pinNumber >= 0, "The pinNumber must be between 0 and 7"
# set pin as input
@pcf8574ic.ic.inputPin @config.pinNumber, @config.inverted? or false
@pcf8574ic.setupChain = @pcf8574ic.setupChain.then( =>
@pcf8574ic.ic.inputPin @config.pinNumber, @config.inverted? or false
).then( =>
# handle input changes
@pcf8574ic.ic.on('input', inputListener = (data) =>
return if data.pin != @config.pinNumber
# handle input changes
@pcf8574ic.ic.on('input', inputListener = (data) =>
return if data.pin != @config.pinNumber
env.logger.debug "#{@id} input event", data
@_setContact data.value
)
env.logger.debug "#{@id} input event", data
@_setContact data.value
)
@on 'destroy', () => @pcf8574ic.ic.removeListener('input', inputListener)
@on 'destroy', () => @pcf8574ic.ic.removeListener('input', inputListener)
return true
)
.then( =>
env.logger.debug "#{@id} input pin initialized"
)
.catch( =>
env.logger.error "Error setting pin as input (device: #{@id}, pcf8574ic: #{@pcf8574ic.id}, pin: #{@config.pinNumber})"
)
super()
......@@ -180,17 +193,27 @@ module.exports = (env) ->
assert 7 >= @config.pinNumber >= 0, "The pinNumber must be between 0 and 7"
# set pin as input
@pcf8574ic.ic.inputPin @config.pinNumber, @config.inverted? or false
@pcf8574ic.setupChain = @pcf8574ic.setupChain.then( =>
@pcf8574ic.ic.inputPin @config.pinNumber, @config.inverted? or false
).then( =>
# handle input changes
@pcf8574ic.ic.on('input', inputListener = (data) =>
return if data.pin != @config.pinNumber
# handle input changes
@pcf8574ic.ic.on('input', inputListener = (data) =>
return if data.pin != @config.pinNumber
env.logger.debug "#{@id} input event", data
@_setPresence data.value
)
env.logger.debug "#{@id} input event", data
@_setPresence data.value
)
@on 'destroy', () => @pcf8574ic.ic.removeListener('input', inputListener)
@on 'destroy', () => @pcf8574ic.ic.removeListener('input', inputListener)
return true
)
.then( =>
env.logger.debug "#{@id} input pin initialized"
)
.catch( =>
env.logger.error "Error setting pin as input (device: #{@id}, pcf8574ic: #{@pcf8574ic.id}, pin: #{@config.pinNumber})"
)
super()
......@@ -220,12 +243,14 @@ module.exports = (env) ->
@_state = lastState?.state?.value or false
# set pin as output
@pcf8574ic.ic.outputPin @config.pinNumber, @config.inverted? or false, @_state
@pcf8574ic.setupChain = @pcf8574ic.setupChain.then( =>
@pcf8574ic.ic.outputPin @config.pinNumber, @config.inverted? or false, @_state
)
.then( =>
env.logger.debug "#{@id} output pin initialized"
)
.catch( =>
env.logger.error "Error setting pin as output (device: #{@id}, pcf8574ic: #{@pcf8574ic.id})"
env.logger.error "Error setting pin as output (device: #{@id}, pcf8574ic: #{@pcf8574ic.id}, pin: #{@config.pinNumber})"
)
super()
......@@ -275,19 +300,23 @@ module.exports = (env) ->
@down_autoStopTime = 0
# set pins as output
@up_pcf8574ic.ic.outputPin @config.up_pinNumber, @config.up_inverted? or false, false
@up_pcf8574ic.setupChain = @up_pcf8574ic.setupChain.then( =>
@up_pcf8574ic.ic.outputPin @config.up_pinNumber, @config.up_inverted? or false, false
)
.then( =>
env.logger.debug "#{@id} output pin for up direction initialized"
)
.catch(=>
env.logger.error "Error setting pin as output for up direction (device: #{@id}, pcf8574ic: #{@up_pcf8574ic.id})"
env.logger.error "Error setting pin as output for up direction (device: #{@id}, pcf8574ic: #{@up_pcf8574ic.id}, pin: #{@config.up_pinNumber})"
)
@down_pcf8574ic.setupChain = @down_pcf8574ic.setupChain.then( =>
@down_pcf8574ic.ic.outputPin @config.down_pinNumber, @config.down_inverted? or false, false
)
@down_pcf8574ic.ic.outputPin @config.down_pinNumber, @config.down_inverted? or false, false
.then( =>
env.logger.debug "#{@id} output pin for down direction initialized"
)
.catch( =>
env.logger.error "Error setting pin as output for down direction (device: #{@id}, pcf8574ic: #{@down_pcf8574ic.id})"
env.logger.error "Error setting pin as output for down direction (device: #{@id}, pcf8574ic: #{@down_pcf8574ic.id}, pin: #{@config.down_pinNumber})"
)
super()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment