Skip to content

Commit

Permalink
Improved volume control mapping between absolute, dB, and slider lev…
Browse files Browse the repository at this point in the history
…els, Added proper removal of event listeners to device destroy() methods to avoid potential resource leak, Cleanup
  • Loading branch information
mwittig committed Sep 30, 2016
1 parent cbb227d commit 5726ec2
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 89 deletions.
14 changes: 0 additions & 14 deletions denon-avr.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,6 @@ module.exports = (env) ->
"class": "DenonAvrInputSelector",
}
]
commands =
POWER: /^(PW)([A-Z]+)/
VOLUME: /^(MV)([0-9]+)/
MAINMUTE: /^(MU)([A-Z]+)/
Z2MUTE: /^(Z2MU)([A-Z]+)/
Z3MUTE: /^(Z3MU)([A-Z]+)/
INPUT: /^(SI)(.+)/
MAIN: /^(ZM)(.+)/
ZONE2: /^(Z2)(.+)/
ZONE3: /^(Z3)(.+)/

settled = (promise) -> promise.reflect()
series = (input, mapper) -> Promise.mapSeries(input, mapper)


# ###DenonAvrPlugin class
class DenonAvrPlugin extends env.plugins.Plugin
Expand Down
14 changes: 8 additions & 6 deletions devices/denon-avr-input-selector.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,27 @@ module.exports = (env) ->
@debug = @plugin.debug || false
for b in @config.buttons
b.text = b.id unless b.text?
@plugin.protocolHandler.on 'response', @_onResponseHandler()
@responseHandler = @_createResponseHandler()
@plugin.protocolHandler.on 'response', @responseHandler
super(@config)
process.nextTick () =>
@_requestUpdate()
@_requestUpdate true

destroy: () ->
@_base.cancelUpdate()
@plugin.protocolHandler.removeListener 'response', @responseHandler
super()

_requestUpdate: () ->
_requestUpdate: (immediate=false) ->
@_base.cancelUpdate()
@_base.debug "Requesting update"
@plugin.protocolHandler.sendRequest @zoneCmd, '?'
@plugin.protocolHandler.sendRequest @zoneCmd, '?', immediate
.catch (error) =>
@_base.error "Error:", error
.finally () =>
@_base.scheduleUpdate @_requestUpdate, @interval * 1000
@_base.scheduleUpdate @_requestUpdate, @interval * 1000, true

_onResponseHandler: () ->
_createResponseHandler: () ->
return (response) =>
@_base.debug "Response", response.matchedResults
if response.command is @zoneCmd and response.param isnt @_lastPressedButton and response.param isnt 'OFF'
Expand Down
14 changes: 8 additions & 6 deletions devices/denon-avr-master-volume.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ module.exports = (env) ->
@volumeLimit = @_base.normalize @config.volumeLimit, 0, 99
@maxAbsoluteVolume = @_base.normalize @config.maxAbsoluteVolume, 0, 99
@debug = @plugin.debug || false
@plugin.protocolHandler.on 'response', @_onResponseHandler()
@responseHandler = @_createResponseHandler()
@plugin.protocolHandler.on 'response', @responseHandler
@attributes = _.cloneDeep(@attributes)
@attributes.volume = {
description: "Volume"
Expand All @@ -33,22 +34,23 @@ module.exports = (env) ->
@_volume = 0
super()
process.nextTick () =>
@_requestUpdate()
@_requestUpdate true

destroy: () ->
@_base.cancelUpdate()
@plugin.protocolHandler.removeListener 'response', @responseHandler
super()

_requestUpdate: () ->
_requestUpdate: (immediate=false) ->
@_base.cancelUpdate()
@_base.debug "Requesting update"
@plugin.protocolHandler.sendRequest 'MV', '?'
@plugin.protocolHandler.sendRequest 'MV', '?', immediate
.catch (error) =>
@_base.error "Error:", error
.finally () =>
@_base.scheduleUpdate(@_requestUpdate, @interval * 1000)
@_base.scheduleUpdate @_requestUpdate, @interval * 1000, true

_onResponseHandler: () ->
_createResponseHandler: () ->
return (response) =>
@_base.debug "Response", response.matchedResults
if response.command is 'MV'
Expand Down
19 changes: 12 additions & 7 deletions devices/denon-avr-mute-switch.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,31 @@ module.exports = (env) ->
when 'ZONE3' then (
@zoneCmd = 'Z3MU'
)
@lastPowerState=null
@interval = @_base.normalize @config.interval, 10
@debug = @plugin.debug || false
@plugin.protocolHandler.on 'response', @_onResponseHandler()
@responseHandler = @_createResponseHandler()
@plugin.protocolHandler.on 'response', @responseHandler
super()
@_state = false;
process.nextTick () =>
@_requestUpdate()
@_requestUpdate true

destroy: () ->
@_base.cancelUpdate()
@plugin.protocolHandler.removeListener 'response', @responseHandler
super()

_requestUpdate: () ->
_requestUpdate: (immediate=false) ->
@_base.cancelUpdate()
@_base.debug "Requesting update"
@plugin.protocolHandler.sendRequest @zoneCmd, '?'
@plugin.protocolHandler.sendRequest @zoneCmd, '?', immediate
.catch (error) =>
@_base.error "Error:", error
.finally () =>
@_base.scheduleUpdate @_requestUpdate, @interval * 1000
@_base.scheduleUpdate @_requestUpdate, @interval * 1000, true

_onResponseHandler: () ->
_createResponseHandler: () ->
return (response) =>
@_base.debug "Response", response.matchedResults

Expand All @@ -53,7 +56,9 @@ module.exports = (env) ->
@_setState if response.param is 'ON' then true else false
)
when 'PW' then (
@_requestUpdate()
powerState = response.param is 'ON'
@_requestUpdate() if powerState isnt @lastPowerState
@lastPowerState = powerState
)

changeStateTo: (newState) ->
Expand Down
14 changes: 8 additions & 6 deletions devices/denon-avr-power-switch.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,28 @@ module.exports = (env) ->
@name = @config.name
@interval = @_base.normalize @config.interval, 10
@debug = @plugin.debug || false
@plugin.protocolHandler.on 'response', @_onResponseHandler()
@responseHandler = @_createResponseHandler()
@plugin.protocolHandler.on 'response', @responseHandler
@_state = false
super()
process.nextTick () =>
@_requestUpdate()
@_requestUpdate true

destroy: () ->
@_base.cancelUpdate()
@plugin.protocolHandler.removeListener 'response', @responseHandler
super()

_requestUpdate: () ->
_requestUpdate: (immediate=false) ->
@_base.cancelUpdate()
@_base.debug "Requesting update"
@plugin.protocolHandler.sendRequest 'PW', '?'
@plugin.protocolHandler.sendRequest 'PW', '?', immediate
.catch (error) =>
@_base.error "Error:", error
.finally () =>
@_base.scheduleUpdate @_requestUpdate, @interval * 1000
@_base.scheduleUpdate @_requestUpdate, @interval * 1000, true

_onResponseHandler: () ->
_createResponseHandler: () ->
return (response) =>
@_base.debug "Response", response.matchedResults
if response.command is 'PW'
Expand Down
18 changes: 10 additions & 8 deletions devices/denon-avr-presence-sensor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ module.exports = (env) ->
@interval = @_base.normalize @config.interval, 10
@volumeDecibel = @config.volumeDecibel
@debug = @plugin.debug || false
@plugin.protocolHandler.on 'response', @_onResponseHandler()
@responseHandler = @_createResponseHandler()
@plugin.protocolHandler.on 'response', @responseHandler
@attributes = _.cloneDeep(@attributes)
@attributes.volume = {
description: "Volume"
Expand All @@ -37,26 +38,27 @@ module.exports = (env) ->
@_input = ""
super()
process.nextTick () =>
@_requestUpdate()
@_requestUpdate true

destroy: () ->
@_base.cancelUpdate()
@plugin.protocolHandler.removeListener 'response', @responseHandler
super()

_requestUpdate: () ->
_requestUpdate: (immediate=false) ->
@_base.cancelUpdate()
@_base.debug "Requesting update"
Promise.all([
@plugin.protocolHandler.sendRequest 'PW', '?'
@plugin.protocolHandler.sendRequest 'SI', '?'
@plugin.protocolHandler.sendRequest 'MV', '?'
@plugin.protocolHandler.sendRequest 'PW', '?', immediate
@plugin.protocolHandler.sendRequest 'SI', '?', immediate
@plugin.protocolHandler.sendRequest 'MV', '?', immediate
])
.catch (error) =>
@_base.error "Error:", error
.finally () =>
@_base.scheduleUpdate @_requestUpdate, @interval * 1000
@_base.scheduleUpdate @_requestUpdate, @interval * 1000, true

_onResponseHandler: () ->
_createResponseHandler: () ->
return (response) =>
@_base.debug "Response", response.matchedResults
switch response.command
Expand Down
14 changes: 8 additions & 6 deletions devices/denon-avr-zone-switch.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,28 @@ module.exports = (env) ->
)
@interval = @_base.normalize @config.interval, 10
@debug = @plugin.debug || false
@plugin.protocolHandler.on 'response', @_onResponseHandler()
@responseHandler = @_createResponseHandler()
@plugin.protocolHandler.on 'response', @responseHandler
@_state = false
super()
process.nextTick () =>
@_requestUpdate()
@_requestUpdate true

destroy: () ->
@_base.cancelUpdate()
@plugin.protocolHandler.removeListener 'response', @responseHandler
super()

_requestUpdate: () ->
_requestUpdate: (immediate=false) ->
@_base.cancelUpdate()
@_base.debug "Requesting update"
@plugin.protocolHandler.sendRequest @zoneCmd, '?'
@plugin.protocolHandler.sendRequest @zoneCmd, '?', immediate
.catch (error) =>
@_base.error "Error:", error
.finally () =>
@_base.scheduleUpdate @_requestUpdate, @interval * 1000
@_base.scheduleUpdate @_requestUpdate, @interval * 1000, true

_onResponseHandler: () ->
_createResponseHandler: () ->
return (response) =>
@_base.debug "Response", response.matchedResults
if response.command is @zoneCmd
Expand Down
14 changes: 8 additions & 6 deletions devices/denon-avr-zone-volume.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ module.exports = (env) ->
@volumeLimit = @_base.normalize @config.volumeLimit, 0, 99
@maxAbsoluteVolume = @_base.normalize @config.maxAbsoluteVolume, 0, 99
@debug = @plugin.debug || false
@plugin.protocolHandler.on 'response', @_onResponseHandler()
@responseHandler = @_createResponseHandler()
@plugin.protocolHandler.on 'response', @responseHandler
@attributes = _.cloneDeep(@attributes)
@attributes.volume = {
description: "Volume"
Expand All @@ -41,22 +42,23 @@ module.exports = (env) ->
@_volume = 0
super()
process.nextTick () =>
@_requestUpdate()
@_requestUpdate true

destroy: () ->
@_base.cancelUpdate()
@plugin.protocolHandler.removeListener 'response', @responseHandler
super()

_requestUpdate: () ->
_requestUpdate: (immediate=false) ->
@_base.cancelUpdate()
@_base.debug "Requesting update"
@plugin.protocolHandler.sendRequest @zoneCmd, '?'
@plugin.protocolHandler.sendRequest @zoneCmd, '?', immediate
.catch (error) =>
@_base.error "Error:", error
.finally () =>
@_base.scheduleUpdate(@_requestUpdate, @interval * 1000)
@_base.scheduleUpdate @_requestUpdate, @interval * 1000, true

_onResponseHandler: () ->
_createResponseHandler: () ->
return (response) =>
@_base.debug "Response", response.matchedResults
if response.command is @zoneCmd and not isNaN response.param
Expand Down
Loading

0 comments on commit 5726ec2

Please sign in to comment.