Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Predicate Provider problem
class PlayingPredicateProvider extends env.predicates.PredicateProvider
constructor: (@framework) ->
parsePredicate: (input, context) ->
kodiDevices = _(@framework.deviceManager.devices).values()
.filter((device) => device.hasAttribute( 'state')).value()
device = null
state = null
negated = null
match = null
stateAcFilter = (v) => v.trim() isnt 'not playing'
M(input, context)
.matchDevice(kodiDevices, (next, d) =>
next.match([' is', ' reports', ' signals'])
.match([' playing', ' stopped',' paused', ' not playing'], {acFilter: stateAcFilter}, (m, s) =>
if device? and device.id isnt d.id
context?.addError(""""#{input.trim()}" is ambiguous.""")
return
device = d
negated = (s.trim() isnt "playing")
match = m.getFullMatch()
)
)
if match?
assert device?
assert negated?
assert typeof match is "string"
return {
token: match
nextInput: input.substring(match.length)
predicateHandler: new PlayingPredicateHandler(device, negated)
}
else
return null
class PlayingPredicateHandler extends env.predicates.PredicateHandler
constructor: (@device, @negated) ->
setup: ->
@playingListener = (p) =>
env.logger.debug (if @negated then not p else p)
@emit 'change', (if @negated then not p else p)
@device.on 'state', @playingListener
super()
getValue: ->
return @device.getUpdatedAttributeValue('state').then(
(p) => (if @negated then not p else p)
)
destroy: ->
@device.removeListener "state", @playingListener
super()
getType: -> 'state'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.